SSH遠程訪問與控制,ssh密鑰對配置


1、加快與X-shell的連接

請參考https://www.cnblogs.com/elin989898/p/11361866.html

2、用戶登錄控制

sshd服務默認允許root用戶登錄,但是在Internet中使用是非常不安全的。普遍的做法如下:先以普通用戶遠程登入,進入安全shell環境后,根據實際需要使用su命令切換為root 用戶

 [root@localhost ~]# vi /etc/ssh/sshd_config

 

 

[root@localhost ~]# systemctl reload sshd 

[root@localhost ~]# useradd jll          創建用戶

 

[root@localhost ~]# passwd -d jll             //清空密碼

測試空密碼用戶是否能夠登錄。

 實驗現象:

(1)超過系統設置的登陸時間,連接對話會直接斷開

(2)連續輸入3次錯誤密碼時連接會話會直接斷開

(3)通過root用戶密碼時反復提示輸入密碼

(4)測試用戶空密碼時不能登錄系統的

3、OpenSSH 服務訪問控制

AllowUsers   僅允許用戶登錄

DenyUsers  僅禁止用戶登錄

(1)AllowUsers   不要與 DenyUsers 同時使用

(2)當服務器在Internet時,控制包含的ip地址時是公司公網地址

 

 

 

4、登錄驗證方式

SSH服務支持兩種驗證方式

~密碼驗證

~密鑰對驗證

可以設置只使用其中一種方式,也可以兩種方式都啟用

密碼驗證:對服務器中本地系統用戶的登錄名稱、密碼進行驗證。這種方式使用最為簡單,但從客戶端角度來看,正在連接的服務器有可能被假冒;從服務器角度來看,當遭遇窮舉(暴力破解)攻擊時防御能力比較弱。

密鑰對驗證:要求提供相匹配的密鑰信息才能通過驗證。通常先在客戶端中創建一對密鑰文件(公鑰。私鑰)然后將公鑰文件放到服務器中指定位置,遠程登錄時,系統將使用公鑰私鑰進行加密/解密關聯驗證,大大增強了遠程管理的安全性。該方式不易被假冒,且可以免交互登錄,在shell中被廣泛使用。 

       當密碼驗證密鑰對驗證都啟用時, 服務器將優先使用密鑰對驗證。對於安全性要求的服務器,建議將密碼驗證方式禁用只允許啟用密鑰對驗證方式:若沒有特殊要求,則兩種方式都可啟用。

 

[root@localhost ~]# vi /etc/ssh/sshd_config

64 PasswordAuthentication yes    //啟用密碼驗證

47 AuthorizedKeysFile      .ssh/authorized_keys   //指定公鑰庫文件(用於保存多個客戶端上傳的公鑰文本)

43 PubkeyAuthentication yes   //啟用密鑰對驗證 (#去不去沒關系,默認是開的)

[root@localhost ~]# systemctl reload sshd   //重啟服務

 5、使用SSH客戶端程序

openssh 客戶端由openssh-clients軟件包提供,默認已安裝。其中包括(ssh遠程登錄命令、scp遠程復制命令、sftp遠程文件傳輸命令等)sftp

[root@localhost ~]# rpm -q openssh-clients
openssh-clients-7.4p1-16.el7.x86_64

5.1命令程序:

(1) ssh命令(遠程安全登錄)

 格式: ssh user@host    (若客戶機登錄用戶與主機用戶名相同,可省去user@)注意:涉及到用戶權限歸屬的文件一定指定用戶名在進行上傳。)

格式: ssh user@host command

端口選項 :  -p 22

案例1:切換另一個操作系統,並創建文件,然后到另一台主機查看是否創建。

[root@localhost ~]# ssh root@192.168.116.66 touch /tmp/ddd.txt
The authenticity of host '192.168.116.66 (192.168.116.66)' can't be established.
ECDSA key fingerprint is SHA256:UT/IZdaaQQzRkB/4C8NNxwBKG88Xj7OW/A0BfYi0qyQ.
ECDSA key fingerprint is MD5:1c:ba:de:c1:ec:8f:08:50:14:98:79:f4:e8:5c:56:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.116.66' (ECDSA) to the list of known hosts.
root@192.168.116.66's password:

案例2:將本機的文件復制到另一主機中。然后去對方主機查看 

格式 :scp 源文件  目標位置 (兩個主機可以互相傳送)

[root@localhost ~]# scp 2.txt root@192.168.116.66:/22.txt
root@192.168.116.66's password:

案例3 :切換到另一個主機

[root@localhost ~]# ssh root@192.168.116.66

(2) scp命令

通過scp 命令可以利用SSH安全連接和遠程主機相互復制文件。 使用scp命令時,除了必須指定復制源。目標以外還應指定目標主機地址、登錄用戶、執行后根據提示輸入驗證密碼即可。

格式:scp [-r] user@host:file1 file2     //將另一台主機的文件復制到本機上

           scp [-r] file1  user@host:file2   //將文件復制到另一台主機上   -r 表示遞歸復制

特別注意的是:用戶名代表了文件的所屬權限問題

(3) rz -E 命令表示文件的上傳和下載

(4)sftp 命令(安全FTP上傳下載)

通過sftp命令可以利用SSH安全連接與遠程主機上傳,下載文件,采用了與FTP類似的登錄過程和交互式環境,以便目錄資源管理

格式:sftp user@host

案例1  在68主機上實現對66主機文件的上傳與下載

68主機:

[root@localhost ~]# sftp root@192.168.116.66
root@192.168.116.66's password:
Connected to 192.168.116.66.
sftp> put 2.txt
Uploading 2.txt to /root/2.txt
2.txt                               100%    0     0.0KB/s   00:00   
sftp> ls
2.txt                  anaconda-ks.cfg        initial-setup-ks.cfg  
下載                 公共                 圖片                
文檔                 桌面                 模板                
視頻                 音樂                
sftp> ls
1.txt                  2.txt                  anaconda-ks.cfg       
initial-setup-ks.cfg   下載                 公共                
圖片                 文檔                 桌面                
模板                 視頻                 音樂                
sftp> get 1.txt
Fetching /root/1.txt to 1.txt
sftp> quit
[root@localhost ~]# ls
1.txt  2.txt  anaconda-ks.cfg

66主機

[root@localhost ~]# ls
2.txt            initial-setup-ks.cfg  模板  圖片  下載  桌面
anaconda-ks.cfg  公共                  視頻  文檔  音樂
[root@localhost ~]# touch 1.txt

5.2  常見的遠程訪問工具:  Xshell    SecureCRT   Putty   Smanager (遠程圖形化界面)等

6、構建密鑰對驗證的SSH體系

密鑰對驗證整個過程包括四步:

a: 首先要在SSH客戶端以root用戶身份創建密鑰對

b: 客戶端將創建的公鑰文件上傳至SSH服務端

c: 服務端將公鑰信息導入用戶root的公鑰數據庫文件中

d:  客戶端以root用戶身份連接服務器端root用戶測試

6.1在客戶端創建密鑰對

在Linux客戶端中通過ssh-keygen命令工具為當前登錄用戶創建密鑰對文件,可用的加密算法:ECDSA、RSA、DSA ,通過  - t  選項指定

案例:將server與client兩台主機之間進行密鑰對連接  這里介紹兩種方法分別將主機當作服務端和客戶端,實現兩台主機之間的互相免密登錄

方法一:

(1)首先在client主機中執行以下操作,其中  root 身份 地址為192.168.116.66  root密碼123456

[root@1111 ~]# ssh-keygen -t rsa     //創建密鑰對   rsa為算法

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:
SHA256:lXdbtLwpUd1jgh7/y+Rn2qUaNzVt9rzHqMoouL2+Dmo root@1111
The key's randomart image is:
+---[RSA 2048]----+
|            .  .+|
|           + .o++|
|          + +.+oo|
|         . o o.o+|
|        S    .o+=|
|              .Bo|
|    ..      . *o=|
|  E..o.  o   o.BB|
| .. .+*+. o.ooo++|
+----[SHA256]-----+

[root@localhost ~]# ls .ssh    //查看 .ssh 文件是否有兩個文件   id_rsa 是私鑰文件   id_rsa.pub 是公鑰文件
id_rsa  id_rsa.pub

[root@localhost ~]# scp .ssh/id_rsa.pub root@192.168.116.68:/tmp      //將公鑰復制到 服務器 的 192.168.116.68 的一個文件中
The authenticity of host '192.168.116.68 (192.168.116.68)' can't be established.
ECDSA key fingerprint is SHA256:nOug2TzzVJZysB+hBvgEnxvaOESr6XmI28kFoJIbfVc.
ECDSA key fingerprint is MD5:80:57:a3:0f:10:58:25:d9:94:4b:e8:ef:31:94:c8:9f.
Are you sure you want to continue connecting (yes/no)? yes  
Warning: Permanently added '192.168.116.68' (ECDSA) to the list of known hosts.
root@192.168.116.68's password:   //輸入IP為192.168.116.68 主機root身份的密碼
id_rsa.pub                          100%  408   266.2KB/s   00:00 

 

 

 (2) 在server主機中操作 root身份,密碼123456 IP地址192.168.116.68

[root@client ~]# ls /tmp    //查看公鑰是否存在
id_rsa.pub
ks-script-YL7_Bo
systemd-private-6723e85d6a6a42d5bb417f690b0e993e-chronyd.service-E1cRvH
systemd-private-c6d528254e314241bb2b1dfb5ab308bc-chronyd.service-EHk4EK
vmware-root_5915-1991519414
vmware-root_6042-692291496
yum.log
[root@client ~]# mkdir .ssh    //系統中可能沒有 . ssh文件 可創建一個 

[root@client ~]# ll /tmp/id_rsa.pub    //查看公鑰文件的權限   
-rw-r--r-- 1 root root 408 8月  16 20:07 /tmp/id_rsa.pub
[root@client ~]# chmod 700 /tmp/id_rsa.pub    //修改權限為700   可執行文件 
[root@client ~]# cat /tmp/id_rsa.pub >> .ssh/authorized_keys   //將公鑰添加到 .ssh 中的authorized_keys(數據庫)文件中, 可用" >> "符號,表示追加 , “>" 表示覆蓋
[root@client ~]# cat .ssh/authorized_keys    //查看公鑰是否存在
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIfAvBJOniBlxKh7/4xYEvtgDl+uN0AkILFwMg4hlRqyx2Xp9DRFRlQXt1FeMVn1kM7oZpw330Nc/Nng5FlinPmQBWOKnEV0yNPxyrNsmwGp6bgq2ui0tFPZd1oeUoQYlkuhdQPLQcV0/v5a+NvcNxDTNiZauuZinvNUNrqx1lYHIy9/XIWPoqFPcfkk4z+zGXf1G0V+lKLajyxto+YeAtLWZi7NfWqadnmnZmPX6r8wHBfx9FvTcHQmOVlpJ2hK/miqDNrTQYS/Ac9myrD0oPrsxIAldNDR6Og4Ww/uCNKJPBniEFGT/bJtXtqLH0M/K/if2J2H0dHPE+wSYC2vkd root@localhost.localdomain   //看這個地方文件的名字

(3) 在client 操作

[root@localhost ~]# ssh root@192.168.116.68     //在客戶端登錄服務器端
Last login: Fri Aug 16 20:05:16 2019 from 192.168.116.1

[root@client ~]#    //登陸成功

這里可以查看以下 IP   使用ip a 命令

方法二

 (1)在client 主機中 root身份  IP地址為192.168.116.68  密碼123456

[root@client ~]# ssh-keygen -t rsa        //創建公鑰私鑰  rsa算法     (以下操作同上,不做講解)
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:
SHA256:qjK/9ThKB7qDHn03QXw32U9aJT19HUmddZ2A2uWWfhM root@client
The key's randomart image is:
+---[RSA 2048]----+
|            ..++#|
|      .    .o. *X|
|       o .o+o..oo|
|      . .....+=E |
|    .  .S   o. ..|
|  .. . ..    . o |
| o....+o      . .|
|. =o.+oo.        |
|...==o...        |
+----[SHA256]-----+
[root@client ~]# ls .ssh
authorized_keys  id_rsa  id_rsa.pub
[root@client ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.116.66    // 使用ssh-copy-id -i 命令直接copy到目標主機的   .ssh/authorized_keys文件中
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.116.66 (192.168.116.66)' can't be established.
ECDSA key fingerprint is SHA256:UT/IZdaaQQzRkB/4C8NNxwBKG88Xj7OW/A0BfYi0qyQ.
ECDSA key fingerprint is MD5:1c:ba:de:c1:ec:8f:08:50:14:98:79:f4:e8:5c:56:b9.
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
root@192.168.116.66's password:     //輸入對方主機密碼
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@192.168.116.66'"
and check to make sure that only the key(s) you wanted were added.

(2)在sever root身份 IP192.168.116.66 密碼123456

[root@client ~]# ll .ssh/    //查看權限
總用量 16
-rw-r--r-- 1 root root  408 8月  16 20:12 authorized_keys
-rw------- 1 root root 1679 8月  16 20:21 id_rsa
-rw-r--r-- 1 root root  393 8月  16 20:21 id_rsa.pub
-rw-r--r-- 1 root root  176 8月  16 20:23 known_hosts
[root@client ~]# cat .ssh/authorized_keys    //查看文件是否存在 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIfAvBJOniBlxKh7/4xYEvtgDl+uN0AkILFwMg4hlRqyx2Xp9DRFRlQXt1FeMVn1kM7oZpw330Nc/Nng5FlinPmQBWOKnEV0yNPxyrNsmwGp6bgq2ui0tFPZd1oeUoQYlkuhdQPLQcV0/v5a+NvcNxDTNiZauuZinvNUNrqx1lYHIy9/XIWPoqFPcfkk4z+zGXf1G0V+lKLajyxto+YeAtLWZi7NfWqadnmnZmPX6r8wHBfx9FvTcHQmOVlpJ2hK/miqDNrTQYS/Ac9myrD0oPrsxIAldNDR6Og4Ww/uCNKJPBniEFGT/bJtXtqLH0M/K/if2J2H0dHPE+wSYC2vkd root@localhost.localdomain

(3) 在client端登錄測試

[root@client ~]# ssh root@192.168.116.66      //進行切換 
Last login: Fri Aug 16 20:05:05 2019 from 192.168.116.1 

[root@localhost ~]#   //切換成功

//可用 ip a 進行檢測

 

 

 

結束!!!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM