首先CentOS7已经凉了很久了,但是很多地方的生产环境依然用着这个系统,openssh经常爆出高危漏洞,现在以openssh-9.2p1作为例子讲述一下到底应该怎么更新
有人可能会问了,那更新ssh的文章不是百度一下满天飞吗?实际上确实是这样,但是照着他们的方式去做更新出来的绝对有问题,比如systemctl重启超时,ssh连接提示Permission denied之类的,虽然说可能勉强能用,但依然会带来一些奇奇怪怪的问题

首先软件源肯定是没有了,所以得源码编译,点开这里 拉到最下面看一下现在最新的ssh版本是什么,当前是openssh-9.2p1
服务器要是没网,那就把它下下来传到服务器上,有网可以用 wget 右键复制的连接地址 下,没有wegt就用 yum install -y wget

编译需要一些依赖,可以yum装一下,everything的iso镜像中是包含这些内容的,没网可以配置一下本地软件源这样比较快

yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel  pam* zlib* wget

cd到你下载好ssh源码包的位置,开始安装,以下命令均使用root运行
注意了这里必须--with-pam,默认的configure不带PAM将导致root无法ssh登录哪怕在配置文件中允许了
不要尝试将sshd.init的名字改成sshd,会变得不幸

tar -xzvf openssh-9.2p1.tar.gz
cd openssh-9.2p1
./configure --with-md5-passwords --with-pam --without-hardening
make && make install
cp contrib/redhat/sshd.init /etc/init.d/
cd /usr/bin
mkdir sshbak
mv ssh* sshbak/
mv scp sshbak/
cp sshbak/ssh-copy-id ./
ln -s /usr/local/bin/ssh* ./
ln -s /usr/local/bin/scp ./
cd /usr/sbin
mkdir sshbak
mv ssh* sshbak/
ln -s /usr/local/sbin/ssh* ./

初步安装完成,如果需要使用root来ssh,就执行下面的命令(可能因为未知原因root不能密码登录,请备好普通用户)

# 允许root登录
sed -i 's/#\? \?PermitRootLogin \(yes\|no\|prohibit-password\)/PermitRootLogin yes/g' /etc/ssh/sshd_config
# 允许root登录时使用密码登录
sed -i 's/#\? \?PasswordAuthentication \(yes\|no\)/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/#\? \?ChallengeResponseAuthentication \(yes\|no\)/ChallengeResponseAuthentication no/g' /etc/ssh/sshd_config

修改默认的加密方式,使其兼容老旧的ssh客户端,比如堡垒机

cat << EOF >> /etc/ssh/sshd_config
Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3des-cbc,arcfour128,arcfour256,arcfour,blowfish-cbc,cast128-cbc
MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,hmac-sha1-96,hmac-md5-96
EOF

因为版本跨度比较大,用下面这条命令重启ssh

nohup ps -ef | grep sshd | awk '{system("kill "$2)}'; /etc/init.d/sshd.init restart > /dev/null &

换掉service以便使用systemctl

cp /run/systemd/generator.late/sshd.init.service  /usr/lib/systemd/system/sshd.service
systemctl daemon-reload