1、ssh免密登錄 ssh ip地址 免密登錄配置 生成公鑰和私鑰 將公鑰拷貝到要免密登錄的目標機器上 .ssh文件夾下(~/.ssh)的文件功能解釋 (1)known_hosts :記錄ssh訪問過電腦的公鑰(public key) (2)id_rsa :生成的私鑰 (3)id_rsa.pub ...
1、ssh免密登錄
ssh ip地址
[root@192 ~]# ssh 192.168.1.102
[email protected]'s password:
Last login: Mon Feb 18 20:40:28 2019 from 192.168.1.101
免密登錄配置
生成公鑰和私鑰
[root@192 ~]# 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:
d4:95:6c:a8:21:9b:27:62:79:43:76:e8:4b:32:6c:fe [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| . o.. |
| = o...+ |
| . = =.o.. |
| O O.o |
| + * =S |
| . . |
| . |
| E |
| |
+-----------------+
[root@192 ~]# ls /root/.ssh/
id_rsa id_rsa.pub
將公鑰拷貝到要免密登錄的目標機器上
.ssh文件夾下(~/.ssh)的文件功能解釋
(1)known_hosts :記錄ssh訪問過電腦的公鑰(public key)
(2)id_rsa :生成的私鑰
(3)id_rsa.pub :生成的公鑰
(4)authorized_keys :存放授權過得無密登錄伺服器公鑰
[root@192 ~]# hostname
192.168.1.101
[root@192 ~]# ssh-copy-id 192.168.1.102
The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.
RSA key fingerprint is 56:57:4c:81:94:e0:47:fe:1e:aa:8c:9c:2a:87:a6:dc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.102' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '192.168.1.102'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@192 ~]# ls /root/.ssh/
id_rsa id_rsa.pub known_hosts
[root@192 ~]# ssh 192.168.1.102
Last login: Mon Feb 18 22:50:53 2019 from 192.168.1.101
[root@192 ~]# ls /root/.ssh/
authorized_keys
2、集群分發腳本xsync
伺服器與伺服器數據拷貝
rsync和scp區別:用rsync做文件的複製要比scp的速度快,rsync只對差異文件做更新。scp是把所有文件都複製過去。
scp
[root@192 ~]# scp -r /tmp/1.txt [email protected]:/tmp/
1.txt
rsync
選項 功能
-r 遞歸
-v 顯示覆制過程
-l 拷貝符號連接
[root@192 ~]# rsync -rvl /tmp/2.txt [email protected]:/tmp/
sending incremental file list
2.txt
sent 84 bytes received 31 bytes 76.67 bytes/sec
total size is 13 speedup is 0.11
集群分發腳本xsync
[root@192 ~]# mkdir bin
[root@192 ~]# cd bin/
[root@192 bin]# touch xsync
[root@192 bin]# vim xsync
[root@192 bin]# chmod 777 xsync
#!/bin/bash
#1 獲取輸入參數個數,如果沒有參數,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
#2 獲取文件名稱
p1=$1
fname=`basename $p1`
echo fname=$fname
#3 獲取上級目錄到絕對路徑
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 獲取當前用戶名稱
user=`whoami`
#5 迴圈
for((host=102; host<104; host++)); do
echo --------------------- 192.168.1.$host ----------------
rsync -rvl $pdir/$fname [email protected].$host:$pdir
#echo ------"rsync -rvl $pdir/$fname [email protected].$host:$pdir complete"------------
done
xsync調用
[root@192 bin]# xsync /tmp/1.txt /tmp/
fname=1.txt
pdir=/tmp
--------------------- 192.168.1.102 ----------------
sending incremental file list
1.txt
sent 516 bytes received 31 bytes 364.67 bytes/sec
total size is 445 speedup is 0.81
--------------------- 192.168.1.103 ----------------
The authenticity of host '192.168.1.103 (192.168.1.103)' can't be established.
RSA key fingerprint is 56:57:4c:81:94:e0:47:fe:1e:aa:8c:9c:2a:87:a6:dc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.103' (RSA) to the list of known hosts.
[email protected]'s password:
sending incremental file list
1.txt
sent 71 bytes received 37 bytes 5.84 bytes/sec
total size is 445 speedup is 4.12