{"id":15529,"date":"2024-11-18T20:06:23","date_gmt":"2024-11-18T12:06:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15529"},"modified":"2024-11-18T20:06:23","modified_gmt":"2024-11-18T12:06:23","slug":"linux%e9%85%8d%e7%bd%aessh%e5%85%ac%e9%92%a5%e8%ae%a4%e8%af%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15529\/","title":{"rendered":"linux\u914d\u7f6essh\u516c\u94a5\u8ba4\u8bc1"},"content":{"rendered":"<p>\u5728\u4e24\u53f0linux\u4e3b\u673a\u4e0a\u7531\u4e8e\u73af\u5883\u7684\u9700\u8981\uff0c\u7ecf\u5e38\u8981\u914d\u7f6e\u4e24\u53f0\u4e3b\u673a\u4e4b\u95f4\u514d\u5bc6\u7801\u767b\u5f55\uff0c\u8fd9\u5c31\u8981\u7528\u5230key\u8ba4\u8bc1\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u516c\u79c1\u94a5\u8ba4\u8bc1\u3002\u4fbf\u4e8e\u7406\u89e3\uff0c\u6211\u8fd9\u91cc\u6307\u5b9a\u4e24\u53f0\u4e3b\u673a\u4e3a A \u548c B \u3002\u5982\u679cA\u4e3b\u673a\u60f3\u514d\u5bc6\u7801\u767b\u5f55\u5230B\u4e3b\u673a\u4e0a\uff0c\u5219A\u4e3b\u673a\u4e0a\u5b58\u653e\u79c1\u94a5\uff0cB \u4e3b\u673a\u4e0a\u5b58\u653e\u516c\u94a5\u3002\u901a\u8fc7ssh-keygen \u547d\u4ee4\u751f\u6210\u7684\u4e24\u4e2a\u6587\u4ef6\u4e3a\uff1a\u516c\u94a5\u6587\u4ef6 ~\/.ssh\/id_rsa.pub\uff1b \u79c1\u94a5\u6587\u4ef6 ~\/.ssh\/id_rsa \u3002\u800cB\u4e3b\u673a\u4e0a\u5b58\u653e\u516c\u94a5\u65f6\uff0c\u9700\u8981\u5c06id_rsa.pub\u7684\u5185\u5bb9\u5b58\u653e\u5230~\/.ssh\/authorized_keys \u6587\u4ef6\u5185\uff0c\u5e76\u4e14\u4fdd\u8bc1\u6743\u9650\u4e3a600 \u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u4e00\u3001\u751f\u6210\u548c\u5bfc\u5165KEY<\/h3>\n<p>A\u4e3b\u673a\u4e0a\u751f\u6210key<\/p>\n<pre>$ ssh-keygen -t rsa\r\n$ ls\r\nid_rsa id_rsa.pub<\/pre>\n<p>\u5c06key\u5bfc\u5165\u5230\u8fdc\u7a0b\u7684B\u4e3b\u673a\u4e0a\uff0c\u5e76\u4fee\u6539\u6743\u9650<\/p>\n<p><strong>A\u4e3b\u673a\u4e0a\u64cd\u4f5c<\/strong><\/p>\n<pre>$ cat \/root\/.ssh\/id_rsa.pub | ssh root@xxx.xxx.xxx.xxx 'cat - &gt;&gt; ~\/.ssh\/authorized_keys'<\/pre>\n<p><strong>B\u4e3b\u673a\u4e0a\u64cd\u4f5c<\/strong><\/p>\n<pre>$ chmod 600 ~\/.ssh\/authorized_keys<\/pre>\n<p>\u4e0d\u8fc7\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e0d\u9700\u8981\u5728B\u4e3b\u673a\u4e0a\u518d\u4fee\u6539\u6743\u9650 \uff0c\u800c\u76f4\u63a5\u5c06\u516c\u94a5\u5185\u5bb9\u5bfc\u5165\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\uff0c\u4f7f\u7528ssh-copy-id\u547d\u4ee4\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>$ ssh-copy-id -i \/root\/.ssh\/id_rsa.pub root@xxx.xxx.xxx.xxx<\/pre>\n<h3>\u4e8c\u3001\u914d\u7f6esshd_config<\/h3>\n<p>\u914d\u7f6e\u5b8ckey\u540e\uff0c\u9700\u8981\u5728sshd_config\u6587\u4ef6\u4e2d\u5f00\u542fkey\u8ba4\u8bc1<\/p>\n<pre>$ vim \/etc\/ssh\/sshd_config\r\nPubkeyAuthentication yes \/\/\u5c06\u8be5\u9879\u6539\u4e3ayes<\/pre>\n<p>\u4fee\u6539\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\/etc\/init.d\/sshd restart \u91cd\u542fssh\u670d\u52a1\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u3002\u5982\u679c\u60f3\u8981\u7981\u7528\u5bc6\u7801\u8ba4\u8bc1\uff0c\u66f4\u6539\u5982\u4e0b\u9879\uff1a<\/p>\n<pre>$ vim \/etc\/ssh\/sshd_config\r\n UsePAM yes\r\n \u4e3a\r\n UserPAM no<\/pre>\n<p>\u66f4\u591a\u914d\u7f6e\u53c2\u6570\u53ca\u5176\u610f\u4e49\uff0c\u53ef\u4ee5\u901a\u8fc7man sshd_config \u67e5\u770b\u3002<\/p>\n<h3>\u4e09\u3001ssh_config\u53ca\u591a\u79c1\u94a5\u914d\u7f6e<\/h3>\n<p>sshd_config\u662f\u4e00\u4e2a\u5168\u5c40\u670d\u52a1\u7aef\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5373\u672c\u673a\u5f00\u542fsshd\u670d\u52a1\u7684\u76f8\u5173\u914d\u7f6e\uff09\uff0c\u800cssh_config\u5219\u662f\u4e00\u4e2a\u5168\u5c40\u5ba2\u6237\u7aef\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4f8b\u5982\uff0cssh_config\u4e2d\u5176\u4e2d\u4e24\u884c\u5c31\u5b9a\u4e49\u4e86\u6bcf\u4e2a\u7528\u6237\u4e0b\u9ed8\u8ba4\u79c1\u94a5key\u7684\u8def\u5f84\uff1a<\/p>\n<p># IdentityFile ~\/.ssh\/id_rsa<br \/>\n# IdentityFile ~\/.ssh\/id_dsa<\/p>\n<p>\u5982\u679c\u51fa\u73b0\u591a\u53f0server \u591a\u4e2a\u79c1\u94a5\u6587\u4ef6\uff0c\u5728client\u4e3b\u673a\u4e0a\u600e\u4e48\u914d\u7f6e\u5462\uff1f\u5168\u90e8\u8ffd\u52a0\u5230 ~\/.ssh\/id_rsa\uff08\u6216id_dsa\uff09\u4e2d\uff1f\u7ecf\u6d4b\u8bd5\uff0c\u8fd9\u662f\u884c\u4e0d\u901a\u7684\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u79c1\u94a5\u53ef\u4ee5\u7528\uff0c\u540e\u9762\u7684\u90fd\u4e0d\u884c\u3002<\/p>\n<p>\u5728\u591a\u53f0server\u65f6\uff0c\u53ef\u4ee5\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>1\u3001\u5728\u4e0d\u540c\u7684\u4e3b\u673a\u4e0a\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u516c\u94a5\uff0c\u5219\u8fd9\u4e9b\u673a\u5668\u7684\u79c1\u94a5\u4e5f\u76f8\u540c\u3002\u5ba2\u6237\u7aef\u4e0a\u53ea\u9700\u8981\u914d\u7f6e\u8fd9\u4e00\u4e2a\u79c1\u94a5\u5c31\u53ef\u4ee5\u767b\u5f55\u6240\u6709\u7684\u4e3b\u673a\u3002<\/p>\n<p>2\u3001\u4e0d\u540c\u7684\u4e3b\u673a\u4e0a\u4f7f\u7528\u4e0d\u540c\u7684\u516c\u94a5\u65f6\uff0c \u8fd9\u65f6\u4f1a\u6709\u591a\u4e2a\u4e0d\u540c\u7684\u79c1\u94a5\u3002\u8fd9\u5c31\u9700\u8981\u4e3a\u4e0d\u540c\u7684\u4e3b\u673a\u6307\u5b9a\u4e0d\u540c\u7684\u79c1\u94a5\u6587\u4ef6\uff0c\u8fd9\u4e2a\u914d\u7f6e\u53ef\u4ee5\u5728ssh_config\u4e2d\u914d\u7f6e\uff08\u5177\u4f53\u53ef\u4ee5\u53c2\u770b\u8be5\u6587\u4ef6\u7684\u914d\u7f6e\u6837\u4f8b\uff09\u3002\u5f53\u7136\u66f4\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u4f1a\u5728\u8981\u4f7f\u7528\u7684\u7528\u6237\u505a\u4e2a\u60c5\u5316\u7684\u914d\u7f6e\uff0c\u914d\u7f6e\u6587\u4ef6\u4e3a~\/.ssh\/config\uff08\u8be5\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0c\u8bf7\u521b\u5efa\u4e4b\uff09\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre> Host xxxx\r\n IdentityFile \u79c1\u94a5\u6587\u4ef6\u540d\r\n Port \u7aef\u53e3\u53f7\r\n User \u4f60\u767b\u9646xxxx\u670d\u52a1\u5668\u7528\u7684\u8d26\u53f7<\/pre>\n<p>\u6ce8\uff1a\u8fd9\u91cc\u7684\u914d\u7f6e\u6587\u4ef6\u540c\u6837\u53ef\u4ee5\u53c2\u770bssh_config\u91cc\u7684\u914d\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7man ssh_config\u83b7\u53d6\u66f4\u591a\u6709\u7528\u4fe1\u606f\u3002<\/p>\n<pre> Host *\r\n User www\r\n Port 22\r\n CheckHostIP no\r\n Compression yes\r\n ForwardAgent yes\r\n Host 10.1.100.*\r\n User dev\r\n Port 22\r\n IdentityFile ~\/.ssh\/dev.key<\/pre>\n<p>\u5982\u4e0a\u9762\u7684\u914d\u7f6e\uff0c\u9ed8\u8ba4\u6211\u4eec\u8fde\u63a5\u6240\u6709\u7684\u4e3b\u673a\uff08\u9664\u540e\u9762\u7ed9\u51fa\u768410.1.100.*\u4e4b\u5916\u7684\u6240\u6709\u4e3b\u673a\uff09\u65f6\uff0c\u9ed8\u8ba4\u4f7f\u7528\u6237\u540d\u4e3awww \uff0c\u5373 ssh 172.20.1.100 = ssh www@172.20.1.100 -p 22 \u3002\u5f53\u6211\u4eec\u8fde\u63a510.1.100.* \u4e0b\u7684\u6240\u6709\u4e3b\u673a\u65f6\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528dev\u7528\u6237\uff0c\u9ed8\u8ba4\u7684\u79c1\u94a5\u4f1a\u7528 ~\/.ssh\/dev.key \u6587\u4ef6\u3002\u540c\u7406\uff0c\u53ef\u4ee5\u589e\u52a0\u66f4\u591a\u914d\u7f6e\u3002<\/p>\n<p>\u6ce8\uff1a\u51fa\u4e8e\u5b89\u5168\u7b49\u8003\u8651\uff0c\u4e00\u5b9a\u8981\u5c06 ~\/.ssh\/config\u6587\u4ef6\u548c ~\/.ssh\/key\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u4e3a600<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e24\u53f0linux\u4e3b\u673a\u4e0a\u7531\u4e8e\u73af\u5883\u7684\u9700\u8981\uff0c\u7ecf\u5e38\u8981\u914d\u7f6e\u4e24\u53f0\u4e3b\u673a\u4e4b\u95f4\u514d\u5bc6\u7801\u767b\u5f55\uff0c\u8fd9\u5c31\u8981\u7528\u5230key\u8ba4\u8bc1\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u516c\u79c1\u94a5\u8ba4\u8bc1\u3002\u4fbf\u4e8e\u7406\u89e3\uff0c\u6211\u8fd9\u91cc\u6307\u5b9a\u4e24\u53f0\u4e3b\u673a\u4e3a A \u548c B \u3002\u5982\u679cA\u4e3b\u673a\u60f3\u514d\u5bc6\u7801\u767b\u5f55\u5230B\u4e3b\u673a\u4e0a\uff0c\u5219A\u4e3b\u673a\u4e0a\u5b58\u653e\u79c1\u94a5\uff0cB \u4e3b\u673a\u4e0a\u5b58\u653e\u516c\u94a5\u3002\u901a\u8fc7ssh-keygen \u547d\u4ee4\u751f\u6210\u7684\u4e24\u4e2a\u6587\u4ef6\u4e3a\uff1a\u516c\u94a5\u6587\u4ef6 ~\/.ssh\/id_rsa.pub\uff1b \u79c1\u94a5\u6587\u4ef6 ~\/.ssh\/id_rsa \u3002\u800cB\u4e3b\u673a\u4e0a\u5b58\u653e\u516c\u94a5\u65f6\uff0c\u9700\u8981\u5c06id_rsa.pub\u7684\u5185\u5bb9\u5b58\u653e\u5230~\/.ssh\/authorized_keys \u6587\u4ef6\u5185\uff0c\u5e76\u4e14\u4fdd\u8bc1\u6743\u9650\u4e3a600 \u3002 &nbsp; \u4e00\u3001\u751f\u6210\u548c\u5bfc\u5165KEY A\u4e3b\u673a\u4e0a\u751f\u6210key $ ssh-keygen -t rsa $ ls id_rsa id_rsa.pub \u5c06key\u5bfc\u5165\u5230\u8fdc\u7a0b\u7684B\u4e3b\u673a\u4e0a\uff0c\u5e76\u4fee\u6539\u6743\u9650 A\u4e3b\u673a\u4e0a\u64cd\u4f5c $ cat \/root\/.ssh\/id_rsa.pub | ssh root@xxx.xxx.xxx.xxx &#8216;cat &#8211; &gt;&gt; ~\/.ssh\/authorized_keys&#8217; B\u4e3b\u673a\u4e0a\u64cd\u4f5c $ chmod 600 ~\/.ssh\/authorized_keys \u4e0d\u8fc7\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e0d\u9700\u8981\u5728B\u4e3b\u673a\u4e0a\u518d\u4fee\u6539\u6743\u9650 \uff0c\u800c\u76f4\u63a5\u5c06\u516c\u94a5\u5185\u5bb9\u5bfc\u5165\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\uff0c\u4f7f\u7528ssh-copy-id\u547d\u4ee4\uff0c\u5982\u4e0b\uff1a $ ssh-copy-id -i \/root\/.ssh\/id_rsa.pub root@xxx.xxx.xxx.xxx \u4e8c\u3001\u914d\u7f6esshd_config \u914d\u7f6e\u5b8ckey\u540e\uff0c\u9700\u8981\u5728sshd_config\u6587\u4ef6\u4e2d\u5f00\u542fkey\u8ba4\u8bc1 $ vim \/etc\/ssh\/sshd_config PubkeyAuthentication yes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-15529","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15529"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15529\/revisions"}],"predecessor-version":[{"id":15530,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15529\/revisions\/15530"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}