一,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
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,修改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)