文章目錄
- Linux OpenSSH 服務管理
- 環境準備
- OpenSSH 服務介紹
- SSH 介紹
- SSH 建立連接的過程
- 加密類型
- 雙向加密過程
- 使用 ssh 訪問遠端CLI
- ssh 工具演示
- ssh工具配置文件
- 配置 ssh 密鑰認證
- ssh 故障模擬
- 故障模擬
- 排故故障
- 自定義 SSH 服務
- 配置文件
- 禁止 root 登錄
- 禁止密碼登錄
- 只允許特定用戶登錄
Linux OpenSSH 服務管理
環境準備
準備兩臺虛擬機:
- server 虛擬機,并設置ip地址為10.1.8.10/24。
- client 虛擬機,并設置ip地址為10.1.8.11/24。
設置主機名稱和名稱解析,以client為例:
永久設置主機名
[root@client ~ 18:49:50]# hostnamectl set-hostname client.hxl.cloud
臨時設置
[root@client ~ 18:50:03]# hostname client.hxl.cloud
驗證
[root@client ~ 18:49:50]# bash
[root@client ~ 18:51:50]#
準備名稱解析
[root@client ~ 18:51:50]# echo "10.1.8.10 server.hxl.cloud server
10.1.8.11 client.hxl.cloud client" >> /etc/hosts[root@client ~ 18:53:06]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.1.8.10 server.hxl.cloud server
10.1.8.11 client.hxl.cloud client
IP地址通過圖形化配置,這里配置Client的IP地址,過程如下
在VM中打開虛擬機,中終端上輸入nmtui
回車
會打開一個界面
選擇第一個選項回車,進入下一個界面,會出現你虛擬機的網卡名稱,選擇這個網卡名稱進行配置
在IPv4下面有個Addresses中填入你需要的配置的IP地址,我這里是10.1.8.11/24的地址
配置完成后用方向鍵上下選中OK
,回到上一個界面選QUIT
或者按ESC
退出回到初始界面選擇第二個選項
在這個界面中,我們需要回車兩次,讓網卡重新讀取我們剛才配置的IP地址,這樣系統才能夠重新識別
最后配置就結束了,全部退出即可
驗證配置是否成功
[root@client ~ 19:02:20]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe55:a3e4/64
可以看見我們的IP地址已經配置完成了
OpenSSH 服務介紹
SSH 介紹
SSH 全稱是 Secure Shell,SSH協議是基于應用層的協議,為遠程登錄會話和其他網絡服務提供安全性的協議。
實現此功能的傳統方式,如 telnet (終端仿真協議)、 rcp、ftp、 rlogin、rsh都是極為不安全的,并且會使用明文傳送密碼。OpenSSH 提供了服務端后臺程序和客戶端工具,用來加密遠程控件和文件傳輸過程中的數據,并由此來代替原來的類似服務。
SSH 建立連接的過程
主要分為下面幾個階段:
- SSH協議版本協商階段,SSH目前包括SSH1和SSH2兩個大版本。
- 密鑰和算法協商階段,SSH支持多種加密算法,雙方根據自己和對端支持的算法進行協商,最終決定要使用的算法。
- 認證階段,服務器和客戶端互相進行身份驗證。
- 會話請求階段,客戶端會向服務器端發送會話請求。會話請求分為這樣幾類:申請對數據傳送進行壓縮、申請偽終端、啟動 X11、TCP/IP 端口轉發、啟動認證代理等。
- 交互會話階段,會話請求通過后,服務器端和客戶端進行信息的交互。例如運行 shell、執行命令、傳遞文件。
加密類型
- 對稱加密,加密和解密都使用一個鑰匙。確保數據的完整性。速度快。
- 非對稱加密,一對鑰匙。公鑰用來加密數據。私鑰用來解密數據。確保數據的安全性。
雙向加密過程
SSH協議是基于非對稱加密方法的,服務器和客戶端都會生成自己的公鑰和私鑰。
- 公鑰用來加密數據。
- 私鑰用來解密數據。
雙向加密過程:
-
服務器創建密鑰對。遠程服務器會在/etc/ssh目錄下生成一個名為多個密鑰對,例如ecdsa類型的密鑰對:ssh_host_ecdsa_key.pub 公鑰和 ssh_host_ecdsa_key 私鑰。之后每回啟動sshd服務的時候,系統會自動在此路徑下查找公鑰。
客戶端請求連接。服務器接到請求后,把公鑰傳給客戶端使用。
-
客戶端記錄服務器公鑰并計算自己的公私鑰。客戶端將服務器傳來的公鑰記錄在**~/.ssh/known_hosts** 中,若是已經記錄有該服務器公鑰,則比對是否一致,一致后就計算客戶端自己的公私鑰。
-
客戶端使用服務器的公鑰加密自己的公鑰并發送給服務器。服務器端擁有客戶端公鑰+自己私鑰,客戶端擁有服務器公鑰+自己私鑰,組成了非對稱加密系統。
-
雙向加解密。服務器發送數據:用客戶端公鑰加密,客戶端收到數據后用自己私鑰解密。客戶端發送數據:用服務器公鑰加密,服務器收到數據后用自己私鑰解密。
使用 ssh 訪問遠端CLI
ssh 工具演示
方式一:只指定IP或主機名
# 通過IP地址
[hxl@client ~ 19:11:26]$ ssh 10.1.8.11
The authenticity of host '10.1.8.11 (10.1.8.11)' can't be established.
ECDSA key fingerprint is SHA256:EkLOT5mPnO2nbsKsf+rIY5J1bXO8pm0taNJxf2MhTU8.
ECDSA key fingerprint is MD5:4e:84:fc:63:a0:ea:b0:a9:35:0e:da:33:d5:bb:2a:8d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.8.11' (ECDSA) to the list of known hosts.
hxl@10.1.8.11's password:
Last login: Sat Sep 13 19:11:26 2025# 通過主機名稱
[hxl@client ~ 19:12:53]$ ssh server
The authenticity of host 'server (10.1.8.10)' can't be established.
ECDSA key fingerprint is SHA256:EkLOT5mPnO2nbsKsf+rIY5J1bXO8pm0taNJxf2MhTU8.
ECDSA key fingerprint is MD5:4e:84:fc:63:a0:ea:b0:a9:35:0e:da:33:d5:bb:2a:8d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server,10.1.8.10' (ECDSA) to the list of known hosts.
hxl@server's password:
Last login: Sat Sep 13 19:12:02 2025
方式二:額外指定用戶名
# 指定root用戶登錄
[hxl@client ~ 19:13:23]$ ssh root@server
root@server's password:
Last login: Sat Sep 13 19:10:29 2025 from client.hxl.cloud
# 或者
[hxl@client ~ 19:13:47]$ ssh -l root server
root@server's password:
Last login: Sat Sep 13 19:13:32 2025 from client.hxl.cloud
方式三:額外指定命令
# 同時指定用戶和命令
[hxl@client ~ 19:14:12]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud# shell提示符,仍然是本機。
[hxl@client ~ 19:14:44]$
ssh工具配置文件
參考 SSH_CONFIG(5)
。
[root@client ~ 19:15:16]# man ssh_config
-
~/.ssh/config,用戶自己的配置,優先級高于全局配置。文件權限不得高于640。
-
/etc/ssh/ssh_config,全局配置,應用于所有用戶。
-
示例:
[hxl@client ~ 11:49:54]$ vim .ssh/config Host *User rootStrictHostKeyChecking no# 清空其他主機秘鑰 [root@client ~ 10:32:42]# > .ssh/known_hosts# 再次登錄不會提示主機key是否校驗,驗證用戶也是root [root@client ~ 11:50:13]# ssh server Warning: Permanently added 'server,10.1.8.10' (ECDSA) to the list of known hosts. root@server's password:
參數說明:
- **Host ***,匹配所有目標服務器。
- StrictHostKeyChecking no,連接目標服務器不校驗主機key,直接接受。
- User root,連接目標服務器默認使用laoma賬戶。
- PreferredAuthentications password,連接目標服務器使用密碼認證。
- IdentityFile,指定私鑰位置。
配置文件權限:建議設置為只能用戶自己讀寫。
[hxl@client ~ 12:50:13]$ ssh server Bad owner or permissions on /home/laoma/.ssh/config[hxl@client ~ 12:51:25]$ chmod 600 .ssh/config [hxl@client ~ 12:51:50]$ ssh server root@server's password:
配置 ssh 密鑰認證
client配置秘鑰登錄服務器
[hxl@client ~ 19:15:35]$ vim .ssh/config
Host *StrictHostKeyChecking noUser root# 查看并修改文件權限[hxl@client ~ 19:32:31]$ ll -d .ssh/config
-rw-rw-r-- 1 hxl hxl 46 9月 13 19:32 .ssh/config
[hxl@client ~ 19:32:37]$ chmod 640 .ssh/config
[hxl@client ~ 19:32:55]$ ll -d .ssh/config
-rw-r----- 1 hxl hxl 46 9月 13 19:32 .ssh/config
[hxl@client ~ 19:32:56]$ ssh-keygen
Generating public/private rsa key pair.# 私鑰保存位置
Enter file in which to save the key (/home/hxl/.ssh/id_rsa): `回車`Enter passphrase (empty for no passphrase): `回車`
Enter same passphrase again: `回車`Your identification has been saved in /home/hxl/.ssh/id_rsa.
Your public key has been saved in /home/hxl/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+kqbNN01EK5SKgVWDdDL0hFEQAF+viCmaLOGz71PvpE hxl@client.hxl.cloud
The key's randomart image is:
+---[RSA 2048]----+
| .oBO*+ . |
| . . .o .. . |
| . .o.o. o |
| o..+o . . |
|... o.o S o |
|+. . o = . . . |
|+o . E . . |
|ooo. = * |
|.oo ooBo. |
+----[SHA256]-----+# 查看生成的文件
[hxl@client ~ 19:35:44]$ ls .ssh/
config id_rsa id_rsa.pub known_hosts# 將公鑰推送給目標服務器上的目標用戶
[hxl@client ~ 19:38:25]$ ssh-copy-id hxl@server
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/hxl/.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
hxl@server's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'hxl@server'"
and check to make sure that only the key(s) you wanted were added.
注意:推給哪個用戶就是哪個用戶,沒有說將密碼推給root用戶,其他普通用戶也可以免輸入進行登陸
驗證
[hxl@client ~ 19:38:58]$ ssh hxl@server hostname
server.hxl.cloud
# 驗證成功
推送公鑰相當于:將公鑰內容保存到目標服務器上目標用戶家目錄下.ssh/authorized_keys中
[hxl@client ~ 12:59:23]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt6I79/d3E9jw/bv0bAJV6FCytohv8ycJ2HBwVXm547dPIVUbDEDYfnzFUGTNtwMF0yUaIZ5YdUwuc114vgQ1KS+pdEpfRaQltunmtWDh58lxyTFKWfQ1WzjzZwYZbECGZ58zsDcS+egvX7mZ9d8bulvMBa4Ye+L8mSLE90xkPfYKdkTpuBHWn4dtVJGSfan78b+MSRIyykmUNkJrvaZgTc5zut9FFxGaAht/DncR6mflYKzdyQCoI5tuqNEMGRFAUSzrvQovXxlPtUx5NOXzlRQvpExUFK/pjYIPXZ3174Lggg1utnaCbBQGBS3wLzdHcFNBpZK9B6bJExKFH3amr hxl@client.hxl.cloud# 在server端查看
[hxl@server ~ 13:00:08]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt6I79/d3E9jw/bv0bAJV6FCytohv8ycJ2HBwVXm547dPIVUbDEDYfnzFUGTNtwMF0yUaIZ5YdUwuc114vgQ1KS+pdEpfRaQltunmtWDh58lxyTFKWfQ1WzjzZwYZbECGZ58zsDcS+egvX7mZ9d8bulvMBa4Ye+L8mSLE90xkPfYKdkTpuBHWn4dtVJGSfan78b+MSRIyykmUNkJrvaZgTc5zut9FFxGaAht/DncR6mflYKzdyQCoI5tuqNEMGRFAUSzrvQovXxlPtUx5NOXzlRQvpExUFK/pjYIPXZ3174Lggg1utnaCbBQGBS3wLzdHcFNBpZK9B6bJExKFH3amr hxl@client.hxl.cloud# 推給目標主機root用戶
[root@server ~ 12:59:50]# mkdir -m 700 .ssh
[root@server ~ 13:02:05]# cp ~laoma/.ssh/authorized_keys .ssh# 客戶端驗證
[laoma@client ~ 13:02:59]$ ssh root@server hostname
server.laoma.cloud
以非交互方式生成密鑰對
[hxl@client ~ 13:05:05]$ ssh-keygen -t rsa -N '' -f id_rsa_new[hxl@client ~ 13:05:31]$ ls *new*
id_rsa_new id_rsa_new.pub
其他選項
# -p選項指定目標服務器 sshd 服務端口號,默認22
[hxl@client ~ 13:05:45]$ ssh -l root -p 1022 server hostname
root@server's password:
server.laoma.cloud# -i 指定私鑰位置
[hxl@client ~ 13:07:18]$ mv .ssh/id_rsa /tmp
[hxl@client ~ 13:10:25]$ ssh -i /tmp/id_rsa root@server hostname
server.laoma.cloud# 如果找不到密鑰,則使用密碼登錄
[hxl@client ~ 13:10:40]$ ssh root@server hostname
root@server's password:
ssh 故障模擬
你是某金融科技公司的 Linux 運維工程師,負責維護核心生產服務器集群。
發現某臺服務器無法通過ssh遠程登錄。
請排查故障,并修復問題。
故障模擬
server設置
可以新添加一個用戶用來模擬故障,這里我接著使用我自己的用戶來模擬
[root@server ~ 08:25:18]# useradd -s /sbin/nologin zhangsan
[root@server ~ 08:26:56]# echo redhat | passwd --stdin zhangsan[root@server ~ 08:27:05]# systemctl stop sshd
client 設置
如果選擇上面的新添加用戶,下面的User 后面寫 zhangsan
[hxl@client ~ 09:24:49]$ chmod 666 .ssh/config
[hxl@client ~ 09:25:40]$ cat > .ssh/config <<EOF
Host *User hxlStrictHostKeyChecking yes
EOF
[hxl@client ~ 09:31:56]$ > .ssh/known_hosts
排故故障
- 錯誤現象:
[hxl@client ~ 09:32:01]$ ssh server
Bad owner or permissions on /home/hxl/.ssh/config
處理方法:
[hxl@client ~ 09:33:02]$ ll -d .ssh/config
-rw-rw-rw- 1 hxl hxl 48 9月 15 09:30 .ssh/config
[hxl@client ~ 09:33:34]$ chmod 600 .ssh/config
[hxl@client ~ 09:34:09]$ ll -d .ssh/config
-rw------- 1 hxl hxl 48 9月 15 09:30 .ssh/config
- 錯誤現象:
[hxl@client ~ 09:34:34]$ ssh server
ssh: connect to host server port 22: Connection refused
處理方法:
[hxl@client ~ 09:36:35]$ ping -c 1 server
PING server.hxl.cloud (10.1.8.10) 56(84) bytes of data.
64 bytes from server.hxl.cloud (10.1.8.10): icmp_seq=1 ttl=64 time=0.279 ms--- server.hxl.cloud ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.279/0.279/0.279/0.000 ms[root@server ~ 09:31:06]# systemctl is-active sshd
inactive
[root@server ~ 09:37:09]# systemctl restart sshd
[root@server ~ 09:37:21]# systemctl is-active sshd
active
- 錯誤現象:
[hxl@client ~ 09:37:49]$ ssh server
No ECDSA host key is known for server and you have requested strict checking.
Host key verification failed.
處理方法:
[hxl@client ~ 09:38:03]$ vim .ssh/config
Host *
User hxl
#StrictHostKeyChecking yes
StrictHostKeyChecking no
- 錯誤現象:
[hxl@client ~ 09:40:59]$ ssh server
Last login: Mon Sep 15 09:04:57 2025 from 10.1.8.1
This account is currently not available.
Connection to server closed.
處理方法:
[root@server ~ 09:37:24]# cat /etc/passwd | grep hxl
hxl:x:1000:1000:hxl:/home/hxl:/sbin/nologin
[root@server ~ 09:42:43]# usermod -s /bin/bash hxl
[root@server ~ 09:42:59]# cat /etc/passwd | grep hxl
hxl:x:1000:1000:hxl:/home/hxl:/bin/bash
- 問題解決:
[hxl@client ~ 09:43:49]$ ssh server
Last login: Mon Sep 15 09:43:46 2025 from client.hxl.cloud
[hxl@server ~ 09:44:23]$
自定義 SSH 服務
配置文件
sshd服務配置文件:/etc/ssh/sshd_config
。幫助 sshd_config(5)
常見配置:
- PermitRootLogin no,禁止 root 用戶登錄。
- root用戶權限不受限制。
- root用戶存在每個linux系統,只需要猜密碼就可以。
- 從審計角度來看,很難跟蹤哪個授權用戶以root身份登錄并進行了更改。 如果用戶必須以普通用戶身份登錄并切換到root帳戶,則會生成一個日志事件,可用于幫助提供問責制。
- PermitRootLogin prohibit-password,禁止root用戶通過密碼登錄。
- PasswordAuthentication no,禁止用戶使用密碼登錄。
- AllowUsers exampleuser,允許特定用戶登錄,該用戶可以提權為root。
- UseDNS no,客戶端連接服務器的時候,服務器不需要反向解析服務端IP地址,提高連接速度。
禁止 root 登錄
進入sshd服務的配置文件中進行配置
[root@server ~ 16:54:35]# vim /etc/ssh/sshd_config
[root@server ~ 16:55:57]# systemctl restart sshd
# 即使配置了免密登錄,也無法遠程登錄
[hxl@client ~ 16:53:10]$ ssh root@server
root@server's password:
Permission denied, please try again.
root@server's password:
Permission denied, please try again.
root@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).# 使用普通用戶登錄,然后提權為root用戶
[hxl@client ~ 16:56:54]$ ssh hxl@server
Last login: Mon Sep 15 13:00:09 2025 from 10.1.8.1
[hxl@server ~ 16:57:01]$ su -
密碼:
上一次登錄:一 9月 15 16:52:57 CST 2025從 client.hxl.cloudpts/4 上
最后一次失敗的登錄:一 9月 15 16:56:31 CST 2025從 client.hxl.cloudssh:notty 上
最有一次成功登錄后有 3 次失敗的登錄嘗試。
[root@server ~ 16:57:08]#
禁止密碼登錄
[root@server ~ 16:57:41]# vim /etc/ssh/sshd_config
[root@server ~ 16:58:43]# systemctl restart sshd
# laowang賬戶未配置密鑰登錄,直接拒絕
[hxl@client ~ 17:05:50]$ ssh laowang@server
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
只允許特定用戶登錄
例如hxl用戶。
[root@server ~ 17:12:05]# vim /etc/ssh/sshd_config
[root@server ~ 17:13:16]# systemctl restart sshd
# laowang輸入正確的密碼也無法登錄
[hxl@server ~ 17:14:01]$ ssh laowang@server
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).[hxl@server ~ 17:14:18]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud
l用戶。
[root@server ~ 17:12:05]# vim /etc/ssh/sshd_config
[root@server ~ 17:13:16]# systemctl restart sshd
[外鏈圖片轉存中…(img-mmRSjzry-1757933486754)]
# laowang輸入正確的密碼也無法登錄
[hxl@server ~ 17:14:01]$ ssh laowang@server
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).[hxl@server ~ 17:14:18]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud