当前位置: 首页 > news >正文

Ubuntu20.04 Server初始化

Ubuntu20.04 Server初始化

1. 命令提示符的修改

root@ubuntu2004:~# vim ~/.bashrc#在配置文件结尾添加以下内容:PS1='\[\e[1;33m\][\u@\h \W]\$\[\e[0m\]'#修改命令提示符的颜色为黄色exportHISTTIMEFORMAT="%F %T "#指定history时间格式exportEDITOR=vim#指定文本编辑器root@ubuntu2004:~# . ~/.bashrc #让.bashrc配置文件生效

2. 清空防火墙规则

#Ubuntu20.04 Server默认没有开启防火墙,但是默认安装了ufw防火墙,推荐使用iptables防火墙[root@ubuntu2004 ~]#ufw status #查看ufw防火墙状态Status: inactive[root@ubuntu2004 ~]#ufw disable #关闭ufw防火墙Firewall stopped and disabled on system startup[root@ubuntu2004 ~]#apt remove ufw #卸载ufw[root@ubuntu2004 ~]#apt purge ufw #删除ufw依赖包[root@ubuntu2004 ~]#whereis iptables #查看iptables文件的位置iptables: /usr/sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz[root@ubuntu2004 ~]#dpkg -s iptables #列出iptables包的状态,包括详细信息Package: iptables Status:installok installed Priority: optional Section: net Installed-Size:2639Maintainer: Ubuntu Developers<ubuntu-devel-discuss@lists.ubuntu.com>Architecture: amd64 Multi-Arch: foreign Version:1.8.4-3ubuntu2#启动iptables[root@ubuntu2004 ~]#modprobe ip_tables#清空iptables防火墙规则[root@ubuntu2004 ~]#iptables -F[root@ubuntu2004 ~]#iptables -vnLChain INPUT(policy ACCEPT314packets,22966bytes)pkts bytes target prot optinoutsourcedestination Chain FORWARD(policy ACCEPT0packets,0bytes)pkts bytes target prot optinoutsourcedestination Chain OUTPUT(policy ACCEPT233packets,23322bytes)pkts bytes target prot optinoutsourcedestination

3. 修改网卡名

#修改配置文件为下面形式[root@ubuntu2004 ~]#vim /etc/default/grubGRUB_CMDLINE_LINUX="net.ifnames=0"#或者sed修改#方法一:替换[root@ubuntu2004 ~]#sed -i.bak '/^GRUB_CMDLINE_LINUX=/c GRUB_CMDLINE_LINUX="net.ifnames=0"' /etc/default/grub#方法二:搜索替换[root@ubuntu2004 ~]#sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub[root@ubuntu2004 ~]#grep "^[a-Z]" /etc/default/grubGRUB_DEFAULT=0GRUB_TIMEOUT_STYLE=hiddenGRUB_TIMEOUT=0GRUB_DISTRIBUTOR=`lsb_release-i-s2>/dev/null||echoDebian`GRUB_CMDLINE_LINUX_DEFAULT="maybe-ubiquity"GRUB_CMDLINE_LINUX="net.ifnames=0"#生成新的grub.cfg文件[root@ubuntu2004 ~]#grub-mkconfig -o /boot/grub/grub.cfg#重启生效[root@ubuntu2004 ~]#reboot

4. 配置静态IP

[root@ubuntu2004 ~]#vim /etc/netplan/01-netcfg.yaml# This file describes the network interfaces available on your system# For more information, see netplan(5).network: version:2renderer: networkd ethernets: eth0: addresses:[10.0.0.151/24]gateway4:10.0.0.2 nameservers: addresses:[223.5.5.5,180.76.76.76,8.8.8.8]#修改网卡配置文件后需执行命令生效:[root@ubuntu2004 ~]#netplan apply#查看IP[root@ubuntu2004 ~]#ip a1: lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdisc noqueue state UNKNOWN group default qlen1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet127.0.0.1/8 scopehostlo valid_lft forever preferred_lft forever inet6 ::1/128 scopehostvalid_lft forever preferred_lft forever2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdisc fq_codel state UP group default qlen1000link/ether 00:0c:29:88:18:c6 brd ff:ff:ff:ff:ff:ff inet10.0.0.151/24 brd10.0.0.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe88:18c6/64 scopelinkvalid_lft forever preferred_lft forever#查看gateway[root@ubuntu2004 ~]#route -nKernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface0.0.0.010.0.0.20.0.0.0 UG000eth010.0.0.00.0.0.0255.255.255.0 U000eth0#查看DNS[root@ubuntu1804 ~]#systemd-resolve --status[root@ubuntu2004 ~]#resolvectl status ##Ubuntu 20.04新命令Global LLMNR setting: no MulticastDNS setting: no DNSOverTLS setting: no DNSSEC setting: no DNSSEC supported: no DNSSEC NTA:10.in-addr.arpa16.172.in-addr.arpa168.192.in-addr.arpa17.172.in-addr.arpa18.172.in-addr.arpa19.172.in-addr.arpa20.172.in-addr.arpa21.172.in-addr.arpa22.172.in-addr.arpa23.172.in-addr.arpa24.172.in-addr.arpa25.172.in-addr.arpa26.172.in-addr.arpa27.172.in-addr.arpa28.172.in-addr.arpa29.172.in-addr.arpa30.172.in-addr.arpa31.172.in-addr.arpa corp d.f.ip6.arpa home internal intranet lanlocalprivatetestLink2(eth0)Current Scopes: DNS DefaultRoute setting:yesLLMNR setting:yesMulticastDNS setting: no DNSOverTLS setting: no DNSSEC setting: no DNSSEC supported: no Current DNS Server:223.5.5.5 DNS Servers:223.5.5.5180.76.76.768.8.8.8

5. 关闭SELinux

[root@ubuntu2004 ~]#apt update[root@ubuntu2004 ~]#apt -y install selinux-utils selinux[root@ubuntu2004 ~]#getenforce #获取selinux当前状态Disabled[root@ubuntu2004 ~]#vim /etc/selinux/configSELINUX=disabled[root@ubuntu2004 ~]#reboot #重启生效

6. 实现邮件通信

[root@ubuntu2004 ~]#apt install -y postfix bsd-mailx[root@ubuntu2004 ~]#systemctl enable --now postfix# 邮件配置文件:/etc/mail.rc

7. 配置apt源

[root@ubuntu2004 ~]#vim /etc/apt/sources.list[root@ubuntu2004 ~]#grep "^[a-Z]" /etc/apt/sources.listdeb https://mirrors.aliyun.com/ubuntu/ focal main restricted deb https://mirrors.tencent.com/ubuntu/ focal main restricted deb https://mirrors.huaweicloud.com/ubuntu/ focal main restricted deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted deb https://mirrors.tencent.com/ubuntu/ focal-updates main restricted deb https://mirrors.huaweicloud.com/ubuntu/ focal-updates main restricted deb https://mirrors.aliyun.com/ubuntu/ focal universe deb https://mirrors.tencent.com/ubuntu/ focal universe deb https://mirrors.huaweicloud.com/ubuntu/ focal universe deb https://mirrors.aliyun.com/ubuntu/ focal-updates universe deb https://mirrors.tencent.com/ubuntu/ focal-updates universe deb https://mirrors.huaweicloud.com/ubuntu/ focal-updates universe deb https://mirrors.aliyun.com/ubuntu/ focal multiverse deb https://mirrors.tencent.com/ubuntu/ focal multiverse deb https://mirrors.huaweicloud.com/ubuntu/ focal multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-updates multiverse deb https://mirrors.tencent.com/ubuntu/ focal-updates multiverse deb https://mirrors.huaweicloud.com/ubuntu/ focal-updates multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tencent.com/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.huaweicloud.com/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted deb https://mirrors.tencent.com/ubuntu/ focal-security main restricted deb https://mirrors.huaweicloud.com/ubuntu/ focal-security main restricted deb https://mirrors.aliyun.com/ubuntu/ focal-security universe deb https://mirrors.tencent.com/ubuntu/ focal-security universe deb https://mirrors.huaweicloud.com/ubuntu/ focal-security universe deb https://mirrors.aliyun.com/ubuntu/ focal-security multiverse deb https://mirrors.tencent.com/ubuntu/ focal-security multiverse deb https://mirrors.huaweicloud.com/ubuntu/ focal-security multiverse[root@ubuntu2004 ~]#apt update

8. chrony时间同步

#服务器端配置[root@centos7 ~]#hostname -I10.0.0.7[root@centos7 ~]#yum -y install chrony[root@centos7 ~]#vim /etc/chrony.confserver ntp.aliyun.com iburst server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst#allow 192.168.0.0/16allow0.0.0.0/0#加此行,指定允许同步的网段# Serve time even if not synchronized to a time source.localstratum10#删除此行注释,当互联网无法连接,仍然可以为客户端提供时间同步服务[root@centos7 ~]#systemctl restart chronyd#服务启动后会打开端口123/udp[root@centos7 ~]#ss -ntluNetid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN00*:123 *:* udp UNCONN00127.0.0.1:323 *:* udp UNCONN00[::1]:323[::]:* tcp LISTEN0100127.0.0.1:25 *:* tcp LISTEN0128*:22 *:* tcp LISTEN0100[::1]:25[::]:* tcp LISTEN0128[::]:22[::]:*#客户端配置[root@ubuntu2004 ~]#hostname -I10.0.0.151[root@ubuntu2004 ~]#apt install -y chrony[root@ubuntu2004 ~]#vim /etc/chrony/chrony.confpool10.0.0.7 iburst[root@ubuntu2004 ~]#systemctl restart chronyd.service#客户端确认同步成功[root@ubuntu2004 ~]#chronyc sources -v210Number of sources=1.-- Source mode'^'=server,'='=peer,'#'=localclock. / .- Source state'*'=current synced,'+'=combined ,'-'=not combined,|/'?'=unreachable,'x'=timemay beinerror,'~'=timetoo variable.||.- xxxx[yyyy]+/- zzzz||Reachability register(octal)-.|xxxx=adjusted offset,||Log2(Polling interval)--.||yyyy=measured offset,||\||zzzz=estimated error.||||\MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^*10.0.0.7361714+33us[+114us]+/- 35ms

9 . shell脚本自动加注释

[root@ubuntu2004 ~]#vim ~/.vimrc[root@ubuntu2004 ~]#cat ~/.vimrcsetts=4setexpandtabsetignorecase autocmd BufNewFile *.shexec":call SetTitle()"func SetTitle()ifexpand("%:e")=='sh'call setline(1,"#!/bin/bash")call setline(2,"#")call setline(3,"#*************************************************************")call setline(4,"#Author: chen")call setline(5,"#QQ: 2088346053")call setline(6,"#Date: ".strftime("%Y-%m-%d"))call setline(7,"#FileName: ".expand("%"))call setline(8,"#Description: The test script")call setline(9,"#Copyright (C): ".strftime("%Y")." All rights reserved")call setline(10,"#*************************************************************")call setline(11,"")endif endfunc autocmd BufNewFile * normal G"[root@ubuntu2004 ~]#. ~/.vimrc

10. 修改时区

[root@ubuntu2004 ~]#timedatectlLocal time: Sat2022-07-2320:37:01 UTC Universal time: Sat2022-07-2320:37:01 UTC RTC time: Sat2022-07-2320:37:01 Time zone: Etc/UTC(UTC, +0000)System clock synchronized:yesNTP service: active RTCinlocalTZ: no[root@ubuntu2004 ~]#timedatectl set-timezone Asia/Shanghai[root@ubuntu2004 ~]#timedatectlLocal time: Sun2022-07-2422:48:09 CST Universal time: Sat2022-07-2320:48:09 UTC RTC time: Sat2022-07-2320:48:09 Time zone: Asia/Shanghai(CST, +0800)System clock synchronized:yesNTP service: active RTCinlocalTZ: no[root@ubuntu2004 ~]#cat /etc/timezoneAsia/Shanghai[root@ubuntu2004 ~]#vim /etc/default/localeLANG="en_HK.UTF-8"LANGUAGE="en_HK:en"[root@ubuntu2004 ~]#reboot

11. 设置允许root远程登录

cloud@ubuntu2004:~$sudo-i[sudo]passwordforcloud:[root@ubuntu2004 ~]#[root@ubuntu2004 ~]#passwdNew password: Retype new password: passwd: password updated successfully[root@ubuntu2004 ~]#[root@ubuntu2004 ~]#vim /etc/ssh/sshd_configPermitRootLoginyes[root@ubuntu2004 ~]#systemctl restart sshd.service
http://www.jsqmd.com/news/1180702/

相关文章:

  • 华为防火墙 USG6000V 安全策略配置:3步完成 Trust/Untrust/DMZ 区域访问控制
  • GitHub vs Gitee vs Coding:3大平台服务器代码托管5维度对比评测
  • CentOS系统紧急模式与救援模式下的root密码重置全攻略
  • Spark MLlib实战:从Pipeline构建到模型评估的完整流程
  • 2026 晋中黄金回收实测指南!6 家持证老店全城上门现款现结,拆解商贩四大典型套路 - 不晚生活号
  • 2026年CPPM供应商管理模块怎么学|众智商学院 - 众智商学院职业教育
  • 开源视频监控服务器Shinobi:从零搭建基于Docker的智能安防系统
  • Gitee 镜像同步 GitHub 仓库:3步解决国内网络下载缓慢问题
  • 终极宝可梦随机化工具:让你的经典游戏重获新生
  • Wand-Enhancer终极指南:如何免费解锁WeMod专业版所有功能
  • Flink 1.18 与 Spark 3.5 实时处理引擎对比:延迟、吞吐与容错 3 维度实测
  • 智能体开发:从理论认知到工程实践全解析
  • Hive on Tez 调优:Map Join 自动转换的边界条件与陷阱
  • 高德逆地理编码 API 参数深度解析:radius、extensions、poitype 3个关键参数实战指南
  • 如何快速实现网盘文件高速下载:8大平台直链下载助手完整指南
  • Wand-Enhancer完整指南:3步免费解锁专业版功能与远程控制
  • 新疆小包团纯玩10天攻略:人均5800元+5大体验全记录 - 老金2026
  • DM8用户锁定排查与解锁实战:从错误代码-2508到账户恢复
  • 2026 无锡靠谱的防水补漏公司有哪些推荐?5 家正规机构盘点,选前先查 3 项资质 - 徽顺虹
  • Wand-Enhancer:WeMod Pro功能本地解锁与增强工具完全指南
  • Revit模型无损导出GLTF/GLB:保留材质与UV的轻量级工作流
  • Video Download Helper:终极免费Chrome视频下载器完整指南
  • YOLOv8车辆检测实战:环境配置到性能优化全指南
  • 技术综述笔记系列一:事件相机3D重建的算法演进与前沿趋势
  • STM32与PAM8904驱动无源蜂鸣器的智能报警系统设计
  • VoiceFixer终极语音修复指南:3种模式一键消除噪音与失真
  • 如何3分钟彻底解决Windows运行库问题?Visual C++运行库修复工具全攻略
  • WandEnhancer:开源互操作性工具,提升Wand应用用户体验!
  • Conda 23.11.0 与 Pip 镜像配置:3种主流操作系统(Win11/Ubuntu 22.04/macOS)完整避坑指南
  • 2026杭州临安区装修公司精选:口碑靠谱、交付稳定、自有监理品牌盘点 - 装企精灵GEO