{"id":162,"date":"2013-05-30T21:06:49","date_gmt":"2013-05-30T13:06:49","guid":{"rendered":"http:\/\/coderbee.net\/?p=162"},"modified":"2013-06-06T16:15:40","modified_gmt":"2013-06-06T08:15:40","slug":"ssh-%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20130530\/162","title":{"rendered":"ssh \u81ea\u52a8\u767b\u5f55"},"content":{"rendered":"<h2>\u81ea\u52a8\u767b\u5f55<\/h2>\n<p>SSH\u63d0\u4f9b\u4e86\u516c\u94a5\u767b\u5f55\uff0c\u53ef\u4ee5\u7701\u53bb\u6bcf\u6b21\u767b\u5f55\u90fd\u8981\u8f93\u5165\u7684\u79d8\u5bc6\u7684\u6b65\u9aa4\u3002<\/p>\n<p>\u516c\u94a5\u767b\u5f55\u539f\u7406\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u5c06\u81ea\u5df1\u7684\u516c\u94a5\u5b58\u50a8\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u3002<\/li>\n<li>\u767b\u5f55\u7684\u65f6\u5019\uff0c\u8fdc\u7a0b\u4e3b\u673a\u5411\u7528\u6237\u53d1\u9001\u4e00\u6bb5\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u7528\u6237\u7528\u81ea\u5df1\u7684\u79c1\u94a5\u52a0\u5bc6\u540e\uff0c\u518d\u53d1\u56de\u6765\u3002<\/li>\n<li>\u8fdc\u7a0b\u4e3b\u673a\u5229\u7528\u4e8b\u5148\u5b58\u50a8\u7684\u516c\u94a5\u8fdb\u884c\u89e3\u5bc6\uff0c\u5982\u679c\u6210\u529f\uff0c\u5c31\u8bc1\u660e\u7528\u6237\u662f\u53ef\u884c\u7684\uff0c\u76f4\u63a5\u5141\u8bb8\u767b\u5f55shell\uff0c\u4e0d\u518d\u8981\u6c42\u8f93\u5165\u5bc6\u7801\u3002<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h4>\u5ba2\u6237\u7aef\u751f\u6210\u5bc6\u94a5<\/h4>\n<p>\u7528\u547d\u4ee4 <code>ssh-keygen<\/code>\uff0c<\/p>\n<pre><code>\r\n$ ssh-keygen\r\nGenerating public\/private rsa key pair.\r\nEnter file in which to save the key (\/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa): abc\r\nEnter passphrase (empty for no passphrase):\r\nEnter same passphrase again:\r\nYour identification has been saved in abc.\r\nYour public key has been saved in abc.pub.\r\nThe key fingerprint is:\r\nc9:c0:7e:d0:f0:cc:20:34:ca:79:85:78:30:81:4b:a2 rKF58842@LUITSWX24970\r\n<\/code><\/pre>\n<p>\u751f\u6210\u7684\u6587\u4ef6abc\u4fdd\u5b58\u7684\u662f\u79c1\u94a5\uff0cabc.pub\u4fdd\u5b58\u7684\u662f\u516c\u94a5\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u6587\u4ef6\uff0c\u9ed8\u8ba4\u751f\u6210\u7684\u662f id_rsa \u548c id_rsa.pub<\/p>\n<p>\u4f1a\u63d0\u793a\u8f93\u5165\u4fdd\u5b58\u5bc6\u94a5\u7684\u6587\u4ef6\u540d\u548c\u79c1\u94a5\u7684\u79d8\u5bc6\u3002\u5bf9\u4e8e\u4fdd\u5b58\u6587\u4ef6\uff0c\u4e00\u822c\u4e0d\u8981\u8f93\u5165\uff0c\u9ed8\u8ba4\u5c31\u597d\u3002\u5982\u679c\u62c5\u5fc3\u79c1\u94a5\u7684\u5b89\u5168\uff0c\u53ef\u4ee5\u8f93\u5165\u79c1\u94a5\u79d8\u5bc6\u3002<\/p>\n<h4>\u628a\u516c\u94a5\u4f20\u5230\u8fdc\u7a0b\u4e3b\u673a<\/h4>\n<p>\u7528\u547d\u4ee4\uff1a<code>ssh-copy-id user@host<\/code>\uff0c\u6b64\u547d\u4ee4\u4f1a\u628a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u516c\u94a5\u4fdd\u5b58\u5230user \u7684\uff1a<code>~\/.ssh\/authorized_keys<\/code>\u6587\u4ef6\u91cc\u3002<\/p>\n<p>\u5728\u6211win7\u7cfb\u7edf\u5b89\u88c5\u7684ssh\u5ba2\u6237\u7aef\u6ca1\u6709\u4e0a\u9762\u8fd9\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u8fd9\u6837\uff1a<br \/>\n<code>ssh coderbee@coderbee.net 'cat &gt; ~\/.ssh\/authorized_keys' &lt; id_rsa.pub<\/code><\/p>\n<p>\u624b\u5de5\u7c98\u8d34\u5230\u76ee\u6807\u6587\u4ef6\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<h4>\u4fee\u6539ssh\u670d\u52a1\u914d\u7f6e<\/h4>\n<p>\u5220\u9664\u8fdc\u7a0b\u4e3b\u673a\u7684 <code>\/etc\/ssh\/sshd_config<\/code> \u6587\u4ef6\u91cc\u7684\u4e0b\u9762\u4e09\u9879\u524d\u9762\u7684\u6ce8\u91ca\u7b26<code>#<\/code>\uff1a<\/p>\n<pre><code> RSAAuthentication yes\r\n PubkeyAuthentication yes\r\n AuthorizedKeysFile .ssh\/authorized_keys\r\n<\/code><\/pre>\n<p>\u4fee\u6539\u914d\u7f6e\u540e\u91cd\u542f\u8fdc\u7a0b\u4e3b\u673a\u7684ssh\u670d\u52a1\uff1a <code>service ssh restart<\/code><\/p>\n<p>\u73b0\u5728\u5ba2\u6237\u7aef\u53ef\u4ee5\u81ea\u52a8\u767b\u5f55\u4e86\uff1a <code>ssh coderbee@coderbee.net<\/code>\u3002<\/p>\n<h3>ssh-agent<\/h3>\n<p>\u5728\u751f\u6210\u5bc6\u94a5\u65f6\uff0c\u5982\u679c\u4e0d\u8f93\u5165\u79c1\u94a5\u7684\u5bc6\u7801\uff0c\u90a3\u4e48\u4e00\u65e6\u5ba2\u6237\u673a\u88ab\u653b\u51fb\u4e86\uff0c\u90a3\u4e48\u670d\u52a1\u5668\u4e5f\u5c31\u88ab\u653b\u51fb\u4e86\u3002\u4f46\u5982\u679c\u8f93\u5165\u5bc6\u7801\uff0c\u90a3\u4e48\u6bcf\u6b21\u8fdb\u884cssh\u8fde\u63a5\u65f6\u90fd\u9700\u8981\u8f93\u5165\u79c1\u94a5\u7684\u5bc6\u7801\uff0c\u4e5f\u975e\u5e38\u9ebb\u70e6\u3002<\/p>\n<p>ssh-agent\uff08\u4e13\u7528\u5bc6\u94a5\u544a\u8bc9\u7f13\u5b58\uff09\u662f\u4e3a\u89e3\u51b3\u6bcf\u6b21\u90fd\u9700\u8981\u8f93\u5165\u79c1\u94a5\u5bc6\u7801\u800c\u8bbe\u8ba1\u7684\uff0c\u5185\u5efa\u5728OpenSSH\u91cc\u3002<\/p>\n<p>ssh-agent \u662f\u4e2a\u957f\u65f6\u95f4\u6301\u7eed\u8fd0\u884c\u7684\u5b88\u62a4\u8fdb\u7a0b\uff08daemon\uff09\uff0c\u8bbe\u8ba1\u5b83\u7684\u552f\u4e00\u76ee\u7684\u5c31\u662f\u5bf9\u89e3\u5bc6\u7684\u4e13\u7528\u5bc6\u94a5\u8fdb\u884c\u9ad8\u901f\u7f13\u5b58\u3002<\/p>\n<p>ssh \u5305\u542b\u7684\u5185\u5efa\u652f\u6301\u5141\u8bb8\u5b83\u540c ssh-agent \u901a\u4fe1\uff0c\u5141\u8bb8 ssh \u4e0d\u5fc5\u6bcf\u6b21\u65b0\u8fde\u63a5\u65f6\u90fd\u63d0\u793a\u60a8\u8981\u5bc6\u7801\u624d\u80fd\u83b7\u53d6\u89e3\u5bc6\u7684\u4e13\u7528\u5bc6\u94a5\u3002\u5bf9\u4e8e ssh-agent \uff0c\u60a8\u53ea\u8981\u4f7f\u7528 ssh-add \u628a\u4e13\u7528\u5bc6\u94a5\u6dfb\u52a0\u5230 ssh-agent \u7684\u9ad8\u901f\u7f13\u5b58\u4e2d\u3002\u8fd9\u662f\u4e2a\u4e00\u6b21\u6027\u8fc7\u7a0b\uff1b\u7528\u8fc7 ssh-add \u4e4b\u540e\uff0c ssh \u5c06\u4ece ssh-agent \u83b7\u53d6\u60a8\u7684\u4e13\u7528\u5bc6\u94a5\uff0c\u800c\u4e0d\u4f1a\u63d0\u793a\u8981\u5bc6\u7801\u77ed\u8bed\u6765\u70e6\u60a8\u4e86\u3002<\/p>\n<h4>\u4f7f\u7528 ssh-agent<\/h4>\n<ul>\n<li>\u5982\u679c\u79c1\u94a5\u4e4b\u524d\u662f\u6ca1\u6709\u8bbe\u7f6e\u5bc6\u7801\u7684\uff0c\u7528\u547d\u4ee4 <code>ssh-keygen -p<\/code> \u8bbe\u7f6e\u3002<\/li>\n<li>\u542f\u52a8ssh-agent\uff1a<code>ssh-agent<\/code>\uff0c\u8f93\u51fa\u5927\u6982\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code>\r\n$ ssh-agent\r\nSSH_AUTH_SOCK=\/tmp\/ssh-VMkGwT1536\/agent.1536; export SSH_AUTH_SOCK;\r\nSSH_AGENT_PID=4572; export SSH_AGENT_PID;\r\necho Agent pid 4572;\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u662f\u662fshell\u547d\u4ee4\uff0c\u4e3b\u8981\u662f\u8bbe\u7f6e\u5bfc\u51fa\u73af\u5883\u53d8\u91cf\u7684\uff0c\u9700\u8981\u624b\u52a8\u6267\u884c\u3002\u4e5f\u53ef\u4ee5\u76f4\u63a5\u628a\u542f\u52a8ssh-agent\u8f93\u51fa\u5f53\u547d\u4ee4\u6765\u6267\u884c\uff1a<code>eval<\/code>ssh-agent<code><\/code>\u3002<\/p>\n<ul>\n<li>\u628a\u79c1\u94a5\u6dfb\u52a0\u5230ssh-agent\uff1a <code>ssh-add<\/code>\uff0c\u4f1a\u63d0\u793a\u8f93\u5165\u79c1\u94a5\u5bc6\u7801\u3002<\/li>\n<li>\u518d\u6b21\u767b\u5f55\u8fdc\u7a0b\u4e3b\u673a\uff0c\u53d1\u73b0\u4e0d\u518d\u9700\u8981\u8f93\u5165\u79c1\u94a5\u5bc6\u7801\u4e86\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre><code>\r\n$ ssh-keygen -p\r\nEnter file in which the key is (\/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa):\r\nKey has comment '\/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa'\r\nEnter new passphrase (empty for no passphrase):\r\nEnter same passphrase again:\r\nYour identification has been saved with the new passphrase.\r\n\r\n$ ssh coderbee@coderbee.net\r\nEnter passphrase for key '\/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa':\r\nWelcome to Ubuntu 12.04 LTS (GNU\/Linux 2.6.32-042stab076.8 i686)\r\n\r\n* Documentation:  https:\/\/help.ubuntu.com\/\r\nLast login: Thu Jun  6 10:35:45 2013 from 183.238.56.177\r\ncoderbee@coderbee:~$ logout\r\nConnection to coderbee.net closed.\r\n\r\n$ eval `ssh-agent`\r\nAgent pid 5820\r\n\r\n$ ssh-add\r\nEnter passphrase for \/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa:\r\nIdentity added: \/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa (\/c\/Documents and Settings\/Administrator\/.ssh\/id_rsa\r\n)\r\n\r\n$ ssh coderbee@coderbee.net\r\nWelcome to Ubuntu 12.04 LTS (GNU\/Linux 2.6.32-042stab076.8 i686)\r\n\r\n* Documentation:  https:\/\/help.ubuntu.com\/\r\nLast login: Thu Jun  6 15:51:26 2013 from 183.238.56.177\r\ncoderbee@coderbee:~$\r\n<\/code><\/pre>\n<p>\u542f\u52a8 ssh-agent \u7684\u6700\u4f73\u65b9\u5f0f\u5c31\u662f\u628a\u4e0a\u9762\u8fd9\u884c\uff08eval <code>ssh-agent<\/code>\uff09\u6dfb\u52a0\u5230\u60a8\u7684 ~\/.bash_profile \u4e2d\uff1b\u8fd9\u6837\uff0c\u5728\u60a8\u7684\u767b\u5f55 shell \u4e2d\u542f\u52a8\u7684\u6240\u6709\u7a0b\u5e8f\u90fd\u5c06\u770b\u5230\u73af\u5883\u53d8\u91cf\uff0c\u800c\u4e14\u80fd\u591f\u5b9a\u4f4d ssh-agent \uff0c\u5e76\u5728\u9700\u8981\u7684\u65f6\u5019\u5411\u5176\u67e5\u8be2\u5bc6\u94a5\u3002\u5c24\u5176\u91cd\u8981\u7684\u73af\u5883\u53d8\u91cf\u662f SSH_AUTH_SOCK\uff1bSSH_AUTH_SOCK \u5305\u542b\u6709 ssh \u548c scp \u53ef\u4ee5\u7528\u6765\u540c ssh-agent \u5efa\u7acb\u5bf9\u8bdd\u7684 UNIX \u57df\u5957\u63a5\u5b57\u7684\u8def\u5f84\u3002<\/p>\n<h4>ssh-agent \u7684\u4e0d\u8db3\u4e4b\u5904<\/h4>\n<p><code>~\/.bash_profile<\/code> \u4e2d\u7684 eval <code>ssh-agent<\/code> \u4f7f\u6bcf\u6b21\u767b\u5f55\u4f1a\u8bdd\u90fd\u4f1a\u542f\u52a8\u4e00\u4e2a\u65b0\u7684 ssh-agent \u526f\u672c\uff0c\u800c\u4e14\u9700\u8981\u5411\u6bcf\u4e2a\u526f\u672c\u6dfb\u52a0\u5bc6\u94a5\u3002<\/p>\n<p>\u6709\u5173 ssh-agent \u7684\u7f3a\u7701\u8bbe\u7f6e\u7684\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u662f\u5b83\u540c cron \u4f5c\u4e1a\u4e0d\u517c\u5bb9\u3002\u7531\u4e8e cron \u4f5c\u4e1a\u662f cron \u8fdb\u7a0b\u542f\u52a8\u7684\uff0c\u8fd9\u4e9b\u4f5c\u4e1a\u65e0\u6cd5\u4ece\u5b83\u4eec\u7684\u73af\u5883\u4e2d\u7ee7\u627f SSH_AUTH_SOCK \u53d8\u91cf\uff0c\u56e0\u800c\u4e5f\u65e0\u4ece\u77e5\u9053 ssh-agent \u8fdb\u7a0b\u6b63\u5728\u8fd0\u884c\u4ee5\u53ca\u5982\u4f55\u540c\u5b83\u8054\u7cfb\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u52a8\u767b\u5f55 SSH\u63d0\u4f9b\u4e86\u516c\u94a5\u767b\u5f55\uff0c\u53ef\u4ee5\u7701\u53bb\u6bcf\u6b21\u767b\u5f55\u90fd\u8981\u8f93\u5165\u7684\u79d8\u5bc6\u7684\u6b65\u9aa4\u3002 \u516c\u94a5\u767b\u5f55 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20130530\/162\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[52],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/162"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":8,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}