天天看點

利用ssh分發實作方法

ssh服務提供兩個服務功能:一個是提供類似telnet遠端服務,另一個就是類似ftp服務提供安全的傳輸

ssh安全的加密協定,用于遠端連接配接伺服器

預設端口22,,安全版本為2

ssh服務提供兩個服務功能

ssh用戶端包含ssh連接配接指令,以及遠端拷貝scp指令等

ssh服務由openssh和用戶端組成

ssh服務認證類型

基于密碼

ssh -p52113 [email protected]

基于秘鑰

公鑰放在伺服器上,私鑰放在自己電腦上

給你一個端口如何查出對應的服務

[root@localhost ~]# lsof -i tcp:22

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

sshd    1023 root    3u  IPv4   9425      0t0  TCP *:ssh (LISTEN)

sshd    1023 root    4u  IPv6   9427      0t0  TCP *:ssh (LISTEN)

sshd    1134 root    3r  IPv4   9867      0t0  TCP 172.16.10.20:ssh->172.16.

losf -i tcp:52113

netstat -an |grep 52113

ssh安全優化

端口更改,禁止root登入,禁止空密碼登入,不使用dns

免密碼驗證小結

1)免密碼登入驗證是單向的

2)基于使用者的,最好不好跨不同使用者

3)ssh連結慢的問題

4)批量分發1000台初始都需要輸入一次密碼,并且第一次要正确

需求:在同一使用者下,實作a機器分發資料資料到B C,在分發過程中不需要

B C提示系統密碼驗證,除了批量分發,還可以批量随意查詢cpu,各種配置等資訊

SSH批量分發實驗

中心分發server172.16.10.10

接收節點1client172.16.10.20

接收節點2client172.16.10.30

實作前期準備

1添加系統賬号,不用root這樣更安全

[root@localhost ~]# useradd luliechu && echo "luliechu@123" |passwd --stdin luliechu

更改使用者 luliechu 的密碼 。

passwd: 所有的身份驗證令牌已經成功更新。

2在任意一台機器上建立秘鑰對都可以

[root@localhost ~]# su - luliechu

[luliechu@localhost ~]$ whoami

luliechu

[luliechu@localhost ~]$ ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/home/luliechu/.ssh/id_rsa):

Created directory '/home/luliechu/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/luliechu/.ssh/id_rsa.

Your public key has been saved in /home/luliechu/.ssh/id_rsa.pub.

The key fingerprint is:

eb:ff:1d:27:8a:f9:d1:80:aa:84:39:b4:96:14:e0:8f [email protected]

The key's randomart image is:

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

|  .              |

| . .             |

|  . .            |

|   o .     .     |

|  E +   S . .    |

|   o =   o   o   |

|    B . o   . + .|

|   . o o   o + + |

|      . ..+o+ .  |

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

[luliechu@localhost ~]$ ll ~/ -la

總用量 24

drwx------  3 luliechu luliechu 4096 3月  28 22:33 .

drwxr-xr-x. 3 root     root     4096 3月  28 22:24 ..

-rw-r--r--  1 luliechu luliechu   18 7月  18 2013 .bash_logout

-rw-r--r--  1 luliechu luliechu  176 7月  18 2013 .bash_profile

-rw-r--r--  1 luliechu luliechu  124 7月  18 2013 .bashrc

drwx------  2 luliechu luliechu 4096 3月  28 22:34 .ssh

[luliechu@localhost ~]$ ls -l .ssh/

總用量 8

-rw------- 1 luliechu luliechu 1675 3月  28 22:34 id_rsa   這個是私鑰匙

-rw-r--r-- 1 luliechu luliechu  412 3月  28 22:34 id_rsa.pub  這個是公鑰,鎖

這裡注意權限問題,權限錯了無法操作

做分發伺服器,把公鑰給b 和c  私鑰留給自己

如果報錯Host key verification failed.“的解決方案

[root@localhost ~]# sed -i '35s#ask#no#g' /etc/ssh/ssh_config   //更改配置檔案改變安全級别

[root@localhost ~]# service sshd restart

停止 sshd:                                               [确定]

正在啟動 sshd:                                           [确定]

在服務端172.16.10.10上   把秘鑰分發給用戶端

[luliechu@localhost ~]$ ssh-copy-id -i .ssh/id_rsa.pub 172.16.10.20

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

RSA key fingerprint is 41:69:b2:97:ec:0f:99:4a:13:48:a3:39:3d:c7:80:79.

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

Host key verification failed.

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

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

[email protected]'s password:

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

  .ssh/authorized_keys 出現這個表示成功

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

[luliechu@localhost ~]$ ssh-copy-id -i .ssh/id_rsa.pub 172.16.10.30

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

RSA key fingerprint is 82:6d:9f:ba:a6:f1:98:07:dd:6b:5a:1c:d5:a1:dd:38.

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

[email protected]'s password:

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

  .ssh/authorized_keys   出現這個表示成功

在用戶端檢查,發現名字就變了

總用量 4

-rw------- 1 luliechu luliechu 412 3月  30 05:33 authorized_keys

在伺服器上測試:

[luliechu@localhost ~]$ ssh -p22 [email protected]

[luliechu@localhost ~]$ ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:BC:70:1A  

          inet addr:172.16.10.30  Bcast:172.16.10.255  Mask:255.255.255.0

很明顯我們不需要再輸入密碼直接就進來了,也可以直接複制拷貝東西不再需要任何認證

------------到此這一步等于是把高速路修通了-------------,接着就可以批量分發東西了

在伺服器上操作

[luliechu@localhost ~]$ echo 123.text >a.txt

[luliechu@localhost ~]$ scp -P22 a.txt [email protected]:~

a.txt                    100%    9     0.0KB/s   00:00    

[luliechu@localhost ~]$ scp -P22 a.txt [email protected]:~

在用戶端上看到

[luliechu@localhost ~]$ ls

a.txt

很明顯就直接就不要任何密碼就過來了,證明實驗成果

把需要拷貝的資料寫在一個腳本裡,自動執行,就實作了批量分發的功能

如果速度過慢,是因為dns的問題,解決方案如下:

在ssh服務端上更改/etc/ssh/sshd_config檔案中的配置為如下内容

UseDNS no

GSSAPIAuthentication no

重新開機sshd服務  service sshd restart

腳本寫法;

[luliechu@localhost ~]$ vi fenfa.sh

for n in 20 30

do

    scp -p22 $1 [email protected].$n:~

done

$1你輸入的第一條指令行的參數  

用法執行個體:在伺服器上執行腳本

[luliechu@localhost ~]$ sh fenfa.sh /etc/hosts

hosts                    100%  158     0.2KB/s   00:00    

hosts                    100%  158     0.2KB/s   00:00

在用戶端檢視測試結果

a.txt  hosts

很明顯完美成果

本文轉自    探花無情   51CTO部落格,原文連結:http://blog.51cto.com/983865387/1917725