Linux遠程訪問及控制

前言
linux運維管理的時候,通常都是經過遠程方式管理,當須要從一個工做站管理數以百計的服務器主機時,遠程維護的方式將更佔優點。
1、SSH遠程管理
SSH是一種安全通道協議,主要用來實現字符界面的遠程管理、遠程複製等功能。SSH協議對通訊雙方的數據傳輸進行了加密處理,其中包括用戶登錄時輸入的用戶口令。
與早期的TELNET(遠程登陸)、RSH(Remote Shell,遠程執行)、RCP(Remote File Copy,遠程文件複製)等應用相比,SSH協議提供了更好的安全性。linux

SSH協議shell

  • 爲客戶機提供安全的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監聽地址

    用戶登陸控制vim

  • 禁止root用戶、空密碼用戶
  • 登陸時間、重試次數
  • AllowUsers、DenyUsers(黑白名單,僅容許和僅拒絕)
    #LoginGraceTime 2m      //會話時間
    #PermitRootLogin yes    //是否進制root登陸
    #StrictModes yes        //是否驗證訪問權限
    #MaxAuthTries 6         //驗證次數6次
    #MaxSessions 10         //訪問的最大連接數
    #PubkeyAuthentication yes  //是否驗證公鑰

    登陸驗證對象
    服務器中的本地用戶帳戶
    登陸驗證方式
    密碼驗證:覈對用戶名、密碼是否匹配
    密鑰對驗證:覈對客戶的私鑰、服務端公鑰是否匹配
    使用SSH客戶端程序
    ssh命令——遠程安全登陸
    scp命令——遠程安全複製
    sftp命令——安全FTP上下載
    get 下載
    put 上傳
    bye 退出
    使用SSH服務
    一、在tast01中進入SSH主服務器配置文件,更改配置文件條目,開啓SSH服務。api

    [root@tast01 ~]# vim /etc/ssh/sshd_config    //進入編輯服務器配置文件信息
    Port 22                                      //開啓端口
    #AddressFamily any
    #ListenAddress 0.0.0.0
    #ListenAddress ::
    :wq                                           //保存退出
    [root@tast01 ~]# systemctl restart sshd       //重啓SSH服務

    二、在tast02中使用SSH服務登陸tast01。安全

    [root@tast02 ~]# ssh root@192.168.144.133          //使用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.
    root@192.168.144.133'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端口

    三、回到tast01服務器,更改SSH服務器配置文件,禁止root用戶登陸。而後再建立siti用戶bash

    [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)

    四、使用tast02登陸tast01的root用戶,看更改的服務是否生效服務器

    [root@tast02 ~]# ssh root@192.168.144.133      //使用SSH服務登陸tast01服務器root用戶
    root@192.168.144.133's password:               //輸入密碼登陸
    Permission denied, please try again.           //拒絕登陸root
    root@192.168.144.133's password:          
    Permission denied, please try again.
    root@192.168.144.133's password: 
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //嘗試輸入密碼三次後彈出,拒絕登陸
    [root@tast02 ~]# ssh siti@192.168.144.133      //使用SSH服務登陸siti用戶
    siti@192.168.144.133'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用戶

    五、經過上面的操做咱們禁止了遠程登陸root可是能夠經過普通用戶切換登陸,這個時候咱們就能夠開啓tast01系統中的pam認證,來提升系統的安全性。session

    [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                                                           //保存退出

    六、查看是否還可以經過siti用戶切換到root用戶app

    [root@tast02 ~]# ssh siti@192.168.144.133               //登陸siti用戶
    siti@192.168.144.133'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 ~]$

    七、由於設定了權限,siti用戶不在wheel組,因此沒法用siti用戶切換root用戶,咱們可不能夠經過siti用戶切換wheel組中sun用戶,再用sun用戶切換root,看是否能夠。運維

    [siti@tast01 ~]$ su - sun            //切換sun用戶
    密碼:                                //輸入密碼
    su: 拒絕權限                          //權限拒絕,沒法切換
    [siti@tast01 ~]$

    八、回到tast01中開啓SSH服務配置密碼驗證次數服務

    [root@tast01 ~]# vim /etc/ssh/sshd_config       //進入服務器配置文件
    #LoginGraceTime 2m
    PermitRootLogin no
    #StrictModes yes
    MaxAuthTries 6                          //開啓密碼驗證次數
    #MaxSessions 10
    :wq                                      //保存退出

    九、進入tast02驗證密碼次數是否成功開啓

    [root@tast02 ~]# ssh sun@192.168.144.133          //登陸sun用戶
    sun@192.168.144.133's password:                   //輸入錯誤密碼
    Permission denied, please try again.              //1次輸錯,拒絕登陸
    sun@192.168.144.133's password:                   //輸入錯誤密碼
    Permission denied, please try again.              //2次輸錯,拒絕登陸
    sun@192.168.144.133's password:                   //輸入錯誤密碼
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).   //3次輸入錯誤直接登出

    十、經過上面的實驗發現並無實現6次密碼後再彈出,而是默認的三次,這個時候咱們就用經過命令來提升默認密碼次數來實現密碼次數的設置。

    [root@tast02 ~]# ssh -o NumberofPasswordPrompts=8 sun@192.168.144.133  //使用命令提升密碼輸入次數
    sun@192.168.144.133's password: 
    Permission denied, please try again.
    sun@192.168.144.133's password: 
    Permission denied, please try again.
    sun@192.168.144.133's password: 
    Permission denied, please try again.
    sun@192.168.144.133's password: 
    Permission denied, please try again.
    sun@192.168.144.133's password: 
    Permission denied, please try again.
    sun@192.168.144.133's password: 
    Received disconnect from 192.168.144.133 port 22:2: Too many authentication failures
    Authentication failed.            //輸入密碼6次後彈出,設設置生效

    黑白名單設置(AllowUsers、DenyUsers)
    在VMware 15中再增長一臺Linux客戶端(tast03IP地址:192.168.144.132),用於遠程鏈接服務器。

一、再tast01中配置ssh服務端配置文件,添加AllowUsers條目,添加僅容許登陸的客戶端

[root@tast01 ~]# vim /etc/ssh/sshd_config       //進入編輯ssh服務端配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
AllowUsers sun@192.168.144.135 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服務

二、分別在tast0二、tast03客戶機使用ssh服務遠程登陸tast01服務器

[root@tast02 ~]# ssh sun@192.168.144.133       //在tast02客戶端中登陸服務器sun用戶
sun@192.168.144.133'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 siti@192.168.144.133         //使用ssh登陸服務器siti用戶
siti@192.168.144.133's password:                  //輸入密碼
Permission denied, please try again.              //拒絕登陸
[root@tast02 ~]# ssh stii@192.168.144.133         //登陸stii用戶
stii@192.168.144.133's password:                  //輸入密碼
[stii@tast01 ~]$                                  //成功登陸
[root@tast03 ~]# ssh sun@192.168.144.133          //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.
sun@192.168.144.133's password:                     //輸入密碼
Permission denied, please try again.                //拒絕登陸
[root@tast03 ~]# ssh siti@192.168.144.133           //tast03客戶機使用ssh服務登陸服務器siti用戶
siti@192.168.144.133's password:                    //輸入密碼
Permission denied, please try again.                //拒絕登陸
[root@tast03 ~]# ssh stii@192.168.144.133           //tast03客戶機使用ssh服務登陸服務器stii用戶
stii@192.168.144.133's password:                    //輸入密碼
Last login: Mon Sep  9 21:55:49 2019 from 192.168.144.135
[stii@tast01 ~]$                                     //成功登陸

三、回到tast01服務器,編輯ssh服務器配置文件

[root@tast01 ~]# vim /etc/ssh/sshd_config        //編輯ssh服務器配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 stii             //刪除僅容許條目,添加拒絕條目

#PubkeyAuthentication yes
:wq                                            //保存退出
[root@tast01 ~]# systemctl restart sshd        //重啓ssh服務

四、分別在tast0二、tast03客戶機使用ssh服務遠程登陸tast01服務器

[root@tast02 ~]# ssh sun@192.168.144.133          //在tast02客戶端中登陸服務器sun用戶
sun@192.168.144.133's password:                   //輸入密碼
Permission denied, please try again.              //拒絕登陸
[root@tast02 ~]# ssh stii@192.168.144.133         //在tast02客戶端中登陸服務器stii用戶
stii@192.168.144.133's password:                  //輸入密碼
Permission denied, please try again.              //拒絕登陸
[root@tast02 ~]# ssh siti@192.168.144.133         //在tast02客戶端中登陸服務器siti用戶
siti@192.168.144.133'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 stii@192.168.144.133           //tast03客戶機使用ssh服務登陸服務器stii用戶
stii@192.168.144.133's password:                    //輸入密碼
Permission denied, please try again.                //拒絕登陸
[root@tast03 ~]# ssh sun@192.168.144.133            //tast03客戶機使用ssh服務登陸服務器sun用戶
sun@192.168.144.133'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 siti@192.168.144.133           //tast03客戶機使用ssh服務登陸服務器siti用戶
siti@192.168.144.133's password:                    //輸入密碼
Last login: Mon Sep  9 22:32:16 2019 from 192.168.144.135
[siti@tast01 ~]$                                     //成功登陸

使用密鑰對驗證登陸
一、首先在tast01服務器中進入編輯ssh配置文件,開啓密鑰驗證條目

[root@tast01 ~]# vim /etc/ssh/sshd_config    //編輯ssh配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 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                                      //保存退出

二、進入客戶端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 siti@192.168.144.133  //指定生成的公鑰文件推送到服務器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
siti@192.168.144.133's password:      //輸入服務器siti用戶密碼

Number of key(s) added: 1         //成功添加文件

Now try logging into the machine, with:   "ssh 'siti@192.168.144.133'"
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加密文件

四、在tast02客戶端中使用siaa用戶進行驗證登陸服務器tast01中siti用戶

[siaa@tast02 .ssh]$ whoami    //使用命令查看當前登陸用戶
siaa                          //肯定當前登陸用戶爲siaa
[siaa@tast02 .ssh]$ ssh siti@192.168.144.133     //使用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用戶

五、設置客戶機信任用戶免驗證登陸服務器

[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 siti@192.168.144.133          //登陸服務器siti用戶
Last login: Mon Sep  9 23:31:28 2019 from 192.168.144.135      
[siti@tast01 ~]$                            //成功登陸,免密碼驗證

SSH客戶端程序
一、進入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服務

二、在tast02中驗證root用戶登陸權限是否成功開啓。

[root@tast02 ~]# ssh root@192.168.144.133        //使用ssh服務登陸服務器root用戶
root@192.168.144.133's password:                 //輸入用戶密碼
Last login: Wed Sep 11 22:56:28 2019 from 192.168.144.135
[root@tast01 ~]#                                 //成功登陸

三、在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 root@192.168.144.133:/home/  //將建立的.txt文件推送到服務器root用戶home目錄下
root@192.168.144.133's password:           //輸入密碼
ssh_client.txt                                                100%   19     6.0KB/s   00:00    //成功推送

四、回到tast01服務器中,查看home目錄下是否有推送過去的文件。

[root@tast01 ~]# ls /home/          //查看home目錄下文件
ssh_client.txt  sun                 //成功添加文件
[root@tast01 ~]# cat /home/ssh_client.txt    //查看文件內容
this is ssh-client                           //顯示文件內容

五、在tast02中把剛建立的文件夾推送給tast01服務器,並在tast01服務器中查看是否成功推送

[root@tast02 opt]# scp -r tast/ root@192.168.144.133:/home/     //推送文件夾
root@192.168.144.133's password:                                //輸入密碼
[root@tast02 opt]#                                             //推送成功
[root@tast01 ~]# ls /home/                          //查看home目錄
ssh_client.txt  sun  tast                           //顯示推送的文件夾
[root@tast01 ~]# ls /home/tast/                     //查看文件夾內容
si11                                                //顯示建立的si11目錄

使用sftp命令實現遠程上傳和下載
一、在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                                        //成功刪除

二、使用sftp命令從tast01服務器中下載文件

[root@tast02 opt]# sftp root@192.168.144.133      //使用sftp命令登陸tast01服務器root用戶
root@192.168.144.133'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                //成功下載

三、將下載的文件更更名字,在使用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 root@192.168.144.133            //使用sftp命令登陸tast01root用戶
root@192.168.144.133'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,找到則拒絕訪問
  • 若兩個文件中均無匹配策略,則默認容許訪問
    策略應用示例

僅容許從如下地址訪問sshd服務
主機61.63.65.67
網段192.168.2.0/24
禁止其餘全部地址訪問受保護的服務
Linux遠程訪問及控制

相關文章
相關標籤/搜索