1. 主机名设置

# 设置主机名
hostnamectl set-hostname testsrv-1
# 地址解析
cat <<EOF >>/etc/hosts
192.168.166.8 testrv-1
EOF

2. SELinux 配置

# 修改配置文件,重启后生效
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# 禁用 SELinux,立即生效
setenforce 0

3. 防火墙配置

systemctl enable firewalld && systemctl restart firewalld

4. SSH 服务优化

加速SSH连接速度。

# Optimize sshd service - Disable GSSAPIAuthentication
sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
# Restart sshd service
systemctl restart sshd
# Optimize sshd service - Disable UseDNS
sed -i '/^#UseDNS yes/a\UseDNS no' /etc/ssh/sshd_config
# Restart sshd service
systemctl restart sshd

5. YUM 源配置

更新为阿里提供的软件源,加速软件下载及安装。

5.1 备份 YUM 源

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

5.2 更新阿里云 YUM 源

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

5.3 生成缓存

yum makecache

6. 时间同步设置

这里我们以 NTP 配置为例。

6.1 NTP 安装

yum install -y ntp

6.2 系统时区设置

timedatectl set-timezone Asia/Shanghai

6.3 开启 NTP 同步

timedatectl set-ntp yes

6.4 配置文件修改

# 修改配置文件,增加以下行服务器,建议内网搭建时间服务器,所有服务器都与此服务器时间同步,这样可以保证内网服务器保持一致。
vim /etc/ntpd.conf
server 192.168.166.23 iburst prefer
server ntp.aliyun.com iburst

6.5 注册并启动 NTP 服务

systemctl enable ntpd && systemctl start ntpd

6.6 时间校准

[root@localhost ~]# ntpq -p

6.7 时间服务状态查看

[root@localhost ~]# timedatectl status
Local time: Wed 2021-03-24 19:44:30 CST
Universal time: Wed 2021-03-24 11:44:30 UTC
RTC time: Wed 2021-03-24 11:44:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

7. 系统服务优化

默认系统安装完成后有一些没有必要的服务,进行禁用或卸载操作,以最小化的原则,减少无必要服务的运行,可以提高系统的安全性。

8. 常用软件安装

根据自己使用的习惯,可以把一些常用的软件工具安装,便于后期的操作。

yum install -y wget lrzsz zip unzip tree vim net-tools telnet rsync

9. 内核版本升级

默认CentOS7.9的内核还是3.10版本,基于安全和场景的要求可以进行升级到当前最新的长期支持版。

9.1 RPM KEY 导入

rpm import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

9.2 YUM 安装

yum install -y https://www.elrepo.org/elrepo-release-7.0-6.el7.elrepo.noarch.rpm

9.3 安装内核

yum --disablerepo='*' --eanblerepo='elrepo-kernel' install -y kernel-lt

9.4 卸载旧版工具包

[root@localhost ~]# yum remove -y kernetl-tools kernel-tools-libs

9.5 安装新版本工具包

[root@localhost ~]# yum install -y kernel-lt-tools

9.6 内核引导顺序修改

[root@localhost ~]# grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg
[root@localhost ~]# grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"

9.7 默认引导内核确认

[root@localhost ~]# grubby --default-kernel
/boot/vmlinuz-3.10.0-1160.95.1.el7.x86_64

9.8 重启系统

[root@localhost ~]# reboot

9.9 确认引导内核

[root@localhost ~]# uname -rs
Linux 5.4.251-1.el7.elrepo.x86_64

查看返回信息,当前系统内核已经正确引导升级后的版本。

10. 操作系统更新

# 更新所有软件包
yum update
# 升级到最新版本,操作请谨慎,特别是已安装服务环境,可能升级会导致组件的运行失败。
yum upgrade