主要参考文档:https://www.jb51.net/article/188120.htm

次要参考文档:https://blog.csdn.net/gdkyxy2013/article/details/88906630

                     https://www.cnblogs.com/liy36/p/12579864.html

                     https://www.cnblogs.com/luckjinyan/p/13124422.html

                     http://www.bubuko.com/infodetail-3633818.html

前期准备

1##### 关闭防火墙并禁止开机自启
2[root@localhost ~]# systemctl stop firewalld.service
3[root@localhost ~]# systemctl disable firewalld
4
5##### 关闭selinux
6[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
7
8#####重启 
9[root@localhost ~]# reboot

telnet 、xinetd 服务的安装和启动

 1###### 安装 telnet 、xinetd
 2[root@localhost ~]# yum install -y xinetd telnet telnet-server
 3
 4###### 设置 telnet 、xinetd 开机自启
 5[root@localhost ~]# systemctl enable xinetd.service
 6[root@localhost ~]# systemctl enable telnet.socket
 7
 8###### 开机启动 telnet 、xinetd
 9[root@localhost ~]# systemctl restart xinetd.service
10[root@localhost ~]# systemctl restart telnet.socket
11
12###### 查看服务状态
13[root@localhost ~]# systemctl status xinetd.service
14[root@localhost ~]# systemctl status telnet.socket
15
16###### 开机不启动 telnet 、xinetd
17[root@localhost ~]# systemctl disable xinetd.service
18[root@localhost ~]# systemctl disable telnet.socket

使用另外一台机器通过 telnet 连接 需要安装 openssh 的机器

注意:telnet默认不允许root用户登录,请通过普通用户登录后再跳转到root用户

1[root@bogon ~]# telnet 192.168.76.133

安装依赖 openssl、openssh的依赖包

1[root@localhost src]# yum -y install gcc make perl zlib zlib-devel pam pam-devel

预先把 openssh-8.3p1.tar.gz 和 openssl-1.1.1g.tar.gz 的压缩包放到任意(一般源代码放到 /usr/local/src )目录下

安装 openssl

 1#### 编译安装
 2[root@localhost src]# tar -zxf openssl-1.1.1g.tar.gz
 3[root@localhost src]# cd openssl-1.1.1g
 4[root@localhost openssl-1.1.1g]# ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared
 5[root@localhost openssl-1.1.1g]# make && make install
 6
 7#### 备份之前的 openssl (如果有的话)
 8[root@localhost openssl-1.1.1g]# mv /usr/bin/openssl /usr/bin/openssl.bak
 9[root@localhost openssl-1.1.1g]# mv /usr/include/openssl /usr/include/openssl.bak
10
11#### 创建软链接
12[root@localhost openssl-1.1.1g]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
13[root@localhost openssl-1.1.1g]# ln -s /usr/local/openssl/include/openssl /usr/include/openssl
14
15#### 更新系统配置
16[root@localhost openssl-1.1.1g]# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
17[root@localhost openssl-1.1.1g]# /sbin/ldconfig
18
19#### 检查版本
20[root@localhost openssl-1.1.1g]# openssl version

安装 openssh

 1#### 停止旧版本的 ssh 服务
 2[root@localhost src]# systemctl stop sshd
 3[root@localhost src]# systemctl disable sshd
 4Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
 5
 6#### 备份 ssh 旧版本的开机服务文件(重要!如果升级失败可以恢复旧版本)
 7[root@localhost openssh-8.3p1]# mkdir ~/important_backup
 8[root@localhost openssh-8.3p1]# mv /usr/lib/systemd/system/sshd.service ~/important_backup
 9
10#### 备份 ssh 旧版本的配置文件(如果有的话)
11[root@localhost src]# mv /etc/ssh /etc/ssh.bak
12
13#### 编译安装 openssh
14[root@localhost src]# tar -zxf openssh-8.3p1.tar.gz 
15[root@localhost src]# cd openssh-8.3p1
16[root@localhost openssh-8.3p1]# ./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/openssl/include --with-ssl-dir=/usr/local/openssl --with-zlib --with-md5-passwords --with-pam --with-ssl-engine
17[root@localhost openssh-8.3p1]# make && make install
18
19#### 修改openssh的配置文件,允许root登录
20[root@localhost openssh-8.3p1]# vi /etc/ssh/sshd_config
21修改如下配置:
22PermitRootLogin yes
23PubkeyAuthentication yes
24PasswordAuthentication yes
25UseDNS no
26
27#### 添加 openssh 开机启动服务的设置
28[root@localhost openssh-8.3p1]# cp -a /usr/local/src/openssh-8.3p1/contrib/redhat/sshd.init /etc/init.d/sshd
29[root@localhost openssh-8.3p1]# cp -a /usr/local/src/openssh-8.3p1/contrib/redhat/sshd.pam /etc/pam.d/sshd.pam
30#### 保证 /etc/init.d/sshd 可执行(如果不可执行的话)
31[root@localhost openssh-8.3p1]# chmod +x /etc/init.d/sshd
32
33#### 备份旧的 openssh 命令
34[root@localhost openssh-8.3p1]# mv /sbin/sshd            /sbin/sshd.bak
35[root@localhost openssh-8.3p1]# mv /usr/bin/scp          /usr/bin/scp.bak
36[root@localhost openssh-8.3p1]# mv /usr/bin/sftp         /usr/bin/sftp.bak
37[root@localhost openssh-8.3p1]# mv /usr/bin/ssh          /usr/bin/ssh.bak
38[root@localhost openssh-8.3p1]# mv /usr/bin/ssh-add      /usr/bin/ssh-add.bak
39[root@localhost openssh-8.3p1]# mv /usr/bin/ssh-agent    /usr/bin/ssh-agent.bak
40[root@localhost openssh-8.3p1]# mv /usr/bin/ssh-keygen   /usr/bin/ssh-keygen.bak
41[root@localhost openssh-8.3p1]# mv /usr/bin/ssh-keyscan  /usr/bin/ssh-keyscan.bak
42
43#### 添加新的 openssh 命令
44[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/sbin/sshd        /sbin/sshd
45[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/scp          /usr/bin/scp
46[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/sftp         /usr/bin/sftp
47[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/ssh          /usr/bin/ssh
48[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/ssh-add      /usr/bin/ssh-add
49[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/ssh-agent    /usr/bin/ssh-agent
50[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/ssh-keygen   /usr/bin/ssh-keygen
51[root@localhost openssh-8.3p1]# ln -s /usr/local/openssh/bin/ssh-keyscan  /usr/bin/ssh-keyscan
52
53#### 检查openssh版本
54[root@localhost openssh-8.3p1]# ssh -V
55
56#### 添加 ssh 服务
57[root@localhost openssh-8.3p1]# chkconfig --add sshd 
58#### 设置 ssh 服务开机启动
59[root@localhost openssh-8.3p1]# systemctl enable sshd
60#### 启动 ssh 服务
61[root@localhost openssh-8.3p1]# systemctl start sshd
62#### 查看 ssh 服务是否开机启动( sshd.socket 显示为 enabled 表示服务是开机启动的)
63[root@localhost openssh-8.3p1]# systemctl list-unit-files | grep sshd

至此openssh服务安装完成,不放心的话可以reboot重启机器

如果从新登录目标服务器,出现类似如下的提示:

 1[root@localhost ~]# ssh 10.1.150.35
 2@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 3@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
 4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 5IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
 6Someone could be eavesdropping on you right now (man-in-the-middle attack)!
 7It is also possible that a host key has just been changed.
 8The fingerprint for the ECDSA key sent by the remote host is
 9SHA256:90FwM2na2MZSzmv5BWbEVjmL5hTuDx7ilxQiPEMxK7I.
10Please contact your system administrator.
11Add correct host key in /root/.ssh/known_hosts to get rid of this message.
12Offending ECDSA key in /root/.ssh/known_hosts:90
13ECDSA host key for 10.1.150.35 has changed and you have requested strict checking.
14Host key verification failed.

这是因为服务器主体发生变更了,即:

第一次SSH连接时,会生成一个认证,储存在客户端(也就是用SSH连线其他电脑的那个,自己操作的那个)中的 known_hosts,

但是如果服务器验证过了,认证资讯当然也会更改,服务器端与客户端不同时,就会跳出错误啦。

解决方法:

1#### 先在当前计算机的 known_hosts 文件中删除目标服务器的ip(或域名):
2[root@localhost ~]# ssh-keygen -R 10.1.150.35
3#### 然后再重新分发秘钥到目标服务器:
4[root@localhost ~]# ssh-copy-id 10.1.150.35
5#### 最后重新登录即可:
6[root@localhost ~]# ssh 10.1.150.35

openssh 免密登录

1#### 第一步: 在本地机器上使用 ssh-keygen 产生公钥私钥对
2[root@localhost ~]# ssh-keygen
3
4#### 第二步: 用ssh-copy-id将公钥复制到远程机器中
5[root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub  用户名字@192.168.x.xxx
6
7#### 第三步: 登录到远程机器不用输入密码
8[root@localhost ~]# ssh 用户名字@192.168.x.xxx

ssh-keygen 命令执行时,在默认情况下,会在 ~/.ssh 目录下生成 id_rsa 和 id_rsa.pub

ssh-copy-id 命令说明: ssh-copy-id 将公匙写到远程机器的 ~/.ssh/authorized_key 文件中

ssh-copy-id 使用方法:ssh-copy-id [-i [identity_file]] [user@]machine

-i:指定公钥文件,可选;默认是 ~/.ssh/id_rsa.pub

user:登录用户

例如:

1[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.1.150.35

openssh 不能使用 root 登录的解决办法

编辑 /etc/ssh/sshd_config,把PermitRootLogin项将其值改为yes

1vim /etc/ssh/sshd_config
2PermitRootLogin yes