{"id":478,"date":"2013-11-19T12:18:20","date_gmt":"2013-11-19T12:18:20","guid":{"rendered":"https:\/\/shan.info\/new\/how-to-scp-ssh-and-rsync-without-prompting-for-password\/"},"modified":"2013-11-19T12:18:20","modified_gmt":"2013-11-19T12:18:20","slug":"how-to-scp-ssh-and-rsync-without-prompting-for-password","status":"publish","type":"post","link":"https:\/\/shan.info\/new\/how-to-scp-ssh-and-rsync-without-prompting-for-password\/","title":{"rendered":"How To scp, ssh and rsync without prompting for password"},"content":{"rendered":"<p style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin-top: 10px; margin-bottom: 10px; color: #555555; font-size: 12px; line-height: 18px;\"><span style=\"font-size: small;\">Whenever you need to use&nbsp;scp&nbsp;to copy files, it asks for passwords. Same with&nbsp;rsync&nbsp;as it&nbsp;<span style=\"font-size: x-small;\">(by default)<\/span>&nbsp;uses&nbsp;ssh&nbsp;as well. Usually&nbsp;scp&nbsp;and&nbsp;rsync&nbsp;commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an&nbsp;expect&nbsp;script to provide the password. Of course, I didn&#8217;t. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it&#8230;<\/span><\/p>\n<p style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin-top: 10px; margin-bottom: 10px; color: #555555; font-size: 12px; line-height: 18px;\"><span style=\"font-size: small;\">Lets say you want to copy between two hosts&nbsp;<\/span><strong><span style=\"font-size: small;\">host_src<\/span><\/strong>&nbsp;<span style=\"font-size: small;\">and&nbsp;<\/span><strong><span style=\"font-size: small;\">host_dest<\/span><\/strong><span style=\"font-size: small;\">.&nbsp;<\/span><strong><span style=\"font-size: small;\">host_src<\/span><\/strong><strong>&nbsp;<\/strong><span style=\"font-size: small;\">is the host where you would run the&nbsp;<\/span><span style=\"font-size: small;\">scp<\/span><span style=\"font-size: small;\">,<\/span>&nbsp;<span style=\"font-size: small;\">ssh<\/span>&nbsp;<span style=\"font-size: small;\">or<\/span>&nbsp;<span style=\"font-size: small;\">rsyn<\/span>&nbsp;<span style=\"font-size: small;\">command,&nbsp;<\/span><strong><span style=\"font-size: small;\">irrespective of the direction of the file copy<\/span><\/strong><span style=\"font-size: small;\">!<\/span><\/p>\n<ol style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin: 10px 0px 10px 17px; color: #555555; font-size: 12px; line-height: 18px;\">\n<li style=\"margin-left: 15px;\">\n<ol style=\"list-style-type: upper-alpha;\">\n<li><strong><span style=\"font-size: small;\">host_src<\/span><\/strong><span style=\"font-size: small;\">, run this command as the user that<\/span><span style=\"font-size: small;\">scp<\/span><span style=\"font-size: small;\">\/<\/span><span style=\"font-size: small;\">ssh<\/span><span style=\"font-size: small;\">\/<\/span><span style=\"font-size: small;\">rsync<\/span><\/li>\n<\/ol>\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">$ ssh-keygen -t rsa<\/span><\/p>\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">This will prompt for a passphrase. Just press the enter key. It&#8217;ll then generate an identification (private key) and a public key. Do not ever share the private key with anyone!&nbsp;ssh-keygen&nbsp;shows where it saved the public key. This is by default~\/.ssh\/id_rsa.pub:<\/span><\/p>\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">Your public key has been saved in &lt;your_home_dir&gt;\/.ssh\/id_rsa.pub<\/span><\/p>\n<\/li>\n<\/ol>\n<ol style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin: 10px 0px 10px 17px; color: #555555; font-size: 12px; line-height: 18px;\" start=\"2\">\n<li style=\"margin-left: 15px;\">\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">Transfer the<\/span>&nbsp;<span style=\"font-size: small;\">id_rsa.pub<\/span>&nbsp;<span style=\"font-size: small;\">file to<\/span>&nbsp;<span style=\"font-size: small;\">host_dest<\/span>&nbsp;<span style=\"font-size: small;\">by either<\/span>&nbsp;<span style=\"font-size: small;\">ftp<\/span><span style=\"font-size: small;\">,&nbsp;<\/span><span style=\"font-size: small;\">scp<\/span><span style=\"font-size: small;\">,<\/span>&nbsp;<span style=\"font-size: small;\">rsync<\/span>&nbsp;<span style=\"font-size: small;\">or any other method.<\/span><\/p>\n<\/li>\n<\/ol>\n<ol style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin: 10px 0px 10px 17px; color: #555555; font-size: 12px; line-height: 18px;\" start=\"3\">\n<li style=\"margin-left: 15px;\">\n<ol style=\"list-style-type: upper-alpha;\">\n<li><strong><span style=\"font-size: small;\">host_dest<\/span><\/strong><span style=\"font-size: small;\">, login as the remote user which you plan to use when you<\/span><span style=\"font-size: small;\">scp<\/span><span style=\"font-size: small;\">,<\/span><span style=\"font-size: small;\">ssh<\/span>&nbsp;<span style=\"font-size: small;\">rsync<\/span>&nbsp;<strong><span style=\"font-size: small;\">host_src<\/span><\/strong><span style=\"font-size: small;\">.<\/span><\/li>\n<\/ol>\n<\/li>\n<li style=\"margin-left: 15px;\">\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">Copy the contents of&nbsp;<\/span><span style=\"font-size: small;\">id_rsa.pub<\/span>&nbsp;<span style=\"font-size: small;\">to&nbsp;<\/span><span style=\"font-size: small;\">~\/.ssh\/authorized_keys<span style=\"color: #ff0000;\">2<\/span><\/span><\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">$ cat id_rsa.pub &gt;&gt;~\/.ssh\/authorized_keys<span style=\"color: #ff0000;\">2<\/span><br \/>$ chmod 700 ~\/.ssh\/authorized_keys<span style=\"color: #ff0000;\">2<\/span><\/span><\/p>\n<p style=\"margin-top: 10px; margin-bottom: 10px;\"><span style=\"font-size: small;\">If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.<\/span><\/p>\n<ol style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin: 10px 0px 10px 17px; color: #555555; font-size: 12px; line-height: 18px;\" start=\"5\">\n<li style=\"margin-left: 15px;\">\n<p style=\"margin-top: 10px; margin-bottom: 10px;\">Note that&nbsp;<strong>ssh<\/strong>&nbsp;by default does not allow root to log in. This has to be explicitly enabled on&nbsp;<strong>host_dest<\/strong>. This can be done by editing&nbsp;<strong>\/etc\/ssh\/sshd_config<\/strong>&nbsp;and changing the option of&nbsp;<strong>PermitRootLogin<\/strong>from&nbsp;<strong>no<\/strong>&nbsp;to&nbsp;<strong>yes<\/strong>. Don&#8217;t forget to restart&nbsp;<strong>sshd<\/strong>&nbsp;so that it reads the modified config file. Do this&nbsp;<i><strong>only<\/strong><\/i>&nbsp;if you want to use the root login.<\/p>\n<\/li>\n<\/ol>\n<p style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; margin-top: 10px; margin-bottom: 10px; color: #555555; font-size: 12px; line-height: 18px;\">Well, thats it. Now you can run&nbsp;scp,&nbsp;ssh&nbsp;and&nbsp;rsync&nbsp;on&nbsp;<strong>host_src<\/strong>&nbsp;connecting to&nbsp;<strong>host_dest<\/strong>&nbsp;and it won&#8217;t prompt for the password. Note that this will still prompt for the password if you are running the commands on&nbsp;host_dest&nbsp;connecting to&nbsp;<strong>host_src<\/strong>. You can reverse the steps above (generate the public key on&nbsp;<strong>host_dest<\/strong>&nbsp;and copy it to&nbsp;<strong>host_src<\/strong>) and you have a two way setup ready!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever you need to use&nbsp;scp&nbsp;to copy files, it asks for passwords. Same with&nbsp;rsync&nbsp;as it&nbsp;(by default)&nbsp;uses&nbsp;ssh&nbsp;as well. Usually&nbsp;scp&nbsp;and&nbsp;rsync&nbsp;commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an&nbsp;expect&nbsp;script [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-centos-6"],"_links":{"self":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":0,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"wp:attachment":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}