OpenSSH技術詳解


一、什么是Openssh   

    OpenSSH 是 SSH (Secure SHell) 協議的免費開源實現。SSH協議族可以用來進行遠程控制, 或在計算機之間傳送文件。而實現此功能的傳統方式,如telnet(終端仿真協議)、 rcp ftp、 rlogin、rsh都是極為不安全的,並且會使用明文傳送密碼。OpenSSH提供了服務端后台程序和客戶端工具,用來加密遠程控件和文件傳輸過程中的數據,並由此來代替原來的類似服務。

知識延伸:
    ssh協議有兩個版本:
        v1:基於CRC-32 做MAC,不安全; (一般用於實現主機認證)
        v2:基於協議協商選擇雙方都支持的最安全的MAC機制
            基於DH做密鑰交換,基於RSA或DSA實現身份認證,從而實現無需輸入賬號面膜
            客戶端通過檢查服務器端的主機秘鑰來判斷是否能夠繼續通信;
    認證方式:
        1、基於口令的認證
        2、基於密鑰的認證

 

二、為什么要使用OpenSSH

    由於傳統的telne、rcp ftp等工具是明文傳輸數據的,對數據安全性存在很大的安全隱患,而OpenSSH可以對傳輸的數據進行加密從而大大提高了數據的安全性。

 

三、OpenSSH程序簡介

  1、OpenSSH的分為客戶端和服務端兩部分

    Clients端的配置文件:/etc/ssh/ssh_config
    Server端的配置文件:/etc/ssh/sshd_config
    Server端服務腳本:/etc/rc.d/init.d/sshd

    OpenSSH在Linux系統中默認是安裝並啟動的

openssh 主要的關鍵包有四個
openssh.x86_64                       5.3p1-104.el6   //服務端和客戶端的公共組件        
openssh-askpass.x86_64               5.3p1-104.el6   //     
openssh-clients.x86_64               5.3p1-104.el6   //客戶端安裝包    
openssh-server.x86_64                5.3p1-104.el6   //服務端安裝包

 

    openssl-clients  幾個常用文件

[root@1inux ssh]# rpm -ql openssh-clients
/etc/ssh/ssh_config  //客戶端配置文件
/usr/bin/scp    //遠程復制文件
/usr/bin/sftp    //遠程文件共享
/usr/bin/slogin
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id
/usr/bin/ssh-keyscan
 

    openssl-server 幾個常用文件

/etc/rc.d/init.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
 

  2、服務器端配置文件/etc/ssh/sshd_config 主要參數詳解

服務端配置文件是讓別人登陸時使用的
注:配置文件中使用“#”注釋掉的一般就是使用默認
    #Port 22    //默認端口號,為了其安全一般要更改為其他端口
    #AddressFamily any    //說明要監聽任意地址
    #ListenAddress 0.0.0.0 //監聽本機所有IPV4的ip
    #ListenAddress ::        //監聽本機所有的IPV6的地址
     Protocol 2   監聽的協議版本
    # HostKey for protocol version 1   //說明key的協議版本
    SyslogFacility AUTHPRIV        //使用AUTHPRIV 記錄日志  
    #LogLevel INFO    //log日志級別
                
                
    #Authentication:            //認證相關
    #LoginGraceTime 2m    //登陸寬限時長  默認2分鍾不登錄自動關閉
    #PermitRootLogin yes    //是否支持管理員直接登陸
    #StrictModes yes    //是否使用嚴格模式 (嚴格檢查用戶的某些相關信息)
    #MaxAuthTries 6        //最大嘗試次數  (6次以后終端斷開)
    #MaxSessions 10        //最大並發允許鏈接數 (超過 將拒絕)
    #RSAAuthentication yes    //是否支持RSA密鑰認證
    #PubkeyAuthentication yes    //是否支持公鑰認證
    #AuthorizedKeysFile     .ssh/authorized_keys  //默認保存口令的文件
    #PermitEmptyPasswords no        //是否支持空密碼登陸
    PasswordAuthentication yes
                
    UsePAM yes //是否使用PAM 認證( 是一種統一認證框架)
    X11Forwarding yes     //是否轉發圖形界面請求 (可以打開遠程服務器圖形界面)
    Subsystem       sftp    /usr/libexec/openssh/sftp-server  
    #UseDNS yes        //是否允許DNS反解  比較浪費時間一般更改為no
                    /etc/ssh/ssh_known_hosts  //保存已經認可主機的文件

 

  3、客戶端配置文件/etc/ssh/ssh_config 主要參數詳解

客戶端配置文件時登陸別人的ssh使用的
    #Host *        //表示連接所有主機
    #Port 22        //默認連接端口
    #Cipher 3des        //加密時使用的加密機制
    #StrictHostKeyChecking ask    //嚴格的主機秘鑰檢查 即第一次連接時是否詢問

 

四、客戶端ssh的使用
    
    1、ssh的基本語法
        ssh [OPTIONS] [user]@server [COMMAND]
                    
            -l user: 以指定用戶身份連接至服務器;默認使用本地用戶為遠程登錄時的用戶;
                ssh user@server
                ssh -l user server

[root@1inux ~]# ssh centos@172.16.66.81
The authenticity of host '172.16.66.81 (172.16.66.81)' can't be established.
RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.
Are you sure you want to continue connecting (yes/no)? 
[root@1inux ~]# ssh -l centos 172.16.66.81
The authenticity of host '172.16.66.81 (172.16.66.81)' can't be established.
RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.
Are you sure you want to continue connecting (yes/no)?
 

            -p PORT:指明要連接的端口:

[root@1inux ~]# ssh -p 22 -l centos 172.16.66.81
The authenticity of host '172.16.66.81 (172.16.66.81)' can't be established.
RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.
Are you sure you want to continue connecting (yes/no)?

            -X :啟用X11Forwarding,即轉發X界面的請求;
            -x: 禁用;
            -Y:  啟用信任的X11Forwarding

    2、ssh 基於秘鑰的認證
        2.1、ssh-keygen語法:
           ssh-keygen [OPTIONS]
            
            -t {rsa|dsa} 密鑰類型 一般使用rsa
            -b # 指明密鑰長度
            -f /PATH/TO/OUTPUT_KEYFILE  指明密鑰文件
            -P '' :指明加密密鑰的密碼,表示使用空密碼

[root@1inux ~]# ssh-keygen -t rsa    //生成密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_90  //定義保存的密鑰文件名
Enter passphrase (empty for no passphrase):         //要求對生成的密鑰對加密,也可以不加密  輸入兩次回車就OK 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_90.        //說明已經生成密鑰
Your public key has been saved in /root/.ssh/id_90.pub.        //說明已經生成公鑰
The key fingerprint is:
7a:17:b3:e7:6f:54:a1:30:23:62:7c:04:37:10:52:08 root@1inux
The key's randomart image is:
+--[ RSA 2048]----+
|   E..+==+       |
|     ..+.o.+   . |
|      . o . + . .|
|             .  .|
|        S o    . |
|       .   +  .  |
|      . . o ..   |
|       . . o  .  |
|            .o.  |
+-----------------+
[root@1inux ~]# 
[root@1inux ~]# ls .ssh/
id_90  id_90.pub  known_hosts
[root@1inux ~]# 
------------------------- 

也可以指明秘鑰路徑 及密碼

[root@1inux .ssh]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''  //經測試 名字必須為id_rsa
Generating public/private rsa key pair.
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:c3:f8:f3:2c:ed:88:cc:db:7a:97:5f:d0:de:ce:d9 root@1inux
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|       o     .   |
|      . S   . .  |
|       . +   o . |
|        o. .  o .|
|     o oo=+  . oo|
|      *+o++..  .E|
+-----------------+
[root@1inux ~]# ls .ssh/
id_rsa  id_rsa.pub  id_www  id_www.pub  known_hosts

 

        2.2、把公鑰部分復制到要登陸遠程主機的特定用戶(可不同於本地用戶)的家目錄下,追加保存至.ssh 目錄中的authorized_keys文件中;

ssh-copy-id -i /PATH/TO/PUBKEY_FILE [user]@server 
//使用此命令會自動將公鑰復制到目標主機指定用戶的家目錄下的.ssh/authorized_keys文件中

[root@1inux ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub henan@172.16.66.81
henan@172.16.66.81's password: 
Now try logging into the machine, with "ssh 'henan@172.16.66.81'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@1inux ~]#
 

        2.3、驗證:

[root@1inux .ssh]# ssh lfs@172.16.66.81
Last login: Wed Apr  8 14:31:52 2015 from 172.16.66.90
[lfs@1inux ~]$         //可以看到已經登錄成功
[henan@1inux ~]$ whoami
henan
[henan@1inux ~]$ ifconfig | grep "172.16.66"
          inet addr:172.16.66.81  Bcast:172.16.255.255  Mask:255.255.0.0

 

基於密鑰的命令總結

1、[root@1inux .ssh]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P '' 
2、[root@1inux ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub henan@172.16.66.81
3ssh lfs@172.16.66.81

 

        2.4、通過ssh直接執行命令

[root@1inux ~]# ssh -l henan 172.16.66.81 date    //由於此用於已經基於密鑰認證所以沒有要求輸入密碼
Wed Apr  8 15:01:15 CST 2015 

五、scp遠程復制工具的使用簡介
    語法:
        scp [OPTIONS] SRC...DEST
    常用選項:
        
        -r: 遞歸復制,復制目錄及內部文件時使用;
        -p: 保存源文件元數據信息中的屬主、屬組及權限;
        -q: 靜默模式
        -P PORT: 指明遠程服務器使用的端口;

    兩種模式:
        PUSH: scp [OPTIONS] /PATH/FROM/SOMEFILE ... user@server:/PATH/TO/DEST   

                       //復制本地文件至遠程主機 (但遠程目錄一定要有寫權限)
        
        PULL: SCP [OPTIONS] user@server:/PATH/FROM/SOMEFILE /PATH/TO/DEST    

                      //遠程目標主機文件至本地 
eg1:復制當前主機上的/etc/ssh/sshd_config至172.16.66.90主機的/tmp/aa目錄下

[root@1inux ~]# scp -p /etc/ssh/sshd_config henan@172.16.66.90:/tmp/aa    
henan@172.16.66.90's password: 
sshd_config                                                                  100% 3879     3.8KB/s   00:00    
 

eg2:復制遠程主機/etc/fstab至當前主機當前目錄下

[root@1inux ~]# ls
anaconda-ks.cfg  Documents  grub.conf           install.log         Music     Public     Videos
Desktop          Downloads  henan@172.16.66.90  install.log.syslog  Pictures  Templates
[root@1inux ~]# scp root@172.16.66.90:/etc/fstab ./
root@172.16.66.90's password: 
fstab                                                                        100%  921     0.9KB/s   00:00    
[root@1inux ~]# ls
anaconda-ks.cfg  Documents  fstab      henan@172.16.66.90  install.log.syslog  Pictures  Templates
Desktop          Downloads  grub.conf  install.log         Music               Public    Videos

六、sftp的使用
    要使用sftp需要編輯/etc/ssh/sshd_config 開啟Subsystem 即:
    Subsystem       sftp    /usr/libexec/openssh/sftp-server  

    語法:

sftp [USER]@server


    常用命令:put get

eg:

[root@1inux ~]# sftp root@172.16.66.90
Connecting to 172.16.66.90...
root@172.16.66.90's password:         //輸入密碼
sftp> help            //可以使用help查看其支持的命令
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp grp path                     Change group of file 'path' to 'grp'
chmod mode path                    Change permissions of file 'path' to 'mode'
chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-P] remote-path [local-path]  Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln oldpath newpath                 Symlink remote file
lpwd                               Print local working directory
ls [-1aflnrSt] [path]              Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-P] local-path [remote-path]  Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help
====================================================================================

 

七、增強服務端sshd配置指南
        1、不要使用默認端口;(修改默認端口為其他端口)
                  配置文件:/etc/ssh/sshd_config    
                                Port 22
                        service sshd restart   //修改后需要重啟服務
        2、不要使用v1版本協議:
                Protocol 2                 
        3、限制可登陸的用戶 {需要添加}
             AllowUsers:允許登陸的用戶白名單 (多個用戶使用空格隔開)
             AllowGroups:允許登陸的組的白名單                    
             DenyUsers
             DenyGroups                    
             /etc/ssh/sshd_config
                
              # service sshd reload 
              ==》 獲取配置文件詳細信息;【 man sshd_conifg 】
        4、設定空閑會話超時時長:
        5、利用防火牆設置ssh訪問策略:
            限定ssh服務僅允許***服務器分配有限的地址段內的主機訪問                
        6、僅監聽特定的IP地址:            
        7、使用強密碼策略:                

[root@1inux ssh]# tr -dc A-Za-z0-9 < /dev/urandom | head -c 30 | xargs
Qe6zOmB2sBNpEONVcKhWS8T4bVrcb0

        8、使用基於密鑰的認證;            
        9、禁止使用空密碼            
        10、禁止root直接登陸
                    PermitRootLogin no 
        11、限制ssh的訪問頻度            
        12、做好日志、經常做日志分析
            /var/log/secure 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM