新聞中心
一、SSH遠程管理
SSH是一種安全通道協(xié)議,主要用來實現(xiàn)字符界面的遠程登錄、遠程復制等功能。SSH協(xié)議對通信雙方的數據傳輸進行了加密處理,其中包括用戶登錄時輸入的用戶口令。與早期的Telent、RSH、RCP、等應用相比,SSH協(xié)議提供了更好的安全性。

1、配置OpenSSH服務端
在centos 7.4系統(tǒng)中,OpenSSH服務器由openssh、openssh-server等軟件包提供(默認已安裝),并已將sshd添加為標準的系統(tǒng)服務。執(zhí)行“systemctl start sshd”命令即可啟動sshd服務,包括root在內的大部分用戶都可以遠程登錄系統(tǒng)。sshd服務的配置文件默認位于/etc/ssh/sshd_config目錄下,正確調整相關配置項,可以進一步提高sshd遠程登錄的安全性。
1)服務監(jiān)聽選項
sshd服務使用的默認端口號為22,必要時建議修改此端口號,并指定監(jiān)聽服務的具體IP地址,以提高在網絡中的隱蔽性。V2版本要比V1版本的安全性要更好,禁用DNS反向解析可以提高服務器的響應速度。
[root@centos01 ~]# vim /etc/ssh/sshd_config
17 Port 22
19 ListenAddress 192.168.100.10
21 Protocol 2
118 UseDNS no
......
[root@centos01 ~]# systemctl restart sshd
2)用戶登錄控制
sshd服務默認允許root用戶登錄,但在Internet中使用時是非常不安全的。關于sshd服務的用戶登錄控制,通常應禁止root用戶或密碼為空的用戶登錄。另外,可以限制登錄驗證的時間(默認為2分鐘)及最大重試次數,若超過限制后仍未能登錄則斷開連接。
[root@centos01 ~]# vim /etc/ssh/sshd_config
37 LoginGraceTime 2m
38 PermitRootLogin yes
40 MaxAuthTries 6
67 PermitEmptyPasswords no
......
[root@centos01 ~]# systemctl restart sshd
2、登錄驗證方式
對于服務器的遠程管理,除了用戶賬戶的安全控制以外,登錄驗證的方式也非常重要。sshd服務支持兩種驗證方式——密碼驗證、密鑰對驗證,可以設置只使用其中一種方式,也可以兩種方式都啟用。
- 密碼驗證:對服務器中本地系統(tǒng)用戶的登錄名稱、密碼進行驗證。這種方式使用最為簡便,但從客戶端角度來看,正在連接的服務器有可能被假冒;從服務器角度來看,當遭遇密碼窮舉第三者時防御能力比較弱。
- 密鑰對驗證:要求提供相匹配的密鑰信息才能通過驗證。通常先在客戶端中創(chuàng)建一對密鑰文件(公鑰、私鑰),然后將公鑰文件放到服務器中的指定位置。遠程登錄時,系統(tǒng)將使用公鑰,私鑰進行加密/解密關聯(lián)驗證,大大增強了遠程管理的安全性。該方式不易被假冒,且可以免交互登錄,在Shell中被廣泛使用。
當密碼驗證,密鑰對驗證都啟用時,服務器將優(yōu)先使用密鑰對驗證。對于安全性要求較高的服務器,建議將密碼驗證方式禁用,只允許啟用密鑰對驗證方式;若沒有特殊要求,則兩種方式都可以啟用。
[root@centos01 ~]# vim /etc/ssh/sshd_config
43 PubkeyAuthentication yes
47 AuthorizedKeysFile .ssh/authorized_keys
66 PasswordAuthentication yes
......
[root@centos01 ~]# systemctl restart sshd
其中,公鑰文件用來保存多個客戶端上傳的公鑰文本,以便與客戶端本地的私鑰文件進行匹配。
二、使用SSH客戶端程序
在Centos 7.4系統(tǒng)中,OpenSSH客戶端由openssh-clients軟件包提供(默認已安裝),其中包括ssh遠程登錄命令,以及scp、sftp遠程復制和文件傳輸命令等。
1、命令程序ssh遠程登錄
通過ssh命令可以遠程登錄sshd服務,為用戶提供一個安全的Shell環(huán)境,以便對服務器進行管理和維護。使用時應指定登錄用戶、目標主機地址作為參數。示例如下:
[root@centos02 ~]# ssh [email protected]
[email protected]'s password:
Last login: Mon Nov 11 19:02:50 2019 from 192.168.100.254
[root@centos01 ~]#
[root@centos01 ~]#
[root@centos01 ~]# ssh [email protected]
The authenticity of host '192.168.100.10 (192.168.100.10)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.10' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Mon Nov 11 19:03:08 2019 from 192.168.100.20
[root@centos01 ~]# who
root pts/1 2019-11-11 19:03 (192.168.100.20)
root pts/2 2019-11-11 19:04 (192.168.100.10)
如果sshd服務器使用了非默認的端口(如2222),則在登錄時必須通過“-p”選項指定端口號。示例如下:
[root@centos01 ~]# vim /etc/ssh/sshd_config
Port 2222
[root@centos01 ~]# systemctl restart sshd
[root@centos02 ~]# ssh -p 2222 [email protected]
[email protected]'s password:
Last login: Mon Nov 11 19:20:28 2019 from 192.168.100.10
[root@centos01 ~]#
2、scp遠程復制
通過scp命令可以利用SSH安全連接與遠程主機相互復制文件,使用scp命令時,除了必須指定復制源、目標之外,還應指定目標主機地址、登錄用戶,執(zhí)行后根據提示輸入驗證口令即可。示例如下:
[root@centos02 ~]# scp
[email protected]:/etc/ssh/sshd_config ./
[email protected]'s password:
sshd_config 100% 3910 3.6MB/s 00:00
[root@centos02 ~]# scp -r ./sshd_config
[email protected]:/opt
[email protected]'s password:
sshd_config 100% 3910 1.2MB/s 00:00
3、sftp安裝FTP
通過sftp命令可以利用SSH安全連接與遠程主機上傳、下載文件,采用了與FTP類似的登錄過程和交互環(huán)境,便于目錄資源管理。示例如下:
[root@centos01 ~]# cd /opt/
[root@centos01 opt]# sftp [email protected]
[email protected]'s password:
Connected to 192.168.100.20.
sftp> pwd
Remote working directory: /root
sftp> put sshd_config
Uploading sshd_config to /root/sshd_config
sshd_config 100% 3910 6.4MB/s 00:00
sftp> get sshd_config
Fetching /root/sshd_config to sshd_config
/root/sshd_config 100% 3910 3.6MB/s 00:00
sftp> exit
三、構建密鑰對驗證的SSH體系
密鑰對驗證方式可以遠程登錄提供更好的安全性。在Linux服務器、客戶端中構建密鑰對驗證SSH體系的基本過程。如下圖所示,整個過程包括四步,首先要在SSH客戶端以zhangsan用戶身份創(chuàng)建密鑰對,并且要將創(chuàng)建的公鑰文件上傳至SSH服務器端,然后要將公鑰信息導入服務器端的目標用戶lisi的公鑰數據庫,最后以服務器端用戶lisi的身份登錄驗證。
1、在客戶端創(chuàng)建密鑰對
在客戶端中,通過ssh-keygen工具為當前用戶創(chuàng)建密鑰對文件。可用的加密算法為ECDSA或DSA(ssh-keygen命令的“-t”選項用于指定算法類型)。示例如下:
[root@centos02 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:zv0EdqIuOfwSovN2Dkij08y9wZ0f1+IyhY7LFNKKzkk root@centos02
The key's randomart image is:
+---[DSA 1024]----+
| |
| |
| |
| . |
| o . o S.+ . |
| * *.+.=.+.= |
|o E.*o+==.+ o |
| =o..*Oo++ + |
| ++oo+*+o. . |
+----[SHA256]-----+
[root@centos02 ~]# ls -lh ~/.ssh/id_dsa*
-rw------- 1 root root 668 11月 12 16:11 /root/.ssh/id_dsa
-rw-r--r-- 1 root root 603 11月 12 16:11 /root/.ssh/id_dsa.pub
新生成的密鑰對文件中,id_das是私鑰文件,權限默認為600,對于私鑰文件必須妥善保管,不能泄露給他人;id_dsa.pub是公鑰文件,用來提供給ssh服務器。
2、將公鑰文件上傳至服務器
將上一步生成的公鑰文件上傳至服務器,并部署到服務器端用戶的公鑰數據庫中。上傳公鑰文件時可以選擇SCP、FTP、HTTP甚至發(fā)送E-mail等任何方式。
root@centos02 ~]# ssh-copy-id -i ./.ssh/id_dsa.pub
[email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "./.ssh/id_dsa.pub"
The authenticity of host '192.168.100.10 (192.168.100.10)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
3、在客戶端使用密鑰對驗證
當私鑰文件(客戶端)、公鑰文件(服務器)均部署到位以后,就可以在客戶端中進行測試了。首先確認客戶端中當前的用戶為root,然后通過ssh命令以服務器端用戶root的身份進行遠程登錄。如果密鑰對驗證方式配置成功,則在客戶端將會要求輸入私鑰短語,以便調用私鑰文件進行匹配(若未設置私鑰短語,則直接登入目標服務器)。
[root@centos02 ~]# ssh [email protected]
Last login: Tue Nov 12 16:03:56 2019 from 192.168.100.254
[root@centos01 ~]# who
root pts/0 2019-11-12 17:35 (192.168.100.20)
root pts/2 2019-11-12 16:03 (192.168.100.254) 本文標題:CentOS7.4中的遠程訪問控制
文章源于:http://fisionsoft.com.cn/article/cdecgig.html


咨詢
建站咨詢
