日常新建linux服务器时,不是安装好操作系统就可以直接使用了,而是要做一些基本的配置,让服务器符合基本的“安全”等方面的要求,才可以交给同事安装企业自研的程序。到底需要哪些基础的配置呢?下面进行一下整理,共分四个部分:1、NTP时钟同步。2、禁止timestamp。3、升级SSH远程登录。4、SSH账号以及端口等的设置。仅供参考:


1、设置NTP同步

1.1、安装NTP服务
yum install -y ntp
1.2、编辑NTP配置文件
vi /etc/ntp.conf
server *.*.*.* iburst
1.3、启动NTP服务,并设置开机自启
systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd
1.4、查看配置是否生效,并查询时间
ntpq -p
date

2、防火墙策略(icmp)

2.1、禁止ICMP timestamp
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p ICMP --icmp-type timestamp-request -m comment --comment "deny ICMP timestamp" -j DROP
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p ICMP --icmp-type timestamp-reply -m comment --comment "deny ICMP timestamp" -j DROP
2.2、更新防火墙规则,并查看设置的规则
firewall-cmd --reload
firewall-cmd --direct --get-all-rules

3、升级sshd9

3.1、查看目前系统的ssh和sshd版本
ssh -V
sshd -V
3.2、安装操作系统依赖包
yum install -y telnet gcc zlib-devel
3.3、下载openssh和openssl,并放到/opt目录下
openssl-1.1.1w.tar.gz和openssh-9.5p1.tar.gz
3.4、编译升级openssl-1.1.1w

3.4.1、进入到openssl-1.1.1w.tar.gz文件所在的目录,执行编译安装命令:

cd /opt
tar -xzvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
mkdir -p /usr/openssh/openssl1.1.1w
./config --prefix=/usr/openssh/openssl1.1.1w
make
make install
ll /usr/openssh/openssl1.1.1w
cd ..
rm -rf openssl-1.1.1w*

3.4.2、修改配置文件:

编辑/etc/profile配置文件(vi /etc/profile),设置环境变量,在profile文件最后添加如下两行:

vi /etc/profile
export LD_LIBRARY_PATH=/usr/openssh/openssl1.1.1w/lib:$LD_LIBRARY_PATH
export PATH=/usr/openssh/openssh9.5p1/bin:/usr/openssh/openssh9.5p1/sbin:/usr/openssh/openssl1.1.1w/bin:$PATH

3.4.3、保存配置退出后,然后执行如下命令,确认openssl版本为1.1.1w:

source /etc/profile
openssl version
3.5、编译升级openssh-9.5p1

3.5.1、进入到openssh-9.5p1.tar.gz文件所在的目录,执行编译安装命令:

tar -xzvf openssh-9.5p1.tar.gz
cd openssh-9.5p1
mkdir -p /usr/openssh/openssh9.5p1
./configure --prefix=/usr/openssh/openssh9.5p1 --with-ssl-dir=/usr/openssh/openssl1.1.1w
make
make install

3.5.2、修改配置文件:

vi /usr/openssh/openssh9.5p1/etc/sshd_config
#允许root登录,默认值prohibit-password表示root用户只能通过公私钥的方式登录,不能以密码的方式登录
PermitRootLogin yes

3.5.3、创建sshd服务自启动文件:

vi /usr/lib/systemd/system/sshd9.service
#文件内容添加一下内容:
[Unit]
Description=OpenSSH server daemon
After=network.target

[Service]
Type=simple
Environment=LD_LIBRARY_PATH=/usr/openssh/openssl1.1.1w/lib
ExecStart=/usr/openssh/openssh9.5p1/sbin/sshd -D -f /usr/openssh/openssh9.5p1/etc/sshd_config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

3.5.4、停用原sshd服务,并备份相关文件

systemctl stop sshd.service
systemctl disable sshd.service

备份文件:

mkdir /home/ssh-old-bak
mv /etc/ssh /home/ssh-old-bak/
mv /usr/sbin/sshd /home/ssh-old-bak/
mv /usr/lib/systemd/system/sshd-keygen.service /home/ssh-old-bak/
mv /usr/lib/systemd/system/sshd.service /home/ssh-old-bak/
mv /usr/lib/systemd/system/sshd@.service /home/ssh-old-bak/
mv /usr/lib/systemd/system/sshd.socket /home/ssh-old-bak/

3.5.5、启动新的sshd服务:

systemctl daemon-reload
systemctl start sshd9.service
systemctl status sshd9.service
systemctl enable sshd9.service
3.6、测试验证

重新连接一个新的窗口来测试新sshd是否可以正常连接,并测试版本号,最后重启服务器测试是否可以重启成功:

sshd -V
ssh -V

版本号正确,且能连接上,说明升级成功!


4、新增系统账户,更改ssh默认端口

4.1 新增系统账户
# 创建账号 user01
useradd -m user01
# 把用户 user01 加入 wheel 组
gpasswd -a user01 -g wheel
# 设置用户密码
passwd user01
4.2 更改ssh默认端口,使用新端口55522
sed -i 's/#Port 22/Port 55522/g' /usr/openssh/openssh9.5p1/etc/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/g' /usr/openssh/openssh9.5p1/etc/sshd_config
4.3 禁用root用户远程登录权限(可以暂不设置
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g'  /usr/openssh/openssh9.5p1/etc/sshd_config
4.4 设置SSH每个连接允许的最大验证尝试次数
sed -i 's/#MaxAuthTries 6/MaxAuthTries 4/g'  /usr/openssh/openssh9.5p1/etc/sshd_config
4.5 设置SSH会话超时时间为5分钟
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 300/g'  /usr/openssh/openssh9.5p1/etc/sshd_config

sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 3/g'  /usr/openssh/openssh9.5p1/etc/sshd_config
4.6 开启系统防火墙
# 开启防火墙命令
systemctl start firewalld
systemctl enable firewalld
4.7 按需放行本机端口
# 放行端口命令
# 举例放行 TCP 55522端口
firewall-cmd --permanent --zone=public --add-port=55522/tcp
firewall-cmd --reload
4.8 更新系统
# 执行以下命令更新系统
yum update -y

注:完成以上配置后,需要重启ssh服务

systemctl restart sshd9


至此,CentOS7.9系统初始化安装配置完成。