先上升级代码:
#!/bin/bash
# openssh升级脚本,升级后默认会允许Root账号远程和密码认证方式,若不需要可以注释相关命令
SSH_PKG=openssh-9.4p1-openssl-1.1.1v.tar.gz
SSH_VER=9.0
# 环境检查
arch | grep x86
if [ $? -ne 0 ];then
echo “update package is only for x86”
exit 1
fi
cat /etc/redhat-release | grep ” 7.” | grep CentOS
if [ $? -ne 0 ];then
echo “update package is not suitable for this system”
exit 1
fi
if [ ! -f $SSH_PKG ];then
echo “update package $SSH_PKG is not found”
exit 1
fi
# 备份
mkdir -p /sysvol/bak
nowtime=`date +”%Y-%m-%d_%H%M%S”`
cp /usr/sbin/sshd /sysvol/bak/sshd.$nowtime.bak
cp /usr/bin/ssh /sysvol/bak/ssh.$nowtime.bak
cp /usr/bin/scp /sysvol/bak/scp.$nowtime.bak
cp /usr/bin/sftp /sysvol/bak/sftp.$nowtime.bak
echo “backup old sshd ssh scp sftp, dst: /sysvol/bak”
# 安装sshd文件
cp $SSH_PKG /tmp/
cd /tmp/
tar xf $SSH_PKG
if [ ! -d /usr/local/lib64 ];then
mkdir -p /usr/local/lib64
fi
if [ ! -f /usr/local/lib64/libcrypto.so.1.1 ];then
cp libcrypto.so.1.1 /usr/local/lib64/
else
cp /usr/local/lib64/libcrypto.so.1.1 /sysvol/bak/libcrypto.so.1.1.$nowtime.bak
mv /usr/local/lib64/libcrypto.so.1.1 /usr/local/lib64/libcrypto.so.1.1.bak
cp libcrypto.so.1.1 /usr/local/lib64/
fi
grep “^/usr/local/lib64$” /etc/ld.so.conf >/dev/null
if [ $? -ne 0 ];then
echo “/usr/local/lib64” >> /etc/ld.so.conf
ldconfig
fi
cd /usr/sbin
mv sshd sshd.bak
# 隐藏sshd版本号
# sed -i “s/OpenSSH_${SSH_VER}/OpenSSH_0.0/g” /tmp/sshd
cp /tmp/sshd /usr/sbin/
find /etc/ssh/ -type f -regex “.*key$” -exec chmod 600 {} ;
cd /usr/bin
mv ssh ssh.bak
mv scp scp.bak
mv sftp sftp.bak
cp /tmp/ssh /tmp/scp /tmp/sftp /usr/bin/
# 重启sshd
systemctl restart sshd
if [ $? -eq 0 ];then
echo “openssh update success”
else
echo “openssh update fail”
fi
# 检查配置文件,允许Root账号远程,开启密码认证方式
echo “fix /etc/ssh/sshd_config”
tail -1 /etc/ssh/sshd_config| cat -A | grep ‘$’
if [ $? -ne 0 ];
then echo >> /etc/ssh/sshd_config
fi
sed -i ‘s/PermitRootLogin no/PermitRootLogin yes/g’ /etc/ssh/sshd_config
sed -i ‘s/PasswordAuthentication no/PasswordAuthentication yes/g’ /etc/ssh/sshd_config
grep “^PermitRootLogin yes$” /etc/ssh/sshd_config
if [ $? -ne 0 ];then
echo “PermitRootLogin yes” >> /etc/ssh/sshd_config
fi
grep “^PasswordAuthentication yes$” /etc/ssh/sshd_config
if [ $? -ne 0 ];then
echo “PasswordAuthentication yes” >> /etc/ssh/sshd_config
fi
grep “PubkeyAcceptedKeyTypes” /etc/ssh/sshd_config >/dev/null
if [ $? -ne 0 ];then
echo “PubkeyAcceptedKeyTypes=+ssh-rsa” >>/etc/ssh/sshd_config
fi
# 重启清理残留
systemctl restart sshd
rm /tmp/$SSH_PKG -f
rm /tmp/libcrypto.so.1.1 -f
rm /tmp/sshd -f
rm /tmp/ssh -f
rm /tmp/scp -f
rm /tmp/sftp -f
升级文件
链接: https://pan.baidu.com/s/1Oj4vXTgCXkXemXsXVxzhsA?pwd=dbfa 提取码: dbfa 复制这段内容后打开百度网盘手机App,操作更方便哦
准备事项:
升级openssh可能会导致无法远程连接,请事先准备好应急处理方案,比如:
- 临时开启telnet,并检查telnet是否能够正常连接;
- 通过网管接口远程访问,检查网管是否可以正常连接;
- 现场直接连接服务器。
升级涉及了四个执行程序:ssh、sshd、scp、sftp
升级操作
-
上传安装包和脚本至目标服务器(所有需要加固的AS服务器),比如:/opt/ssh
- 执行升级脚本:
/bin/bash openssh-update.sh
显示以上信息则为升级成功,备份文件会放入/sysvol/bak/
升级完成后不要断开当前远程会话,验证没问题后才能关闭!!!
备份目录和文件:
原始路径如下:
验证结果
-
检查版本:ssh -V
sshd -V - 检查远程连接是否异常:不要关闭当前会话,重新开启一个新的远程连接,检查是否报错或者连接失败
- 检查ulimit配置(针对Centos7的版本需要检查,Centos6跳过此步骤)
在新的会话窗口执行:
ulimit -a
如果open files、max user processes值过小则为异常,比如值小于1万(10000)
如果ulimit相关值异常先检查openssh是否开启了PAM选项,默认没有开启
检查PAM配置是否存在:
备份相关配置文件:/etc/ssh/sshd_config /etc/pam.d/sshd
调整配置文件:
- /etc/ssh/sshd_config 中UsePAM设为开启: UsePAM yes
-
/etc/pam.d/sshd配置示例:#%PAM-1.0
auth required pam_sepermit.so
auth include password-auth
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session optional pam_keyinit.so force revoke
session include password-auth - 调整完成后,重启sshd:systemctl restart sshd,然后再开新会话进行验证
- 如果open files、max user processes值过小,可以尝试修改系统配置文件:
增加两行配置
* – nofile 99999
* – nproc 99999
vi /etc/security/limits.conf
重启sshd服务后再重连检查参数
systemctl restart sshd
ulimit -a
- 此类错误可能为ssh客户端太旧,不支持新的加密算法,需要用新版的ssh工具
回退步骤
- 还原执行文件:
scp、sftp、ssh还原至/usr/bin目录下
sshd 还原至/usr/sbin目录下
比如:
cp scp.2021-08-31_102913.bak /usr/bin/scp
cp ssh.2021-08-31_102913.bak /usr/bin/ssh
cp sftp.2021-08-31_102913.bak /usr/bin/sftp
cp sshd.2021-08-31_102913.bak /usr/sbin/sshd
- 还原之前修改的配置文件
- 重启sshd
Centos7下执行:
systemctl restart sshd
Centos6下执行:
service sshd restart