在 CentOS 實作 SSH 連線

How to use ssh protocol on CentOS

作者: 七味粉 |
文章2013-04-16
132 分享
196
    很多人都搞不懂如何在Linux上面實作SSH,
    這邊我就簡易說明一下。


  • 以一個使用者(user)的身分產生密鑰在/home/user/.ssh/底下
    
    
    [shell]cd /home/user/.ssh
    [shell]ssh-keygen -t rsa



  • 將公鑰灌進去
    
    
    [shell]cd authorized_keys
    [shell]cat id_rsa.pub >> authorized_keys [tell]灌公鑰進去[/tell]
    [shell]chmod 400 authorized_keys [tell]設定authorized_keys權限[/tell]




  • 設定sshd_config
    
    
    [shell]vim /etc/ssh/sshd_config
    [vim]Protocol 2
    ServerKeyBits 1024
    PermitRootLogin no [com]禁止root登錄[/com]
    [com]以下三行沒什麼要改的,把默認的#註釋去掉就行了[/com]
    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
    PasswordAuthentication no [com]KEY與密碼一定要選一個[/com]
    PermitEmptyPasswords no [com]禁止使用空密碼登入[/com]



  • 設定哪些IP可使用SSH連線
    
    
    /etc/hosts.allow 允許此IP使用SSH登入
    /etc/hosts.deny 拒絕此IP使用SSH登入

    
    
    [shell]vim /etc/hosts.allow
    [vim][com]允許此ip使用SSH登入[/com]
    sshd:192.168.10.130[/vim]

    
    
    [shell]vim /etc/host.deny
    [vim][com]限制IP使用SSH 連線[/com]
    sshd:all[/vim]



  • 重啟服務
    
    
    [shell]service sshd restart



  • client連線(私鑰)
    
    
    [shell]ssh -i id_rsa [email protected]
    [tell]用root登入遠端,選擇私鑰 id_rsa[/tell]



    1. 小提示:
    2. server 端用 public key,client 端用 private key

    3. key傳的方式 mail or FTP 方式傳輸

    4. 若要讓putty 使用,請下載 puttygen 轉成 .ppk

    5. 若puttygen無法使用,請移到D槽再試看看。

    6. .ssh的資料夾權限務必設成755

    7. id_rsa.pub and authorized_keys 權限設成644

    8. id_rsa 權限設成600

    9. 用戶目錄權限設成755 or 700

    貼心小提醒
    1.您目前未登入,若於此狀態進行文章收藏,紀錄可能無法長期保留。
    2.建議登入後進行收藏,好文會幫你保存著,隨時要看也不怕找不到唷~

    KiKiNote持續為您提供最優質的資訊內容,謝謝您!
    不再顯示此視窗

    複製成功