CentOS之SSH安裝與配置

來源:互聯網
上載者:User

標籤:des   http   io   os   使用   ar   for   檔案   資料   


    sshlinux
    SSH 為 Secure Shell 的縮寫,由 IETF 的網路工作小組(Network Working Group)所制定;SSH 為建立在應用程式層和傳輸層基礎上的安全性通訊協定。
     
    傳統的網路服務程式,如FTP、POP和Telnet其本質上都是不安全的;因為它們在網路上用明文傳送資料、使用者帳號和使用者口令,很容易受到中間人(man-in-the-middle)攻擊方式的攻擊。就是存在另一個人或者一台機器冒充真正的伺服器接收使用者傳給伺服器的資料,然後再冒充使用者把資料傳給真正的伺服器。
     
    而 SSH 是目前較可靠,專為遠程登入工作階段和其他網路服務提供安全性的協議。利用 SSH 協議可以有效防止遠端管理過程中的資訊泄露問題。透過 SSH 可以對所有傳輸的資料進行加密,也能夠防止 DNS 欺騙和 IP 欺騙。
     
    系統及版本:CentOS release 5.3 (Final)
     
    安裝SSH
     

    yum install ssh

    啟動SSH
     

    service sshd start

    設定開機運行
     

    chkconfig sshd on

     
     
    SSH相關設定檔的修改
     
      首先修改SSH的設定檔。如下:
     

    [[email protected] ~]# vi /etc/ssh/sshd_config

  ← 用vi開啟SSH的設定檔
     
     
    #Protocol 2,1 ← 找到此行將行頭“#”刪除,再將行末的“,1”刪除,只允許SSH2方式的串連
     ↓
    Protocol 2 ← 修改後變為此狀態,僅使用SSH2
     
    #ServerKeyBits 768 ← 找到這一行,將行首的“#”去掉,並將768改為1024
     ↓
    ServerKeyBits 1024 ← 修改後變為此狀態,將ServerKey強度改為1024位元
     
    #PermitRootLogin yes  ← 找到這一行,將行首的“#”去掉,並將yes改為no
     ↓
    PermitRootLogin no  ← 修改後變為此狀態,不允許用root進行登入
     
    #PasswordAuthentication yes ← 找到這一行,將yes改為no
     ↓
    PasswordAuthentication no ← 修改後變為此狀態,不允許密碼方式的登入
     
    #PermitEmptyPasswords no  ← 找到此行將行頭的“#”刪除,不允許空密碼登入
     ↓
    PermitEmptyPasswords no  ← 修改後變為此狀態,禁止空密碼進行登入
     
      然後儲存並退出。(vi儲存退出的命令為ZZ)
     
      因為我們只想讓SSH服務為管理系統提供方便,所以在不通過外網遠端管理系統的情況下,只允許內網用戶端通過SSH登入到伺服器,以最大限度減少不安全因素。設定方法如下:
     

    [[email protected] ~]# vi /etc/hosts.deny

  ← 修改屏蔽規則,在文尾添加相應行
     
     
    #
    # hosts.deny This file describes the names of the hosts which are
    # *not* allowed to use the local INET services, as decided
    # by the ‘/usr/sbin/tcpd’ server.
    #
    # The portmap line is redundant, but it is left to remind you that
    # the new secure portmap uses hosts.deny and hosts.allow. In particular
    # you should know that NFS uses portmap!
    sshd: ALL  ← 添加這一行,屏蔽來自所有的SSH串連請求
     

    [[email protected] ~]# vi /etc/hosts.allow

  ← 修改允許規則,在文尾添加相應行
     
    #
    # hosts.allow This file describes the names of the hosts which are
    # allowed to use the local INET services, as decided
    # by the ‘/usr/sbin/tcpd’ server.
    #
     
    sshd: 192.168.0.  ← 添加這一行,只允許來自內網的SSH串連請求
     
     
     
    重啟動SSH啟動
      在修改完SSH的設定檔後,需要重新啟動SSH服務才能使新的設定生效。
     

    [[email protected] ~]# /etc/rc.d/init.d/sshd restart

  ← 重新啟動SSH伺服器
     
     
    Stopping sshd:             [ OK ]
    Starting sshd:             [ OK ]  ← SSH伺服器重新啟動成功
     
      這時,在遠程終端(自用PC等等)上,用SSH用戶端軟體以正常的密碼的方式是無法登入伺服器的。為了在客戶能夠登入到伺服器,我們接下來建立SSH用的公開金鑰與私密金鑰,以用於用戶端以“鑰匙”的方式登入SSH伺服器。
     
    SSH2的公開金鑰與私密金鑰的建立
      登入狀態一個一般使用者,基於這個使用者建立公開金鑰與私密金鑰。(這裡以centospub使用者為例)
     

    [[email protected] ~]# su – centospub

 ← 登入狀態一般使用者centospub
     
     
    [centospub@sample ~]$ ssh-keygen -t rsa  ← 建立公開金鑰與私密金鑰
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/kaz/.ssh/id_rsa):  ← 鑰匙的檔案名稱,這裡保持預設直接斷行符號
    Created directory ‘/home/kaz/.ssh’
    Enter passphrase (empty for no passphrase):  ← 輸入口令
    Enter same passphrase again:   ← 再次輸入口令
    Your identification has been saved in /home/kaz/.ssh/id_rsa.
    Your public key has been saved in /home/kaz/.ssh/id_rsa.pub.
    The key fingerprint is:
    tf:rs:e3:7s:28:59:5s:93:fe:33:84:01:cj:65:3b:8e [email protected]
     
      然後確認一下公開金鑰與密鑰的建立,以及對應於用戶端的一些處理。
     

    [[email protected] ~]$ cd ~/.ssh

  ← 進入使用者SSH設定檔的目錄
     
     

    [[email protected] .ssh]$ ls -l

  ← 列出檔案
    total 16
    -rw——- 1 centospub centospub 951 Sep 4 19:22 id_rsa  ← 確認私密金鑰已被建立
    -rw-r–r– 1 centospub centospub 241 Sep 4 19:22 id_rsa.pub  ← 確認公開金鑰已被建立
     

    [[email protected] .ssh]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

  ← 公開金鑰內容輸出到相應檔案中

         [[email protected] .ssh]$ rm -f ~/.ssh/id_rsa.pub

  ← 刪除原來的公開金鑰檔案
     

    [[email protected] .ssh]$ chmod 400 ~/.ssh/authorized_keys 

← 將建立立的公開金鑰檔案屬性設定為400
     
      然後,將私密金鑰通過安全的方式轉移到欲通過SSH串連到伺服器的PC上。這裡,以通過3.5寸磁碟為介質為例:
     
 

   [email protected] .ssh]$ exit  

← 退出一般使用者的登入(返回root的登入)
     
     

    [[email protected] ~]# mount /mnt/floppy/

  ← 載入軟碟機
     
 

   [[email protected] ~]# mv /home/centospub/.ssh/id_rsa /mnt/floppy/

 ← 將剛剛建立的私密金鑰移動到磁碟片
     

    [[email protected] ~]# umount /mnt/floppy/

 ← 卸載軟碟機
     


CentOS之SSH安裝與配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.