linux\u4e0b\u600e\u6837\u8bbe\u7f6essh\u65e0\u5bc6\u7801\u767b\u5f55
\u4f60\u597dssh\u914d\u7f6e\u3000\u3000
\u4e3b\u673aA\uff1a10.0.5.199
\u4e3b\u673aB\uff1a10.0.5.198
\u9700\u8981\u914d\u7f6e\u4e3b\u673aA\u65e0\u5bc6\u7801\u767b\u5f55\u4e3b\u673aA\uff0c\u4e3b\u673aB
\u5148\u786e\u4fdd\u6240\u6709\u4e3b\u673a\u7684\u9632\u706b\u5899\u5904\u4e8e\u5173\u95ed\u72b6\u6001\u3002
\u5728\u4e3b\u673aA\u4e0a\u6267\u884c\u5982\u4e0b\uff1a
\u30001.\u3000$cd ~/.ssh
\u30002.\u3000$ssh-keygen -t rsa --------------------\u7136\u540e\u4e00\u76f4\u6309\u56de\u8f66\u952e\uff0c\u5c31\u4f1a\u6309\u7167\u9ed8\u8ba4\u7684\u9009\u9879\u5c06\u751f\u6210\u7684\u5bc6\u94a5\u4fdd\u5b58\u5728.ssh/id_rsa\u6587\u4ef6\u4e2d\u3002
\u30003.\u3000$cp id_rsa.pub authorized_keys
\u8fd9\u6b65\u5b8c\u6210\u540e\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u5c31\u53ef\u4ee5\u65e0\u5bc6\u7801\u767b\u5f55\u672c\u673a\u4e86\uff0c\u5373ssh localhost\uff0c\u65e0\u9700\u8f93\u5165\u5bc6\u7801\u3002
\u30004.\u3000$scp authorized_keys [email protected]:/home/summer/.ssh ------\u628a\u521a\u521a\u4ea7\u751f\u7684authorized_keys\u6587\u4ef6\u62f7\u4e00\u4efd\u5230\u4e3b\u673aB\u4e0a.\u3000\u3000
\u30005.\u3000$chmod 600 authorized_keys
\u3000\u3000 \u8fdb\u5165\u4e3b\u673aB\u7684.ssh\u76ee\u5f55\uff0c\u6539\u53d8authorized_keys\u6587\u4ef6\u7684\u8bb8\u53ef\u6743\u9650\u3002
\u3000\u3000 (4\u548c5\u53ef\u4ee5\u5408\u6210\u4e00\u6b65\uff0c\u6267\u884c: $ssh-copy-id -i [email protected] )
\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e0a\u9762\u51e0\u6b65\u6267\u884c\u5b8c\u6210\u540e\uff0c\u4ece\u4e3b\u673aA\u6240\u5728\u673a\u5668\u5411\u4e3b\u673aA\u3001\u4e3b\u673aB\u6240\u5728\u673a\u5668\u53d1\u8d77ssh\u8fde\u63a5\uff0c\u53ea\u6709\u5728\u7b2c\u4e00\u6b21\u767b\u5f55\u65f6\u9700\u8981\u8f93\u5165\u5bc6\u7801\uff0c\u4ee5\u540e\u5219\u4e0d\u9700\u8981\u3002
\u7528SSL\u52a0\u5bc6Key\u5b9e\u73b0\u81ea\u52a8\u767b\u5f55
\u9700\u8981\u6750\u6599\uff1a
1 \u88ab\u7ba1\u7406\u7684SSH\u670d\u52a1\u5668\u4e00\u53f0\u3002
2 \u7ba1\u7406\u7aef\u7535\u8111\u4e00\u53f0\u3002
\u73af\u5883\uff1a
\u7ba1\u7406\u670d\u52a1\u5668\uff1a ip\uff1a192.168.0.1 \u673a\u5668\u540d\uff1aserver
\u88ab\u7ba1\u7406\u670d\u52a1\u5668\uff1aip\uff1a192.168.0.2 \u673a\u5668\u540d\uff1aclient
\u751f\u6210\u5bc6\u94a5\u5bf9\uff1a
\u751f\u6210\u516c\u94a5\u5bc6\u94a5\u5bf9\u662f\u5728\u7ba1\u7406\u670d\u52a1\u5668\u4e0a\u751f\u6210\u7684\uff1a
[root@server ~]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair. #\u63d0\u793a\u6b63\u5728\u751f\u6210rsa\u5bc6\u94a5\u5bf9
Enter file in which to save the key (/home/usrname/.ssh/id_dsa): #\u8be2\u95ee\u516c\u94a5\u548c\u79c1\u94a5\u5b58\u653e\u7684\u4f4d\u7f6e\uff0c\u56de\u8f66\u7528\u9ed8\u8ba4\u4f4d\u7f6e\u5373\u53ef
Enter passphrase (empty for no passphrase): #\u8be2\u95ee\u8f93\u5165\u79c1\u94a5\u5bc6\u8bed\uff0c\u8f93\u5165\u5bc6\u8bed
Enter same passphrase again: #\u518d\u6b21\u63d0\u793a\u8f93\u5165\u5bc6\u8bed\u786e\u8ba4
Your identification has been saved in /home/usrname/.ssh/id_dsa. #\u63d0\u793a\u516c\u94a5\u548c\u79c1\u94a5\u5df2\u7ecf\u5b58\u653e\u5728/root/.ssh/\u76ee\u5f55\u4e0b
Your public key has been saved in /home/usrname/.ssh/id_dsa.pub.
The key fingerprint is:
x6:68:xx:93:98:8x:87:95:7x:2x:4x:x9:81:xx:56:94 root@server #\u63d0\u793akey\u7684\u6307\u7eb9
\u62f7\u8d1d\u4f60\u7684\u516c\u94a5\u5230\u88ab\u7ba1\u7406\u7684\u670d\u52a1\u5668\u4e0a
\u5728\u4f60\u7684\u7ba1\u7406\u670d\u52a1\u5668\u4e0a\u628a\u4f60\u7684\u516c\u94a5\u62f7\u8d1d\u5230\u88ab\u7ba1\u7406\u670d\u52a1\u5668\u4e0a\u8981\u8fdb\u884c\u81ea\u52a8\u767b\u9646\u7684\u7528\u6237\u76ee\u5f55\u4e0b\u3002
[root@server ~]# scp .ssh/id_dsa.pub [email protected]: #\u6bd4\u5982\u4f60\u60f3\u4f7f\u7528\u7528\u6237peter\u767b\u9646\uff0c\u5219remote_usrname\u8bf7\u4ee5peter\u4ee3\u66ff
\u6539\u540d\u548c\u8fdb\u884c\u6743\u9650\u8bbe\u7f6e
\u767b\u9646\u88ab\u7ba1\u7406\u7684\u670d\u52a1\u5668\uff0c\u8fdb\u5165\u9700\u8981\u8fdc\u7a0b\u767b\u9646\u7684\u7528\u6237\u76ee\u5f55\uff0c\u628a\u516c\u94a5\u653e\u5230\u7528\u6237\u76ee\u5f55\u7684 .ssh \u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff08\u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u521b\u5efa~/.ssh\u76ee\u5f55\uff0c\u5e76\u628a\u76ee\u5f55\u6743\u9650\u8bbe\u7f6e\u4e3a700\uff09\uff0c\u628a\u516c\u94a5\u6539\u540d\u4e3aauthorized_keys2\uff0c\u5e76\u4e14\u628a\u5b83\u7684\u7528\u6237\u6743\u9650\u8bbe\u6210600\u3002
[peter@client ~]$ ls
id_rsa.pub
[peter@client ~]$ mkdir ~/.ssh #\u5982\u679c\u5f53\u524d\u7528\u6237\u76ee\u5f55\u4e0b\u6ca1\u6709 .ssh \u76ee\u5f55\uff0c\u8bf7\u5148\u521b\u5efa\u76ee\u5f55
[peter@client ~]$ chmod 700 ~/.ssh
[peter@client ~]$ mv id_rsa.pub ~/.ssh
[peter@client ~]$ cd ~/.ssh
[peter@client ~]$ cat id_rsa.pub >> authorized_keys2
[peter@client ~]$ rm -f id_rsa.pub
[peter@client ~]$ chmod 600 authorized_keys2
[peter@client ~]$ ls -l
total 4
-rw------- 1 peter peter 225 Oct 10 11:28 authorized_keys2
\u6d4b\u8bd5\u4f7f\u7528\u5bc6\u94a5\u5bf9\u8fdb\u884c\u8fdc\u7a0b\u767b\u9646
[root@server ~]# ssh [email protected]
Enter passphrase for key '/root/.ssh/id_rsa': #\u63d0\u793a\u8f93\u5165\u5bc6\u7801\u77ed\u8bed\uff0c\u8bf7\u8f93\u5165\u521a\u624d\u8bbe\u7f6e\u7684\u5bc6\u7801\u77ed\u8bed
Last login: Sun Oct 10 11:32:14 2010 from 192.168.0.1
[peter@client ~]$
\u5982\u679c\u4f60\u4e0d\u80fd\u7528\u6b63\u786e\u7684\u767b\u5f55\uff0c\u5e94\u8be5\u91cd\u65b0\u68c0\u67e5\u4e00\u4e0b\u4f60\u7684authorized_keys2\u7684\u6743\u9650\u3002\u4e5f\u53ef\u80fd\u8981\u68c0\u67e5.ssh\u76ee\u5f55\u7684\u6743\u9650\u3002
\u4f7f\u7528 ssh-agent\uff08ssh\u4ee3\u7406\uff09\u81ea\u52a8\u8f93\u5165\u5bc6\u7801\u77ed\u8bed
\u7262\u8bb0\u4f60\u7684\u201c\u5bc6\u7801\u77ed\u53e5\u201d\uff0c\u73b0\u5728\u4f60\u53ef\u4ee5\u7528\u4f60\u7684\u5bc6\u94a5\u800c\u4e0d\u662f\u5bc6\u7801\u6765\u767b\u5f55\u4f60\u7684\u670d\u52a1\u5668\u4e86\uff0c\u4f46\u662f\u8fd9\u6837\u4ecd\u7136\u6ca1\u6709\u7701\u4ec0\u4e48\u4e8b\uff0c\u4f60\u8fd8\u662f\u8981\u8f93\u5165\u5bc6\u94a5\u7684\u201c\u5bc6\u7801\u77ed\u8bed\u201d\u3002\u6709\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\u5417\uff1f\u7b54\u6848\u5c31\u662f\u91c7\u7528SSH\u4ee3\u7406\uff08ssh-agent\uff09\uff0c\u4e00\u4e2a\u7528\u6765\u5e2e\u4f60\u8bb0\u4f4f\u201c\u5bc6\u7801\u77ed\u8bed\u201d\u7684\u7a0b\u5e8f\u3002 ssh-agent\u662fOpenSSH\u4e2d\u9ed8\u8ba4\u5305\u62ec\u7684ssh\u4ee3\u7406\u7a0b\u5e8f\u3002
\u767b\u9646\u7ba1\u7406\u670d\u52a1\u5668
[root@server ~]# ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-vEGjCM2147/agent.2147; export SSH_AUTH_SOCK;
SSH_AGENT_PID=2148; export SSH_AGENT_PID;
echo Agent pid 2148;
\u5f53\u4f60\u8fd0\u884cssh-agent\uff0c\u5b83\u4f1a\u6253\u5370\u51fa\u6765\u5b83\u4f7f\u7528\u7684 ssh \u7684\u73af\u5883\u548c\u53d8\u91cf\u3002\u8981\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u624b\u52a8\u8fdb\u884c\u58f0\u660e\u73af\u5883\u53d8\u91cf\uff0c\u53e6\u4e00\u79cd\u662f\u8fd0\u884ceval\u547d\u4ee4\u81ea\u52a8\u58f0\u660e\u73af\u5883\u53d8\u91cf\u3002
\u65b9\u6cd5\u4e00\uff1a\u624b\u52a8\u58f0\u660e\u73af\u5883\u53d8\u91cf
[root@server ~]# SSH_AUTH_SOCK=/tmp/ssh-vEGjCM2147/agent.2147; export SSH_AUTH_SOCK;
[root@server ~]# SSH_AGENT_PID=2148; export SSH_AGENT_PID;
[root@server ~]# printenv | grep SSH #\u68c0\u67e5 ssh \u73af\u5883\u53d8\u91cf\u662f\u5426\u5df2\u7ecf\u52a0\u5165\u5f53\u524d\u4f1a\u8bdd\u7684\u73af\u5883\u53d8\u91cf
SSH_AGENT_PID=2148
SSH_AUTH_SOCK=/tmp/ssh-vEGjCM2147/agent.2147
\u65b9\u6cd5\u4e8c\uff1a\u8fd0\u884ceval\u547d\u4ee4\u81ea\u52a8\u58f0\u660e\u73af\u5883\u53d8\u91cf
[root@server ~]# eval `ssh-agent`
Agent pid 2157
[root@server ~]# printenv | grep SSH #\u68c0\u67e5 ssh \u73af\u5883\u53d8\u91cf\u662f\u5426\u5df2\u7ecf\u52a0\u5165\u5f53\u524d\u4f1a\u8bdd\u7684\u73af\u5883\u53d8\u91cf
SSH_AGENT_PID=2148
SSH_AUTH_SOCK=/tmp/ssh-vEGjCM2147/agent.2147
\u73b0\u5728 ssh-agent \u5df2\u7ecf\u5728\u8fd0\u884c\u4e86\uff0c\u4f46\u662f ssh-agent \u91cc\u9762\u662f\u7a7a\u767d\u7684\u4e0d\u4f1a\u6709\u89e3\u5bc6\u7684\u4e13\u7528\u5bc6\u94a5\u3002\u6211\u4eec\u8981\u544a\u8bc9\u5b83\u6211\u4eec\u6709\u79c1\u94a5\u548c\u8fd9\u4e2a\u79c1\u94a5\u5728\u54ea\u513f\u3002\u8fd9\u5c31\u9700\u8981\u4f7f\u7528 ssh-add \u547d\u4ee4\u628a\u6211\u4eec\u7684\u4e13\u7528\u5bc6\u94a5\u6dfb\u52a0\u5230 ssh-agent \u7684\u9ad8\u901f\u7f13\u5b58\u4e2d\u3002
[root@server ~]# ssh-add ~/.ssh/id_dsa
Enter passphrase for /home/user/.ssh/id_dsa: #\u8f93\u5165\u4f60\u7684\u5bc6\u7801\u77ed\u8bed
Identity added: /home/user/.ssh/id_dsa (/home/user/.ssh/id_dsa)
[root@server ~]# ssh-add -l #\u67e5\u770b ssh\u4ee3\u7406\u7684\u7f13\u5b58\u5185\u5bb9
1024 72:78:5e:6b:16:fd:f2:8c:81:b1:18:e6:9f:77:6e:be /root/.ssh/id_rsa (RSA)
\u8f93\u5165\u4e86\u5bc6\u7801\u77ed\u53e5\uff0c\u73b0\u5728\u597d\u4e86\uff0c\u4f60\u53ef\u4ee5\u767b\u5f55\u4f60\u7684\u8fdc\u7a0b\u670d\u52a1\u5668\u800c\u4e0d\u7528\u8f93\u5165\u4f60\u7684\u5bc6\u7801\u77ed\u8bed\u4e86\uff0c\u800c\u4e14\u4f60\u7684\u79c1\u94a5\u662f\u5bc6\u7801\u4fdd\u62a4\u7684\u3002