1. 程式人生 > 實用技巧 >linux系統中配置sshd服務(遠端控制服務)

linux系統中配置sshd服務(遠端控制服務)

SSH(Secure Shell)是一種能夠以安全的方式提供遠端登入的協議,也是目前遠端管理Linux的首先方式。在此之前,一般使用FTP或Telnet來進行遠端登入。但是因為它們以明文的形式在網路中傳輸賬戶密碼和資料資訊,因此很不安全,很容易受到黑客發起的中間人的攻擊,者輕則篡改傳輸的資料資訊,重則直接抓取伺服器的賬戶密碼。

sshd是基於SSH協議開發的一款遠端管理服務程式,不僅使用起來方便快捷,而且能夠提供兩種安全驗證的方法:

基於口令的驗證---用賬戶和密碼來驗證登入;

基於金鑰的驗證---需要在本地生成金鑰對,然後把金鑰對中的公鑰上傳至伺服器,並與伺服器中的公鑰進行比較;該方式相較來說更安全。

sshd服務的配置資訊儲存在/etc/ssh/sshd_config檔案中。運維人員一般會把儲存著最主要配置資訊的檔案稱為主配置檔案,而配置檔案中有許多以#開頭的註釋行,要想讓這些配置引數生效,需要在修改引數後再去掉前面的#號。

RHEL7中,已經預設安裝並啟用了sshd服務程式。

使用兩臺虛擬機器進行測試,兩臺虛擬機器模式均為僅主機模式,手動配置IP地址、子網、閘道器等

1、檢視虛擬機器基本資訊

[root@host1 network-scripts]# cat ifcfg-eno16777728  ## 虛擬機器1 網絡卡配置
TYPE=Ethernet
BOOTPROTO
=static NAME=eno16777728 ONBOOT=yes IPADDR=192.168.10.10 NETMASK=255.255.255.0 GATEWAY=192.168.10.1 DNS1=192.168.10.1 [root@host1 network-scripts]# ifconfig | head -n 3 ## IP eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link> [root@host2 network-scripts]# cat ifcfg-eno16777728 ## 虛擬機器2網絡卡配置 TYPE=Ethernet BOOTPROTO=static NAME=eno16777728 ONBOOT=yes IPADDR=192.168.10.20 NETMASK=255.255.255.0 GATEWAY=192.168.10.1 DNS1=192.168.10.1 [root@host2 network-scripts]# ifconfig | head -n 3 ## IP eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link>

2、測試使用ssh命令在虛擬機器1中遠端控制虛擬機器2:

[root@host1 network-scripts]# ifconfig | head -n 3  ## 當前主機IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fee4:f7b9  prefixlen 64  scopeid 0x20<link>
[root@host1 network-scripts]# ping -c 3 192.168.10.20  ##  測試和虛擬機器2的聯通性
PING 192.168.10.20 (192.168.10.20) 56(84) bytes of data.
64 bytes from 192.168.10.20: icmp_seq=1 ttl=64 time=0.177 ms
64 bytes from 192.168.10.20: icmp_seq=2 ttl=64 time=0.241 ms
64 bytes from 192.168.10.20: icmp_seq=3 ttl=64 time=0.221 ms

--- 192.168.10.20 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.177/0.213/0.241/0.026 ms
[root@host1 network-scripts]# ssh 192.168.10.20  ## 遠端控制
root@192.168.10.20's password:   ## 此處輸入虛擬機器2的root密碼
Last login: Tue Nov  3 20:25:41 2020
[root@host2 ~]# ifconfig | head -n 3  ## 檢視當前IP,已經變為虛擬機器2的IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.20  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:feaa:2b29  prefixlen 64  scopeid 0x20<link>
[root@host2 ~]# exit  ## 退出
logout
Connection to 192.168.10.20 closed.
[root@host1 network-scripts]# ifconfig | head -n 3  ## 回到虛擬機器終端
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>

3、通過調整虛擬機器2的配置檔案,將虛擬機器2設定為拒絕root遠端登入

[root@host2 network-scripts]# vim /etc/ssh/sshd_config 

將下圖箭頭所指處有yes改為no(第48行),去掉行首的#號,儲存退出

4、在虛擬機器1中測試:

[root@host1 network-scripts]# ssh 192.168.10.20
root@192.168.10.20's password: 
Last login: Tue Nov  3 20:34:34 2020 from 192.168.10.10
[root@host2 ~]# exit
logout
Connection to 192.168.10.20 closed.

5、一般的服務程式並不會在修改配置檔案之後立即生效,需要手動重啟相應的服務程式。

[root@host2 network-scripts]# systemctl restart sshd
[root@host2 network-scripts]# systemctl enable sshd

6、再次在虛擬機器1中測試

[root@host1 network-scripts]# ssh 192.168.10.20  ## 測試,已經拒絕登入
root@192.168.10.20's password: 
Permission denied, please try again.
root@192.168.10.20's password: