天天看點

linux基礎知識第9單元 ssh和sshkey的應用

一、openssh-server

功能:讓遠端主機可以通過網絡通路sshd服務,開始一個安全shell

二、用戶端連接配接方式

  ssh    遠端主機使用者@遠端主機ip           

[root@desktop0 ~]# ifconfig         ##檢視本機IP帳号,可以在要登陸的主機上用此指令檢視

$$$$$PS:設定IP方法

[root@desktop0 ~]# nm-connection-editor  ##圖形界面設定IP,具體其他方法将在後續單元裡詳細介紹

圖示:設定IP步驟一【點選add添加确定】

linux基礎知識第9單元 ssh和sshkey的應用

圖示:設定IP步驟二【目錄是Ethernet,點選create】

linux基礎知識第9單元 ssh和sshkey的應用

圖示:設定IP步驟三【connection:IP名稱自己拟定,這裡是“westos”,Device MAC addresses:選項為括号裡有eth0的那一項】

linux基礎知識第9單元 ssh和sshkey的應用

圖示:設定IP步驟四【選IPv4 Settings,Method選擇Manual為靜态IP,即為自己設定IP,點選add,Addresses輸入所設定的IP,Netmask為24,Gateway網關不填寫,但是需要選中一下,最後點選save】

linux基礎知識第9單元 ssh和sshkey的應用

圖示:設定IP最後結果

linux基礎知識第9單元 ssh和sshkey的應用

[root@desktop0 ~]# ssh [email protected]

The authenticity of host '172.25.254.244 (172.25.254.244)' 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    ##連接配接陌生主機時需要建立認證關系

Warning: Permanently added '172.25.0.11' (ECDSA) to the list of known hosts.

[email protected]'s password:                 ##遠端使用者密碼

Last login: Mon Oct  3 03:13:47 2016

[root@server0 ~]#                    ##登陸成功

圖示:遠端主機登陸

linux基礎知識第9單元 ssh和sshkey的應用

ssh     遠端主機使用者@遠端主機ip -X                ##調用遠端主機圖形工具

圖示:遠端主機圖形登陸

linux基礎知識第9單元 ssh和sshkey的應用

圖示:被登陸主機上出現建立的gedit檔案

linux基礎知識第9單元 ssh和sshkey的應用

三、ssnkey 加密【被登陸主機】

1.生成公鑰私鑰

[root@server0 ~]# ssh-keygen         ##生成公鑰私鑰工具,不斷按Enter鍵,直至出現指令行

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):[enter]     ##加密字元儲存檔案(建議用預設)

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase):         [enter]        ##密鑰密碼,必須>4個字元

Enter same passphrase again:                 [enter]        ##确認密碼

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:                        

ab:3c:73:2e:c8:0b:75:c8:39:3a:46:a2:22:34:84:81 [email protected]

The key's randomart p_w_picpath is:

+--[ RSA 2048]----+

|o                |

|E.               |

|..               |

|.  . o           |

|.o. * . S        |

|oo.o o   .       |

|+ =. .  .        |

|o. oo.+..        |

|    ..o*.        |

+-----------------+

[root@server0 ~]# ls /root/.ssh/

authorIzed_keys  id_rsa.pub

id_rsa        ##私鑰,就是鑰匙

id_rsa.pub    ##公鑰,就是鎖

圖示:被登陸主機生成公鑰,私鑰

linux基礎知識第9單元 ssh和sshkey的應用

                   2.添加key認證方式

[root@server0 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub  [email protected]

ssh-copy-id        ##添加key認證方式的工具

-i            ##指定加密key檔案

/root/.ssh/id_rsa.pub    ##加密key

root            ##加密使用者為root

172.25.0.11        ##被加密主機ip

圖示:被登陸主機認證成功

linux基礎知識第9單元 ssh和sshkey的應用

3.分發鑰匙給client主機

[root@server0 ~]# scp /root/.ssh/id_rsa [email protected]:/root/.ssh/

圖示:登陸主機/root/.ssh/初始狀态

linux基礎知識第9單元 ssh和sshkey的應用

圖示:被登陸主機傳送成功

linux基礎知識第9單元 ssh和sshkey的應用

圖示:登陸主機接受成功

linux基礎知識第9單元 ssh和sshkey的應用

4.測試

[root@desktop0 ~]# ssh [email protected]        ##通過id_rsa直接連接配接不需要輸入使用者密碼

Last login: Mon Oct  3 03:58:10 2016 from 172.25.0.250

[root@server0 ~]# 

圖示:通過鑰匙登陸成功

linux基礎知識第9單元 ssh和sshkey的應用

四、提升openssh安全級别

openssh-server配置檔案:/etc/ssh/sshd_config

[root@desktop0 ~]# vim /etc/ssh/sshd_config

78 PasswordAuthentication yes|no        ##是否開啟使用者密碼認證,yes為支援no為關閉

圖示:是否開啟使用者驗證密碼

linux基礎知識第9單元 ssh和sshkey的應用

48 PermitRootLogin yes|no            ##是否允許超級使用者登陸

49 AllowUsers student             ##使用者白名單,隻有在名單中出現的使用者可以使用sshd建立shell

50 DenyUsers westos                ##使用者黑名單

繼續閱讀