一,openssh服务版本号的查看

1,查看当前sshd的版本号 :

[root@yjweb ~]# sshd --help
unknown option -- -
OpenSSH_7.8p1, OpenSSL 1.1.1 FIPS  11 Sep 2018

说明:sshd没有专门的打印版本号和帮助文档的命令,

         当参数不正确时会自动打印这些信息

 

2,查看当前ssh客户端程序的版本号

[root@yjweb ~]# ssh -V
OpenSSH_7.8p1, OpenSSL 1.1.1 FIPS  11 Sep 2018

 

 说明:作者:刘宏缔

 

二,修改sshd的配置文件后,如何测试是否有效?

在重启 sshd 前检查配置文件的有效性和密匙的完整性,运行:

[root@yjweb ~]# sshd -t

无反馈则表示配置文件无问题 

 

三,openssh的安全配置

编辑 配置文件:

 

[root@yjweb ~]# vi /etc/ssh/sshd_config

 

内容包括:

1,禁止使用root登录ssh

#PermitRootLogin yes
PermitRootLogin no

说明:默认值是yes,要修改为no

 

2,修改默认端口,不使用22,避免受到攻击

#Port 22
Port 31234

 

3,新添加一行:只允许指定的用户登录

AllowUsers webop

 

4,禁止密码登录:

PasswordAuthentication no

 

说明:要启用这一项,需要先配置可登录到server的密钥

 

四,修改配置后重启openssh

 

[root@yjweb ~]# systemctl restart sshd

 

五,禁止root登录之后,再次用root连接ssh服务会提示报错:

[SSH] FAIL: x.x.x.x:22 - Connection refused

x.x.x.x是服务器的ip

 

说明:如果使用云服务器ecs,请注意修改服务器的安全规则,

        在安全规则中增加我们为sshd修改的端口,

        因为安全规则比我们自己的防火墙规则位于更外层

 

六,如果禁用密码登录,则需要设置密钥ssh登录:

1,在ssh客户端生成密码钥对

[root@yjweb ~]# ssh-keygen -t rsa

 

说明:中间一路回车即可,

不要设置密码

-t参数可以指定四种算法类型

[-t dsa | ecdsa | ed25519 | rsa]

 

我们选择 rsa

说明:查看参数可以使用通用的帮助命令:

[root@yjweb ~]# man ssh-keygen

 

密钥生成后,可以从用户的home目录下.ssh目录看到

[root@yjweb ~]# ls .ssh/
authorized_keys  id_rsa  id_rsa.pub  known_hosts

 

2,复制公钥到ssh服务器

用ssh-copy-id命令复制

[root@yjweb ~]# ssh-copy-id -i .ssh/id_rsa.pub root@121.122.123.134
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@121.122.123.134's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@121.122.123.134'"
and check to make sure that only the key(s) you wanted were added.

 

登录到ssh服务器,查看authorized_keys

[root@os3 ~]# more .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDiZNKuLsJqi0M......

 

可以看到我们在a服务器的公钥已经成功添加到了ssh服务器的 authorized_keys文件上

 

3,登录时需要指定私钥文件的路径

如果你的私钥位于你当前账号的home目录下的.ssh文件夹下,这个是默认位置,即你没有改动过私钥文件的位置,则可以直接ssh,无需指定文件路径

例如:

[root@yjweb ~]# ssh root@121.122.123.134
Last login: Wed Mar 18 18:53:07 2020 from 121.105.107.47
[root@os3 ~]#

 

如果密钥的路径改名过或在特定目录,则需要指定位置,

用 -i参数即可

[root@yjweb ~]# ssh -p 1234 -i /root/testsshkey root@121.122.123.134
[root@os3 ~]#

 

 七,查看当前的centos版本

[root@yjweb ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)