{"id":811,"date":"2023-03-08T14:42:24","date_gmt":"2023-03-08T06:42:24","guid":{"rendered":"https:\/\/www.servergigabit.com\/blog\/?p=811"},"modified":"2025-12-23T17:45:14","modified_gmt":"2025-12-23T09:45:14","slug":"pass-password-to-scp-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.servergigabit.com\/blog\/linux\/pass-password-to-scp-command-in-linux","title":{"rendered":"Master Secure and Fast Ways to Pass Password to SCP Command in Linux"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p data-start=\"54\" data-end=\"345\">Pass Password to SCP Command in Linux is a common requirement for system administrators who frequently copy directories and files from one Linux system to another. SCP (Secure Copy) is a Linux command-line tool used to safely transfer files and directories between servers.<\/p>\n<p data-start=\"347\" data-end=\"592\">SCP is a lightweight command-line program that allows you to securely copy a file or directory from a local system to a remote system, from a remote system to a local system, or even between two remote systems directly from your local machine.<\/p>\n<p data-start=\"594\" data-end=\"803\">When transferring files using the SCP command, the user must provide the remote user\u2019s password for authentication. In this blog, you will find instructions on how to pass passwords to the Linux SCP command.<\/p>\n<p data-start=\"805\" data-end=\"1056\" data-is-last-node=\"\" data-is-only-node=\"\">As you continue learning, you will understand how Linux desktop-to-server and server-to-server connections work. These methods rely on Linux commands for directory cloning and file transfers, helping you manage operating system tasks more efficiently.<\/p>\n<p data-start=\"805\" data-end=\"1056\" data-is-last-node=\"\" data-is-only-node=\"\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-1555 aligncenter\" src=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-300x171.jpg\" alt=\"Requirements for Pass Password to SCP Command in Linux\" width=\"1007\" height=\"574\" srcset=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-300x171.jpg 300w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-1024x585.jpg 1024w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-768x439.jpg 768w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-370x211.jpg 370w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-800x457.jpg 800w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux-390x223.jpg 390w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Requirements-for-Pass-Password-to-SCP-Command-in-Linux.jpg 1400w\" sizes=\"(max-width: 1007px) 100vw, 1007px\" \/><\/p>\n<h2><strong>Requirements for Pass Password to SCP Command in Linux<\/strong><\/h2>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>a<a href=\"https:\/\/www.servergigabit.com\/linux-vps-hosting\/\"> Linux server<\/a> on the <a href=\"https:\/\/cloud.google.com\/\" target=\"_blank\" rel=\"noopener\">Cloud Platform<\/a><\/li>\n<li>A root password configured on your server<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><strong>Using the command line, copy files to a remote server:<\/strong><\/h2>\n<p>The following basic syntax demonstrates how to copy a file from the local system to a distant server or system:<\/p>\n<pre><code>scp filename user@remotehost:\/directory\/path<\/code><\/pre>\n<p>Copy a file with the name file1.txt from a local system with the IP address &#8220;192.168.1.100&#8221; inside the \/mnt directory to a distant machine, for instance. Use the following command to run SCP :<\/p>\n<pre><code>scp file1.txt root@192.168.1.100:\/mnt\/<\/code><\/pre>\n<p>The following simple syntax is shown to copy a file from a remote host or remote system to a local system:<\/p>\n<pre><code>scp user@remotehost:\/file\/path local\/path<\/code><\/pre>\n<p>For instance, run the command below to copy the file &#8220;file1.txt&#8221; from the distant system with the IP address 192.168.1.100 to the local system&#8217;s \/opt directory:<\/p>\n<pre><code>scp root@192.168.1.100:\/mnt\/file1.txt \/opt\/<\/code><\/pre>\n<p>The following remote password will be required of you:<\/p>\n<pre><code>root@192.168.1.100 password:<\/code><\/pre>\n<h2><strong>Syntax<\/strong><\/h2>\n<p>The following is the SCP command&#8217;s syntax:<\/p>\n<pre><code>scp [options] username1@source_host:directory1\/filename1<\/code><\/pre>\n<pre><code>username2@destination_host:directory2\/filename2<\/code><\/pre>\n<p>Indicate the source file&#8217;s location, mentioning:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><strong>Username1:<\/strong>\u00a0name of the account on the host computer<\/li>\n<li><strong>Source_host:<\/strong>\u00a0the hostname of the computer<\/li>\n<li><strong>Directory1:<\/strong>\u00a0name of the directory containing the source file<\/li>\n<li><strong>Filename1:<\/strong>\u00a0the name of files 1<\/li>\n<li><strong>filename2:\u00a0<\/strong>the name of files 2<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>When entering the source and destination routes, the user must include a space. Moreover, take extra care when copying files with the same name across both hosts. If not, you risk overwriting the data you want to secure.<\/p>\n<h2><strong>Examples<\/strong><\/h2>\n<p>Let&#8217;s assume that you are logged into your account on the computer and that your username is &#8220;syna&#8221; for the purposes of the following demonstrations.<\/p>\n<p>Type the following to move a file called fortune.txt from your home directory to the &#8220;star&#8221; directory in your account at livestar.com:<\/p>\n<pre><code>scp ~\/fortune.txt syna@livestar.com:~\/star<\/code><\/pre>\n<p>On the destination system, you will be prompted to enter your password. The command won&#8217;t function properly unless you use the right password.<\/p>\n<p>To copy a directory and all of the files it contains, use the SCP command with the -r option. These instruct SCP on how to recursively copy the contents of the source directory.<\/p>\n<p>Type: to transfer the directory from one account to the other.<\/p>\n<pre><code>scp -r syna@livestar.com:~\/star~\/star<\/code><\/pre>\n<p>Your password must be entered manually. If the password is entered incorrectly, the command will not execute.<\/p>\n<p>Users can use wildcards, like &#8220;or,?&#8221; to copy several files from a directory. Nevertheless, you must include quotations at the start and end of the file when utilizing wildcards to copy more than two files from a remote system. That is necessary because the Unix shell script, not the SCP command, extends unquoted wildcards.<\/p>\n<p>However, enter the following command to copy all of the &#8220;.txt&#8221; files from your account&#8217;s directory to the other directory:<\/p>\n<pre><code>scp syna@livestar.com:\"star\/.txt\" ~\/star\/<\/code><\/pre>\n<p>You will be required to enter the password on your sources system. If the password is entered incorrectly, the command will not execute.<\/p>\n<p>Assuming that livestar.com or any other account is not where the user &#8220;syna&#8221; is currently logged in. On livestar.com, type: to copy luke.txt from the home directory to your &#8220;star&#8221; directory.<\/p>\n<pre><code>scp syna@empire.gov:~\/luke.txt syna@livestar.com:~\/star<\/code><\/pre>\n<p>Two passwords will be required from you, and if one or both of them are entered wrong, the program will not run.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>One for the source system<\/li>\n<li>One for the destination server<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><strong>Using the SCP Command to Transfer Files Securely:<\/strong><\/h2>\n<p>Users can remotely copy files and directories from one Linux environment to other thanks to the SCP command line tool&#8217;s simplicity. The change could be made from a desktop to a remote server or from one server to another remote server.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-1553 aligncenter\" src=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-300x169.jpg\" alt=\"Securely transfer files with scp\" width=\"563\" height=\"317\" srcset=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-300x169.jpg 300w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-1024x576.jpg 1024w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-768x432.jpg 768w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-370x208.jpg 370w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-1230x692.jpg 1230w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-800x450.jpg 800w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp-390x219.jpg 390w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Securely-transfer-files-with-scp.jpg 1280w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/p>\n<p>When using the SCP command, password encryption is strengthened to prevent leakage or eavesdropping whether transferring a single file or a number of files and directories. The following syntax is what the conventional or understood method of running the SCP command attaches to.<\/p>\n<pre><code>$ scp [Username@SourceHost:]Path_to_File_to_Copy [Username@DestinationHost:]Path_to_Destination_of_Copied_File<\/code><\/pre>\n<p>For instance, it will successfully copy a file from a Linux desktop environment to one server or remote servers in the way as follows:<\/p>\n<pre><code>$ scp linuxshelltips.txt ubuntu@18.118.208.xx:\/home\/ubuntu\/<\/code><\/pre>\n<p>When the SCP command is used as indicated, the following password-entering screen appears:<\/p>\n<pre><code>ubuntu@18.118.208.xx's password:<\/code><\/pre>\n<p>The targeted file will only be replicated to its restricted destination if the Linux user enters a valid password for this command to continue.<\/p>\n<p>What if you could replicate your files and folders to the appropriate location with a one-line command rather than waiting for a password entry prompt?<\/p>\n<h2><strong>Adding the SSH Password prompt to SCPCommand after installing SSHPass on Linux:<\/strong><\/h2>\n<p>When installing ssh pass on Linux, a user might use the Ack command. You can supply passwords to the command prompt using this straightforward, lightweight command-line program. When a shell script uses a cron job to carry out backups, this time is valuable.<\/p>\n<p>To answer the aforementioned query, we must use the SCP and SSHPASS utilities in a single line of code. Let&#8217;s examine the features that the SSHPASS utility provides.<\/p>\n<p>Files and directories from the host Linux machines are copied to the target machine using the SCP command. A subsequent password prompt for the OS user to enter the login password is generated when the password to the SCP command is not sufficient to adapt password authentication under one-line command usage.<\/p>\n<p>When SSHPASS is used in conjunction with SCP, it rejects the demand for a password prompt prior to the user copying a file or directory.<\/p>\n<pre><code>$ sudo apt-get install sshpass [On Debian, Ubuntu and Mint]<\/code><\/pre>\n<pre><code>$ sudo yum install sshpass [On RHEL\/CentOS\/Fedora and Rocky Linux\/AlmaLinux]<\/code><\/pre>\n<pre><code>$ sudo emerge -a sys-apps\/sshpass [On Gentoo Linux]<\/code><\/pre>\n<pre><code>$ sudo pacman -S sshpass [On Arch Linux]<\/code><\/pre>\n<pre><code>$ sudo zypper install sshpass [On OpenSUSE]<\/code><\/pre>\n<p><img decoding=\"async\" class=\"alignnone wp-image-1554\" src=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-300x53.png\" alt=\"Example of SSH Pass\" width=\"604\" height=\"107\" srcset=\"https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-300x53.png 300w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-1024x181.png 1024w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-768x136.png 768w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-370x66.png 370w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-800x142.png 800w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass-390x69.png 390w, https:\/\/www.servergigabit.com\/blog\/wp-content\/uploads\/2023\/03\/Example-of-SSH-Pass.png 1078w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/p>\n<h2><strong>Password Passing using SCP Command:<\/strong><\/h2>\n<p>The following example shows the fundamental syntax for supplying a password using the SCP command:<\/p>\n<pre><code>sshpass -p \"remote-user-password\" scp filename user@remotehost:\/dir\/path\/<\/code><\/pre>\n<p>For instance, if you desire to\u00a0copy a file name\u00a0file1.txt\u00a0to the\u00a0remote server\u00a0with IP\u00a0192.168.1.100, use the\u00a0following command in Linux:<\/p>\n<pre><code>sshpass -p \"password\" scp file1.txt root@192.168.1.100:\/mnt\/<\/code><\/pre>\n<p>You can copy files and directories from one system to another by using a one-line command, as you can see in the example below, using the sshpass + SCP command.<br \/>\nHow to use Linux&#8217;s SCP command while passing a password.<\/p>\n<p>The following command syntax shows how SSHPASS non-interactive password authentication use subject can be used in conjunction with SCP.<\/p>\n<pre><code>$ sshpass -p \"REMOTE_USER_PASSWORD\" scp UserName@Remote_Host:\/DESTINATION_PATH_TO_COPIED_FILES<\/code><\/pre>\n<p>For instance, if a user uses the next technique to copy a file to a distant server:<\/p>\n<pre><code>$ sshpass -p \"REMOTE_USER_PASSWORD\" scp linuxshelltips_v2.txt ubuntu@18.118.208.79:\/home\/ubuntu\/<\/code><\/pre>\n<p>In the aforementioned case, if the user wanted to recursively copy all of the folders, subfolders, and other files within the selected directory, he would use the -r option.<\/p>\n<p>Enter the command as follows:<\/p>\n<pre><code>$ sshpass -p \"REMOTE_USER_PASSWORD\" scp -r Some_Directory\/ ubuntu@18.118.208.79:\/home\/ubuntu\/<\/code><\/pre>\n<p>As previously said, the SSHPASS + SCP commands will enable Linux users to copy system or user password files &amp; folders to the destination machine from the host machine with a one-liner command.<\/p>\n<p>Linux desktop or Linux remote server environments could be the targeted storage background for the directories and transferred files. The usage of SSHPASS + SCP tools makes file copying swift, effective, and simple because no password is required before user-initiated actions may be carried out.<\/p>\n<p>In a network with many of users, SSHPASS would be the ideal option because a malicious user may easily steal your remote user password file.<\/p>\n<p>The user must create an ssh key in the following ways to obtain password-protected SSH access:<\/p>\n<pre><code>ssh-keygen -t rsa -C \"your_email@youremail.com\"<\/code><\/pre>\n<p>Then duplicate the text:<\/p>\n<pre><code>~\/.ssh\/id_rsa.pub<\/code><\/pre>\n<p>Lastly, add it to the remote machines<\/p>\n<pre><code>~\/.ssh\/authorized_keys<\/code><\/pre>\n<p>Ensure the remote computer has the necessary permissions.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>0700 for ~.\/ssh folder<\/li>\n<li>0600 for ~\/.ssh\/authorized_keys<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Pass Password to SCP Command in Linux is a common requirement for system administrators who frequently copy directories and files from one Linux system to another. SCP (Secure Copy) is a Linux command-line tool used to safely transfer files and directories between servers. SCP is a lightweight command-line program that allows you to securely copy a file or directory&hellip;<\/p>\n","protected":false},"author":1,"featured_media":987,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[598],"class_list":["post-811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-scp-command"],"_links":{"self":[{"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/posts\/811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/comments?post=811"}],"version-history":[{"count":8,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":1551,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/posts\/811\/revisions\/1551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/media\/987"}],"wp:attachment":[{"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.servergigabit.com\/blog\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}