centos7下自动编译升级openssh和OpenSSL的shell

#!/bin/bash
cd /opt

# 更新 openssh 
yum update openssh -y

# 安装依赖
for i in pcre pcre-devel git wget gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-devel;do
  rpm -q $i || yum -y install $i
done
yum install -y pam* zlib*

# 下载 openssh、openssl 源码
if [[ ! -f "/opt/openssh-8.4p1.tar.gz" ]];then
  wget -c http://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.4p1.tar.gz
fi
if [[ ! -f "/opt/openssl-1.1.1i.tar.gz" ]];then
  wget -O /opt/openssl-1.1.1i.tar.gz https://www.openssl.org/source/openssl-1.1.1i.tar.gz
fi

# 解压操作
tar zxf openssh-8.4p1.tar.gz

# 解压、备份、安装 openssl
if [[ `openssl version |awk '{print $2}'` == 1.1.1i ]];then
        echo "openssl is ok,Don't need update"
else
    mv /usr/bin/openssl /usr/bin/openssl_bak
    mv /usr/include/openssl /usr/include/openssl_bak
    tar zxf openssl-1.1.1i.tar.gz
    cd /opt/openssl-1.1.1i/
    make clean
    ./config shared --openssldir=/usr/local/openssl --prefix=/usr/local/openssl
    make && make install
    echo $?
fi

cd /opt

# 链接文件
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/openssl/include/openssl /usr/include/openssl
echo "/usr/local/openssl/lib/" >> /etc/ld.so.conf
/sbin/ldconfig
openssl version

# 编译安装 openssh 
cd /opt/openssh-8.4p1
chown -R root.root /opt/openssh-8.4p1
cp -r /etc/ssh /tmp/
rm -rf /etc/ssh
./configure --prefix=/usr/ --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/openssl/include --with-ssl-dir=/usr/local/openssl --with-zlib --with-md5-passwords --with-pam && make && make install
echo $?

# 修改配置文件
cat > /etc/ssh/sshd_config <<EOF
port 22201
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
UseDNS no
Subsystem sftp /usr/libexec/sftp-server
EOF
grep "^PermitRootLogin" /etc/ssh/sshd_config
cat /tmp/ssh/sshd_config |grep -v '#' |grep -v '^$'
cp -a contrib/redhat/sshd.init /etc/init.d/sshd
cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam
chmod +x /etc/init.d/sshd
chkconfig --add sshd
systemctl enable sshd
mv /usr/lib/systemd/system/sshd.service /opt/
mv /usr/lib/systemd/system/sshd.socket /opt/
chkconfig sshd on

# 启动
service sshd restart

# 测试
openssl version
ssh -V

猜你喜欢

转载自blog.51cto.com/m51cto/2596872