Linux--ssh服務
1.簡介
SSH是Secure Shell Protocol的縮寫,它是一種安全通道協議,主要用來實現安全的遠端登入,遠端複製等功能,SSH協議對通訊雙方的資料傳輸進行加密處理,加密後再對資料傳輸,確保了傳輸資料的安全性。
在預設的狀態下,SSH服務主要提供兩個服務的功能:
- 一: 是提供類似telent遠端聯機伺服器的服務,及SSH服務。
- 二:類似FTP服務的sftp-server,藉助SSH協議來傳輸資料的,提供更安全的SFTP服務。
SSH服務預設埠為tcp 22 號埠
2.SSH服務搭建環境介紹
此次服務搭建用到的是linux系統的centos7,SSH服務需要一個客戶端和一個服務端構成,具體情況如下:
-
伺服器
IP
作用
server
192.168.2.101
服務端
client
192.168.2.10
客戶端
- ·服務名稱:sshd
- ·服務端主程式:/usr/sbin/sshd
- ·服務端配置檔案:/etc/ssh/sshd_config
- ·客戶端配置檔案:/etc/ssh/ssh_config
3.SSH服務搭建
1.配置SSH服務端
-
進入檔案所在目錄
[root@server ~]# cd /etc/ssh/
將服務端的配置檔案進行備份
[root@server ssh]# cp -r sshd_config sshd_config.bak
-
在伺服器配置檔案最後一行中填加使用者:
-
AllowUsers:使用者 amber 在任何(admin)客戶端均可登入;使用者 hanjaili 只允許在 IP 地址為 192.168.2.101 的客戶端登入。且僅允許此使用者通過 ssh 協議遠端登入
- DenyUsers:禁止使用者lisi登入注意:AllowUsers不要與DenyUsers 同時使用
-
[root@ssh-server ssh]# vim sshd_config
AllowUsers amber hanjiali @192.168.2.10
#DenyUsers lisi
-
重新啟動服務
-
[root@ssh-server ssh]# systemctl restart sshd
sshd_config 配置檔案詳解(監聽IP換成192.168.2.15)
2.配置SSH客戶端
- 建立使用者admin,hanjiali併為其設定密碼
-
[root@client ~]# useradd admin
[root@client ~]# useradd hanjiali
[root@client ~]# echo "zxc123" |passwd --stdin admin &> /dev/null
[root@client ~]# echo "zxc123" |passwd --stdin hanjiali &> /dev/null
4.驗證使用者及密碼是否能登陸
-
驗證是否能登入成功
- ssh命令(遠端安全登入)格式:ssh user@host (若客戶機與主機使用者名稱相同,可省去user@) 埠選項:-p 22
-
[root@client admin]# su - admin 上一次登入:六 10月 10 15:37:50 CST 2020pts/0 上 [admin@client ~]$ ssh [email protected]
-
驗證服務端和客戶端是否能夠互相傳遞資訊
-
scp命令(遠端安全複製)格式1:scp user@host:file1 file2 格式2:scp file1 user@host:file2
- 客戶端從服務端複製檔案
-
服務端建立檔案
[root@server ~]# mkdir /Carrie
[root@server ~]# cd /Carrie
[root@server Carrie]# touch hanjiali{0..5}
[root@server Carrie]# ls
hanjiali0 hanjiali1 hanjiali2 hanjiali3 hanjiali4 hanjiali5
客戶端建立目錄[root@client ~]# mkdir /hanjiali
客戶端從伺服器中複製檔案成功[root@client ~]# scp [email protected]:/Carrie/* /hanjiali
The authenticity of host '192.168.2.10 (192.168.2.10)' can't be established.
ECDSA key fingerprint is SHA256:A5nQ3zTujOsKvkf1OMaUzYDwj338rg1umdkOqIlS1BY.
ECDSA key fingerprint is MD5:ba:e8:94:4e:d6:be:7b:68:66:29:34:31:ce:7c:ed:f5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.10' (ECDSA) to the list of known hosts.
[email protected]'s password:
hanjiali0 100% 0 0.0KB/s 00:00
hanjiali1 100% 0 0.0KB/s 00:00
hanjiali2 100% 0 0.0KB/s 00:00
hanjiali3 100% 0 0.0KB/s 00:00
hanjiali4 100% 0 0.0KB/s 00:00
hanjiali5
- 客戶端傳資訊給服務端
-
[root@client ~]# ls 123.txt anaconda-ks.cfg client.txt han [root@client ~]# scp 123.txt [email protected]:/han [email protected]'s password: scp: /han/123.txt: Permission denied [root@client ~]# scp 123.txt [email protected]:/han [email protected]'s password: 123.txt 100% 0 0.0KB/s 00:00 [root@server ~]# ls /han 123.txt
5.實現免密碼登入
- 1.構建金鑰對認證:
-
[admin@client ~]$ ssh-keygen -t rsa
或者ssh-keygen -t rsa -P “zxc123” -f
- 2. 將公鑰傳給伺服器
-
[admin@client ~]$ scp .ssh/id_rsa .ssh/id_rsa.pub [email protected]:/Carrie/ [email protected]'s password: id_rsa 100% 1766 927.4KB/s 00:00 id_rsa.pub 100% 394 210.7KB/s 00:00
- 3. 在伺服器中匯入公鑰
-
[root@client ~]# ssh-copy-id -I [email protected] /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/admin/.ssh/id_rsa.pub" /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed Enter passphrase for key '/home/admin/.ssh/id_rsa': /bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system. (if you think this is a mistake, you may want to use -f option) [root@server ~]# su - hanjiali 上一次登入:六 10月 10 19:30:36 CST 2020pts/0 上 [hanjiali@server ~]$ ll .ssh/ 總用量 4 -rw------- 1 hanjiali hanjiali 394 10月 10 19:58 authorized_keys [hanjiali@server ~]$ cat .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTSNm8st2eOTXIPUUTDvnBwUDk/3s1yst/7ocy+HPhqxijOYPwPI3NPMTKXvpDYGQYxRvmrTibjheWt+HUg5fsq47ebSCjAqoggPhrqVdUQX6TQ+uo4ezeZWGGVGxsKdwly1wJGRwr7YEU4Q4LWop19c1bKVNuHIqOkC1K2KqGNfgabSsS5pL8m9zjXzsmreWRuQcelZd5ZRzl9s40VwYwY9Uqg30O4/3NKNHrXm37JxKNUXON3jFiFMv7frVr4Oq9huQ/itUMnLvioy+6h+063Rm6NXK/zl9STgCbpl9kOITuddEBwndKP7KLRzZJtG/IckaKCUPs4hWRs9dDd+jJ admin@client
- 4.在客戶端使用金鑰對驗證
-
1,改配置檔案 vim /etc/ssh/sshd_config 2.客戶端登入 [root@client ~]# ssh [email protected] [email protected]'s password: Last login: Sat Oct 10 20:00:02 2020
至此:SS服務構建成功