#1.ssh服務?
SSH 爲 Secure Shell 的縮寫,是應用層的安全協議。SSH 是目前較可靠,專爲遠程登錄會話和其他網絡服務提供安全性的協議。利用 SSH協議可以有效防止遠程管理過程中的信息泄露問題。
#2.ssh登陸方式
##2.1.第一種方式(基於口令的安全驗證)
只要你知道自己帳號和口令,就可以登錄到遠程主機。但會出現“中間人”攻擊。
[student@host ~]$ ssh remoteuser@remotehost //以遠程主機的remoteuser用戶身份登陸連接remotehost主機
remoteuser@remotehost's password:
[student@host ~]$ ssh remoteuser@remotehost hostname //遠程登陸主機但不進入遠程主機,執行命令hostname,返回命令結果
remoteuser@remotehost's password:
remotehost.example.com
##2.2.第二種方式(基於密匙的安全驗證)
你必須爲自己創建一對密匙,把公匙放在需要訪問的服務器上。如果你要連接到SSH服務器上,客戶端軟件就會向服務器發出請求,請求用你的密匙進行安全驗證。服務器收到請求之後,先在該服務器上你的主目錄下尋找你的公匙,然後把它和你發送過來的公匙進行比較。如果兩個密匙一致,服務器就用公用密匙加密“質詢”(challenge)並把它發送給客戶端軟件。客戶端軟件收到“質詢”之後就可以用你的私人密匙解密再把它發送給服務器。
#3.無密碼連接
實驗準備:兩臺服務器
desktop 172.25.0.10
server 172.25.0.11
目的:實現desktop主機無密碼連接server主機
1. server主機上生成公鑰和私鑰,保存在當前用戶家目錄的.ssh目錄下;
[root@server0 ~]# ssh-keygen //生成公鑰私鑰工具
[root@server0 ~]# ls /root/.ssh/ //id_rsa:私鑰,就是鑰匙
id_rsa id_rsa.pub //id_rsa.pub:公鑰,就是鎖
2. 分發鑰匙給client主機
[root@server0 ~]# scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
3. 測試
[root@desktop0 ~]# ssh [email protected] //通過id_rsa直接連接不需要輸入用戶密碼
Last login: Mon Oct 3 03:58:10 2016 from 172.25.0.25
#4.ssh服務安全配置
##4.1.ssh服務的配置文件爲/etc/sysconfig/sshd_config
配置文件的幾個參數;
PasswordAuthentication yes|no //是否開啓用戶密碼認證,yes爲支持no爲關閉
PermitRootLogin yes|no //是否允許超級用戶登陸
AllowUsers student westos //用戶白名單
DenyUsers westos //用戶黑名單ssh服務安全配置
##4.2.ssh服務的配置文件爲/etc/ssh/sshd_config
配置文件的幾個參數;
PasswordAuthentication yes|no //是否開啓用戶密碼認證,yes爲支持no爲關閉
PermitRootLogin yes|no //是否允許超級用戶登陸
AllowUsers student westos //用戶白名單
DenyUsers westos //用戶黑名單
##4.3.在ssh的配置文件中設置相關參數後,並不會即時生效,那麼我們必須重啓服務
systemctl status sshd //查看ssh服務的狀態
systemctl stop sshd //關閉ssh服務
systemctl start sshd //開啓ssh服務
systemctl restart sshd //重新啓動ssh服務
systemctl enable sshd //設定sshd服務開機啓動
systemctl disable sshd //設定sshd服務開機關閉
#5.服務管理
systemctl list-units //列出當前系統服務的狀態
systemctl list-unit-files //列出服務的開機狀態
systemctl mask sshd //鎖定ssh服務,不能啓動服務,除非解鎖
systemctl unmask sshd //解鎖ssh服務
systemctl set-default multi-user.target //開機不開啓圖形
systemctl set-default graphical.target //開機啓動圖形