{"id":11360,"date":"2019-03-13T07:14:21","date_gmt":"2019-03-13T07:14:21","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11360"},"modified":"2019-03-13T07:14:21","modified_gmt":"2019-03-13T07:14:21","slug":"10-scp-commands-to-transfer-files-folders-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/10-scp-commands-to-transfer-files-folders-in-linux\/","title":{"rendered":"10 SCP Commands to Transfer Files\/Folders in Linux"},"content":{"rendered":"<p>Linux administrator should be familiar with\u00a0<strong>CLI<\/strong>\u00a0environment. Since\u00a0<strong>GUI<\/strong>\u00a0mode in Linux servers is not a common to be installed.\u00a0<strong>SSH<\/strong>\u00a0may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with\u00a0<strong>SSH<\/strong>\u00a0command there is\u00a0<strong>SCP<\/strong>\u00a0command.\u00a0<strong>SCP<\/strong>\u00a0is used to copy file(s) between servers in secure way.<\/p>\n<div id=\"attachment_4338\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/scp-commands-examples\/scp-commands\/\" rel=\"attachment wp-att-4338\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4338\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/SCP-Commands.png\" alt=\"Linux SCP Commands\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">10 Linux SCP Commands<\/p>\n<\/div>\n<p>The below command will read as \u201c<strong>copy source_file_name<\/strong>\u201d into \u201c<strong>destination_folder<\/strong>\u201d at \u201c<strong>destination_host<\/strong>\u201d using \u201c<strong>username account<\/strong>\u201d.<\/p>\n<h5>Basic syntax of SCP<\/h5>\n<pre>scp source_file_name username@destination_host:destination_folder<\/pre>\n<p>There are much parameters in\u00a0<strong>SCP<\/strong>\u00a0command that you can use. Here are the parameters that may useful on daily basis usage.<\/p>\n<h3>Provide the detail information of SCP process using -v parameter<\/h3>\n<p>Basic\u00a0<strong>SCP<\/strong>\u00a0command without parameter will copy the files in background. User will see nothing unless the process is done or some error appears. You can use \u201c<strong>-v<\/strong>\u201d parameter to print debug information into the screen. It can help you debugging connection, authentication and configuration problems.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -v Label.pdf mrarianto@202.x.x.x:.<\/pre>\n<h5>Sample Output<\/h5>\n<pre>Executing: program \/usr\/bin\/ssh host 202.x.x.x, user mrarianto, command scp -v -t .\r\nOpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012\r\ndebug1: Reading configuration data \/etc\/ssh\/ssh_config\r\ndebug1: \/etc\/ssh\/ssh_config line 19: Applying options for *\r\ndebug1: Connecting to 202.x.x.x [202.x.x.x] port 22.\r\ndebug1: Connection established.\r\ndebug1: Host '202.x.x.x' is known and matches the RSA host key.\r\ndebug1: Found key in \/home\/pungki\/.ssh\/known_hosts:1\r\ndebug1: ssh_rsa_verify: signature correct\r\ndebug1: Next authentication method: password\r\nmrarianto@202.x.x.x's password:\r\ndebug1: Authentication succeeded (password).\r\nAuthenticated to 202.x.x.x ([202.x.x.x]:22).\r\nSending file modes: C0770 3760348 Label.pdf\r\nSink: C0770 3760348 Label.pdf\r\nLabel.pdf 100% 3672KB 136.0KB\/s 00:27\r\nTransferred: sent 3766304, received 3000 bytes, in 65.2 seconds\r\nBytes per second: sent 57766.4, received 46.0\r\ndebug1: Exit status 0<\/pre>\n<h3>Provide modification times, access times, and modes from original files<\/h3>\n<p>The \u201c<strong>-p<\/strong>\u201d parameter will help you on this. An estimated time and the connection speed will appear on the screen.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -p Label.pdf mrarianto@202.x.x.x:.<\/pre>\n<h5>Sample Output<\/h5>\n<pre>mrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 126.6KB\/s 00:29<\/pre>\n<h3>Make file transfer faster using -C parameter<\/h3>\n<p>One of parameter that can faster your file transfer is \u201c<strong>-C<\/strong>\u201d parameter. The \u201c<strong>-C<\/strong>\u201d parameter will compress your files on the go. The unique thing is the compression is only happen in the network. When the file is arrived to the destination server, it will returning into the original size as before the compression happen.<\/p>\n<p>Take a look of these commands. It is using a single file of\u00a0<strong>93 Mb<\/strong>.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -pv messages.log mrarianto@202.x.x.x:.<\/pre>\n<h5>Sample Output<\/h5>\n<pre>Executing: program \/usr\/bin\/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .\r\nOpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012\r\ndebug1: Reading configuration data \/etc\/ssh\/ssh_config\r\ndebug1: \/etc\/ssh\/ssh_config line 19: Applying options for *\r\ndebug1: Connecting to 202.x.x.x [202.x.x.x] port 22.\r\ndebug1: Connection established.\r\ndebug1: identity file \/home\/pungki\/.ssh\/id_rsa type -1\r\ndebug1: Found key in \/home\/pungki\/.ssh\/known_hosts:1\r\ndebug1: ssh_rsa_verify: signature correct\r\ndebug1: Trying private key: \/home\/pungki\/.ssh\/id_rsa\r\ndebug1: Next authentication method: password\r\nmrarianto@202.x.x.x's password:\r\ndebug1: Authentication succeeded (password).\r\nAuthenticated to 202.x.x.x ([202.x.x.x]:22).\r\ndebug1: Sending command: scp -v -p -t .\r\nFile mtime 1323853868 atime 1380425711\r\nSending file timestamps: T1323853868 0 1380425711 0\r\nmessages.log 100% 93MB 58.6KB\/s 27:05\r\nTransferred: sent 97614832, received 25976 bytes, in 1661.3 seconds\r\nBytes per second: sent 58758.4, received 15.6\r\ndebug1: Exit status 0<\/pre>\n<p>Copying file without \u201c<strong>-C<\/strong>\u201d parameter will result\u00a0<strong>1661.3<\/strong>\u00a0second. Yo may compare the result to the command below which using \u201c<strong>-C\u201d<\/strong>\u00a0parameter.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -Cpv messages.log mrarianto@202.x.x.x:.<\/pre>\n<h5>Sample Output<\/h5>\n<pre>Executing: program \/usr\/bin\/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .\r\nOpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012\r\ndebug1: Reading configuration data \/etc\/ssh\/ssh_config\r\ndebug1: \/etc\/ssh\/ssh_config line 19: Applying options for *\r\ndebug1: Connecting to 202.x.x.x [202.x.x.x] port 22.\r\ndebug1: Connection established.\r\ndebug1: identity file \/home\/pungki\/.ssh\/id_rsa type -1\r\ndebug1: Host '202.x.x.x' is known and matches the RSA host key.\r\ndebug1: Found key in \/home\/pungki\/.ssh\/known_hosts:1\r\ndebug1: ssh_rsa_verify: signature correct\r\ndebug1: Next authentication method: publickey\r\ndebug1: Trying private key: \/home\/pungki\/.ssh\/id_rsa\r\ndebug1: Next authentication method: password\r\nmrarianto@202.x.x.x's password:\r\ndebug1: Enabling compression at level 6.\r\ndebug1: Authentication succeeded (password).\r\nAuthenticated to 202.x.x.x ([202.x.x.x]:22).\r\ndebug1: channel 0: new [client-session]\r\ndebug1: Sending command: scp -v -p -t .\r\nFile mtime 1323853868 atime 1380428748\r\nSending file timestamps: T1323853868 0 1380428748 0\r\nSink: T1323853868 0 1380428748 0\r\nSending file modes: C0600 97517300 messages.log\r\nmessages.log 100% 93MB 602.7KB\/s 02:38\r\nTransferred: sent 8905840, received 15768 bytes, in 162.5 seconds\r\nBytes per second: sent 54813.9, received 97.0\r\ndebug1: Exit status 0\r\ndebug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09\r\ndebug1: compress incoming: raw data 7885, compressed 3821, factor 0.48<\/pre>\n<p>As you can see, when you are using compression, transfer process is done in\u00a0<strong>162.5<\/strong>\u00a0second. It is\u00a0<strong>10<\/strong>\u00a0times faster than not using \u201c<strong>-C<\/strong>\u201d parameter. If you are copying a lot files across the network, \u201c<strong>-C<\/strong>\u201d parameter would help you to decrease the total time you need.<\/p>\n<p>The thing that we should notice that compression method will not work on any files. When the source file is already compressed, you will not find any improvement there. Files such as\u00a0<strong>.zip<\/strong>,\u00a0<strong>.rar<\/strong>,\u00a0<strong>pictures<\/strong>, and\u00a0<strong>.iso<\/strong>\u00a0files will not affected by \u201c<strong>-C<\/strong>\u201d parameter.<\/p>\n<h3>Select another cipher to encrypt files<\/h3>\n<p>By default\u00a0<strong>SCP<\/strong>\u00a0using \u201c<strong>AES-128<\/strong>\u201d to encrypt files. If you want to change to another cipher to encrypt it, you can use \u201c<strong>-c<\/strong>\u201d parameter. Take a look of this command.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -c 3des Label.pdf mrarianto@202.x.x.x:.\r\n\r\nmrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 282.5KB\/s 00:13<\/pre>\n<p>Above command tell\u00a0<strong>SCP<\/strong>\u00a0to use\u00a0<strong>3des algorithm<\/strong>\u00a0to encrypt file. Please be careful that this parameter using \u201c<strong>-c<\/strong>\u201d not \u201c<strong>-C<\/strong>\u201c.<\/p>\n<h3>Limiting bandwidth usage<\/h3>\n<p>Another parameter that may useful is \u201c<strong>-l<\/strong>\u201d parameter. The \u201c<strong>-l<\/strong>\u201d parameter will limit the bandwidth to use. It will be useful if you do an automation script to copy a lot of file, but you don\u2019t want the bandwidth is drained by the\u00a0<strong>SCP<\/strong>process.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -l 400 Label.pdf mrarianto@202.x.x.x:.\r\n\r\nmrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 50.3KB\/s 01:13<\/pre>\n<p>The\u00a0<strong>400<\/strong>\u00a0value behind \u201c<strong>-l<\/strong>\u201d parameter is mean that we limit the bandwidth for\u00a0<strong>SCP<\/strong>\u00a0process only\u00a0<strong>50 KB\/sec<\/strong>. One thing to remember that bandwidth is specified in\u00a0<strong>Kilobits<\/strong>\/<strong>sec<\/strong>\u00a0(<strong>kbps<\/strong>). It is mean that\u00a0<strong>8 bits<\/strong>\u00a0equal with\u00a0<strong>1 byte<\/strong>.<\/p>\n<p>While\u00a0<strong>SCP<\/strong>\u00a0counts in\u00a0<strong>Kilobyte<\/strong>\/<strong>sec<\/strong>\u00a0(<strong>KB\/s<\/strong>). So if you want to limit your bandwidth for\u00a0<strong>SCP<\/strong>\u00a0maximum only\u00a0<strong>50<\/strong>\u00a0<strong>KB\/s<\/strong>, you need to set it into\u00a0<strong>50 x 8<\/strong>\u00a0=<strong>\u00a0400<\/strong>.<\/p>\n<h3>Specify specific port to use with SCP<\/h3>\n<p>Usually\u00a0<strong>SCP<\/strong>\u00a0is using port\u00a0<strong>22<\/strong>\u00a0as a default port. But for security reason, you may change the port into another port. For example, we are using port\u00a0<strong>2249<\/strong>. Then the command should be like this.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -P 2249 Label.pdf mrarianto@202.x.x.x:.\r\n\r\nmrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 262.3KB\/s 00:14<\/pre>\n<p>Make sure that it use capital \u201c<strong>P<\/strong>\u201d not \u201c<strong>p<\/strong>\u201c, since \u201c<strong>p<\/strong>\u201d is already used for preserved times and modes.<\/p>\n<h3>Copy files inside directory recursively<\/h3>\n<p>Sometimes we need to copy directory and all\u00a0<strong>files<\/strong>\u00a0\/\u00a0<strong>directories<\/strong>\u00a0inside it. It will be better if we can do it in\u00a0<strong>1<\/strong>command.\u00a0<strong>SCP<\/strong>\u00a0support that scenario using \u201c<strong>-r<\/strong>\u201d parameter.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -r documents mrarianto@202.x.x.x:.\r\n\r\nmrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 282.5KB\/s 00:13\r\nscp.txt 100% 10KB 9.8KB\/s 00:00<\/pre>\n<p>When the copy process is done, at the destination server you will found a directory named \u201c<strong>documents<\/strong>\u201d with all it\u2019s files. The folder \u201c<strong>documents<\/strong>\u201d is automatically created.<\/p>\n<h3>Disable progress meter and warning \/ diagnostic message<\/h3>\n<p>If you choose not to see progress meter and warning \/ diagnostic messages from SCP, you may disable it using \u201c<strong>-q<\/strong>\u201d parameter. Here\u2019s the example.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -q Label.pdf mrarianto@202.x.x.x:.\r\n\r\nmrarianto@202.x.x.x's password:\r\npungki@mint ~\/Documents $<\/pre>\n<p>As you can see, after the you enter the password, there is no any information about SCP process. After the process is complete, you will be see a prompt again.<\/p>\n<h3>Copy files using SCP through Proxy<\/h3>\n<p>Proxy server is usually used in office environment. Natively, SCP is not proxy configured. When your environment using proxy, you have to \u201ctell\u201d SCP to communicate with the proxy.<\/p>\n<p>Here\u2019s the scenario. The proxy address is\u00a0<strong>10.0.96.6<\/strong>\u00a0and the proxy port is\u00a0<strong>8080<\/strong>. The proxy also implemented user authentication. First, you need to create \u201c<strong>~\/.ssh\/config\u201d<\/strong>\u00a0file. Second you put this command inside it.<\/p>\n<pre>ProxyCommand \/usr\/bin\/corkscrew 10.0.96.6 8080 %h %p ~\/.ssh\/proxyauth<\/pre>\n<p>Then you need to create file \u201c<strong>~\/.ssh\/proxyauth<\/strong>\u201d which contain.<\/p>\n<pre>myusername:mypassword<\/pre>\n<p>After that you can do SCP transparently as usual.<\/p>\n<p>Please notice that corkscrew is might not installed yet on your system. On my Linux Mint, I need to install it first, using standard Linux Mint installation procedure.<\/p>\n<pre>$ apt-get install corkscrew<\/pre>\n<p>For other yum based systems, users can install corkscrew using the following yum command.<\/p>\n<pre># yum install corkscrew<\/pre>\n<p>Another thing that since \u201c<strong>~\/.ssh\/proxyauth<\/strong>\u201d file contain your \u201c<strong>username<\/strong>\u201d and \u201c<strong>password<\/strong>\u201d in clear-text format, please make sure that the file can be accessed by you only.<\/p>\n<h3>Select different ssh_config file<\/h3>\n<p>For mobile user who often switch between company network and public network, it will be suffer to always change settings in SCP. It is better if we can put a different\u00a0<strong>ssh_config<\/strong>\u00a0file to match our needs.<\/p>\n<h5>Here\u2019s a sample scenario<\/h5>\n<p>Proxy is used in company network but not in public network and you are regularly switch network.<\/p>\n<pre>pungki@mint ~\/Documents $ scp -F \/home\/pungki\/proxy_ssh_config Label.pdf\r\n\r\nmrarianto@202.x.x.x:.\r\nmrarianto@202.x.x.x's password:\r\nLabel.pdf 100% 3672KB 282.5KB\/s 00:13<\/pre>\n<p>By default \u201c<strong>ssh_config<\/strong>\u201d file per user will be placed in \u201c<strong>~\/.ssh\/config<\/strong>\u201c. Creating a specific \u201c<strong>ssh_config<\/strong>\u201d file with proxy compatible, will make you easier to switch between networks.<\/p>\n<p>When you are on company network, you can use \u201c<strong>-F<\/strong>\u201d parameter. When you are on public network, you can skip \u201c<strong>-F<\/strong>\u201d parameter.<\/p>\n<p>That\u2019s all about\u00a0<strong>SCP<\/strong>. You can see\u00a0<strong>man pages<\/strong>\u00a0of\u00a0<strong>SCP<\/strong>\u00a0for more detail. Please feel free to leave comments and suggestions.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/scp-commands-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux administrator should be familiar with\u00a0CLI\u00a0environment. Since\u00a0GUI\u00a0mode in Linux servers is not a common to be installed.\u00a0SSH\u00a0may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with\u00a0SSH\u00a0command there is\u00a0SCP\u00a0command.\u00a0SCP\u00a0is used to copy file(s) between servers in secure way. 10 Linux SCP Commands The below command will &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/10-scp-commands-to-transfer-files-folders-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 SCP Commands to Transfer Files\/Folders in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11360","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11360"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11360\/revisions"}],"predecessor-version":[{"id":11361,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11360\/revisions\/11361"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}