具體需求:
在同一使用者hequan下 實作 A 從本地分發資料到B。過程中不需要密碼提示
建立使用者
#useradd hequan # id hequan
#echo 123456 | passwd --stdin hequan
# su - hequan
RSA 既可以加密也,也可以數字簽名
DSA 隻能用于數字簽名
開始建立密鑰
[hequan@A ~]$ ssh-keygen -t dsa 預設RSA
Generating public/private dsa key pair.
Enter file in which to save the key (/home/hequan/.ssh/id_dsa):
Created directory '/home/hequan/.ssh'.
Enter passphrase (empty for no passphrase):
[hequan@A ~]$ ll -l .ssh/
總用量 8
-rw------- 1 hequan hequan 668 3月 28 04:07 id_dsa 私鑰 保留
-rw-r--r-- 1 hequan hequan 598 3月 28 04:07 id_dsa.pub 公鑰 分發
[hequan@A ~]$ ll -ld .ssh/
drwx------ 2 hequan hequan 4096 3月 28 04:07 .ssh/
分發公鑰
格式: ssh-copy-id [-i [identity_file]] [user@]machine
[hequan@A ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected] 分發
Are you sure you want to continue connecting (yes/no)? yes
.ssh/auhorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[hequan@B ~]$ ll -l .ssh/
-rw------- 1 hequan hequan 598 4月 4 04:49 authorized_keys
[hequan@A ~]$ ssh [email protected] 不要密碼
[hequan@B ~]$
測試發檔案
[hequan@A ~]$ echo hequan > hequan.txt
[hequan@A ~]$ scp hequan.txt [email protected]:~
如果測試時,ssh特别慢,可以修改如下參數。
# vi /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
# /etc/init.d/sshd restart
[hequan@A ~]$ scp -rp /etc/ [email protected]:~ 發送目錄 到 ~
[hequan@A ~]$ scp -rp [email protected]:~/etc/ /tmp/ 把B的/etc/目錄 拷到本機/tmp
免密碼登陸小結:
免密碼驗證是單向的
基于使用者的,最好不要跨使用者
連接配接慢 (可以看上面的解決辦法)
批量分發初始都需要輸入一次密碼,并且第一次連接配接要确認(expect)
[root@A ~]# ssh -v [email protected] -v 調試 整個連接配接過程
測試腳本
#!/bin/sh
if [ $# -ne 1 ]
then
echo "/bin/sh $0 arg1"
exit 1
fi
for n in 11
do
scp -rp $1 [email protected].$n:~;
done
批量分發批量處理 權限問題
visudo
hequan ALL=(ALL) NOPASSWD:/bin/cp
# visudo -c
/etc/sudoers:解析正确
遠端sudo
# Disable "ssh hostname sudo <cmd>", because it will show the passwor
d in clear.
# You have to run "ssh -t hostname sudo <cmd>".
Defaults requiretty 或者注釋掉這一條
$ scp -rp quan [email protected]:~ 先把quan拷貝到家目錄,
$ ssh [email protected] -t sudo /bin/cp ~/quan /tmp 然後拷貝quan檔案到 /tmp 最後是root權限
rsync
chmod u+s `which rsync` 有root的權限 suid
-rwsr-xr-x 1 root root 415000 10月 31 2013 /usr/bin/rsync
ssh [email protected] /usr/bin/rsync ~ /yy /tmp
小結:
利用root
利用visudo 推薦2
利用rsync
本文轉自 295631788 51CTO部落格,原文連結:http://blog.51cto.com/hequan/1762853,如需轉載請自行聯系原作者