{"id":6123,"date":"2016-11-15T16:43:40","date_gmt":"2016-11-15T08:43:40","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6123"},"modified":"2016-11-15T16:43:59","modified_gmt":"2016-11-15T08:43:59","slug":"6123","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6123.html","title":{"rendered":"\u4f7f\u7528SSH\u53cd\u5411\u96a7\u9053\u8fdb\u884c\u5185\u7f51\u7a7f\u900f"},"content":{"rendered":"<h1>\u5bf9\u5e94\u7684\u60c5\u51b5<\/h1>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528SSH \u53cd\u5411\u96a7\u9053\u7a7f\u900fNAT\uff0c\u5e76\u6f14\u793a\u4e86\u5982\u4f55\u7ef4\u6301\u4e00\u6761\u7a33\u5b9a\u7684SSH \u96a7\u9053\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u673a\u5668A \u548cB\uff0cA \u6709\u516c\u7f51IP\uff0cB \u4f4d\u4e8eNAT \u4e4b\u540e\u5e76\u65e0\u53ef\u7528\u7684\u7aef\u53e3\u8f6c\u53d1\uff0c\u73b0\u5728\u60f3\u7531A \u4e3b\u52a8\u5411B \u53d1\u8d77SSH \u8fde\u63a5\u3002\u7531\u4e8eB \u5728NAT \u540e\u7aef\uff0c\u65e0\u53ef\u7528\u516c\u7f51IP + \u7aef\u53e3 \u8fd9\u6837\u4e00\u4e2a\u7ec4\u5408\uff0c\u6240\u4ee5A \u65e0\u6cd5\u7a7f\u900fNAT\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5e94\u5bf9\u7684\u5c31\u662f\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u9996\u5148\u6709\u5982\u4e0b\u7ea6\u5b9a\uff0c\u56e0\u4e3a\u5f88\u91cd\u8981\u6240\u4ee5\u653e\u5728\u524d\u9762\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u673a\u5668\u4ee3\u53f7<\/th>\n<th>\u673a\u5668\u4f4d\u7f6e<\/th>\n<th>\u5730\u5740<\/th>\n<th>\u8d26\u6237<\/th>\n<th>ssh\/sshd \u7aef\u53e3<\/th>\n<th>\u662f\u5426\u9700\u8981\u8fd0\u884csshd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>A<\/td>\n<td>\u4f4d\u4e8e\u516c\u7f51<\/td>\n<td>a.site<\/td>\n<td>usera<\/td>\n<td>22<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>\u4f4d\u4e8eNAT \u4e4b\u540e<\/td>\n<td>127.0.0.1<\/td>\n<td>userb<\/td>\n<td>22<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>\u4f4d\u4e8eNAT \u4e4b\u540e<\/td>\n<td>127.0.0.1<\/td>\n<td>userc<\/td>\n<td>22<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u9ed8\u8ba4\u4f60\u7684\u7cfb\u7edfinit \u7a0b\u5e8f\u4e3asystemd\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u5176\u4ed6\u7684init \u7a0b\u5e8f\uff0c\u5982\u679c\u6ca1\u6709\u7279\u6b8a\u7406\u7531\u8fd8\u662f\u6362\u5230\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684GNU\/Linux \u7cfb\u7edf\u5427\u2026\u2026<\/p>\n<h1>SSH \u53cd\u5411\u96a7\u9053<\/h1>\n<p>\u8fd9\u79cd\u624b\u6bb5\u5b9e\u8d28\u4e0a\u662f\u7531B \u5411A \u4e3b\u52a8\u5730\u5efa\u7acb\u4e00\u4e2aSSH \u96a7\u9053\uff0c\u5c06A \u76846766 \u7aef\u53e3\u8f6c\u53d1\u5230B \u768422 \u7aef\u53e3\u4e0a\uff0c\u53ea\u8981\u8fd9\u6761\u96a7\u9053\u4e0d\u5173\u95ed\uff0c\u8fd9\u4e2a\u8f6c\u53d1\u5c31\u662f\u6709\u6548\u7684\u3002\u6709\u4e86\u8fd9\u4e2a\u7aef\u53e3\u8f6c\u53d1\uff0c\u53ea\u9700\u8981\u8bbf\u95eeA \u76846766 \u7aef\u53e3\u53cd\u5411\u8fde\u63a5B \u5373\u53ef\u3002<\/p>\n<p>\u9996\u5148\u5728B \u4e0a\u5efa\u7acb\u4e00\u4e2aSSH \u96a7\u9053\uff0c\u5c06A \u76846766 \u7aef\u53e3\u8f6c\u53d1\u5230B \u768422 \u7aef\u53e3\u4e0a\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ ssh -p 22 -qngfNTR 6766:127.0.0.1:22 usera@a.site\r\n<\/pre>\n<p>\u7136\u540e\u5728A \u4e0a\u5229\u75286766 \u7aef\u53e3\u53cd\u5411SSH \u5230B\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nA $ ssh -p 6766 userb@127.0.0.1\r\n<\/pre>\n<p>\u8981\u505a\u7684\u4e8b\u60c5\u5176\u5b9e\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<h1>\u96a7\u9053\u7684\u7ef4\u6301<\/h1>\n<p>\u7a33\u5b9a\u6027\u7ef4\u6301<\/p>\n<p>\u7136\u800c\u4e0d\u5e78\u7684\u662fSSH \u8fde\u63a5\u662f\u4f1a\u8d85\u65f6\u5173\u95ed\u7684\uff0c\u5982\u679c\u8fde\u63a5\u5173\u95ed\uff0c\u96a7\u9053\u65e0\u6cd5\u7ef4\u6301\uff0c\u90a3\u4e48A \u5c31\u65e0\u6cd5\u5229\u7528\u53cd\u5411\u96a7\u9053\u7a7f\u900fB \u6240\u5728\u7684NAT \u4e86\uff0c\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u6848\u6765\u63d0\u4f9b\u4e00\u6761\u7a33\u5b9a\u7684SSH \u53cd\u5411\u96a7\u9053\u3002<\/p>\n<p>\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662fautossh\uff0c\u8fd9\u4e2a\u8f6f\u4ef6\u4f1a\u5728\u8d85\u65f6\u4e4b\u540e\u81ea\u52a8\u91cd\u65b0\u5efa\u7acbSSH \u96a7\u9053\uff0c\u8fd9\u6837\u5c31\u89e3\u51b3\u4e86\u96a7\u9053\u7684\u7a33\u5b9a\u6027\u95ee\u9898\uff0c\u5982\u679c\u4f60\u4f7f\u7528Arch Linux\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u83b7\u5f97\u5b83\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ sudo pacman -S autossh\r\n<\/pre>\n<p>\u4e0b\u9762\u5728B \u4e0a\u505a\u4e4b\u524d\u7c7b\u4f3c\u7684\u4e8b\u60c5\uff0c\u4e0d\u540c\u7684\u662f\u8be5\u96a7\u9053\u4f1a\u7531autossh \u6765\u7ef4\u6301\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ autossh -p 22 -M 6777 -NR 6766:127.0.0.1:22 usera@a.site\r\n<\/pre>\n<p>-M \u53c2\u6570\u6307\u5b9a\u7684\u7aef\u53e3\u7528\u6765\u76d1\u542c\u96a7\u9053\u7684\u72b6\u6001\uff0c\u4e0e\u7aef\u53e3\u8f6c\u53d1\u65e0\u5173\u3002<\/p>\n<p>\u4e4b\u540e\u4f60\u53ef\u4ee5\u5728A \u4e0a\u901a\u8fc76766 \u7aef\u53e3\u8bbf\u95eeB \u4e86\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nA $ ssh -p 6766 userb@127.0.0.1\r\n<\/pre>\n<p>\u96a7\u9053\u7684\u81ea\u52a8\u5efa\u7acb<\/p>\n<p>\u7136\u800c\u8fd9\u53c8\u6709\u4e86\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679cB \u91cd\u542f\u96a7\u9053\u5c31\u4f1a\u6d88\u5931\u3002\u90a3\u4e48\u9700\u8981\u6709\u4e00\u79cd\u624b\u6bb5\u5728B \u6bcf\u6b21\u542f\u52a8\u65f6\u4f7f\u7528autossh \u6765\u5efa\u7acbSSH \u96a7\u9053\u3002\u5f88\u81ea\u7136\u7684\u4e00\u4e2a\u60f3\u6cd5\u5c31\u662f\u505a\u6210\u670d\u52a1\uff0c\u4e4b\u540e\u4f1a\u7ed9\u51fa\u5728systemd \u4e0b\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h1>\u201c\u6253\u6d1e\u201d<\/h1>\n<p>\u4e4b\u6240\u4ee5\u6807\u9898\u8fd9\u4e48\u8d77\uff0c\u662f\u56e0\u4e3a\u81ea\u5df1\u89c9\u5f97\u8fd9\u4ef6\u4e8b\u60c5\u6709\u70b9\u7c7b\u4f3c\u4e8eUDP \u6253\u6d1e\uff0c\u5373\u901a\u8fc7\u4e00\u53f0\u5728\u516c\u7f51\u7684\u673a\u5668\uff0c\u8ba9\u4e24\u53f0\u5206\u522b\u4f4d\u4e8e\u5404\u81eaNAT \u4e4b\u540e\u7684\u673a\u5668\u53ef\u4ee5\u5efa\u7acbSSH \u8fde\u63a5\u3002<\/p>\n<p>\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u4f7f\u7528SSH \u53cd\u5411\u96a7\u9053\uff0c\u8ba9C \u8fde\u63a5\u5230B\u3002<\/p>\n<p>\u9996\u5148\u5728A \u4e0a\u7f16\u8f91sshd \u7684\u914d\u7f6e\u6587\u4ef6\/etc\/ssh\/sshd_config\uff0c\u5c06GatewayPorts \u5f00\u5173\u6253\u5f00\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nGatewayPorts yes\r\n<\/pre>\n<p>\u7136\u540e\u91cd\u542fsshd\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nA $ sudo systemctl restart sshd\r\n<\/pre>\n<p>\u7136\u540e\u5728B \u4e0a\u5bf9\u4e4b\u524d\u7528\u5230\u7684autossh \u6307\u4ee4\u7565\u52a0\u4fee\u6539\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ autossh -p 22 -M 6777 -NR '*:6766:127.0.0.1:22' usera@a.site\r\n<\/pre>\n<p>\u4e4b\u540e\u5728C \u4e0a\u5229\u7528A \u76846766 \u7aef\u53e3SSH \u8fde\u63a5\u5230B\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC $ ssh -p 6766 userb@a.site\r\n<\/pre>\n<p>\u81f3\u6b64\u4f60\u5df2\u7ecf\u8f7b\u800c\u6613\u4e3e\u7684\u7a7f\u900f\u4e86\u4e24\u5c42NAT\u3002<\/p>\n<h1>\u6700\u7ec8\u7684\u89e3\u51b3\u65b9\u6848<\/h1>\n<p>\u6574\u5408\u4e00\u4e0b\u524d\u9762\u63d0\u5230\u7684\uff0c\u6700\u7ec8\u7684\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n<p>\u9996\u5148\u6253\u5f00A \u4e0asshd \u7684GatewayPorts \u5f00\u5173\uff0c\u5e76\u91cd\u542fsshd\uff08\u5982\u6709\u9700\u8981\uff09\u3002<\/p>\n<p>\u7136\u540e\u5728B \u4e0a\u65b0\u5efa\u4e00\u4e2a\u7528\u6237autossh\uff0c\u6839\u636e\u6743\u9650\u6700\u5c0f\u5316\u601d\u60f3\uff0cB \u4e0a\u7684autossh \u670d\u52a1\u5c06\u4ee5autossh \u7528\u6237\u7684\u8eab\u4efd\u8fd0\u884c\uff0c\u4ee5\u5c3d\u5927\u53ef\u80fd\u907f\u514d\u51fa\u73b0\u5b89\u5168\u95ee\u9898\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ sudo useradd -m autossh\r\nB $ sudo passwd autossh\r\n<\/pre>\n<p>\u7d27\u63a5\u7740\u5728B \u4e0a\u4e3aautossh \u7528\u6237\u521b\u5efaSSH \u5bc6\u94a5\uff0c\u5e76\u4e0a\u4f20\u5230A\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ su - autossh\r\nB $ ssh-keygen -t 'rsa' -C 'autossh@B'\r\nB $ ssh-copy-id usera@a.site\r\n<\/pre>\n<p>\u6ce8\u610f\u8be5\u5bc6\u94a5\u4e0d\u8981\u8bbe\u7f6e\u5bc6\u7801\uff0c\u4e5f\u5c31\u662f\u8fd0\u884cssh-keygen \u6307\u4ee4\u65f6\u5c3d\u7ba1\u4e00\u8def\u56de\u8f66\uff0c\u4e0d\u8981\u8f93\u5165\u989d\u5916\u7684\u5b57\u7b26\u3002<\/p>\n<p>\u7136\u540e\u5728B \u4e0a\u521b\u5efa\u4ee5autossh \u7528\u6237\u6743\u9650\u8c03\u7528autossh \u7684service \u6587\u4ef6\u3002\u5c06\u4e0b\u9762\u6587\u672c\u5199\u5165\u5230\u6587\u4ef6\/lib\/systemd\/system\/autossh.service\uff0c\u5e76\u8bbe\u7f6e\u6743\u9650\u4e3a644\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Unit]\r\nDescription=Auto SSH Tunnel\r\nAfter=network-online.target\r\n\r\n&#x5B;Service]\r\nUser=autossh\r\nType=simple\r\nExecStart=\/bin\/autossh -p 22 -M 6777 -NR '*:6766:127.0.0.1:22' usera@a.site -i \/home\/autossh\/.ssh\/id_rsa\r\nExecReload=\/bin\/kill -HUP $MAINPID\r\nKillMode=process\r\nRestart=always\r\n\r\n&#x5B;Install]\r\nWantedBy=multi-user.target\r\nWantedBy=graphical.target\r\n<\/pre>\n<p>\u5728B \u4e0a\u8bbe\u7f6e\u8be5\u670d\u52a1\u81ea\u52a8\u542f\u52a8\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ sudo systemctl enable autossh\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u613f\u610f\uff0c\u5728\u8fd9\u4e4b\u540e\u53ef\u4ee5\u7acb\u523b\u542f\u52a8\u5b83\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nB $ sudo systemctl start autossh\r\n<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u5728A \u4e0a\u4f7f\u7528\u8fd9\u6761\u53cd\u5411\u96a7\u9053\u7a7f\u900fB \u6240\u5728\u7684NAT SSH \u8fde\u63a5\u5230B\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nA $ ssh -p 6766 userb@127.0.0.1\r\n<\/pre>\n<p>\u6216\u8005\u662f\u5728C \u4e0a\u76f4\u63a5\u7a7f\u900f\u4e24\u5c42NAT SSH \u8fde\u63a5\u5230B\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC $ ssh -p 6766 userb@a.site\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u5bf9SSH \u8db3\u591f\u719f\u6089\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u6761\u96a7\u9053\u505a\u66f4\u591a\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982\u4f60\u53ef\u4ee5\u5728\u53cd\u5411\u8fde\u63a5\u65f6\u6307\u5b9a\u52a8\u6001\u7aef\u53e3\u8f6c\u53d1\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC $ ssh -p 6766 -qngfNTD 7677 userb@a.site\r\n<\/pre>\n<p>\u5047\u8bbeC \u662f\u4f60\u5bb6\u4e2d\u7684\u7535\u8111\uff0cA \u662f\u4f60\u7684VPS\uff0cB \u662f\u4f60\u516c\u53f8\u7684\u7535\u8111\u3002\u5982\u679c\u4f60\u8fd9\u6837\u505a\u4e86\uff0c\u90a3\u4e48\u4e3a\u6d4f\u89c8\u5668\u8bbe\u7f6e\u7aef\u53e3\u4e3a7677 \u7684sock4 \u672c\u5730\uff08127.0.0.1\uff09\u4ee3\u7406\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u5bb6\u91cc\u7684\u6d4f\u89c8\u5668\u4e0a\u770b\u5230\u516c\u53f8\u5185\u7f51\u7684\u7f51\u9875\u3002<\/p>\n<p>\u539f\u6587\uff1ahttp:\/\/arondight.me\/2016\/02\/17\/%E4%BD%BF%E7%94%A8SSH%E5%8F%8D%E5%90%91%E9%9A%A7%E9%81%93%E8%BF%9B%E8%A1%8C%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u5e94\u7684\u60c5\u51b5 \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528SSH \u53cd\u5411\u96a7\u9053\u7a7f\u900fNAT\uff0c\u5e76\u6f14\u793a\u4e86\u5982\u4f55\u7ef4\u6301\u4e00\u6761\u7a33\u5b9a\u7684SSH \u96a7\u9053\u3002 \u5047 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6123","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6123"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6123\/revisions"}],"predecessor-version":[{"id":6125,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6123\/revisions\/6125"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}