遠程連接服務---SSH

一、SSH 簡介

      SSH(Secure Shell)是一套協議標準,可以用來實現兩臺機器之間的安全登錄以及安全的數據傳送,其保證數據安全的原理是非對稱加密
      傳統的對稱加密使用的是一套祕鑰,數據的加密以及解密用的都是這一套祕鑰,可想而知所有的客戶端以及服務端都需要保存這套祕鑰,泄露的風險很高,而一旦祕鑰便泄露便保證不了數據安全。
      非對稱加密解決的就是這個問題,它包含兩套祕鑰 - 公鑰以及 私鑰,其中公鑰用來加密,私鑰用來解密,並且通過公鑰計算不出私鑰,因此私鑰謹慎保存在服務端,而公鑰可以隨便傳遞,即使泄露也無風險。
      保證SSH安全性的方法,簡單來說就是客戶端和服務端各自生成一套私鑰和公鑰,並且互相交換公鑰,這樣每一條發出的數據都可以用對方的公鑰來加密,對方收到後再用自己的私鑰來解密。

 

由上一張圖可以看出來,兩臺機器除了各自的一套公、私鑰之外,還保存了對方的公鑰,因此必然存在一個交換各自公鑰的步驟

二、軟件配置啓動

      在CentOS7系統中,默認安裝中就已經安裝了包含SSH服務所需要的的軟件OpenSSL和OpenSSH,可以通過下面的命令進行驗證。

[root@web01 ~]# rpm -qa|egrep "openss"
openssh-server-7.4p1-21.el7.x86_64
openssl-libs-1.0.2k-19.el7.x86_64
openssh-clients-7.4p1-21.el7.x86_64
openssl-1.0.2k-19.el7.x86_64
openssh-7.4p1-21.el7.x86_64

啓動SSH服務

[root@web01 ~]# systemctl start sshd

查看是否有對應的SSH的22端口

[root@web01 ~]# netstat -lntup|grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2132/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      2132/sshd 

查看軟件配置

[root@fbforever ~]# cat /etc/ssh/sshd_config 

Port 22   # SSH默認端口號,建議修改

ListenAddress 0.0.0.0   # 監聽網卡地址

PermitRootLogin no      # 是否允許root用戶登錄

PermitEmptyPasswords no # 是否允許空密碼登錄

GSSAPIAuthentication no # 解決遠程連接慢的問題

UseDNS no               # 不使用,解決遠程連接慢的問題

三、 SSH服務安全配置

  1. 看好後門策略。用密鑰登錄,不用密碼登陸
  2. 看好後門策略牤牛陣法:解決SSH安全問題
      a.防火牆封閉SSH,指定源IP限制局域網(信任公網)
      b.開啓SSH只監聽本地內網IP(ListenAddress 172.16.1.61)
  3. 看好後門策略。儘量不給服務器外網IP
  4. 各種最小化原則(軟件安裝-授權)
  5. 給系統的重要文件或命令做一個指紋,隨時可以對比文件增刪改並報警
  6. 給核心文件加鎖 chattr +i
  7. 看好後門策略。禁止直接遠程SSH連接,登錄VPN服務在連接
  8. 看好後門策略。更改默認端口22,禁止管理員root遠程連接

四、遠程連接命令

  1. 基本語法

ssh -p22 [email protected] [命令] 

   例:

ssh -p22 [email protected] uptime  # 執行後提示輸入密碼信息

  2. 其他命令

   scp命令使用:

# 從遠程主機向本地複製文件
[root@web01 ~]# scp -rp [email protected]:/home/lk /root
[email protected]'s password: 
k2.sql                                                     100%    0     0.0KB/s   00:00    
k.zip                                                      100%  176     0.2KB/s   00:00    
.bash_history                                              100%   32     0.0KB/s   00:00  

# 從本地向遠程主機複製文件  
[root@web01 ~]# scp -r /root [email protected]:/home/lk
[email protected]'s password: 
k2.sql                                                     100%    0     0.0KB/s   00:00    
k3.sql                                                     100%    0     0.0KB/s   00:00    
.bash_profile                                              100%  193     0.2KB/s   00:00    

   sftp命令使用:

[root@web01 ~]# sftp -oPort=22 [email protected]
Connecting to 10.0.0.8...
[email protected]'s password:

五、 免密方式登錄配置

  1. 生產密鑰對:

[root@m01 ~]# 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.

  2. 分發密鑰對:

[root@m01 ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected] "-o StrictHostKeyChecking=no"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/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

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -o ' StrictHostKeyChecking=no' '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

  3. 進行遠程連接測試:

[root@m01 ~]# ssh 172.16.1.7
Last login: Tue Feb 25 17:38:15 2020 from 10.0.0.1
[root@web01 ~]# 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章