<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>gHacks Technology News &#124; Latest Tech News, Software And Tutorials &#187; ssh key authentication</title> <atom:link href="http://www.ghacks.net/tag/ssh-key-authentication/feed/" rel="self" type="application/rss+xml" /><link>http://www.ghacks.net</link> <description>A technology news blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description> <lastBuildDate>Sat, 11 Feb 2012 17:32:23 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/> <item><title>Securely copy files with scp</title><link>http://www.ghacks.net/2009/04/11/securely-copy-files-with-scp/</link> <comments>http://www.ghacks.net/2009/04/11/securely-copy-files-with-scp/#comments</comments> <pubDate>Sat, 11 Apr 2009 17:57:25 +0000</pubDate> <dc:creator>Jack Wallen</dc:creator> <category><![CDATA[Advice]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Networks]]></category> <category><![CDATA[Open Source]]></category> <category><![CDATA[Security]]></category> <category><![CDATA[Tutorials Advanced]]></category> <category><![CDATA[scp]]></category> <category><![CDATA[ssh]]></category> <category><![CDATA[ssh key authentication]]></category> <category><![CDATA[ssh-keygen]]></category> <guid
isPermaLink="false">http://www.ghacks.net/?p=11915</guid> <description><![CDATA[I have to copy files from machine to machine all the time. Most of the time this copying is done over a network connection. When using that transport method I always like to ensure my copying is being done securely. Fortunately Linux has an app for that (sorry, couldn&#8217;t resist). That app is scp. Scp [...]]]></description> <content:encoded><![CDATA[<p>I have to copy files from machine to machine all the time. Most of the time this copying is done over a network connection. When using that transport method I always like to ensure my copying is being done securely. Fortunately Linux has an app for that (sorry, couldn&#8217;t resist). That app is scp.</p><p>Scp is a part of the secure shell application. If you have installed ssh (secure shell) then you have scp installed on your machine. The only problem with scp is figuring out exactly how to use it. It&#8217;s not predictable and the man page is absolutely no help. That&#8217;s where gHacks comes in. In this article you will learn how to securely copy files from one machine to another using scp.</p><p><span
id="more-11915"></span><strong>The Setup</strong></p><p>Let&#8217;s first get out of the way the setup of a test network. We&#8217;ll use MachineA, with an IP address of 192.168.1.1, for the local machine and MachineB, with an IP address of 192.168.1.2, for the remote machine. Both machines will be Linux machines and both will have ssh installed. For the examples we will be copying the file sample.pdf and the directory <strong>/home/jlwallen/TEMP</strong>. The username we will use is jlwallen.</p><p><strong>The syntax</strong></p><p>The syntax of the scp command is basically:</p><p>scp FILENAME USERNAME@ADDRESS_OF_REMOTE_SERVER:FILENAME</p><p>One very important issue is that the FILENAME should be the full path to the file to be copied.</p><p><strong>Copy file from A to B (While logged into A)</strong></p><p>To copy sample.pdf from A to B when you&#8217;re logged into A issue the command:</p><p>scp /home/jlwallen/sample.pdf jlwallen@192.168.1.2:/home/jlwallen/sample.pdf</p><p>You will be prompted for jlwallen&#8217;s password. Once you enter that password the copy will occur.</p><p><strong>Copy file from A to B (While logged into B)</strong></p><p>scp jlwallen@192.168.1.1:/home/jlwallen/sample.pdf /home/jlwallen/sample.pdf</p><p>You will be prompted for jlwallen&#8217;s password.</p><p><strong>Copy Directory from A to B(While logged into A)<br
/> </strong></p><p>scp -r /home/jlwallen/TEMP jlwallen@192.168.1.2:/home/jlwallen/TEMP</p><p>You will be prompted for jlwallen&#8217;s password. This command will copy the entire contents of the TEMP directory.</p><p><strong>Copy Director from B to A (While logged into B)<br
/> </strong></p><p>scp -r jlwallen@192.168.1.1:/home/jlwallen/TEMP /home/jlwallen/TEMP</p><p>Pretty simple stuff once you actually know the structure of the command.</p><p><strong>Make it passwordless</strong></p><p>If you&#8217;re like me you like to automate things. You can do this with scp but you have to set it up so you can log in without passwords. The best way to do this is by using keys. Here is how it is done:</p><p>On the local machine generate a keypair with the following:</p><p>ssh-keygen -t rsa</p><p>You will accept the defaults and just hit enter. Do not give this a passphrase.</p><p>This will generate two files in the ~/.ssh directory: id_rsa and id_rsa.pub. You need to first give your id_rsa the right permissions with the command chmod 700 ~/.ssh/id_rsa. Now you need to copy the id_rsa.pub file over to the server you want to log into. Do this with the command:</p><p><em>scp ~/.ssh/id_rsa.pub 192.168.1.2:~/.ssh/authorized_keys</em></p><p>Now log into the remote machine (via ssh) and make sure the ~/.ssh directory has the right permissions with the command <em>chmod 700 .ssh</em></p><p>The next step is to configure ssh and sshd. On the local machine open up the file <strong>/etc/ssh/ssh_config </strong>and look for the line:</p><p><em>ForwardAgent yes</em></p><p>This line will most likely be commented out. Remove the &#8220;#&#8221; character and save the file.</p><p>Now on the remote machine open up the <strong>/etc/ssh/sshd_config</strong> file and make sure you have the following lines:</p><p>RSAAuthentication yes<br
/> PubkeyAuthentication yes<br
/> AuthorizedKeysFile????? .ssh/authorized_keys</p><p>Save that file and restart sshd with the command <em>/etc/rc.d/init.d/sshd restart</em></p><p>Back on the local machine issue the two following commands:</p><p>ssh-agent</p><p>ssh-add</p><p>You shouldn&#8217;t be prompted for a password for the second command.</p><p>Now attempt to ssh to the remote machine like so:</p><p>ssh 192.168.1.2</p><p>You should not be prompted for a password. You are now able to ssh and scp without having to enter a password.</p><p><strong>Final Thoughts</strong></p><p>Now that you know how to scp (and do so without user intervention), you can create all sorts of fun automated backup scripts that will backup a local machine to a remote server. There is one warning I will issue: if someone can get your <strong>id_rsa </strong>file they might be able to get into your machine. So make sure the machine that holds that file is safe.</p> ]]></content:encoded> <wfw:commentRss>http://www.ghacks.net/2009/04/11/securely-copy-files-with-scp/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
