天天看點

在RHEL5下搭建SSH遠端登入伺服器

SSH(Secure SHell, 安全指令解釋器)是目前比較流行和實用的遠端登入方式,通過SSH協定可以有效防止遠端管理過程中的資訊洩露問題。本文将以執行個體說明如何在Linux系統中建構SSH遠端登入伺服器。以下是本次實驗的拓撲圖:

要求如下:

1.在Web伺服器上啟用OpenSSH服務,使用端口号為3000,允許網站管理者webmaster從任何用戶端遠端登入Web伺服器,允許使用者xiangxiang隻能從Linux用戶端遠端登入Web伺服器。

2.分别使用密碼驗證和密鑰對(證書)驗證方式遠端登入伺服器。

3.在Windows用戶端使用PuTTY、WinSCP工具實作遠端服務。

步驟如下:

一.SSH伺服器的配置:

在RHEL5系統中,OpenSSH伺服器和用戶端的相關軟體包是預設安裝的,并已将sshd服務添加為标準的系統服務,是以,隻需要在Web伺服器中執行“service sshd start”就可以開啟預設配置sshd服務,包括root在内的大部分使用者(隻要有能執行指令的有效shell)都可以遠端登入系統。但這樣做并不安全,我們需要修改配置檔案(位于/etc/ssh/sshd_config),允許指定的使用者來通路SSH伺服器:

1.開啟sshd服務:

#service  sshd  start

2.修改配置檔案,允許允許網站管理者webmaster從任何用戶端遠端登入Web伺服器,允許使用者xiangxiang隻能從Linux用戶端(192.168.0.77)遠端登入Web伺服器,SSH預設監聽的端口号22,修改為3000,以提高安全性:

#vi /etc/ssh/sshd_config    

Port   3000        //修改監聽端口号為3000,預設為22 

ListenAddress 192.168.0.66     //隻在Web伺服器上提供服務 

PermitRootLogin   no         //禁止root使用者遠端登入 

PermitEmptyPassword   no     //禁止空密碼使用者登入 

LoginGraceTime   1m              //登入驗證過程時間為1分鐘 

MaxAuthTries   3                  //允許使用者登入驗證最大重試次數為3次 

PasswordAuthentication    yes    //允許使用密碼驗證 

AllowUsers    webmaster    [email protected]     //此項需要手動添加,允許webmaster使用者可以從任何用戶端登入,允許使用者xiangxiang隻能從192.168.0.77客戶機登入,其他使用者均拒絕

【注】當root使用者被禁止登入時,可以先使用普通賬号遠端進入系統,在需要執行管理任務時再使用“su -”的方式切換為root,或者在伺服器配置sudo以執行部分管理指令,這樣以提高系統的安全性。

3.建立允許遠端登入Web伺服器的使用者:

#useradd webmaster 

#useradd xiangxiang 

#passwd webmaster 

#passwd xiangxiang

4.重新啟動sshd服務,接下來就可以在用戶端使用密碼驗證方式遠端登入Web伺服器:

#service sshd restart

二.用戶端使用SSH方式登入Web伺服器:

1.驗證webmaster從Linux用戶端(192.168.0.77)SSH遠端登入Web伺服器:(可以登入)

2.驗證xiangxiang從Linux用戶端(192.168.0.77)SSH遠端登入Web伺服器:(可以登入)

3.驗證webmaster和xiangxiang從Windows用戶端(192.168.0.77)SSH遠端登入Web伺服器

使用者webmaster登入成功!

拒絕使用者xiangxiang從除192.168.0.77之外的用戶端登入!

三.配置使用密鑰對(證書)方式遠端登入Web伺服器:

1.在Web伺服器調整/etc/ssh/sshd_config配置檔案,配置使用密鑰對驗證方式登入:

#vi /etc/ssh/sshd_config 

PasswordAuthentication    no   //禁止使用密碼驗證方式

PubkeyAuthentication    yes    //使用密鑰對(證書)方式進行登入驗證

AuthorizedKeysFile     ./ssh/authorized_keys  //指定儲存各使用者公鑰内容的資料檔案位置

2.重新啟動sshd服務:

3.在用戶端(192.168.0.77)建立密鑰對:

[root@localhost ~]# ssh-keygen -t 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: 

33:ee:01:7d:c3:74:83:13:ef:67:ee:d7:60:2d:e1:16 root@localhost 

[root@localhost ~]# ll -a .ssh/ 

總計 24 

drwxrwxrwx    2 root root 4096 10-08 19:29 . 

drwxr-x--- 21 root root 4096 10-08 19:25 .. 

-rw-------    1 root root 1743 10-08 19:29 id_rsa  //建立的私鑰

-rw-r--r--    1 root root    396 10-08 19:29 id_rsa.pub  //建立的公鑰

-rw-r--r--    1 root root    790 2015-11-04 known_hosts 

4.上傳公鑰檔案到Web伺服器:(可以通過FTP,Samba,HTTP,SCP等方式上傳)

[root@localhost ~]# scp -P 3000 .ssh/id_rsa.pub [email protected]:/home/webmaster 

Address 192.168.0.66 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! 

[email protected]'s password:    

id_rsa.pub                          100%    396         0.4KB/s     00:00

檢視公鑰檔案是否上傳成功:(成功!)

在Web伺服器端(SSH伺服器),将公鑰檔案的内容添加至使用者webmaster、xiangxiang授權密鑰庫:

[root@linux5234 ~]# mkdir -p /home/webmaster/.ssh 

[root@linux5234 ~]# mkdir -p /home/xiangxiang/.ssh

[root@linux5234 ~]# cat /home/webmaster/id_rsa.pub >> /home/webmaster/.ssh/authorized_keys 

[root@linux5234 ~]# cat /home/webmaster/id_rsa.pub >> /home/xiangxiang/.ssh/authorized_keys 

[root@linux5234 ~]# ls -l /home/webmaster/.ssh/authorized_keys    

-rw-r--r-- 1 root root 396 10-08 19:56 /home/webmaster/.ssh/authorized_keys 

[root@linux5234 ~]# ls -l /home/xiangxiang/.ssh/authorized_keys    

-rw-r--r-- 1 root root 396 10-08 19:57 /home/xiangxiang/.ssh/authorized_keys

四.在Linux用戶端以密鑰對(證書)驗證方式登入SSH伺服器:

1.以root使用者登入用戶端系統,執行ssh指令,以使用者webmaster遠端登入SSH伺服器:

2.以root使用者登入用戶端系統,執行ssh指令,以使用者xiangxiang遠端登入SSH伺服器:

3.以其它普通使用者(tom)登入用戶端系統,執行ssh指令,以使用者webmaster遠端登入SSH伺服器:

[root@localhost ~]# useradd tom 

[root@localhost ~]# mkdir -p /home/tom/.ssh 

[root@localhost ~]# cp .ssh/id_rsa /home/tom/.ssh/ 

[root@localhost ~]# chown tom.tom /home/tom/.ssh/id_rsa    

[root@localhost ~]# ll /home/tom/.ssh/id_rsa    

-rw------- 1 tom tom 1743 10-08 20:22 /home/tom/.ssh/id_rsa 

登入:

[root@localhost ~]# su - tom 

[tom@localhost ~]$ ssh -p 3000 [email protected] 

The authenticity of host '192.168.0.66 (192.168.0.66)' can't be established. 

RSA key fingerprint is 6f:ef:59:01:b4:cf:73:36:03:42:88:94:73:82:52:43. 

Are you sure you want to continue connecting (yes/no)? yes            

Failed to add the host to the list of known hosts (/home/tom/.ssh/known_hosts). 

Enter passphrase for key '/home/tom/.ssh/id_rsa':      

Last login: Fri Oct    8 20:03:36 2010 from 192.168.0.77 

[webmaster@linux5234 ~]$    

五.在Windows用戶端使用Putty、WinSCP以密鑰對(證書)驗證方式登入SSH伺服器:

1.使用FTP或其他方法從Linux用戶端導出私鑰至Windows用戶端(步驟略)。

2.使用PUTTYGEN工具導入私鑰檔案并轉換成.ppk格式的私鑰:

3.使用Putty登入:

4.使用WinSCP工具遠端登入SSH伺服器(需要先在用戶端安裝WinSCP軟體),以安全的方式上傳和下載下傳檔案:

安裝好WinSCP後,打開程式,看到以下界面:

通過以上的配置,我們可以很友善遠端登入Linux各種伺服器,實作安全便捷的管理!

本文轉自 kk5234 51CTO部落格,原文連結:http://blog.51cto.com/kk5234/402512,如需轉載請自行聯系原作者

繼續閱讀