開源服務專題之------ssh防止暴力破解及fail2ban的使用方法

   15年出現的JAVA反序列化漏洞,另外一個是redis配置不當致使機器入侵。只要redis是用root啓動的而且未受權的話,就能夠經過set方式直接寫入一個authorized_keys到系統的/root/.ssh/目錄下實現免密碼登錄他人的Linux服務器。從而達到入侵成功的效果。fail2ban是一款很棒的開源服務軟件,能夠監視你的系統日誌,而後匹配日誌的錯誤信息(正則式匹配)執行相應的屏蔽動做(通常狀況下是防火牆),並且能夠發送e-mail通知系統管理員,很好、很實用、很強大!簡單來講其功能就是防止暴力破解。工做的原理是經過分析必定時間內的相關服務日誌,將知足動做的相關IP利用iptables加入到dorp列表必定時間。 本文將從如下四個方面是說明ssh防止暴力破解的經常使用方法以及如何用fail2ban來防止暴力破解。python

 

1、實戰: sshd服務防止暴力破解git

2、fail2ban的安裝。github

3、fail2ban如何防止暴力破解web

4、ssh服務器免祕鑰登陸redis

 說明:本文的服務器和客戶端說明shell

服務端:xiaolyu76        IP:192.168.31.76vim

 

客戶端:xiaolyu77         IP:192.168.31.77api

 

1、實戰: sshd服務防止暴力破解安全

一、密碼足夠複雜,不使用弱口令bash

密碼的長度要大於8位最好大於14位。密碼的複雜度是密碼要儘量有數字、大小寫字母和特殊符號混合組成,
拓展:弱口令
運維安全最核心的問題就是弱口令
qwe@123 1qaz2wsx 6yhnNHY^ 這些都是弱口令
爲何是弱口令? 由於這些都是英文鍵盤常見的組合。
強口令: 8位數以上的無規則大小寫和數字組合
以前發生的漏洞: 15年出現的JAVA反序列化漏洞,另外一個是redis配置不當致使機器入侵。
只要redis是用root啓動的而且未受權的話,就能夠經過set方式直接寫入一個authorized_keys到系統的/root/.ssh/目錄下實現免密碼登錄他人的Linux服務器。從而達到入侵成功的效果。

二、修改默認端口號

1)安裝nmap 掃描工具。nmap  

[root@xiaolyu77 ~]# rpm -qa | grep nmap
[root@xiaolyu77 ~]# yum install -y nmap

 

[root@xiaolyu77 ~]# nmap xiaolyu77 #掃描本身的ip地址 Starting Nmap 5.51 ( http://nmap.org ) at 2016-09-19 08:56 CST
Nmap scan report for xiaolyu77 (192.168.31.77) Host is up (0.0000080s latency). Not shown: 999 closed ports PORT STATE SERVICE 22/tcp open  ssh #22端口,判斷出來是ssh服務。 Nmap done: 1 IP address (1 host up) scanned in 0.19 seconds [root@xiaolyu77 ~]# nmap xiaolyu76  #掃描xiaolyu76服務器的ip地址 Starting Nmap 5.51 ( http://nmap.org ) at 2016-09-19 08:57 CST
Nmap scan report for xiaolyu76 (192.168.31.76) Host is up (0.00028s latency). Not shown: 999 closed ports PORT STATE SERVICE 222/tcp open  rsh-spx #222端口判斷不出來是ssh服務,發生誤判。 MAC Address: 00:0C:29:58:6F:7B (VMware) Nmap done: 1 IP address (1 host up) scanned in 1.20 seconds

 

 

如今將服務器的端口修改爲8099:

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

三、不使用root用戶名登陸。

   是否能夠禁止root身份登陸? 不行,由於有些程序須要使用root身份登陸並運行。另外判斷一個用戶是否是超級管理員,看的是用戶的ID是否爲0。

 

[root@xiaolyu76 ~]# useradd mk #建立一個用戶mk,用它來冒充root用戶。

 [root@xiaolyu76 ~]# passwd mk   #給mk用戶添加密碼。
 Changing password for user mk.
 New password:
 BAD PASSWORD: it is too simplistic/systematic
 BAD PASSWORD: is too simple
 Retype new password:
 passwd: all authentication tokens updated successfully.

[root@xiaolyu76 ~]# vim /etc/passwd # passwd文件修改前 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
xiao:x:500:500::/home/xiao:/bin/bash
mk:x:501:501::/home/mk:/bin/bash
#passwd文件修改後:(只修改了文件中表藍色的部分,修改後變成了這種顏色)

root:x:0:0:root:/root:/sbin/nologin   #修改後的root用戶,不在具備登陸權限了。
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
xiao:x:500:500::/home/xiao:/bin/bash
mk:x:0:0::/home/mk:/bin/bash  #修改後的mk用戶,它的uid,gid都爲0。

也就是將/etc/passwd中的這兩行作了修改:

改:mk:x:500:500::/home/mk:/bin/bash

爲:mk:x:0:0::/home/mk:/bin/bash

 

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

爲:root:x:0:0:root:/sbin/nologin

 好了,修改完成,分別用root和mk這兩個用戶來登陸一下試試:

 

[root@xiaolyu77 ~]# hostname -i 192.168.31.77 [root@xiaolyu77 ~]# ssh -p 8099 xiaolyu76 root@xiaolyu76's password: 
Last login: Mon Sep 19 10:04:10 2016 from 192.168.31.160 Warning ! From now on, all of your operations have been recorded! This account is currently not available. Connection to xiaolyu76 closed. [root@xiaolyu77 ~]# ssh -p 8099 mk@xiaolyu76 mk@xiaolyu76's password: 
Last login: Mon Sep 19 10:06:00 2016 from xiaolyu77 Warning ! From now on, all of your operations have been recorded! [root@xiaolyu76 ~]# hostname -i 192.168.31.76 [root@xiaolyu76 ~]# whoami root [root@xiaolyu76 ~]# 

 

固然了,修改下面內容後,mk用戶也是登陸不上的。只判斷UID是否爲0,不查看用戶名的:

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

 

改:

#PermitRootLogin yes

爲:

PermitRootLogin no 

再來登陸一下試試。

[root@xiaolyu76 ~]# ssh -p 8099 mk@xiaolyu76

 

 通常狀況這個就能夠解決了暴力破解的問題了。

 2、fail2ban的安裝。

  對於上面的三種方法來防止暴力破解,是遠遠不夠的,有一種狀況,暴力破解問題比較嚴重, 須要把暴力破解的用的IP地址直接禁掉。

實戰背景:

最近公網網站一直被別人暴力破解sshd服務密碼。雖然沒有成功,但會致使系統負載很高,緣由是在暴力破解的時候,系統會不斷地認證用戶,從而增長了系統資源額外開銷,致使訪問公司網站速度很慢。

fail2ban能夠監視你的系統日誌,而後匹配日誌的錯誤信息(正則式匹配)執行相應的屏蔽動做(通常狀況下是防火牆),並且能夠發送e-mail通知系統管理員,很好、很實用、很強大!

簡單來講其功能就是防止暴力破解。工做的原理是經過分析必定時間內的相關服務日誌,將知足動做的相關IP利用iptables加入到dorp列表必定時間。 

 

1. fail2ban的安裝:

方法1、使用rpm升級,再經過yum進行安裝

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm   #Uvh升級該軟件包。

而後 yum install fail2ban 

 #由於yum安裝比較簡單,基本上就像傻瓜式安裝,這裏不演示了。

 

方法2、源碼安裝

首先去官網下載最新版fail2ban :  http://www.fail2ban.org

下面是打開官網的樣子:

 

 選擇下載fail2ban,進入以下界面:

 

 

我這裏爲了直接從CentOS中下載(免去了用xshell或其它工具再拖放到虛擬機 的麻煩),將下載地址複製出來了:

 

而後,我直接在虛擬機中下載安裝:

 

 

 這裏出現了一個問題:目前尚未搞定,爲何我在線wget下載的fail2ban文件名單單隻有0.8.14呢?

 

對於陌生的軟件,若是進行編譯安裝的話,一般都要先看它的readme文件,如何進行安裝。

解壓查看readme文件

[root@xiaolyu76 ~]# tar  -zxvf fail2ban-0.8.14.tar.gz

[root@xiaolyu76 fail2ban-0.8.14]# vim README.md #說明:下面的readme僅僅是我擇要摘取,原文太長了。 This README is a quick introduction to Fail2ban. More documentation, FAQ, HOWTOs are available in fail2ban(1) manpage and on the website http://www.fail2ban.org
 Installation: -------------

**It is possible that Fail2ban is already packaged for your distribution. In this case, you should use it instead.** Required: - [Python >= 2.4](http://www.python.org)
 Optional: - [pyinotify >= 0.8.3](https://github.com/seb-m/pyinotify)
  - Linux >= 2.6.13
- [gamin >= 0.0.21](http://www.gnome.org/~veillard/gamin)
 To install, just do: tar xvfj fail2ban-0.8.12.tar.bz2 cd fail2ban-0.8.12 python setup.py install This will install Fail2Ban into /usr/share/fail2ban. The executable scripts are placed into /usr/bin, and configuration under /etc/fail2ban.

 

須要安裝python開發環境,而且版本要大於2.4

查看當前系統中python的版本:

[root@xiaolyu76 ~]# python -V
Python 2.6.6
[root@xiaolyu76 ~]# cd fail2ban-0.8.14

 

[root@xiaolyu76 fail2ban-0.8.14]# ls setup.py
setup.py
[root@xiaolyu76 fail2ban-0.8.14]# python setup.py install

  

生成服務啓動腳本:

#下面是生成服務啓動腳本的程序: [root@xiaolyu76 fail2ban-0.8.14]# pwd 
/root/fail2ban-0.8.14 [root@xiaolyu76 fail2ban-0.8.14]# grep chkconfig ./* -R --color #查看服務啓動腳本是哪一個文件,同時着色顯示。 ./files/redhat-initd:# chkconfig: - 92 08 [root@xiaolyu76 fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban #將服務啓動腳本複製到存放開機啓動的服務的目錄下 [root@xiaolyu76 fail2ban-0.8.14]# chkconfig --add fail2ban #設置服務的開機啓動 [root@xiaolyu76 fail2ban-0.8.14]# service fail2ban start #service fail2ban start|stop|status|restart測試服務是否正常 Starting fail2ban: [ OK ] [root@xiaolyu76 fail2ban-0.8.14]# service fail2ban status fail2ban-server (pid 5874) is running... Status |- Number of jail: 0 `- Jail list: [root@xiaolyu76 fail2ban-0.8.14]# service fail2ban restart Stopping fail2ban: [ OK ] Starting fail2ban: [ OK ] [root@xiaolyu76 fail2ban-0.8.14]# service fail2ban stop Stopping fail2ban: [ OK ] [root@xiaolyu76 fail2ban-0.8.14]# 

 爲何要生成服務啓動腳本呢?生成服務啓動腳本,我能夠設置開機自啓動,可使用服務的相關命令service 服務名  start|stop|restart|status等等。很是方便。

 

拓展:

grep的用法

grep  ssh   /etc/passwd

2)echo -e  「this is a world \n nest line」 |grep world

3)打印除包含 math_pattern 行以外的全部的行

grep -v match_pattern file

4)統計文件或文本中包含匹配字符串的行數:

grep -c 「test」 filename

5)忽略大小寫

echo 「hello  world」 |grep -I 「HELLO」

 二、用tree /etc/fail2ban來查看fail2ban的文件目錄樹。

[root@xiaolyu76 ~]# tree /etc/fail2ban

 

 經過文件樹,能夠看到在安裝fail2ban的過程當中生成了不少文件,這裏給出相關主要文件說明:

/etc/fail2ban/action.d            #動做文件夾,內含默認文件。iptables以及mail等動做配置

/etc/fail2ban/fail2ban.conf    #定義了fai2ban日誌級別、日誌位置及sock文件位置

/etc/fail2ban/filter.d              #條件文件夾,內含默認文件。過濾日誌關鍵內容設置

/etc/fail2ban/jail.conf           #主要配置文件,模塊化。主要設置啓用ban動做的服務及動做閥值 ( jail   [dʒeɪl]  監獄)

/etc/rc.d/init.d/fail2ban        #啓動腳本文件

 

3、fail2ban如何防止暴力破解

實例:

設置條件:ssh遠程登陸5分鐘內3次密碼驗證失敗,禁止用戶IP訪問主機1小時,1小時該限制自動解除,用戶可從新登陸。

由於動做文件(action.d/iptables.conf)以及日誌匹配條件文件(filter.d/sshd.conf )安裝後是默認存在的。基本不用作任何修改。全部主要須要設置的就只有jail.conf文件。啓用sshd服務的日誌分析,指定動做閥值便可。實例文件/etc/fail2ban/jail.conf及說明以下:

fail2ban自身設置

 

一、fail2ban自己配置

[root@xiaolyu76 ~]# vim /etc/fail2ban/fail2ban.conf

默認fail2ban.conf裏面就三個參數,並且都有註釋。
loglevel = 3                                            #默認日誌的級別
logtarget = /var/log/fail2ban.log              #日誌的存放路徑
socket = /var/run/fail2ban/fail2ban.sock   #socket的位置

二、jail.conf

vim /etc/fail2ban/jail.conf

 

[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space separator. ignoreip = 127.0.0.1/8 # External command that will take an tagged arguments to ignore, e.g. <ip>, # and return true if the IP is to be ignored. False otherwise. # # ignorecommand = /path/to/command <ip> ignorecommand = # "bantime" is the number of seconds that a host is banned. bantime = 600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 3 # "backend" specifies the backend used to get files modification. # Available options are "pyinotify", "gamin", "polling" and "auto". # This option can be overridden in each jail as well. # # pyinotify: requires pyinotify (a file alteration monitor) to be installed. # If pyinotify is not installed, Fail2ban will use auto. # gamin: requires Gamin (a file alteration monitor) to be installed. # If Gamin is not installed, Fail2ban will use auto. # # pyinotify: requires pyinotify (a file alteration monitor) to be installed. # If pyinotify is not installed, Fail2ban will use auto. # gamin: requires Gamin (a file alteration monitor) to be installed. # If Gamin is not installed, Fail2ban will use auto. # polling: uses a polling algorithm which does not require external libraries. # auto: will try to use the following backends, in order: # pyinotify, gamin, polling. backend = auto # "usedns" specifies if jails should trust hostnames in logs, # warn when DNS lookups are performed, or ignore all hostnames in logs # # yes: if a hostname is encountered, a DNS lookup will be performed. # warn: if a hostname is encountered, a DNS lookup will be performed, # but it will be logged as a warning. # no: if a hostname is encountered, will not be used for banning, # but it will be logged as info. usedns = warn # This jail corresponds to the standard configuration in Fail2ban. # The mail-whois action send a notification e-mail with a whois request # in the body.

[DEFAULT]               #全局設置

ignoreip = 127.0.0.1/8    10.10.10.0/24   #忽略的IP列表,不受設置限制  若是有二組以上用空白作爲間隔

bantime  = 600             #屏蔽時間,單位:秒(設置IP被封鎖的時間)

findtime  = 600             #這個時間段內超過規定次數會被ban掉(設定多長時間內達到最大次數就解鎖。)

maxretry = 3                #最大嘗試次數

backend = auto            #日誌修改檢測機制(gamin、polling和auto這三種)

 

 

[ssh-iptables] #enabled = false enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"] #logpath = /var/log/sshd.log logpath = /var/log/secure #maxretry = 5 maxretry = 3

 

[ssh-iptables]                   #單個服務檢查設置,如設置bantime、findtime、maxretry和全局衝突,服務優先級大於全局設置。

enabled  = true             #是否激活此項(true/false)修改爲 true

filter       = sshd             #過濾規則filter的名字,對應filter.d目錄下的sshd.conf

action   = iptables[name=SSH, port=ssh, protocol=tcp]             #動做的相關參數,對應action.d/iptables.conf文件

sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"]  

#觸發報警的收件人

 dest=收件者電郵地址, sender=寄件者電郵地址

logpath  = /var/log/secure   #檢測的系統的登錄日誌文件。這裏要寫sshd服務日誌文件。 默認爲logpath  = /var/log/sshd.log

#5分鐘內3次密碼驗證失敗,禁止用戶IP訪問主機1小時。 配置以下

bantime  = 3600   #禁止用戶IP訪問主機1小時

findtime  = 300    #在5分鐘內內出現規定次數就開始工做

maxretry = 3    #3次密碼驗證失敗

  

注意:在作這個實驗以前要先作以下準備工做:

1. 將/etc/passwd文件中用戶mk和root的相關修改恢復過來。#由於用root用戶便於作實驗。

2. 將/etc/ssh/sshd_config文件中,將禁止root用戶登陸修改成容許。#說明生產上是要禁止root登陸,這裏爲了方便作實驗。

3. iptables防火牆須要配置。

    由於我在前面說了fail2ban的工做的原理就是經過分析必定時間內的相關服務日誌,將知足動做的相關IP利用iptables加入到dorp列表必定時間。 

因此首先要啓動防火牆iptables的相關配置:

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH  --update --seconds 1800 --hitcount 5 -j DROP
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --set -j ACCEPT

 特別注意的是若是這個地方dport 端口設置爲22,那麼/etc/ssh/sshd_config中的Port端口也要設置爲22.總之一句話,兩個設置端口要一致。

4. sshd服務器的端口,要和iptables 中dport端口要一致,我這裏講兩個都設置爲22了。

[root@xiaolyu76 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH  --update --seconds 1800 --hitcount 5 -j DROP [root@xiaolyu76 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --set -j ACCEPT [root@xiaolyu76 ~]# service iptables -L #該命令若是返回防火牆規則,說明防火牆起做用了,不然不起做用。 Usage: iptables {start|stop|reload|restart|condrestart|status|panic|save} [root@xiaolyu76 ~]# 

 啓動服務:

[root@xiaolyu76 ~]# > /var/log/secure #清空該日誌,從如今開始從新寫 [root@xiaolyu76 ~]# cat /var/log/secure [root@xiaolyu76 ~]# service fail2ban restart #重啓fail2ban Stopping fail2ban: [ OK ] Starting fail2ban: [ OK ] [root@xiaolyu76 ~]# iptables -L -n #生成關於fail2ban的規則鏈 Chain INPUT (policy ACCEPT) target prot opt source destination fail2ban-SSH tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain fail2ban-SSH (1 references) target prot opt source destination RETURN all -- 0.0.0.0/0 0.0.0.0/0 

 

 

測試:故意輸入錯誤密碼3次,再進行登陸時,會拒絕登陸

 

[root@xiaolyu77 ~]# ssh xiaolyu76 root@xiaolyu76's password: 
Permission denied, please try again. root@xiaolyu76's password: 
Permission denied, please try again. root@xiaolyu76's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@xiaolyu77 ~]# ssh xiaolyu76 ssh: connect to host xiaolyu76 port 22: Connection refused

 

 

如何查看登陸被拒絕的相關信息呢?

1. 咱們能夠用iptables查看鏈接被屏蔽的狀況:iptables -L 

[root@xiaolyu76 ~]# iptables -L |tail -4

2. 還能夠經過fail2ban-client status來查看被屏蔽的狀況:

說明: 若是fail2ban-client status ssh-iptables  顯示被ban的ip和數目就表示成功了,若是都是0,說明沒有成功

3. 也能夠經過查看fail2ban的日誌看到相關的信息

[root@xiaolyu76 ~]# tail /var/log/fail2ban.log

如何解除被禁止的IP ?

1. 直接重啓fail2ban服務即立刻解除全部IP 

  由於從新啓動fail2ban,iptables中的規則就會所有被清空。

2. 到了fail2ban規定時間以後自動解除

那如何查看發送的郵件呢?

 

4、ssh服務器免祕鑰登陸:

1. 在客戶端(我這裏是xiaolyu77主機)執行ssh-keygen生成祕鑰對

[root@xiaolyu77 ~]# ssh-keygen

2. 將該祕鑰對複製到服務器上:

[root@xiaolyu77 .ssh]# ssh-copy-id  -i   /root/.ssh/id_rsa.pub  xiaolyu76

3. 如今,服務器xiaolyu76的主機上ssh服務的公鑰是客戶端xiaolyu77的主機上ssh服務的私鑰,那麼能否在xiaolyu76(服務器端)來免密碼登陸

xiaolyu77(客戶端)呢?

能夠看到,顯然不能夠,這是爲何呢?由於xiaolyu76的私鑰和xiaolyu77的公鑰之間沒有任何關係。由於是私鑰加密,公鑰解密。

說通俗一點就是若是私鑰是鑰匙,那麼公鑰就是鎖。xiaolyu76的私鑰和xiaolyu77的公鑰沒有什麼關係,就是鑰匙打不開鎖,因此不可以免密碼登陸。

相關文章
相關標籤/搜索