CentOS5/6/7/8 OpenSSH升级(源码编译安装)

目录

安装依赖 

编译安装OpenSSL(选)

在线安装OpenSSL

编译安装OpenSSH 

CentOS7、8安装后配置

CentOS6安装后配置

FAQ

编译安装curl & wget 

本篇文章饱含了openssh、curl、wget等的升级操作,但其实最重要的是openssl,因为openssl是很多软件的依赖,且这种依赖是有版本对应的;因此,我们升级时最好自定义安装路径,且openssl升级后并不能完全取代已有旧版本(版本依赖)。

安装依赖 

yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pam-devel 

zlib是必须的,同时没有libcrypto(包含在openssl-devel)编译安装OpenSSH是被允许的,但是其所使用的加密算法会被严格限制。
libcrypto from either of LibreSSL or OpenSSL.  Building without libcrypto is supported but severely restricts the available ciphers and algorithms. 

引用自《OpenSSH安装文档

编译安装OpenSSL(选)

如果系统自带openssl版本够用,在编译安装openssh时不报错,这里无需安装新版本openssl。 

yum -y install perl perl-Test-Simple 
./config --prefix=/usr/local/openssl shared -fPIC
make depend
make tests
make && make install
#检查函数库,通过检查确认缺少的函数库
ldd /usr/local/openssl/bin/openssl
#添加所缺函数库
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v #更新函数库
openssl/bin/openssl version #查看新安装的版本
#查看旧版本openssl命令在哪里,这里根据实际情况,进行下一步
which openssl
mv /usr/bin/openssl /usr/bin/openssl.old #将旧版本openssl移除
#新版本制作软链接
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
openssl version  最后查看版本,更新完毕

-fPIC 编译OpenSSL为位置无关的库,否则openssh安装的时候无法找到OpenSSL
LibreSSL/OpenSSL should be compiled as a position-independent library(i.e. -fPIC, eg by configuring OpenSSL as "./config [options] -fPIC" or LibreSSL as "CFLAGS=-fPIC ./configure") otherwise OpenSSH will not be able to link with it.  If you must use a non-position-independent libcrypto, then you may need to configure OpenSSH --without-pie.

引用自《OpenSSH安装文档

在线安装OpenSSL

系统默认镜像库的openssl版本普遍较低。需安装epel第三方扩展镜像库安装。centos5很可能已经无法安装epel-release.rpm了,那么手动进行配置。但是,在线yum升级openssl(openssl11、openssl11-devel、openssl11-libs)无法被openssh编译引用( Your OpenSSL headers do not match your library)。
手动配置epel镜像源:

cat >> /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 5 or 6 - $basearch
baseurl=https://archives.fedoraproject.org/pub/archive/epel/\$releasever/\$basearch
enabled=1
gpgcheck=1
EOF

wget https://archives.fedoraproject.org/pub/archive/epel/RPM-GPG-KEY-EPEL-5 -P /etc/pki/rpm-gpg
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-5
echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-5" >> /etc/yum.repos.d/epel.repo 

yum clean all
yum makecache

yum list |grep openssl
openssl.x86_64                             0.9.8e-12.el5_5.7           installed
openssl101e.x86_64                         1.0.1e-11.el5               epel

yum -y install openssl101e.x86_64

编译安装OpenSSH 

# 如果在升级前删除旧的openssh,则/etc/ssh/* /usr/lib/system/systemd/sshd.service /etc/pam.d/sshd等会被删除,所以提前要做备份(如果不删除则只需要备份/etc/ssh):
mv /etc/ssh{,.bak}
# 如果有使用ssh1的特殊需求,必须通过--with-ssh1 指定,默认不会启动
./configure --prefix=/usr/local/openssh --exec-prefix=/usr --sysconfdir=/etc/ssh --with-ssl-dir=/usr/local/openssl --with-md5-passwords --with-pam     --with-selinux --with-privsep-path=/var/lib/sshd
# 或者(如果是编译安装的zlib,需要通过--with-zlib=/usr/local/zlib指定)
./configure --prefix=/usr/local/openssh --exec-prefix=/usr --sysconfdir=/etc/ssh --with-ssl-dir=/usr/local/openssl --with-md5-passwords --with-pam --mandir=/usr/share/man --with-selinux --with-privsep-path=/var/lib/sshd --without-hardening

make && make install

--with-ssl-dir=/usr/local/openssl 指定OpenSSL安装目录
--with-ssh1 加载ssh1协议,如果不加载ssh -1不被支持
在安装之前就备份/etc/ssh/sshd_config{,.bak},令其生成新的配置文件,避免因新老版本之间的差异导致启动报错,甚至无法启动sshd服务。

CentOS7、8安装后配置

需要查看/etc/ssh/是否自动将新的sshd_config拷贝到该目录,如果没有,需要手动拷贝:
cp openssh-8.7p1/sshd_config /etc/ssh/
同时需要按照之前配置修改该sshd_config文件。自openssh7.0之后,usePAM默认为no,所以需要手动开启,否则密码认证将失败。

因为CentOS7开始sshd服务被systemd管理。如果按照上面的步骤安装完成之后,通过systemctl restart sshd会发现服务一直timeout,就是启动不成功。还需要进行如下配置:

cp contrib/redhat/sshd.init /etc/init.d/sshd
/etc/init.d/sshd start
# mv /usr/lib/systemd/system/sshd.service{,old}
# cp /run/systemd/generator.late/sshd.init.service /usr/lib/systemd/system/sshd.service
# systemctl daemon-reload
# 不要进行上述操作,否则sshd无法开机自启动
systemctl status sshd

# 添加开机自启动
chkconfig --add sshd
chkconfig --list
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

 

CentOS6安装后配置

将源码安装包下的sshd.init拷贝到/etc/init.d/
cp contrib/redhat/sshd.init /etc/init.d/
查看sshd.init中调用的命令目录是否和安装目录一致,如不一致则手动进行替换:
sed -i 's|/usr/bin/|/usr/local/bin|g' /etc/init.d/sshd.init
启动openssh服务
/etc/init.d/sshd.init start
替换原有的sshd服务文件
mv /etc/init.d/sshd{,.old}
mv /etc/init.d/sshd{.init,}
service sshd status

此外,安装配置时如果没有带 --with-selinux参数,那么需要关闭selinux,否则需要根据提示配置安全上下文;否则,重启系统是手动将无法开机自启动。如:

chcon /etc/init.d/sshd --reference=/etc/init.d/sshd.bak  

FAQ

make tests,失败:
../test/recipes/95-test_external_krb5.t          (Wstat: 512 Tests: 0 Failed: 0)
  Non-zero exit status: 2
  Parse errors: No plan found in TAP output
../test/recipes/95-test_external_pyca.t          (Wstat: 512 Tests: 0 Failed: 0)
  Non-zero exit status: 2
  Parse errors: No plan found in TAP output
../test/recipes/99-test_ecstress.t               (Wstat: 512 Tests: 0 Failed: 0)
  Non-zero exit status: 2
  Parse errors: No plan found in TAP output
../test/recipes/99-test_fuzz.t                   (Wstat: 512 Tests: 0 Failed: 0)
  Non-zero exit status: 2
  Parse errors: No plan found in TAP output
Files=158, Tests=0,  1 wallclock secs ( 0.20 usr  0.14 sys +  0.67 cusr  0.33 csys =  1.34 CPU)
Result: FAIL
make[1]: *** [_tests] Error 1
make[1]: Leaving directory `/root/openssh/openssl'
make: *** [tests] Error 2

解决:
缺少perl-Test-Simple,如果已经安装说明版本较低,请下载较高版本再试。

openssl编译安装,执行make test报错:
BEGIN failed--compilation aborted at .././test/run_tests.pl line 112.
解决:
yum install perl-devel -y

 SysV管理的sshd,其debug日志比较难找,我们可以直接运行/usr/local/sbin/sshd -d -E openssh.log查看debug日志,以方便定位问题。

/etc/ssh/sshd_config line 74: Unsupported option GSSAPIAuthentication
/etc/ssh/sshd_config line 76: Unsupported option GSSAPICleanupCredentials

解决:
这是旧的/etc/ssh/sshd_config中的配置项,去掉即可。

使用YUM在线安装依赖包,报:Error: Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again。

解决:
在线获取repomd.xml的URL错误导致:1、DNS错误服务解析YUM源地址;2、$releasever获取版本信息和镜像源不一致;

配置OpenSSH时,报错:configure: error: *** OpenSSL headers missing - please install first or check config.log ***

解决:openssl-devel没有安装或者没有编译安装新的openssl。

配置OpenSSH时,报错:configure: error: *** Can't find recent OpenS SL libcrypto (see config.log for details) ***

解决:
编译安装OpenSSL不正确导致无法找到 libcrypto,按照上面的步骤重新编译安装即可。

编译安装OpenSSL到1.0.2h版本后,header version正确,但是library 检测到的依然是旧的:
checking OpenSSL header version... 1000208f (OpenSSL 1.0.2h  3 May 2016)
checking OpenSSL library version... configure: error: OpenSSL >= 0.9.8f required (have "0090802f (OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008)")

checking whether OpenSSL's headers match the library... no configure: error: Your OpenSSL headers do not match your library. Check config.log for details.
解决:(完事之后,可以改回去,不影响openssh使用)
1、指定openssl安装目录--with-ssl-dir=/usr/local/openssl;
2、mv /usr/bin/openssl /usr/bin/openssl.bak
      mv /usr/include/openssl /usr/include/openssl.bak
      ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
      ln -s /usr/local/openssl/include/openssl /usr/include/openssl
      echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
      ldconfig -v
3、重新执行openssh的配置命令

编译安装openssh时,报错:

Permissions 0640 for '/etc/ssh/ssh_host_rsa_key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
……
sshd: no hostkeys available -- exiting.
make: [check-config] Error 1 (ignored)
解决:
# chmod 600 /etc/ssh/ssh_host_rsa_key
# chmod 600 /etc/ssh/ssh_host_ecdsa_key
# chmod 600 /etc/ssh/ssh_host_ed25519_key 

问题:配置过程提示如下错误
configure: error: PAM headers not found
解决:
# yum -y install pam-devel

问题:
升级之后通过密码认证登录,提示密码错误,但是密码是正确的
解决:
# mv /etc/init.d/sshd{,.bak}
# cp contrib/redhat/sshd.init /etc/init.d/sshd 

注:如果还不行:1、请关闭selinux(setenforce 0;因为升级操作不当,可能还有其他的文件的安全上下文有异~);2、请尝试普通用户登录(默认禁止root登录)3、可能是/etc/pam.d/sshd文件被替换。。

编译安装curl & wget 

openssl升级之后使用wget或curl 访问https可能会报:
SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
应该是因为wget和curl基于旧版本openssl编译,不支持tlsv1以上的版本。
解决:重新编译安装wget和curl;如果操作系统比较老,比如CentOS5,不要下载太高版本的介质;因为GCC的版本也比较老,会导致编译失败。
wget:
./configure --with-ssl=openssl --with-libssl-prefix=/usr/local/openssl/  --prefix=/usr/local/wget
make && make install
mv /usr/bin/wget{,.bak} && ln -s  /usr/local/wget/bin/wget  /usr/bin/wget

curl:
CPPFLAGS="-I/usr/local/openssl/include" LDFLAGS="-L/usr/local/openssl/lib" ./configure --prefix=/usr/local/curl  --with-ssl=/usr/local/openssl -without-nss
make && make install 

mv /usr/bin/curl{,.bak} && ln -s /usr/local/curl/bin/curl /usr/bin/curl

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值