常用軟體安裝及使用目錄
第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 [email protected]
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.
[email protected]'s password:
[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.
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.
[email protected]'s password:
[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
說明:p 參數指定端口号,通常在路由裡做端口映射時,我們不會把22端口直接映射出去,而是轉換成其他端口号,這時就需要使用-p端口号指令格式。
[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
[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了。
[root@m01 ~]# ssh -t 172.16.1.31 ssh -p52113 [email protected]
The authenticity of host '[172.16.1.41]:52113 ([172.16.1.41]:52113)' can't be established.
Warning: Permanently added '[172.16.1.41]:52113' (RSA) to the list of known hosts.
[email protected]'s password:
[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"
1.4 通過SSH運作遠端shell指令:
ssh -l name remoteserver ‘command’
說明:連接配接到遠端主機,并執行遠端主機的command指令。例如:檢視遠端主機的記憶體使用情況。
$ ssh -l root 192.168.1.100 svmon -G
[root@m01 ~]# ssh -l oldboy 172.16.1.31 free -m
[email protected]'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
[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 ]
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使用者登入。
[root@m01 ~]# ssh [email protected]
Last login: Sat Oct 21 23:25:45 2017 from 172.16.1.31
[oldboy@backup ~]$ logout
Connection to 172.16.1.41 closed.
[root@m01 ~]# ssh [email protected]
[email protected]'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檔案中的提示資訊。
# no default banner path
#Banner none
banner /machangwei/qianyan
[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
1.9 進行端口映射:
假如公司内網有台web伺服器,但是隻對内不對外,這樣,外網就無法通路,可以用ssh進行端口映射來實作外網通路内網的web伺服器。假如web伺服器名為webserver,webserver可以用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: 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"
拷貝檔案時,如果檔案很大,又不想影響網絡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
#!/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
[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
[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 [email protected] 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.
Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts.
[email protected]'s password:
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 [email protected]
ssh: connect to host 172.16.81.221 port 22: Connection refused
這是由于對方server的ssh服務沒有開。這個server端開啟服務即可。
2.7 問題二 PermitRootLogin no
ssh到server上的時候密碼是對的但是報如下息:
[root@nfs01 ~]# ssh 172.16.1.8
[email protected]'s password:
Last login: Sun Oct 22 06:55:06 2017 from 10.0.0.1
[root@web01 ~]#
這個是由于如果不輸入使用者名的時候預設的是root使用者,但是安全期間ssh服務預設沒有開root使用者的ssh權限
解決方法:
要修改root的ssh權限,即修改 /etc/ssh/sshd_config檔案中 PermitRootLogin no 改為 PermitRootLogin yes
執行個體2-1
[root@m01 ~]# ssh 172.16.1.8
2.8 問題三
登入是出現如下提示: ssh [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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. SSH是Secure 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
[email protected]'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 [email protected] [指令]
#→SSH連接配接遠端主機指令的基本文法;
#→-p(小寫)接端口,預設22端口時可以省略-p22;
#→“@”前面為使用者名,如果用目前使用者連接配接,可以不指定使用者。
#→“@”後面為要連接配接的伺服器的IP. 更多用法,請man ssh;
ssh –p52113 [email protected] [指令] --- 遠端執行指令
ssh –p52113 [email protected] --- 遠端登入
3.6 scp的基本文法使用
scp的基本文法使用:scp - secure copy (remote file copy program)
每次都是全量拷貝,增量拷貝rsync
推:PUSH
scp -P22 -rp /tmp/oldboy [email protected]:/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 [email protected]:/tmp/oldboy /opt/
說明:還可以把遠端目錄抓到本地
結論:scp為遠端拷貝檔案或目錄的指令,更多用法,請man scp;拷貝權限為連接配接的使用者對應的權限。
3.6.1 登陸FTP
登陸FTP的方法就是sftp [email protected] 如果ssh端口為52113則登陸指令為如下:
[root@A ~]# sftp -oPort=52113 [email protected] <- 特殊端口的sftp連接配接指令
Connecting to 10.0.0.142...
[email protected]'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 --- 表示上傳多個檔案