{"id":5470,"date":"2026-10-11T09:00:28","date_gmt":"2026-10-11T09:00:28","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=5470"},"modified":"2026-10-11T09:03:57","modified_gmt":"2026-10-11T09:03:57","slug":"linux-scp-copy-files-between-systems","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=5470","title":{"rendered":"Linux SCP &#8211; Copy Files Between Systems"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SCP (Secure Copy Protocol) is a useful command to copy files between systems, we&#8217;ll explore a few example scenarios that you&#8217;ll commonly use. I&#8217;m not going into great detail about all the various arguments and options you can use, these you can find in other articles elsewhere. Instead this is going to be a very high level and quick &#8220;cheat sheet&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In all cases you&#8217;ll need to ensure that the user account you are using on the remote host actually has permissions to read\/write\/change files in the location, or the copy may fail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy File from Local Host to a Remote Host (i.e. Upload)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the file <strong>myfile.txt<\/strong> from my local host to a remote host <strong>192.168.1.10<\/strong> into the remote directory: <strong>\/home\/bobsmith <\/strong>(i.e. Bob Smith&#8217;s home directory on the remote host), logging on as the user <strong>bob<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp myfile.txt bob@192.168.1.10:\/home\/bobsmith<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or putting that in vanilla terms:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp &lt;localfile> &lt;username>@&lt;hostname>:&lt;remotedirectory><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Copy File from Remote Host to Local Host (i.e. Download)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the file <strong>myfile.txt<\/strong> from the remote host <strong>192.168.1.10<\/strong> into my current local directory (that&#8217;s the dot &#8220;.&#8221; at the end of the command), from Bob Smith&#8217;s home directory <strong>\/home\/bobsmith<\/strong> on the remote host, and logging on as the user <strong>bob<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp bob@192.168.1.10:\/home\/bobsmith .<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or putting that in vanilla terms:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp &lt;username>@&lt;hostname>:&lt;remotedirectory> &lt;localdirectory><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Copy File from Remote Host 1 to Remote Host 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ok, so now for a potentially useful one where you can copy a file between two remote hosts. In this example we are copying the <strong>myfile.txt<\/strong> from the remote host <strong>192.168.1.10<\/strong> to the second remote host <strong>192.168.1.11<\/strong> where we are copy from Bob Smith&#8217;s home directory on each of the remote hosts. Bear in mind that you must be able to logon to both of the remote hosts in order to copy the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scp bob@192.168.1.10:\/home\/bobsmith\/myfile.txt bob@192.168.1.11:\/home\/bobsmith\/myfile.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In vanilla terms, that is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scp &lt;username>@&lt;hostname1>:&lt;remotedirectory> &lt;username>@&lt;hostname2>:&lt;remotedirectory><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So bear in mind that since OpenSSH 8.7 that the data is actually routed through your local machine, so you are in effect logging into host1, copying the file via your machine to host2. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may use a method whereby host1 copies direct to host2, by adding the &#8220;-R&#8221; argument, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scp -R bob@192.168.1.10:\/home\/bobsmith\/myfile.txt bob@192.168.1.11:\/home\/bobsmith\/myfile.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, be aware that for this to work, you must be able to logon to the second (destination) host without a password prompt, i.e. using an SSH key authentication.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Extra Options<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some useful extra options:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">-r = Copy a directory and all of its contents (recursive).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp -r \/local\/directory &lt;username>@192.168.1.10:\/remote\/directory<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy a number of files in a single command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp myfile.txt secondfile.log thirdfile.bin bob@192.168.1.10:\/home\/bobsmith<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">-p = Preserve the file&#8217;s metadata (modification time, access time etc.):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp -r myfile.txt  bob@192.168.1.10:\/home\/bobsmith<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And if you want to use SSH key for authentication, it&#8217;s as simple as specifying the key with &#8220;-i&#8221;, obviously you need to have already pre-staged the key on the remote host!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp -i ~\/.ssh\/id_ed25519 myfile.txt  bob@192.168.1.10:\/home\/bobsmith<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Extra Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an extra example, if you wanted to only copy specific file types from a remote host, for example just the log files within a directory, something like the following can be used, but you need to remember that without single quoting the path, your local shell tries to pattern match.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ scp bob@192.168.1.0:'\/var\/log\/*.log' .<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also escape the slashes in the pathname, that has the same effect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SCP (Secure Copy Protocol) is a useful command to copy files between systems, we&#8217;ll explore a few example scenarios that you&#8217;ll commonly use. I&#8217;m not going into great detail about all the various arguments and options you can use, these you can find in other articles elsewhere. Instead this is going to be a very &#8230; <a title=\"Linux SCP &#8211; Copy Files Between Systems\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=5470\" aria-label=\"Read more about Linux SCP &#8211; Copy Files Between Systems\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,20],"tags":[],"class_list":["post-5470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-random"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5470"}],"version-history":[{"count":3,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5470\/revisions"}],"predecessor-version":[{"id":5475,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5470\/revisions\/5475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/5471"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}