六、SSH遠程管理服務實戰

1.SSH基本概述

SSH是一個安全協議,在進行數據傳輸時,會對數據包進行加密處理,加密後在進行數據傳輸。確保了數據傳輸安全。那SSH服務主要功能有哪些呢? 1.提供遠程鏈接服務器的服務、 2.對傳輸的數據進行加密 那麼除了SSH協議能提供遠程鏈接服務,Telnet也能提供遠程鏈接服務, 那麼分別的區別是什麼呢? ssh服務會對傳輸數據進行加密, 監聽在本地22/tcp端口, ssh服務默認支持root用戶登陸 telnet服務不對數據進行加密, 監聽在本地23/tcp端口, Telnet默認不支持root用戶登陸git

服務鏈接方式 服務數據傳輸 服務監聽端口 服務登錄用戶
ssh 加密 22/tcp 默認支持root用戶登錄
telnet 明文 23/tcp 不支持root用戶登錄

1.1案例: 使用wireshark驗證telnet明文傳輸與ssh加密傳輸

1.安裝telnet服務並運行github

[root@m01 ~]# yum install telnet-server -y
[root@m01 ~]# systemctl start telnet.socket

2.使用wireshark檢測vmnet8網卡上telnet的流量 3.telnet是沒法使用root用戶登陸Linux系統,須要建立普通用戶web

[root@m01 ~]# useradd bgx
[root@m01 ~]# echo "123456"| passwd --stdin bgx

4.使用普通用戶進行telnet登陸 5.搜索wireshark包含telnet相關的流量 chrome

6.使用wireshark分析ssh流量 shell

2.SSH相關命令

SSH有客戶端與服務端,咱們將這種模式稱爲C/S架構,ssh客戶端支持Windows、Linux、Mac等平臺。 在ssh客戶端中包含 ssh|slogin遠程登錄、scp遠程拷貝、sftp文件傳輸、ssh-copy-id祕鑰分發等應用程序。 1.ssh遠程登陸服務器命令示例bootstrap

ssh -p22 root@10.0.0.61

# -p指定鏈接遠程主機端口,默認22端口可省略
# root@remotehost
# "@"前面爲用戶名,若是用當前用戶鏈接,能夠不指定用戶
# "@"後面爲要鏈接的服務器的IP

2.scp複製數據至遠程主機命令(全量複製)vim

# -P 指定端口,默認22端口可不寫
# -r 表示遞歸拷貝目錄
# -p 表示在拷貝文件先後保持文件或目錄屬性不變
# -l (limit)限制傳輸使用帶寬(默認kb)

#推:將本地/tmp/oldboy推送至遠端服務器10.0.0.61的/tmp目錄,使用對端的root用戶
[root@m01 ~]# scp -P22 -rp /tmp/oldboy oldboy@10.0.0.61:/tmp

#拉:將遠程10.0.0.61服務器/tmp/oldboy文件拉取到本地/opt/目錄下
[root@m01 ~]# scp -P22 -rp root@10.0.0.61:/tmp/oldboy /opt/

#限速
[root@m01 ~]# scp /opt/1.txt root@172.16.1.31:/tmp
root@172.16.1.31 password: 
test                        100%  656MB  '83.9MB/s'   00:07 
#限速爲8096kb,換算爲MB,要除以 8096/8=1024KB=1MB
[root@m01 ~]# scp -rp -l 8096  /opt/1.txt root@172.16.1.31:/tmp
root@172.16.1.31s password: 
test                        7%   48MB   '1.0MB/s'   09:45

結論: 1.scp經過ssh協議加密方式進行文件或目錄拷貝。 2.scp鏈接時的用戶做爲拷貝文件或目錄的權限。 3.scp支持數據推送和拉取,每次都是全量拷貝,效率較低。windows

3.Sftp遠程數據傳輸命令後端

#默承認以經過sftp命令鏈接sftp服務
sftp root@10.0.0.61
sftp -oPort=52113 root@10.0.0.61  #sftp的特殊端口鏈接

# sftp使用get下載文件至於本地服務器
sftp> get conf.txt /tmp/

# sftp使用put上傳本地服務器文件至遠程服務器
sftp> put /root/t1.txt /root/

3.SSH驗證方式

3.1.基於帳戶密碼遠程登陸

知道服務器的IP端口,帳號密碼,便可經過ssh客戶端命令登錄遠程主機。centos

➜  ~ ssh -p22 root@10.0.0.61
root@10.0.0.61 password:
[root@m01 ~]#

3.2.基於祕鑰遠程登陸

默認狀況下,經過ssh客戶端命令登錄遠程服務器,須要提供遠程系統上的賬號與密碼,但爲了下降密碼泄露的機率和提升登錄的方便性,建議使用密鑰驗證方式。

1.在服務器上生成非對稱密鑰,使用-t指定密鑰類型, 使用-C指定用戶郵箱

[root@m01 ~]# ssh-keygen -t rsa -C xuliangwei@qq.com
...
#默認一路回車便可
...

2.將A服務器上的公鑰推送至B服務器

#命令示例: ssh-copy-id [-i [identity_file]] [user@]machine
ssh-copy-id #命令
-i          #指定下發公鑰的路徑
[user@]     #以什麼用戶身份進行公鑰分發(root),若是不輸入,表示以當前系統用戶身份分發公鑰
machine     #下發公鑰至那臺服務器, 填寫遠程主機IP地址

#分發祕鑰,[將A服務器的公鑰寫入B服務器~/.ssh/authorized_keys文件中]
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41

3.A服務器鏈接B服務器是無需密碼的,若是能直接鏈接無需密碼則表示祕鑰已配置成功

#遠程登陸對端主機方式
[root@m01 ~]# ssh root@172.16.1.41
[root@nfs ~]#

#不登錄遠程主機bash,但可在對端主機執行命令
[root@m01 ~]# ssh root@172.16.1.41 "hostname -i"
172.16.1.41

4.SSH場景實踐

實踐場景,用戶經過Windows/MAC/Linux客戶端鏈接跳板機免密碼登陸,跳板機鏈接後端無外網的Linux主機實現免密登陸,架構圖以下。 實踐多用戶登錄一臺服務器無密碼 實踐單用戶登錄多臺服務器免密碼

4.1.windows客戶端使用Xshell生成祕鑰對,並下發公鑰至跳板機

  1. Xshell-->選擇工具->新建密鑰生成工具
  2. 生成公鑰對,選擇下一步
  3. 填寫祕鑰名稱。祕鑰增長密碼則不建議配置
  4. Windows會提示密碼,繼續便可
  5. 生成祕鑰後,點擊Xshell->工具->用戶祕鑰管理者->選擇對應祕鑰的屬性
  6. 選擇對應祕鑰的公鑰,將其複製
  7. 將從WIndows下複製好的公鑰粘貼至跳板機~/.ssh/authorized_keys中,而後測試
[root@m01 ~]# cd ; umask 077; mkdir -p .ssh ;cd .ssh
[root@m01 .ssh]# vim authorized_keys  #添加windows公鑰

4.2.跳板機下發公鑰至後端主機

1) 在跳板機上生成祕鑰對
[root@m01 ~]# ssh-keygen -t rsa -C liyang1401190055@163.com
2) 拷貝跳板機上的密鑰至後端主機,若是SSH不是使用默認22端口, 使用-p指定對應端口
[root@m01 ~]# ssh-copy-id  -i /root/.ssh/id_rsa.pub "-p22 root@172.16.1.31"
[root@m01 ~]# ssh-copy-id  -i /root/.ssh/id_rsa.pub "-p22 root@172.16.1.41"
3) 在m01管理機上測試是否成功登錄兩臺服務器
[root@m01 ~]# ssh root@172.16.1.41
[root@nfs01 ~]# exit

[root@m01 ~]# ssh root@172.16.1.31
[root@backup ~]# exit

4.3.經過跳板機能實現scp拷貝文件免密碼

[root@manager ~]# scp manager-web root@172.16.1.31:/tmp
manager-web                 100%    0     0.0KB/s   00:00    
[root@manager ~]# scp manager-web root@172.16.1.41:/tmp
manager-web                 100%    0     0.0KB/s   00:00

4.4.SSH安全優化

SSH做爲遠程鏈接服務,一般咱們須要考慮到該服務的安全,因此須要對該服務進行安全方面的配置。 1.更改遠程鏈接登錄的端口 2.禁止ROOT管理員直接登陸 3.密碼認證方式改成密鑰認證 4.重要服務不使用公網IP地址 5.使用防火牆限制來源IP地址

SSH服務登陸防禦需進行以下配置調整,先對以下參數進行了解

Port 6666                       # 變動SSH服務遠程鏈接端口
PermitRootLogin         no      # 禁止root用戶直接遠程登陸
PasswordAuthentication  no      # 禁止使用密碼直接遠程登陸
UseDNS                  no      # 禁止ssh進行dns反向解析,影響ssh鏈接效率參數
GSSAPIAuthentication    no      # 禁止GSS認證,減小鏈接時產生的延遲

將以下具體配置添加至/etc/ssh/sshd_config文件中,參數需根據實際狀況進行調整

###SSH###
#Port 6666
#PasswordAuthentication no
#PermitRootLogin no
GSSAPIAuthentication no
UseDNS no
###END###

4.5.SSH安全防禦

fail2ban能夠監控系統日誌,而且根據必定規則匹配異常IP後使用Firewalld將其屏蔽,尤爲是針對一些爆破/掃描等很是有效。

1.開啓Firewalld防火牆
	[root@bgx ~]# systemctl start firewalld
	[root@bgx ~]# systemctl enable firewalld
	[root@bgx ~]# firewall-cmd --state
	running
2.修改firewalld規則,啓用Firewalld後會禁止一些服務的傳輸,但默認會放行經常使用的22端口 
若是想添加更多,如下是放行SSH端口(22)示例,供參考:

	#放行SSHD服務端口
	[root@bgx ~]# firewall-cmd --permanent --add-service=ssh --add-service=http 
	#重載配置
	[root@bgx ~]# firewall-cmd --reload
	#查看已放行端口
	[root@bgx ~]# firewall-cmd  --list-service
3.安裝fail2ban,須要有epel
	[root@bgx ~]# yum install fail2ban fail2ban-firewalld mailx -y

4.配置fail2ban規則.local會覆蓋.conf文件
[root@bgx fail2ban]# cat /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 86400
findtime = 600
maxretry = 5
banaction = firewallcmd-ipset
action = %(action_mwl)s

[sshd]
enabled = true
filter  = sshd
port    = 22
action = %(action_mwl)s
logpath = /var/log/secure

5.啓動服務,並檢查狀態
[root@bgx ~]# systemctl start fail2ban.service
[root@bgx ~]# fail2ban-client status sshd

6.清除被封掉的IP地址
[root@bgx ~]# fail2ban-client set sshd unbanip 10.0.0.1

4.6.若是有ssh祕鑰沒法正常鏈接的狀況,能夠嘗試使用以下的調試模式.開啓ssh debug調試模式。

(1)31服務器上開啓臨時ssh服務 /usr/sbin/sshd -p 10001 -d
(2)61服務器上訪問 ssh -vvv -p 10001 root@10.0.0.31
(3)31服務器上查看debug日誌,發現身份驗證被拒絕,目錄的全部權錯誤。
	Authentication refused: bad ownership or modes for directory /root
 解決方法:
 出現這個錯誤,是由於/root目錄的權限或者屬主屬組不對,修改權限或屬主屬組

5.ssh密碼+ Google Authenticator 實現雙向認證

一般咱們直接經過ssh輸入密碼鏈接服務器,但這樣很容易出現暴力破解狀況,因此咱們能夠結合google的動態認證+ssh密碼,這樣可以大大的提高登錄的安全。
簡單來講,就是當用戶經過ssh登錄系統時,先輸入google的隨機驗證碼,而後在輸入服務器的ssh密碼

5.1.安裝依賴包,環境屬於centos7,centos6請自行查閱網上資料

[root@bgx ~]# yum -y install wget gcc make pam-devel libpng-devel pam-devel

5.2.安裝Google Authenticator PAM插件安裝

[root@bgx ~]# wget https://github.com/google/google-authenticator-libpam/archive/1.04.tar.gz
[root@bgx ~]# tar xf 1.04.tar.gz
[root@bgx ~]# cd google-authenticator-libpam-1.04/
[root@bgx google-authenticator-libpam-1.04]# ./bootstrap.sh
[root@bgx google-authenticator-libpam-1.04]# ./configure
[root@bgx google-authenticator-libpam-1.04]# make && make install
[root@bgx google-authenticator-libpam-1.04]# cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/

5.3.初始配置 Google Authenticator

[root@bgx google-authenticator-libpam-1.04]# google-authenticator

是否基於時間的認證,爲了防止不一樣跨時區的問題,這裏選擇n

Do you want authentication tokens to be time-based (y/n) n

而後會跳出一個google的二維碼
紅色框框是: 生成的密鑰
綠色框框是: 生成的5個一次性緊急驗證碼,用於緊急狀況下,使用過一次後該驗證碼即失效了。

image

是否更新用戶的 Google Authenticator 配置文件,選擇 y 才能使上面操做對當前 root 用戶生效,其實就是在對應用戶的 Home 目錄下生成了一個 .google_authenticator 文件,若是你想停用這個用戶的 Google Authenticator 驗證,只須要刪除這個用戶 Home 目錄下的 .google_authenticator 文件就能夠了。

Do you want me to update your "/root/.google_authenticator" file? (y/n) y

每次生成的認證碼是否同時只容許一我的使用?這裏選擇 y。

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

每次生成的令牌30s生成一次,最高容許存在偏差4分鐘。

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y

5.4.SSH調用及客戶端配置,添加pam認證,在第一行添加

[root@bgx ~]# vim  /etc/pam.d/sshd  #添加以下行
auth       required     pam_google_authenticator.so

5.5.修改sshd配置,關聯google認證

[root@bgx ~]# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes     #修改成yes
[root@bgx ~]# systemctl restart sshd    #重啓sshd服務

5.6.客戶端經過ssh鏈接服務器測試

image

須要輸入動態密碼,動態密碼經過手機獲取以下圖所示

image

5.7.查看服務端的安全日誌文件,能夠看到是先進程google動態密碼認證

image

5.8.注意事項:

1.用password + google authenticator,若是使用公鑰登陸的話,會跳過google authenticator驗證直接登陸服務器的。
2.若是是內網測試使用,建議安裝google authenticator 瀏覽器插件實踐。若是是公網服務器建議安裝手機版的Authenticator

5.9.安裝Google authenticator

Andorid版: 「自行百度」 iOS版: 下載 「Authenticator」 chrome瀏覽器有google authenticator的插件

相關文章
相關標籤/搜索