scp免密码传送文件
一、單向
ssh-keygen -t rsa
單向無密碼訪問遠程服務(wù)器操作比較簡單,比如服務(wù)器A需要無密碼訪問服務(wù)器B(A–>B),那么只需要在服務(wù)器A生成密鑰對,將生成的公鑰上傳到服務(wù)器B的相關(guān)用戶目錄下的.ssh目錄中(沒有的話手動創(chuàng)建,注意,它的目錄權(quán)限是700),并將公鑰文件名稱改為authorized_keys(注意,這個文件的權(quán)限應(yīng)該是644),請注意.ssh目錄及authorized_keys文件的權(quán)限,權(quán)限不符,會使配置無效。
二、雙向
將本機的id_rsa.pub文件中的內(nèi)容加到對方的authorized_keys文件中即可。
三、遇到的問題
有時候按步驟設(shè)置好了,ssh時仍然提示需要輸入密碼,此時可以用ssh ip -v 來跟蹤,或者在文件/var/log/secure中查看對應(yīng)的日志。
例如我設(shè)置ssh無密碼連接Route主機后,仍然提示輸入密碼,經(jīng)過查看secure日志,發(fā)現(xiàn)如下信息:
Mar 6 14:00:11 Route sshd[10353]:?Authentication refused: bad ownership or modes for directory /home/tianyc
Mar 6 14:01:06 Route sshd[10354]: Connection closed by 10.1.1.1
Mar 6 14:01:42 Route su: pam_unix(su:session): session opened for user root by root(uid=201301)
查看該目錄權(quán)限,被設(shè)置成了777,修改為755后即正常:
[root@Route /]# cd home/
[root@Route home]# ll
總用量 108
drwxr-xr-x. 2 root root 4096 1月 21 23:52 lvs
drwxrwxrwx. 6 tianyc NEU 4096 3月 6 10:57 tianyc
drwx------. 3 wensong wensong 4096 1月 21 17:24 wensong
[root@Route home]# chmod 755 tianyc
[root@Route home]# ll
總用量 108
drwxr-xr-x. 2 root root 4096 1月 21 23:52 lvs
drwxr-xr-x. 6 tianyc NEU 4096 3月 6 10:57 tianyc
drwx------. 3 wensong wensong 4096 1月 21 17:24 wensong
[root@Route home]# exit
exit
[tianyc@Route /]$?ssh route
Last login: Wed Mar 6 11:14:29 2013 from route
[tianyc@Route ~]$
在Linux環(huán)境下,兩臺主機之間傳輸文件一般使用scp命令,通常用scp命令通過ssh獲取對方linux主機文件的時候都需要輸入密碼確認。
不過通過建立信任關(guān)系,可以實現(xiàn)不輸入密碼。
這里假設(shè)A的IP:192.168.10.1
B的IP:192.168.10.2
需要從A免密碼輸入復(fù)制文件至B。
1. 在主機A上執(zhí)行如下命令來生成配對密鑰:?
ssh-keygen -t rsa?
按照提示操作,注意,不要輸入passphrase。提示信息如下?
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:?
ff:8e:85:68:85:94:7c:2c:46:b1:e5:2d:41:5c:e8:9b??root@localhost.domain?
2. 將 .ssh 目錄中的 id_rsa.pub 文件復(fù)制到 主機B 的 ~/.ssh/ 目錄中,并改名為??authorized_keys。?
scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/authorized_keys?
以后從A主機scp到B主機就不需要密碼了。
3.使用此方法需要注意:復(fù)制的兩臺計算機需要用相同的賬戶名,這里都是用的root。為了安全起見,需要在兩臺機器中創(chuàng)建相同的賬號,然后在A上用su命令切換至賬號下,執(zhí)行第1步,第二步中復(fù)制文件時可能.ssh目錄并不存在,需要手動創(chuàng)建。
在第2步中如果機器中已經(jīng)存在authorized_keys文件,則需注意,這個文件可以包含多個SSH驗證信息,這時可用 cat >>命令將驗證文件內(nèi)容附加上去。
比如復(fù)制到scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/a.pub
然后執(zhí)行cat ~/.ssh/a.pub >> ~/.ssh/authorized_keys
?
轉(zhuǎn)載于:https://www.cnblogs.com/peizhe123/p/5699052.html
總結(jié)
以上是生活随笔為你收集整理的scp免密码传送文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Axure交互样式的简单使用介绍
- 下一篇: 集中式发电景气下滑分布式光伏迎新机遇