ssh軟件及命令的使用


  

常用軟件安裝及使用目錄

 

第1章 ssh常用用法小結

1.1 連接到遠程主機:

命令格式

ssh name@remoteserver 或者

ssh remoteserver -l name

說明:以上兩種方式都可以遠程登錄到遠程主機,server代表遠程主機,name為登錄遠程主機的用戶名。

實例1-1 

[root@m01 ~]# ssh 172.16.1.31            用戶名可以省略

Last login: Sun Oct 22 07:01:34 2017 from 10.0.0.1

wo shi ma chang wei

[root@nfs01 ~]# ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.31  Bcast:10.0.0.255  Mask:255.255.255.0

實例1-2 

[root@nfs01 ~]# ssh oldboy@172.16.1.8

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

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

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

Warning: Permanently added '172.16.1.8' (RSA) to the list of known hosts.

oldboy@172.16.1.8's password:

wo shi ma chang wei

[oldboy@web01 ~]$ ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0

實例1-3 

[oldboy@web01 ~]$ ssh 172.16.1.7 -l oldboy

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

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

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

Warning: Permanently added '172.16.1.7' (RSA) to the list of known hosts.

oldboy@172.16.1.7's password:

wo shi ma chang wei

[oldboy@web02 ~]$ ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0

 

1.2 連接到遠程主機指定的端口:

命令格式:

ssh name@remoteserver -p 2222 或者

ssh remoteserver -l name -p 2222

說明:參數指定端口號,通常在路由里做端口映射時,我們不會把22端口直接映射出去,而是轉換成其他端口號,這時就需要使用-p端口號命令格式。

實例1-1 

[root@m01 ~]# ssh 172.16.1.41 

ssh: connect to host 172.16.1.41 port 22: Connection refused

[root@m01 ~]# ssh -p52113 172.16.1.41

Last login: Sat Oct 21 23:18:08 2017 from 10.0.0.1

wo shi ma chang wei

[root@backup ~]# ip a|sed -n "8p"

    inet 10.0.0.41/24 brd 10.0.0.255 scope global eth0

1.3 通過遠程主機1跳到遠程主機2

命令格式:

ssh -t remoteserver1 ssh remoteserver2

說明:當遠程主機remoteserver2無法直接到達時,可以使用-t參數,然后由remoteserver1跳轉到remoteserver2。在此過程中要先輸入remoteserver1的密碼,然后再輸入remoteserver2的密碼,然后就可以操作remoteserver2了。

實例1-1 

[root@m01 ~]# ssh -t 172.16.1.31 ssh -p52113 oldboy@172.16.1.41

The authenticity of host '[172.16.1.41]:52113 ([172.16.1.41]:52113)' can't be established.

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

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

Warning: Permanently added '[172.16.1.41]:52113' (RSA) to the list of known hosts.

oldboy@172.16.1.41's password:

wo shi ma chang wei

[oldboy@backup ~]$ ip a|sed -n "2p"

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

[oldboy@backup ~]$ ip a|sed -n "8p"

    inet 10.0.0.41/24 brd 10.0.0.255 scope global eth0

1.4 通過SSH運行遠程shell命令:

命令格式:

ssh -l name remoteserver ‘command’

說明:連接到遠程主機,並執行遠程主機的command命令。例如:查看遠程主機的內存使用情況。

$ ssh -l root 192.168.1.100 svmon -G

實例1-1 

[root@m01 ~]# ssh -l oldboy 172.16.1.31 free -m

oldboy@172.16.1.31's password:

             total       used       free     shared    buffers     cached

Mem:           980        193        786          0         34         51

-/+ buffers/cache:        107        873

Swap:          767          0        767

 

1.5 修改SSH監聽端口:

默認情況下,SSH監聽連接端口22,攻擊者使用端口掃描軟件就可以看到主機是否運行有SSH服務,將SSH端口修改為大於1024的端口是一個明智的選擇,因為大多數端口掃描軟件(包括nmap)默認情況都不掃描高位端口。打開/etc/ssh/sshd_config文件並查找下面這樣的行:

Port  22

去掉該行前面的號,然后修改端口號並重新啟動SSH服務:

$ /etc/init.d/ssh restart

實例1-1 

[root@backup ~]# ss -lntup|grep sshd

tcp    LISTEN     0      128                   :::52113                :::*      users:(("sshd",1415,4))

tcp    LISTEN     0      128                    *:52113                 *:*      users:(("sshd",1415,3))

[root@backup ~]# vim /etc/ssh/sshd_config

#Port 52113

[root@backup ~]# /etc/init.d/sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

[root@backup ~]# ss -lntup|grep sshd

tcp    LISTEN     0      128                   :::22                   :::*      users:(("sshd",1543,4))

tcp    LISTEN     0      128                    *:22                    *:*      users:(("sshd",1543,3))

1.6 僅允許SSH協議版本2

有兩個SSH協議版本,僅使用SSH協議版本2會更安全,SSH協議版本1有安全問題,包括中間人攻擊(man-in-the-middle)和注入(insertion)攻擊。編輯/etc/ssh/sshd_config文件並查找下面這樣的行:

# Protocol 2,1

修改為

Protocol 2

1.7 禁止root用戶登錄:

通常情況下,不采用直接用root用戶登錄到遠程主機,由於root用戶擁有超級權限,這樣會帶來安全隱患,所以,一般我們用普通用戶登錄,當需要管理遠程主機時,再切換到root用戶下。打開/etc/ssh/sshd_config文件並查找下面這樣的行:

#PermitRootLogin yes

#號去掉,然后將yes修改成no,重啟ssh服務,這樣就可以禁止root用戶登錄。

實例1-1 

[root@m01 ~]# ssh oldboy@172.16.1.41

oldboy@172.16.1.41's password:

Last login: Sat Oct 21 23:25:45 2017 from 172.16.1.31

wo shi ma chang wei

[oldboy@backup ~]$ logout

Connection to 172.16.1.41 closed.

[root@m01 ~]# ssh root@172.16.1.41

root@172.16.1.41's password:

Permission denied, please try again.

1.8 設置登錄時提示信息

首先編輯一個文件,如bannertest.txt,文件內容自行定義。然后打開/etc/ssh/sshd_config文件並查找下面這樣的行:

#Banner /some/path

#號去掉,然后將bannertest.txt文件的全路徑替換/some/path,然后保存,重啟ssh服務。當客戶端登錄時,就會看到bannertest.txt文件中的提示信息。

實例1-1 

# no default banner path

#Banner none

banner /machangwei/qianyan           

[root@backup ~]# /etc/init.d/sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

 

[root@m01 ~]# ssh 172.16.1.41

wo hen shan liang de

Last login: Sat Oct 21 23:50:38 2017 from 172.16.1.61

wo shi ma chang wei

1.9 進行端口映射:

假如公司內網有台web服務器,但是只對內不對外,這樣,外網就無法訪問,可以用ssh進行端口映射來實現外網訪問內網的web服務器。假如web服務器名為webserverwebserver可以用ssh訪問到遠端主機remoteserver,登錄到webserver,然后用下面命令進行映射

命令格式:

ssh -R 3000:localhost:80 remoteserver

執行完成后,在remoteserver機器上,執行netstat -an | grep 3000,查看有沒有開通3000端口。並執行以下命令觀察是否可以打開webserver上的網頁

$ w3m http://127.0.0.1:3000

如果能打開界面,說明映射成功.但是,這只限於本機訪問web服務器,即只能remoteserver機器訪問webserver。因為3000端口綁定的是remoteserver機器的127.0.0.1端口。可以編輯remoteserver機器上的/etc/ssh/sshd_config文件並添加如下內容:

添加 GatewayPorts yes  內容,把監聽端口3000綁定到 0.0.0.0 地址上,這樣外部的所有機器都能訪問到這個監聽端口,然后保存退出。並重啟ssh服務。完成后其它機器就可以在瀏覽器中輸入 http://remoteserver:3000來訪問webserver了。

 

第2章 幾個ssh管道用法

2.1 查看SSH客戶端版本

有的時候需要確認一下SSH客戶端及其相應的版本號。使用ssh -V命令可以得到版本號。需要注意的是,Linux一般自帶的是OpenSSH: 下面的例子即表明該系統正在使用OpenSSH

$ ssh -V 
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003

下面的例子表明該系統正在使用SSH2

$ ssh -V 
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu

 

1. remote file copy

[root@m01 ~]# cat 1.txt

  root:x:0:0:root:/root:/bin/bash

[root@m01 ~]# cat 1.txt|ssh 172.16.1.31 'cat - >~/aaa'

[root@m01 ~]# ssh 172.16.1.31 "cat aaa"

  root:x:0:0:root:/root:/bin/bash


拷貝文件時,如果文件很大,又不想影響網絡IO可以用pv工具進行流量控制

pv -L10m test.pl | ssh 10.1.74.76 'cat - > /tmp/test.pl'

這里pv的行為跟cat比較類似,但是支持IO流量控制,這里設置10M/s.

2.2 local script remote execute

[root@m01 ~]# cat 1.txt

#!/bin/sh

hostname

[root@m01 ~]# cat 1.txt|ssh 172.16.1.31 "sh"

nfs01

[root@m01 ~]# ssh 172.16.1.31 'sh' <1.txt 

nfs01

[root@m01 ~]# ssh 172.16.1.31 'sh 1.txt'  

sh: 1.txt: No such file or directory


這樣就不用把腳本拷貝到遠端去執行了

參考:

http://linux.icydog.net/ssh/piping.php

http://www.ivarch.com/programs/quickref/pv.shtml

http://www.mysqlperformanceblog.com/2009/05/20/hint-throttling-xtrabackup/

 

2.3 故障1/var/empty/sshd

Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable.                                                             [FAILED]

 

解決方案:

這個是權限的問題

可采取以下兩步解決

chown   -R   root.root    /var/empty/sshd

chmod 744 /var/empty/sshd

service sshd restart

就可以解決上述的問題

[c:\~]$ ssh 10.0.0.61                連接外網IP

Connecting to 10.0.0.61:22...

Connection established.

To escape to local shell, press 'Ctrl+Alt+]'.

 

Last login: Sun Oct 22 18:06:38 2017 from 10.0.0.1

wo shi ma chang wei

[root@m01 ~]#

2.4 故障2

 

 

/home/admin 權限應該是 700,屬性admin admin

/home/admin/.ssh 權限應該是 755 屬性 admin admin

/home/admin/.ssh/authorized_keys 權限應該是644 屬性admin admin

 

故障3

 

通過ssh連接不上了 

[root@localhost webapps]# ssh root@192.168.0.10 ssh_exchange_identification: Connection closed by remote host

 

聯系機房使用KVM也輸入用戶名和密碼后又自動退出跳轉到登陸界面,通過KVM切換用戶模式的時候提示INIT: cannot fork, retry..初步懷疑系統資源肯定消耗干了,要么等待系統釋放部分資源以后再進入,但是現在已經有部分應用不能使用,只能重啟系統. 重啟系統以后查看/var/log/messages日志中發現 Jan 24 11:32:55 localhost automount[2831]: expire_proc: expire thread create for /misc failed 而且故障時間也基本吻合更進一步說明是線程數創建過多,並且有死鎖導致

 

注釋 通過cat /proc/sys/kernel/threads-max 能查看到系統的最大線程數

 

2.5 普通用戶連接其他用戶權限:

.

實例2-1 普通用戶連接其他用戶:

[p@web02 ~]$ ssh 172.16.1.31

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

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

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

Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts.

p@172.16.1.31's password:

Permission denied, please try again.

 

2.6 問題一   No route to host 

ssh登錄的時候鏈接端口失敗  

提示(1):  # ssh 172.16.81.221  

ssh: connect to host 172.16.81.221 port 22: No route to host  

這由於server端沒有開機或是網絡不通(這個原因很多,最簡單的是網線沒有插。還有就是可能會是網卡down了等)  

提示(2):  

# ssh work@172.16.81.221  

ssh: connect to host 172.16.81.221 port 22: Connection refused  

這是由於對方serverssh服務沒有開。這個server端開啟服務即可。  

 

2.7 問題二 PermitRootLogin no  

sshserver上的時候密碼是對的但是報如下息:

[root@nfs01 ~]# ssh 172.16.1.8

root@172.16.1.8's password:

Permission denied, please try again.

root@172.16.1.8's password:

Last login: Sun Oct 22 06:55:06 2017 from 10.0.0.1

wo shi ma chang wei

[root@web01 ~]#

這個是由於如果不輸入用戶名的時候默認的是root用戶,但是安全期間ssh服務默認沒有開root用戶的ssh權限  

解決方法:  

要修改rootssh權限,即修改 /etc/ssh/sshd_config文件中  PermitRootLogin no 改為 PermitRootLogin yes  

實例2-1 

[root@m01 ~]# ssh 172.16.1.8

root@172.16.1.8's password:

Permission denied, please try again.

2.8 問題三  

登錄是出現如下提示:  ssh root@172.16.81.221  

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  

@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @  

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!  Someone could be eavesdropping on you right now (man-in-the-middle attack)!  It is also possible that the RSA host key has just been changed.  The fingerprint for the RSA key sent by the remote host is  76:fb:b3:70:14:48:19:d6:29:f9:ba:42:46:be:fb:77.  Please contact your system administrator.  

Add correct host key in /home/fante/.ssh/known_hosts to get rid of this  message.  

Offending key in /home/fante/.ssh/known_hosts:68  

RSA host key for 172.16.81.221 has changed and you have requested strict checking.  Host key verification failed.  

server端密碼或是其他發生改變的時候。解決方法一般就需要刪除~/.ssh/known_hosts的對應行,然后再登錄即可。

 

第3章 ssh

3.1 SSH服務協議說明:

01. SSHSecure Shell Protocol的簡寫

02. SSH先對聯機數據包通過加密技術進行加密處理,加密后在進行數據傳輸。確保了傳遞的數據安全

 

3.2 SSH服務主要提供兩個服務功能:

01. 一是提供類似telnet遠程聯機服務器的服務,即上面提到的SSH服務;

02. 另一個是類似FTP服務的sftp-server,借助SSH協議來傳輸數據的,提供更安全的SFTP服務(vsftp,proftp)

 

3.3 SSH服務軟件詳細說明

openssh-clients軟件大禮包包含內容

/etc/ssh/ssh_config    ---ssh服務客戶端配置文件

/usr/bin/.ssh.hmac

/usr/bin/scp           ---ssh服務遠程傳輸復制命令

/usr/bin/sftp          ---ssh服務遠程傳輸文件服務

/usr/bin/slogin        ---ssh遠程登錄命令

/usr/bin/ssh           ---ssh遠程登錄命令

/usr/bin/ssh-add

/usr/bin/ssh-agent

/usr/bin/ssh-copy-id   ---遠程分發公鑰命令

/usr/bin/ssh-keyscan

 

openssh-server軟件大禮包包含內容

/etc/rc.d/init.d/sshd  ---ssh服務啟動腳本文件

/etc/ssh/sshd_config   ---ssh服務配置文件

/etc/sysconfig/sshd    ---ssh服務創建秘鑰有關  

/usr/sbin/.sshd.hmac   ---ssh加密算法有關文件

/usr/sbin/sshd         ---ssh服務進程啟動命令

 

 

ssh服務認證類型:

01. 基於口令認證方式

02. 基於秘鑰認證方式

 

3.4 ssh服務配置文件說明:

01. 配置文件中所有注釋信息,表示默認參數配置

02. 配置文件中#空格 后面內容表示說明信息

              #參數 表示配置參數信息

03. 配置文件參數信息修改后,一旦變為注釋,即還原為默認配置

              

3.4.1 重點配置參數說明:

#Port 22                --- 表示修改ssh服務默認端口號

#AddressFamily any      --- 指定監聽ipv4地址,或是ipv6地址,或者所有都監聽

#ListenAddress 0.0.0.0  --- 監聽地址只能監聽本地網卡上配置的地址,監聽的網卡可以對請求做出相應

 

 

 

 

3.4.2 配置步驟

利用telnet協議實現遠程登錄方法:

第一個里程:安裝telnet服務

yum install telnet telnet-server -y

 

第二個里程:啟動telnet服務,利用xinetd服務啟動

vim /etc/xinetd.d/telnet

disabled = no

/etc/init.d/xinetd start

 

第三個里程:遠程登錄測試

telnet 10.0.0.31

login

password

至此,登錄成功

 

 

3.4.3 基於秘鑰登錄配置過程:

#環境准備:

01. 管理服務器 m01     10.0.0.61

02. 備份服務器 backup  10.0.0.41

03. 存儲服務器 nfs01   10.0.0.31

 

backup  (創建秘鑰對)

nfs01   (被管理服務器,接收公鑰信息)

 

第一個里程碑:創建秘鑰對

# 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:

1a:80:62:7e:50:5b:3e:30:16:1f:f1:fa:38:94:0f:c2 root@backup

The key's randomart image is:

+--[ RSA 2048]----+

|   *.+.          |

|  o.B o          |

|.o...+ .         |

|o.o  .+          |

| . E =. S        |

|  . o =o         |

|     o.o         |

|      .          |

|                 |

+-----------------+

[root@backup ssh]# ll ~/.ssh/

total 12

-rw------- 1 root root 1675 Oct 18 11:07 id_rsa

-rw-r--r-- 1 root root  393 Oct 18 11:07 id_rsa.pub

-rw-r--r-- 1 root root  784 Oct 11 16:27 known_hosts

 

 

第二個里程:將公鑰分發給存儲服務器

ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.31

root@172.16.1.31's password:

Now try logging into the machine, with "ssh '172.16.1.31'", and check in:

 

  .ssh/authorized_keys

 

to make sure we haven't added extra keys that you weren't expecting.

 

第三個里程碑:進行登錄測試

[root@backup ssh]# ssh 172.16.1.31

Last login: Wed Oct 18 10:16:12 2017 from 10.0.0.41

[root@nfs01 ~]# exit

 

 

3.5 ssh基本語法使用

ssh p52113 oldboy@10.0.0.150 [命令]

#SSH連接遠程主機命令的基本語法;

#-p(小寫)接端口,默認22端口時可以省略-p22;

#→“@”前面為用戶名,如果用當前用戶連接,可以不指定用戶。

#→“@”后面為要連接的服務器的IP. 更多用法,請man ssh;

ssh p52113 oldboy@10.0.0.150 [命令]   --- 遠程執行命令

ssh p52113 oldboy@10.0.0.150          --- 遠程登錄

 

3.6 scp的基本語法使用

scp的基本語法使用:scp - secure copy (remote file copy program)

每次都是全量拷貝,增量拷貝rsync

推:PUSH

scp -P22 -rp /tmp/oldboy oldboy@10.0.0.143:/tmp

<- -P(大寫,注意和ssh命令的不同)接端口,默認22端口時可以省略-P22;

<- -r遞歸,表示拷貝目錄;

<- -p表示在拷貝前后保持文件或目錄屬性;

<- -l limit 限制速度。

<- /tmp/oldboy為本地的目錄。“@”前為用戶名,“@”后為要連接的服務器的IP

IP后的:/tmp目錄,為遠端的目標目錄。

說明:以上命令作用是把本地/tmp/oldboy拷貝到遠端服務器10.0.0.143/tmp目錄;

 

拉:PULL

scp -P22 -rp root@10.0.0.7:/tmp/oldboy /opt/

說明:還可以把遠端目錄抓到本地

結論:scp為遠程拷貝文件或目錄的命令,更多用法,請man scp;拷貝權限為連接的用戶對應的權限。

 

3.6.1 登陸FTP

登陸FTP的方法就是sftp oldboy@10.0.0.142 如果ssh端口為52113則登陸命令為如下:

[root@A ~]# sftp -oPort=52113 oldboy@10.0.0.142 <- 特殊端口的sftp連接命令

Connecting to 10.0.0.142...

oldboy@10.0.0.142's password:

sftp> cd /home/oldboy <- 還可以像命令行那樣切到目標目錄

sftp> put /etc/hosts <- /etc/hosts從客戶端本地傳到sftp服務器當前連接目錄,

/home/oldboy  

 

ls    

cd

pwd

說明:操作遠程服務器

 

lls

lcd

lpwd

說明:操作本地服務器

 

get   --- 表示從遠程服務器下載數據(單個文件)

mget  --- 表示下載多個文件

put   --- 表示從本地服務器上傳數據(單個文件)

mput  --- 表示上傳多個文件

 


免責聲明!

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



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