1. 程式人生 > >CentOS 7.1下SSH遠程登錄服務器詳解-轉

CentOS 7.1下SSH遠程登錄服務器詳解-轉

info which 開啟 如何 pty wan public keygen ger

轉自:http://www.linuxidc.com/Linux/2016-03/129204.htm

一、明文傳輸與加密傳輸

明文傳輸:當我們的數據包在網絡上傳輸的時候,以數據包的原始格式進行傳輸,別人很容易截獲我們的數據包,得到我們的信息。

加密傳輸:當兩個主機之間傳輸信息或者是A主機遠程控制B主機的時候,在兩個主機傳輸數據包之前,加密過之後才通過網絡傳輸過去。因此,就算有人截獲了傳輸的數據包,也不知道傳輸的內容。

二、SSH(Secure Shell)簡介

SSH是建立在傳輸層和應用層上面的一種安全的傳輸協議。SSH目前較為可靠,專為遠程登錄和其他網絡提供的安全協議。在主機遠程登錄的過程中有兩種認證方式:

技術分享

基於口令認證:只要你知道自己帳號和口令,就可以登錄到遠程主機。所有傳輸的數據都會被加密,但是不能保證你正在連接的服務器就是你想連接的服務器。可能會有別的服務器在冒充真正的服務器,也就是受到“中間人”這種方式的攻擊。

基於秘鑰認證:需要依靠秘鑰,也就是你必須為自己創建一對秘鑰,並把公用的秘鑰放到你要訪問的服務器上,客戶端軟件就會向服務器發出請求,請求用你的秘鑰進行安全驗證。服務器收到請求之後,現在該服務器你的主目錄下尋找你的公用秘鑰,然後吧它和你發送過來的公用秘鑰進行比較。弱兩個秘鑰一致服務器就用公用秘鑰加密“質詢”並把它發送給客戶端軟件,客戶端軟件收到質詢之後,就可以用你的私人秘鑰進行解密再把它發送給服務器。

用這種方式,你必須要知道自己的秘鑰口令。但是與第一種級別相比,地為主不需要再網絡上傳輸口令

第二種級別不僅加密所有傳送的數據,而且“中間人”這種攻擊方式也是不可能的(因為他沒有你的私人密匙)。但是整個登錄的過程可能需要10秒。

三、啟動SSH服務

3.1 註意:

CentOS 7.1安裝完之後默認已經啟動了ssh服務我們可以通過以下命令來查看ssh服務是否啟動。

3.2查看22端口是否開放

#netstat -tnl

技術分享

3.3查看ssh服務是否啟動

#systemctl status sshd.service

技術分享

四、SSH客戶端連接服務器(口令認證)

4.1 直接連接到對方的主機,這樣登錄服務器的默認用戶

$ssh 192.168.142.84
[[email protected] ~]$ ssh 192.168.142.84
[email protected] password:
Last login: Sat Jun 27 10:16:21 2015 from 10.66.85.46
Hello world
Welcome to this Linux ! 2015年 06月 27日 星期六 10:54:54 CST

$exit 退出遠程登錄

4.2 使用賬號登錄對方主機nii用戶

[[email protected] ~]$ ssh [email protected]

[email protected] password: 
Last login: Fri Jun 26 14:48:03 2015 from 192.168.142.35
Hello world

[[email protected] ~]$ exit
登出
Connection to 192.168.142.84 closed.

五、SSH客戶端連接服務器(秘鑰認證)

這種認證方式是比較安全的。

秘鑰認證步驟:

  1. 生成公鑰和私鑰,生成的秘鑰默認在/root/.ssh/文件夾裏面

    [[email protected] admin]# ssh-keygen
    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:
    7e:fa:8c:c4:a5:0d:e0:db:8e:87:25:15:4a:d2:9b:0b [email protected]
    The key’s randomart image is:
    +–[ RSA 2048]—-+
    | . |
    | . o . |
    | o.+ . |
    | E.+.. |
    | ..oS . |
    | o=.= |
    | .+* o |
    | .+.= |
    | ..+.o |
    +—————–+

  2. 把生成的公鑰發送到對方的主機上去,用ssh-copy-id命令,自動保存在對方主機的/root/.ssh/authorized_keys 文件中去

    [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
    /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
    [email protected] password:
    Hello world

    Number of key(s) added: 1

    Now try logging into the machine, with: “ssh >[email protected]
    and check to make sure that only the key(s) you wanted were added.

    [[email protected] ~]# ssh 192.168.142.84 登錄不需要密碼了
    Last login: Sat Jun 27 10:01:07 2015 from 192.168.142.191
    Hello world

下面關於SSH相關的文章您也可能喜歡,不妨參考下:

Ubuntu 下配置 SSH服務全過程及問題解決 http://www.linuxidc.com/Linux/2011-09/42775.htm

Ubuntu 14.04 下安裝Samba 及SSH 服務端的方法 http://www.linuxidc.com/Linux/2015-01/111971.htm

SSH服務遠程訪問Linux服務器登陸慢 http://www.linuxidc.com/Linux/2011-08/39742.htm

提高Ubuntu的SSH登陸認證速度的辦法 http://www.linuxidc.com/Linux/2014-09/106810.htm

開啟SSH服務讓Android手機遠程訪問 Ubuntu 14.04 http://www.linuxidc.com/Linux/2014-09/106809.htm

如何為Linux系統中的SSH添加雙重認證 http://www.linuxidc.com/Linux/2014-08/105998.htm

在 Linux 中為非 SSH 用戶配置 SFTP 環境 http://www.linuxidc.com/Linux/2014-08/105865.htm

Linux 上SSH 服務的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm

更多CentOS相關信息見CentOS 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=14

本文永久更新鏈接地址:http://www.linuxidc.com/Linux/2016-03/129204.htm

CentOS 7.1下SSH遠程登錄服務器詳解-轉