Linux的ssh服務以及管理命令
阿新 • • 發佈:2018-11-22
SSH 為 Secure Shell 的縮寫,由 IETF 的網路小組(Network Working Group)所制定;SSH 為建立在應用層基礎上的安全協議。SSH 是目前較可靠,專為遠端登入會話和其他網路服務提供安全性的協議。利用 SSH 協議可以有效防止遠端管理過程中的資訊洩露問題。
一、ssh基本服務
1.sshd環境搭建
設定兩個虛擬機器IP地址172.25.254.100和172.25.254.200
2.連線方式:ssh
[email protected] ##文字模式的連結 ssh -X [email protected] ##可以在連結成功後開機圖形
注意:
第一次連結陌生主機是要建立認證檔案
所以會詢問是否建立,需要樹入yes
在次連結此臺主機時,因為已經生成~/.ssh/know_hosts檔案所以不需要再次輸入yes
3.遠端複製:scp file [email protected]:dir ##上傳 scp [email protected]:file dir ##下載
二、sshd 的key認證
1.生成認證KEY
[[email protected] ~]# ssh-keygen ##生成金鑰的命令 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): ##指定儲存加密字元的檔案(使用預設) Enter passphrase (empty for no passphrase): ##設定密碼(使用空密碼) Enter same passphrase again: ##確認密碼 Your identification has been saved in "/root/.ssh/id_rsa". ##私鑰(鑰匙) Your public key has been saved in "/root/.ssh/id_rsa.pub". ##公鑰(鎖) The key fingerprint is: 86:61:e4:f1:6e:51:3a:4b:d7:3c:1b:2f:e8:3f:b0:5d
[email protected] The key's randomart image is: +--[ RSA 2048]----+ | o . | | o o o o | | + * . = | | . = = . = | | . S . o . | | o .. .E | | .+ . | | ..o | | .. | +-----------------+
2.加密服務[[email protected] .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] ##加密sshd服務 The authenticity of host '172.25.254.200 (172.25.254.200)' can't be established. ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08. 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. [[email protected] .ssh]# ls authorized_keys id_rsa id_rsa.pub known_hosts ## 此檔案出現表示加密完成
3.分發鑰匙scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
4.測試
在客戶主機中(172.25.254.100)
ssh [email protected] ##連線時發現直接登陸不需要root登陸系統的密碼認證
三、.sshd的安全設定
vim /etc/ssh/sshd_config ##root許可權下配置 78 PasswordAuthentication yes|no ##是否允許使用者通過登陸系統的密碼做sshd的認證 48 PermitRootLogin yes|no ##是否允許root使用者通過sshd服務的認證 52 Allowusers student westos ##設定使用者白名單,白名單出現預設不再名單中的使用者不能使用sshd 53 Denyusers westos ##設定使用者黑名單,黑名單出現預設不再名單中的使用者可以使用sshd ##本次實驗我將Allowusers 和Denyusers 放在 79 80 中
四、.新增sshd登陸登陸資訊
vim /etc/motd ##檔案內容就是登陸後顯示的資訊、
五、使用者的登陸審計
w ##檢視正在使用當前系統的使用者 -f ##檢視使用來源 -i ##顯示IP
last
last /var/run/utmp ##檢視使用過並退出的使用者資訊 /var/log/wtmp ##試圖登陸但沒成功的使用者 /var/log/btmp