SSH遠端管理
SSH遠端控制介紹
SSH
是一種安全通道協定,主要用來實作字元界面的遠端登入、遠端複制等功能。
SSH
協定對通信雙方的資料傳輸進行了加密處理,其中包括使用者登入時輸入的使用者密碼。與早期的
telnet、rsh、rcp
等應用相比,
SSH
協定提供了更好的安全性。
OpenSSH伺服器
SSH協定
- 為客戶機提供安全的
環境,用于與遠端管理shell
- 預設端口:
TCP 22
OpenSSH
- 服務名稱:
sshd
- 服務端主程式:
/usr/sbin/sshd
- 服務端配置檔案:
/etc/ssh/sshd_config
- 用戶端配置檔案:
ssh_config
服務監聽選項
- 端口号、協定版本、監聽IP位址
- 禁用反向解析
#Port 22 //端口号
#AddressFamily any
#ListenAddress 0.0.0.0 //ipv4監聽位址
#ListenAddress :: //ipv6監聽位址
使用者登入控制
- 禁止root使用者、空密碼使用者
- 登入時間、重試次數
-
(黑白名單,允許和拒絕)AllowUsers、DenyUsers
#LoginGraceTime 2m //會話時間
#PermitRootLogin yes //是否進制root登入
#StrictModes yes //是否驗證通路權限
#MaxAuthTries 6 //驗證次數6次
#MaxSessions 10 //通路的最大連結數
#PubkeyAuthentication yes //是否驗證公鑰
登入驗證對象
- 伺服器中的本地使用者賬戶
登入驗證方式
- 密碼驗證:核對使用者名、密碼是否比對
- 密鑰對驗證:核對客戶的私鑰、服務端公鑰是否比對
使用SSH用戶端程式
-
指令——遠端安全登入ssh
-
指令——遠端安全複制scp
-
指令——安全FTP上下載下傳sftp
- get 下載下傳
- put 上傳
- bye 退出
Demo
在
VMware 15
中打開兩天
Linux
系統,一台用作主伺服器(
tast01IP位址:192.168.144.133
),一台用作遠端終端(
tast02IP位址:192.168.144.135
),使用
SSH
協定登入主伺服器
使用SSH服務
1、在
tast01
中進入
SSH
主伺服器配置檔案,更改配置檔案條目,開啟
SSH
服務。
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯伺服器配置檔案資訊
Port 22 //開啟端口
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
:wq //儲存退出
[root@tast01 ~]# systemctl restart sshd //重新開機SSH服務
2、在
tast02
中使用
SSH
服務登入
tast01
。
[root@tast02 ~]# ssh [email protected] //使用SSH服務登入tast01伺服器
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是否建立會話
Warning: Permanently added '192.168.144.133' (ECDSA) to the list of known hosts.
[email protected]'s password: //輸入密碼
Last login: Mon Sep 9 13:59:09 2019
[root@tast01 ~]# //成功登入tast01
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02端口
3、回到
tast01
伺服器,更改
SSH
伺服器配置檔案,禁止
root
使用者登入。然後再建立
siti
使用者
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯主配置檔案
#LoginGraceTime 2m
PermitRootLogin no //開啟是否啟用禁用root登入,更改yes為no,禁止root使用者登入
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
:wq //儲存退出
[root@tast01 ~]# systemctl restart sshd //重新開機服務
[root@tast01 ~]# useradd siti //建立siti普通使用者
[root@tast01 ~]# passwd siti //設定使用者密碼
更改使用者 siti 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字元
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast01 ~]# id siti //檢視建立使用者siti資訊
uid=1001(siti) gid=1001(siti) 組=1001(siti)
[root@tast01 ~]# id sun //檢視使用者sun資訊
uid=1000(sun) gid=1000(sun) 組=1000(sun),10(wheel)
4、使用
tast02
登入
tast01
的
root
使用者,看更改的服務是否生效
[root@tast02 ~]# ssh [email protected] //使用SSH服務登入tast01伺服器root使用者
[email protected]'s password: //輸入密碼登入
Permission denied, please try again. //拒絕登入root
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //嘗試輸入密碼三次後彈出,拒絕登入
[root@tast02 ~]# ssh [email protected] //使用SSH服務登入siti使用者
[email protected]'s password:
[siti@tast01 ~]$ //成功登入tast01伺服器siti使用者
[siti@tast01 ~]$ su - root //再siti使用者下使用su切換root使用者
]密碼: //輸入密碼
上一次登入:一 9月 9 15:16:00 CST 2019從 192.168.144.135pts/1 上
最後一次失敗的登入:一 9月 9 15:33:03 CST 2019從 192.168.144.135ssh:notty 上
最有一次成功登入後有 3 次失敗的登入嘗試。
[root@tast01 ~]# //成功登入root使用者。
[root@tast01 ~]# exit //退出
登出
[siti@tast01 ~]$ exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02使用者
5、通過上面的操作我們禁止了遠端登入
root
但是可以通過普通使用者切換登入,這個時候我們就可以開啟
tast01
系統中的
pam
認證,來提高系統的安全性。
[root@tast01 ~]# vim /etc/pam.d/su //進入編輯pam配置檔案
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid //開啟pam認證
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
~
~
~
:wq //儲存退出
6、檢視是否還能夠通過
siti
使用者切換到
root
[root@tast02 ~]# ssh [email protected] //登入siti使用者
[email protected]'s password: //輸入密碼
Last failed login: Mon Sep 9 16:09:32 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 15:47:20 2019 from 192.168.144.135
[siti@tast01 ~]$ su - root //登入siti使用者,并切換root使用者
密碼: //輸入密碼
su: 拒絕權限 //權限拒絕,無法切換
[siti@tast01 ~]$
7、因為設定了權限,
siti
使用者不在
wheel
組,是以無法用
siti
使用者切換
root
使用者,我們可不可以通過
siti
wheel
組中
sun
使用者,再用
sun
root
,看是否可以。
[siti@tast01 ~]$ su - sun //切換sun使用者
密碼: //輸入密碼
su: 拒絕權限 //權限拒絕,無法切換
[siti@tast01 ~]$
9、回到tast01中開啟SSH服務配置密碼驗證次數服務
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入伺服器配置檔案
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6 //開啟密碼驗證次數
#MaxSessions 10
:wq //儲存退出
10、進入
tast02
驗證密碼次數是否成功開啟
[root@tast02 ~]# ssh [email protected] //登入sun使用者
[email protected]'s password: //輸入錯誤密碼
Permission denied, please try again. //1次輸錯,拒絕登入
[email protected]'s password: //輸入錯誤密碼
Permission denied, please try again. //2次輸錯,拒絕登入
[email protected]'s password: //輸入錯誤密碼
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //3次輸入錯誤直接登出
11、通過上面的實驗發現并沒有實作6次密碼後再彈出,而是預設的三次,這個時候我們就用通過指令來提高預設密碼次數來實作密碼次數的設定。
[root@tast02 ~]# ssh -o NumberofPasswordPrompts=8 [email protected] //使用指令提高密碼輸入次數
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Received disconnect from 192.168.144.133 port 22:2: Too many authentication failures
Authentication failed. //輸入密碼6次後彈出,設設定生效
黑白名單設定( AllowUsers、DenyUsers
)
AllowUsers、DenyUsers
在VMware 15中再增加一台Linux用戶端(
tast03IP位址:192.168.144.132
),用于遠端連接配接伺服器。
1、再
tast01
中配置
ssh
服務端配置檔案,添加
AllowUsers
條目,添加僅允許登入的用戶端
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯ssh服務端配置檔案
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
AllowUsers [email protected] stii //在此處添加條目,僅允許IP位址為192.168.144.135客戶機登入sun使用者
僅允許用戶端登入stii使用者
#PubkeyAuthentication yes
:wq //儲存退出
[root@tast01 ~]# useradd stii //添加stii使用者
[root@tast01 ~]# passwd stii //設定stii使用者密碼
更改使用者 stii 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字元
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast01 ~]# systemctl restart sshd //重新開機ssh服務
2、分别在
tast02、tast03
客戶機使用
ssh
服務遠端登入
tast01
伺服器
[root@tast02 ~]# ssh [email protected] //在tast02用戶端中登入伺服器sun使用者
[email protected]'s password: //輸入密碼
Last failed login: Mon Sep 9 17:24:32 CST 2019 from 192.168.144.135 on ssh:notty
There were 6 failed login attempts since the last successful login.
Last login: Mon Sep 9 17:21:47 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登入
[sun@tast01 ~]$ exit //退出使用者
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# ssh [email protected] //使用ssh登入伺服器siti使用者
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast02 ~]# ssh [email protected] //登入stii使用者
[email protected]'s password: //輸入密碼
[stii@tast01 ~]$ //成功登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器sun使用者
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是否建立會話,輸入yes确定建立會話
Warning: Permanently added '192.168.144.133' (ECDSA) to the list of known hosts.
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器siti使用者
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器stii使用者
[email protected]'s password: //輸入密碼
Last login: Mon Sep 9 21:55:49 2019 from 192.168.144.135
[stii@tast01 ~]$ //成功登入
3、回到tast01伺服器,編輯ssh伺服器配置檔案
[root@tast01 ~]# vim /etc/ssh/sshd_config //編輯ssh伺服器配置檔案
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers [email protected] stii //删除僅允許條目,添加拒絕條目
#PubkeyAuthentication yes
:wq //儲存退出
[root@tast01 ~]# systemctl restart sshd //重新開機ssh服務
4、分别在
tast02、tast03
ssh
tast01
[root@tast02 ~]# ssh [email protected] //在tast02用戶端中登入伺服器sun使用者
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast02 ~]# ssh [email protected] //在tast02用戶端中登入伺服器stii使用者
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast02 ~]# ssh [email protected] //在tast02用戶端中登入伺服器siti使用者
[email protected]'s password: //輸入密碼
Last failed login: Mon Sep 9 22:02:00 CST 2019 from 192.168.144.132 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Mon Sep 9 21:53:53 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器stii使用者
[email protected]'s password: //輸入密碼
Permission denied, please try again. //拒絕登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器sun使用者
[email protected]'s password: //輸入密碼
Last failed login: Mon Sep 9 22:30:55 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 22:24:51 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登入
[root@tast03 ~]# ssh [email protected] //tast03客戶機使用ssh服務登入伺服器siti使用者
[email protected]'s password: //輸入密碼
Last login: Mon Sep 9 22:32:16 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登入
使用密鑰對驗證登入
1、首先在
tast01
伺服器中進入編輯
ssh
配置檔案,開啟密鑰驗證條目
[root@tast01 ~]# vim /etc/ssh/sshd_config //編輯ssh配置檔案
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers [email protected] stii
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 //密鑰存放位置
:wq //儲存退出
2、進入用戶端
tast02
客戶機中,配置密鑰
[root@tast02 ~]# useradd siaa //在tast02客戶機中建立使用者
[root@tast02 ~]# passwd siaa //設定使用者目錄
更改使用者 siaa 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字元
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast02 ~]# su - siaa //切換至使用者siaa
[siaa@tast02 ~]$ ssh-keygen -t ecdsa //制作ecdsa類型密鑰
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/siaa/.ssh/id_ecdsa): //密鑰存放位置,保持不變,直接回車
Created directory '/home/siaa/.ssh'.
Enter passphrase (empty for no passphrase): //輸入要設定的密碼
Enter same passphrase again: //再次輸入密碼
Your identification has been saved in /home/siaa/.ssh/id_ecdsa.
Your public key has been saved in /home/siaa/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:5mTvLU19q7uUUXECnEmNldB3S4gUiNZdvm1zupFUf0Y siaa@tast02
The key's randomart image is:
+---[ECDSA 256]---+
| o +=B@+o.|
| o o o*.+o=|
| . ..oE|
| ++.| //生成ecdsa密鑰
| S +.+=|
| = . ..=+=|
| . .o o+..|
| ...o + |
| ...+= |
+----[SHA256]-----+
[siaa@tast02 ~]$ ls -a //檢視使用者家目錄隐藏檔案
. .. .bash_logout .bash_profile .bashrc .cache .config .mozilla .ssh
[siaa@tast02 ~]$ cd .ssh //進入生成的.ssh目錄
[siaa@tast02 .ssh]$ ls //檢視目錄内容
id_ecdsa id_ecdsa.pub //生成的私鑰與公鑰檔案
[siaa@tast02 .ssh]$ ssh-copy-id -i id_ecdsa.pub [email protected] //指定生成的公鑰檔案推送到伺服器siti使用者
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.pub"
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是推送,輸入yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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: //輸入伺服器siti使用者密碼
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.
[siaa@tast02 .ssh]$ ls //檢視目錄資訊
id_ecdsa id_ecdsa.pub known_hosts //建立檔案Known_hosts
[siaa@tast02 .ssh]$ vim known_hosts //檢視檔案資訊
192.168.144.133 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC6sBj5BEqQkEIXTdcRDCzDlQRfhaoaY7OvyWzxcNxt+n6ZjbA1PSYK2SeTW3MAhUZOry7T6gNDFL7YyfMfXOGo= //成功将ecdsa生成的密鑰推送給伺服器
tast01
伺服器中檢視
siti
家目錄中是否有推送的檔案
[root@tast01 ~]# cd /home/siti //進入siti家目錄
[root@tast01 siti]# ls -a //檢視隐藏檔案
. .bash_history .bash_profile .cache .mozilla
.. .bash_logout .bashrc .config .ssh
[root@tast01 siti]# cd .ssh //進入添加的.ssh目錄
[root@tast01 .ssh]# ls //檢視資訊
authorized_keys
[root@tast01 .ssh]# cat authorized_keys //檢視資訊内容
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBD6B4elJHibp7lYDfogSfd7krTUPyKzvLHZNk75GTm1oibrA0aMirgtwxxfUEOi+9+ZGU2V0C3+zH6vQpjvvPoo= siaa@tast02 //siaa@tast02的ecdsa加密檔案
4、在
tast02
用戶端中使用
siaa
使用者進行驗證登入伺服器
tast01
中
siti
[siaa@tast02 .ssh]$ whoami //使用指令檢視目前登入使用者
siaa //确定目前登入使用者為siaa
[siaa@tast02 .ssh]$ ssh [email protected] //使用ssh服務登入伺服器siti使用者
Enter passphrase for key '/home/siaa/.ssh/id_ecdsa': //輸入設定的ecdsa密碼
Last login: Mon Sep 9 22:37:19 2019 from 192.168.144.132
[siti@tast01 ~]$ //成功登入伺服器siti使用者
5、設定客戶機信任使用者免驗證登入伺服器
[siti@tast01 ~]$ exit //退出目前使用者
登出
Connection to 192.168.144.133 closed.
[siaa@tast02 .ssh]$ ssh-agent bash //回到tast02中siaa使用者,使用指令代理bash環境
[siaa@tast02 .ssh]$ ssh-add //使用指令添加驗證密碼
Enter passphrase for /home/siaa/.ssh/id_ecdsa: //輸入驗證密碼
Identity added: /home/siaa/.ssh/id_ecdsa (/home/siaa/.ssh/id_ecdsa) //成功添加密碼
[siaa@tast02 .ssh]$ ssh [email protected] //登入伺服器siti使用者
Last login: Mon Sep 9 23:31:28 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登入,免密碼驗證
SSH用戶端程式
1、進入tast01伺服器,編輯SSH配置檔案,打開root登入,因為在Linux系統中有些路徑沒有root權限,無法實作複制功能,
[root@tast01 ~]# vim /etc/ssh/sshd_config
...//省略部分内容...
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes //開啟登入root使用者權限
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
:wq //儲存退出
[root@tast01 ~]# systemctl restart sshd //重新開機SSH服務
2、在tast02中驗證root使用者登入權限是否成功開啟。
[root@tast02 ~]# ssh [email protected] //使用ssh服務登入伺服器root使用者
[email protected]'s password: //輸入使用者密碼
Last login: Wed Sep 11 22:56:28 2019 from 192.168.144.135
[root@tast01 ~]# //成功登入
3、在tast02中退出伺服器root使用者登入,在opt目錄下建立檔案,使用scp指令推送給tast01使用者
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# cd /opt/ //進入opt目錄
[root@tast02 opt]# ls //檢視
rh
[root@tast02 opt]# echo "this is ssh-client" > ssh_client.txt //建立.txt檔案
[root@tast02 opt]# mkdir -p tast/si11 //遞歸建立tast目錄并在tast目錄下建立si11目錄
[root@tast02 opt]# ls //檢視
rh ssh_client.txt tast //成功建立檔案與目錄
[root@tast02 opt]# scp ssh_client.txt [email protected]:/home/ //将建立的.txt檔案推送到伺服器root使用者home目錄下
[email protected]'s password: //輸入密碼
ssh_client.txt 100% 19 6.0KB/s 00:00 //成功推送
4、回到tast01伺服器中,檢視home目錄下是否有推送過去的檔案。
[root@tast01 ~]# ls /home/ //檢視home目錄下檔案
ssh_client.txt sun //成功添加檔案
[root@tast01 ~]# cat /home/ssh_client.txt //檢視檔案内容
this is ssh-client //顯示檔案内容
5、在tast02中把剛建立的檔案夾推送給tast01伺服器,并在tast01伺服器中檢視是否成功推送
[root@tast02 opt]# scp -r tast/ [email protected]:/home/ //推送檔案夾
[email protected]'s password: //輸入密碼
[root@tast02 opt]# //推送成功
[root@tast01 ~]# ls /home/ //檢視home目錄
ssh_client.txt sun tast //顯示推送的檔案夾
[root@tast01 ~]# ls /home/tast/ //檢視檔案夾内容
si11 //顯示建立的si11目錄
使用sftp指令實作遠端上傳和下載下傳
1、在tast02中删除建立的檔案與檔案夾
[root@tast02 opt]# ls //檢視資訊
rh ssh_client.txt tast //顯示内容
[root@tast02 opt]# rm -rf ssh_client.txt //删除txt檔案
[root@tast02 opt]# rm -rf tast/ //删除檔案夾
[root@tast02 opt]# ls //檢視
rh //成功删除
2、使用sftp指令從tast01伺服器中下載下傳檔案
[root@tast02 opt]# sftp [email protected] //使用sftp指令登入tast01伺服器root使用者
[email protected]'s password: //輸入密碼
Connected to 192.168.144.133.
sftp> ls //成功登入并檢視目錄資訊
anaconda-ks.cfg initial-setup-ks.cfg 下載下傳 公共
圖檔 文檔 桌面 模闆 //此時在root使用者家目錄下
視訊 音樂
sftp> cd /home/ //進入home目錄
sftp> ls //檢視
ssh_client.txt sun tast //顯示内容
sftp> get ssh_client.txt //使用get指令下載下傳txt檔案
Fetching /home/ssh_client.txt to ssh_client.txt
/home/ssh_client.txt 100% 19 19.3KB/s 00:00
sftp> bye //退出
[root@tast02 opt]# ls //檢視目錄下是否有内容
rh ssh_client.txt //成功下載下傳
3、将下載下傳的檔案更改名字,在使用sftp指令将檔案上傳至tast01伺服器home目錄,并回到tast01伺服器中檢視資訊
[root@tast02 opt]# mv ssh_client.txt ssh_server.txt //更改檔案名稱
[root@tast02 opt]# ls //檢視
rh ssh_server.txt //已更改
[root@tast02 opt]# sftp [email protected] //使用sftp指令登入tast01root使用者
[email protected]'s password: //輸入密碼
Connected to 192.168.144.133.
sftp> cd /home/ //進入home目錄
sftp> ls //檢視内容
ssh_client.txt sun tast
sftp> put ssh_server.txt //将檔案上傳至tast01伺服器home目錄中
Uploading ssh_server.txt to /home/ssh_server.txt
ssh_server.txt 100% 19 15.6KB/s 00:00
sftp> bye //退出
[root@tast02 opt]#
[root@tast01 ~]# ls /home/ //檢視home目錄内容
ssh_client.txt ssh_server.txt sun tast //成功上傳檔案
TCP wrappers通路控制
TCP wrappers概述
保護原理
TCP wrappers
将其他的
TCP
服務程式“包裹”起來,增加了一個安全的檢測過程,外來的連接配接請求必須先通過這層安全檢測,獲得許可後才能通路真正的服務程式。
TCP wrappers
還可以記錄所有企圖通路被保護服務的行為,為管理者提供豐富的安全分析資料。
TCP wrappers
的通路是基于
TCP
協定的應用服務。
保護機制的實作方式
- 方式1:通過
主程式對其他服務程式進行包裝tcpd
- 方式2:有其他服務程式調用
連接配接庫libwrap.os.*
通路控制政策的配置檔案
-
/etc/hosts.allow
-
/etc/hosts.deny
TCP Wrappers政策應用
設定通路控制政策
- 政策格式:服務清單:客戶機位址清單
- 服務清單
- 多個服務以逗号分隔,
表示所有服務ALL
- 多個服務以逗号分隔,
- 客戶機位址清單
- 多個位址以逗号分隔,
表示所有位址ALL
- 允許使用通配符
和?
*
- 網段位址,如:
或者192.168.4.0
192.168.4.0/255.255.255.0
- 區域位址,如:
.benet.com
- 多個位址以逗号分隔,
政策的應用順序
- 先檢查
,找到比對則允許通路hosts.allow
- 否則再檢查
,找到則拒絕通路hosts.deny
- 若兩個檔案中均無比對政策,則預設允許通路
實驗環境:
搭建實驗
[root@tast01 ~]# cd /etc //進入etc目錄
[root@tast01 etc]# ls ./ | grep *.allow //檢視目前目錄内容,并過濾所有字尾為.allow的檔案
hosts.allow //顯示檔案
[root@tast01 etc]# vim hosts.allow //進入編輯配置檔案
## hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
#
sshd:192.168.144.132 //添加條目,僅允許192.168.144.132客戶機通路伺服器
~
~
~
:wq //儲存退出
[root@tast01 etc]# vim hosts.deny
#
# hosts.deny This file contains access rules which are used to
# deny connections to network services that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd:ALL //編輯條目拒絕所有客戶機通路(注意,此處使用大寫)
~
~
~
:wq //儲存退出
[root@tast03 ~]# ssh [email protected] //使用tast03客戶機通路伺服器
[email protected]'s password: //輸入密碼
Last login: Mon Sep 16 13:43:33 2019
[root@tast01 ~]# //成功登入
[root@tast02 ~]# ssh [email protected] //使用tast02客戶機通路伺服器
ssh_exchange_identification: read: Connection reset by peer //拒絕通路
[root@tast02 ~]#