1 SSH簡介
SSH (secure shell)為建立在應用層基礎上的安全協定。SSH 是目前較可靠,專為遠端登陸會話和其他網絡服務提供安全性的協定。TCP port 22
2 縱向向比較
telnet(telecommunications network)
消息資料包為明文不加密,安全性差,容易被破解。TCP port 23
RDP (remote desktop protocal ) 連接配接
windows遠端桌面管理(圖形界面) TCP port 3389 and UDP port 3389
RFB (remote frame buffer) 圖形化遠端管理協定 連接配接
VNC (Virtual Network Computing)使用的協定。(在linux unix Macos 下的圖形界面遠端管理工具)
3 橫向比較
SSH協定規範存在一些小版本的差異,但是有兩個主要的大版本:SSH1 (版本号 1.XX) 和 SSH2 (版本号 2.00)。事實上,SSH1和SSH2是兩個完全不同互不相容的協定。SSH2明顯地提升了SSH1中的很多方面。首先,SSH是宏設計,幾個不同的功能(如:認證、傳輸、連接配接)被打包進一個單一的協定,SSH2帶來了比SSH1更強大的安全特性,如基于MAC的完整性檢查,靈活的會話密鑰更新、充分協商的加密算法、公鑰證書等等。
SSH2由IETF标準化,且它的實作在業界被廣泛部署和接受。由于SSH2對于SSH1的流行和加密優勢,許多産品對SSH1放棄了支援。在寫這篇文章的時候,OpenSSH仍舊支援SSH1和SSH2,然而在所有的現代Linux發行版中,OpenSSH伺服器預設禁用了SSH1。
sshd服務
提供兩種安全驗證方式
1 基于密碼驗證:經過驗證賬号和密碼即可遠端登陸主機,所有傳輸的資料都會被加密,但是不能保證你正在連接配接的伺服器就是你想連接配接的伺服器。可能會有别的伺服器在冒充真正的伺服器,也就是受到“中間人”這種方式的攻擊。
2 基于密鑰的安全驗證:需要在本地生成密鑰對後,将公鑰發送給遠端主機。用戶端軟體就會向伺服器送出請求,請求用你的密匙進行安全驗證。伺服器收到請求之後,先在該伺服器上你的主目錄下尋找你的公用密匙,伺服器就用公用密匙加密“質詢”(challenge)并把它發送給用戶端軟體。用戶端軟體收到“質詢”之後就可以用你的私人密匙解密再把它發送給伺服器。
sshd服務配置檔案 (/etc/ssh/sshd_config)
#Port 22 //預設端口号22
#ListenAddress 0.0.0.0 //設定ssh監聽的ip位址
# The default requires explicit activation of protocol 1
#Protocol 2 //協定版本号
# HostKey for protocol version 1 //私鑰
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2 //私鑰
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key //DSA私鑰
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
PubkeyAuthentication yes //允許密鑰登陸
#LogLevel INFO 日志的等級
# Authentication:
#LoginGraceTime 2m 登入的寬限時間,預設2分鐘沒有輸入密碼,則自動斷開連接配接
#PermitRootLogin no
PermitRootLogin yes 是否允許管理者直接登入,'yes'表示允許
#StrictModes yes 是否讓sshd去檢查使用者主目錄或相關檔案的權限資料
#MaxAuthTries 6 最大認證嘗試次數,最多可以嘗試6次輸入密碼。之後需要等待某段時間後才能再次輸入密碼
#MaxSessions 10 允許的最大會話數
#RSAAuthentication yes
#PubkeyAuthentication yes
The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
.AuthorizedKeysFile .ssh/authorized_keys 伺服器生成一對公私鑰之後,會将公鑰放到.ssh/authorizd_keys裡面,将公鑰發給用戶端
PasswordAuthentication yes 是否允許支援基于密碼的認證
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no 是否允許任何的密碼認證
# Kerberos options 是否支援kerberos(基于第三方的認證,如LDAP)認證的方式,預設為no
#UseDNS yes 是否反解DNS,如果想讓用戶端連接配接伺服器端快一些,這個可以改為no
Subsystem sftp /usr/libexec/openssh/sftp-server 支援 SFTP ,如果注釋掉,則不支援sftp連接配接
AllowUsers user1 user2 登入白名單(預設沒有這個配置,需要自己手動添加),允許遠端登入的使用者。如果名單中沒有的使用者,則提示拒絕登入
sshd使用執行個體
1修改ssh使用端口
vim /etc/ssh/sshd_config
Port + 端口号
Systemctl restart sshd
Systemctl stop firewalld
或
添加防火牆允許政策
[[email protected] ~]# firewall-cmd --permanent --add-port=2222/tcp
[[email protected] ~]# firewall-cmd –reload #添加自定義端口到服務
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 4444
注意:如果執行該指令時出現以下提示,請換個端口再試:
ValueError: Port tcp/4444 already defined
2拒絕root使用者遠端登陸
Vim /etc/ssh/sshd_config
PermitRootLogin no
Systemctl restart sshd
3允許特定使用者使用ssh登陸,其他使用者都無法登陸
使用白名單
Vim /etc/ssh/sshd_config
最後寫入
AllowUsers + 使用者名
Systemctl restart sshd
允許使用ssh登陸的使用者 如果沒寫root; Permitroot yes root使用者無法登陸
4虛拟機與虛拟機之間實作ssh互信,免密登陸
#建立密鑰對
[[email protected] ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/redhat/.ssh/id_rsa): /選密鑰對儲存路徑
Created directory ‘/home/redhat/.ssh’.
Enter passphrase (empty for no passphrase): /輸入密碼
Enter same passphrase again:
Your identification has been saved in /home/redhat/.ssh/id_rsa.
Your public key has been saved in /home/redhat/.ssh/id_rsa.pub.
#發送公鑰給遠端登陸主機
1)[[email protected] .ssh]# ssh-copy-id 172.24.8.131
2)scp /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
#遠端登陸主機端
Vim /etc/ssh/sshd_config
#将允許密碼登陸驗證的參數設定為no
PasswordAuthentication no
#将允許密鑰驗證設定為yes
PubkeyAuthentication yes //允許密碼同時允許密鑰密碼優先
Systemctl restart sshd
效果
5通過xshell遠端免密登陸
在xshell中選工具菜單點選建立使用者密鑰生成向導 (如圖)
密鑰類型可選 長度可選
名稱任意,可添加密碼,增強安全性
生成公鑰