{"id":70162,"date":"2023-08-04T22:23:24","date_gmt":"2023-08-04T14:23:24","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=70162"},"modified":"2023-08-04T22:23:24","modified_gmt":"2023-08-04T14:23:24","slug":"linux-ssh-withon-passwd","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-ssh-withon-passwd.html","title":{"rendered":"Linux\u4e0bssh\u9ad8\u7ea7\u4f7f\u7528–\u514d\u5bc6\u767b\u5f55"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728linux\u7cfb\u7edf\u4e2d\u5bc6\u7801\u7684\u4fdd\u5b58\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5728\u4f01\u4e1a\u4e2d\u7684WEB\u96c6\u7fa4\u73af\u5883\u4e2d\uff0c\u8fdb\u884c\u7edf\u4e00\u7684\u7248\u672c\u66f4\u65b0\u548c\u914d\u7f6e\u6587\u4ef6\u7684\u540c\u6b65\u514d\u5bc6\u767b\u5f55\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5728\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u7edf\u4e00\u7ba1\u7406\u90fd\u662f\u79bb\u4e0d\u5f00Linux\u7684\u514d\u5bc6\u767b\u5f55\u3002\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e0b\u514d\u5bc6\u767b\u5f55\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.Linux\u4e0b\u751f\u6210\u5bc6\u94a5<\/strong><\/div>\n

ssh-keygen\u7684\u547d\u4ee4\u624b\u518c\uff0c\u901a\u8fc7\u201dman ssh-keygen\u201c\u547d\u4ee4\uff1a
\n\"\"<\/p>\n

\u901a\u8fc7\u547d\u4ee4\u201dssh-keygen -t rsa\u201c
\n\"\"<\/p>\n

\u751f\u6210\u4e4b\u540e\u4f1a\u5728\u7528\u6237\u7684\u6839\u76ee\u5f55\u751f\u6210\u4e00\u4e2a \u201c.ssh\u201d\u7684\u6587\u4ef6\u5939
\n\"\"<\/p>\n

\u8fdb\u5165\u201c.ssh\u201d\u4f1a\u751f\u6210\u4ee5\u4e0b\u51e0\u4e2a\u6587\u4ef6
\n\"\"<\/p>\n

authorized_keys:\u5b58\u653e\u8fdc\u7a0b\u514d\u5bc6\u767b\u5f55\u7684\u516c\u94a5,\u4e3b\u8981\u901a\u8fc7\u8fd9\u4e2a\u6587\u4ef6\u8bb0\u5f55\u591a\u53f0\u673a\u5668\u7684\u516c\u94a5\r\n\r\nid_rsa : \u751f\u6210\u7684\u79c1\u94a5\u6587\u4ef6\r\n\r\nid_rsa.pub \uff1a \u751f\u6210\u7684\u516c\u94a5\u6587\u4ef6\r\n\r\nknow_hosts : \u5df2\u77e5\u7684\u4e3b\u673a\u516c\u94a5\u6e05\u5355\r\n\r\n\u5982\u679c\u5e0c\u671bssh\u516c\u94a5\u751f\u6548\u9700\u6ee1\u8db3\u81f3\u5c11\u4e0b\u9762\u4e24\u4e2a\u6761\u4ef6\uff1a<\/pre>\n

1) .ssh\u76ee\u5f55\u7684\u6743\u9650\u5fc5\u987b\u662f700 2) .ssh\/authorized_keys\u6587\u4ef6\u6743\u9650\u5fc5\u987b\u662f600<\/p><\/blockquote>\n

2.\u8fdc\u7a0b\u514d\u5bc6\u767b\u5f55<\/strong><\/div>\n

\u539f\u7406\u56fe\uff1a<\/p>\n

\"\"<\/p>\n

\u5e38\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n

2.1 \u901a\u8fc7ssh-copy-id\u7684\u65b9\u5f0f<\/strong><\/span><\/div>\n

\u547d\u4ee4\uff1a ssh-copy-id -i ~\/.ssh\/id_rsa.put<\/p><\/blockquote>\n

\u4e3e\u4f8b\uff1a<\/p>\n

[root@test .ssh]# ssh-copy-id -i ~\/.ssh\/id_rsa.pub 192.168.91.135\r\nroot@192.168.91.135's   password:\r\nNow try logging into the machine, with \"ssh '192.168.91.135'\", and check in:\r\n.ssh\/authorized_keys  to make sure we haven't added extra keys that you weren't expecting.\r\n[root@test .ssh]# ssh root@192.168.91.135 \r\nLast login: Mon Oct 10 01:25:49 2016 from 192.168.91.133 \r\n[root@localhost ~]# \r\n<\/pre>\n

\u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n

\r\n[root@test ~]# ssh-copy-id -i ~\/.ssh\/id_rsa.pub 192.168.91.135\r\n-bash: ssh-copy-id: command not found \/\/\u63d0\u793a\u547d\u4ee4\u4e0d\u5b58\u5728\r\n<\/pre>\n

\u89e3\u51b3\u529e\u6cd5\uff1ayum -y install openssh-clients<\/p>\n

2.2\u3000\u901a\u8fc7scp\u5c06\u5185\u5bb9\u5199\u5230\u5bf9\u65b9\u7684\u6587\u4ef6\u4e2d<\/strong><\/span><\/div>\n

\u547d\u4ee4\uff1ascp -p ~\/.ssh\/id_rsa.pub root@:\/root\/.ssh\/authorized_keys<\/p><\/blockquote>\n

\u4e3e\u4f8b\uff1a<\/p>\n

[root@test .ssh]# scp -p ~\/.ssh\/id_rsa.pub root@192.168.91.135:\/root\/.ssh\/authorized_keys\r\n root@192.168.91.135's password:  \r\nid_rsa.pub 100% 408 0.4KB\/s 00:00\r\n  \r\n[root@test .ssh]#   \r\n[root@test .ssh]# ssh root@192.168.91.135\r\nLast login: Mon Oct 10 01:27:02 2016 from 192.168.91.133 \r\n[root@localhost ~]# \r\n<\/pre>\n
2.3 \u901a\u8fc7Ansible\u5b9e\u73b0\u6279\u91cf\u514d\u5bc6<\/strong><\/span><\/div>\n

2.3.1 \u5c06\u9700\u8981\u505a\u514d\u5bc6\u64cd\u4f5c\u7684\u673a\u5668hosts\u6dfb\u52a0\u5230\/etc\/ansible\/hosts\u4e0b\uff1a<\/p>\n

[Avoid close]\r\n192.168.91.132\r\n192.168.91.133\r\n192.168.91.134\r\n<\/pre>\n

2.3.2 \u6267\u884c\u547d\u4ee4\u8fdb\u884c\u514d\u5bc6\u64cd\u4f5c<\/p>\n

ansible -m authorized_key -a \"user=root key='{{ lookup('file','\/root\/.ssh\/id_rsa.pub') }}'\" -k<\/p><\/blockquote>\n

\u793a\u4f8b\uff1a<\/p>\n

[root@test sshpass-1.05]# ansible test -m authorized_key -a \"user=root key='{{ lookup('file','\/root\/.ssh\/id_rsa.pub') }}'\" -k \u3000\u3000\r\nSSH password: ----->\u8f93\u5165\u5bc6\u7801 \u3000\u3000192.168.91.135 | success >>{ \u3000\u3000\r\n\"changed\": true,  \u3000\u3000\r\n\"key\": \"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3\/uhkK7RkaGavBbAlprp5bxp3i0TyNxa\/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1\/18B6FV5moE\/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC\/SJffLuT\/ug\/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM\u3000\u3000WQmpKUltj2nltuv3w== root@localhost.localdomain\",  \r\n\"key_options\": null,  \u3000\u3000\r\n\"keyfile\": \"\/root\/.ssh\/authorized_keys\",  \u3000\u3000\r\n\"manage_dir\": true,  \u3000\u3000\r\n\"path\": null,  \u3000\u3000\r\n\"state\": \"present\",  \u3000\u3000\r\n\"unique\": false,  \u3000\u3000\r\n\"user\": \"root\"\u3000\u3000\r\n} \u3000\u3000\r\n[root@test sshpass-1.05]# \r\n<\/pre>\n
2.4 \u624b\u5de5\u590d\u5236\u7c98\u8d34\u7684\u65b9\u5f0f<\/strong><\/span><\/div>\n

\u5c06\u672c\u5730id_rsa.pub\u6587\u4ef6\u7684\u5185\u5bb9\u62f7\u8d1d\u81f3\u8fdc\u7a0b\u670d\u52a1\u5668\u7684~\/.ssh\/authorized_keys\u6587\u4ef6\u4e2d<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.jb51.net\/article\/94599.htm<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/linux-ssh-withon-passwd.html<\/a>\u7f16\u8f91\uff1a\u9676\u6b66\u6770\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u5728linux\u7cfb\u7edf\u4e2d\u5bc6\u7801\u7684\u4fdd\u5b58\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5728\u4f01\u4e1a\u4e2d\u7684WEB\u96c6\u7fa4\u73af\u5883\u4e2d\uff0c\u8fdb\u884c\u7edf\u4e00\u7684\u7248\u672c\u66f4\u65b0\u548c\u914d\u7f6e\u6587\u4ef6\u7684\u540c\u6b65 […]<\/p>\n","protected":false},"author":63,"featured_media":70176,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-70162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/70162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=70162"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/70162\/revisions"}],"predecessor-version":[{"id":70232,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/70162\/revisions\/70232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/70176"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=70162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=70162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=70162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}