1. SSH是安全的加密協定,用于連接配接Linux伺服器
2. SSH預設端口是22,安全協定版本為SSH2,SSH1有漏洞
3. SSH服務端隻要包含兩個服務功能SSH遠端連接配接和SFTP服務
4. Linux SSH用戶端包含ssh遠端連接配接指令以及遠端拷貝scp指令等
SSH2同時支援RSA和DSA密鑰
SSH服務認證類型:基于密碼的安全驗證和基于密鑰的安全驗證
檢視是否安裝服務
[root@m01 ~]# rpm -qa openssh openssl
openssh-5.3p1-94.el6.x86_64 (遠端連接配接)
openssl-1.0.1e-15.el6.x86_64 (加密服務)
ssh服務優化:
[root@m01 ~]# cp /etc/ssh/sshd_config{,.ori} #優化之前先備份
[root@m01 ~]# ll /etc/ssh/sshd* #檢視備份
[root@m01 ~]# vim /etc/ssh/sshd_config #編輯配置檔案
Port 52113 #修改端口,預設22端口
ListenAddress 172.16.1.61:52133 #隻允許對應的ip登入
PermitRootLogin no #禁止使用root使用者遠端登入
PermitEmptyPasswords no #禁止空密碼登入
GSSAPIAuthentication no #解決Linux之間使用SSH遠端連接配接慢的問題
#GSSAPIAuthentication yes
UseDNS no #不使用DNS
[root@m01 ssh]# /etc/init.d/sshd reload #平滑重新開機
快速修改和備份配置檔案對比:
[root@m01 ssh]# sed -ir '13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nGSSAPIAuthentication no\nUseDNS no\n#ListenAddress 172.16.1.61:52133' /etc/ssh/sshd_config
[root@m01 ssh]# diff sshd_config.ori sshd_config
12a13,18
> Port 52113
> PermitRootLogin no
> PermitEmptyPasswords no
> GSSAPIAuthentication no
> UseDNS no
> #ListenAddress 172.16.1.61:52133
[root@m01 ssh]# /etc/init.d/sshd reload #平滑重新開機
用戶端測試:
[root@nfs01 ~]# ssh 10.0.0.61
ssh: connect to host 10.0.0.61 port 22: Connection refused #22端口拒絕通路
[root@nfs01 ~]# ssh -p52113 10.0.0.61
The authenticity of host '[10.0.0.61]:52113 ([10.0.0.61]:52113)' can't be established.
RSA key fingerprint is ac:ec:68:11:b5:07:aa:3f:3e:10:f8:17:f1:98:aa:0d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.61]:52113' (RSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again. (root超級使用者拒絕通路)
注:因為ssh服務已經設定禁止root超級使用者遠端登入,所有設定一個普通使用者登入
[root@m01 ssh]# useradd ceshi
[root@m01 ssh]# echo 123456|passwd --stdin ceshi
如何防止SSH登入入侵小結:
1.用密鑰登入,不使用密碼登入
2.牤牛陣法:解決SSH安全問題
1.防火牆封閉SSH,指定源IP限制(區域網路、信任公網)
2.開啟SSH隻監聽本地内網IP
3.盡量不給伺服器外網IP
ssh用戶端指令:
ssh -p22 [email protected] [指令] -p:指定端口
[root@nfs01 ~]# ssh -p52113 [email protected]
[root@nfs01 ~]# ssh -p52113 [email protected] /sbin/ifconfig eth0
[root@nfs01 ~]# ssh -p22 [email protected]
ssh: connect to host 10.0.0.61 port 22: Connection refused #通路被拒絕
報錯字元串對應可能的問題: no route to host #可能是防火牆影響
1. 可能是防火牆阻擋
2. 可能是連接配接的對方端口服務沒開或端口改變
scp遠端拷貝指令:
[root@nfs01 tmp]# scp -P52113 -rp /etc [email protected]:/tmp #将本地檔案推向遠端
[root@nfs01 tmp]# scp -P52113 -rp [email protected]:/tmp/etc /data #從遠端拉到本地
scp知識小結:
1. scp是加密的遠端拷貝,而cp僅為本地拷貝
2. 可以把資料從一台機器推送到另一台機器,也可以從其他伺服器把資料拉回到本地執行指令的伺服器
3. 每次都是全量完整拷貝,是以,效率不高,适合第一次拷貝用,如果需要增量拷貝,用rsync。
sftp服務及指令
[root@nfs01 ~]# sftp -oPort=52113 [email protected]
Connecting to 10.0.0.61...
[email protected]'s password:
sftp> pwd #可以使用指令行指令
Remote working directory: /home/ceshi
sftp> put /etc/hosts #把/etc/hosts從用戶端本地傳到sftp伺服器的家目錄
Uploading /etc/hosts to /home/ceshi/hosts
/etc/hosts 100% 325 0.3KB/s 00:00
sftp> put /etc/hosts /tmp #把/etc/hosts從用戶端本地傳到sftp伺服器指定的目錄/tmp
Uploading /etc/hosts to /tmp/hosts
/etc/hosts
sftp> get hosts
Fetching /home/ceshi/hosts to hosts #把hosts下載下傳到本地連接配接前的目錄
/home/ceshi/hosts 100% 325 0.3KB/s
sftp指令小結:
1. Linux下連接配接指令 sftp -oPort=52113 [email protected]
2. 上傳put加用戶端本地路徑 put /etc/hosts
3. 下載下傳get服務端的内容 get hosts (Linux下載下傳到本地連接配接前的目錄),也可指定下載下傳路徑 get hosts /tmp
4. 連接配接到的遠端家目錄為預設目錄,也可以切換到其他有權限的目錄
章節小結:
1. ssh為加密的遠端連接配接協定,相關軟體有openssh和openssl
2. 預設端口22
3. 協定版本1.X和2.X,2.X更安全。
4. 服務端ssh遠端連接配接服務,sftp服務。sshd守護程序,開機要自啟動
5. ssh用戶端包含ssh,scp,sftp指令