How Can I Pass a Password to SCP Command in Linux

How Can I Pass a Password to SCP Command in Linux

SCP cmd pass password in linux

Copying directories and files from one Linux system to another is a routine task performed by system administrators. SCP, Secure Copy, is a Linux command-line tool specifically designed for securely copying or transferring files between servers.

SCP serves as a lightweight command-line utility, enabling secure file or directory transfers between a local system and a remote system, or vice versa. It also facilitates file transfers between two remote systems directly from a local system.

When using the SCP command, the user is required to provide the remote user’s SCP password before initiating a file transfer. In the subsequent sections of this blog, you will gain insights into passing passwords to the SCP command in Linux.

As you delve into the intricacies of Linux operating system user password management, you will become adept at connecting from a Linux desktop to a server or between Linux servers. These techniques align with core OS objectives, utilizing Linux commands for tasks such as directory copies and file transfers.

Jump To...


  • A Cloud Platform-hosted server running the Linux operating system
  • A root password set up on your server

Transfer files to a remote server using the command line

The fundamental syntax for copying a file from the local system to a remote server or system is illustrated below:

scp filename user@remotehost:/directory/path

As an illustration, let’s copy a file named file1.txt to a remote system from a local system with the IP address “” into the /mnt directory. Execute the SCP command as follows:

scp file1.txt root@

The basic syntax to copy a file to a local system from remote host or remote system is displayed as follows:

scp user@remotehost:/file/path local/path

The fundamental syntax for copying a file from a remote host or system to a local system is depicted as follows:

scp root@ /opt/

You will be prompted to input the remote password, as demonstrated below:

root@ password:


The syntax for the scp command is outlined below:

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

Indicate the path of the source file, incorporating:

  • Username1: name of the account on the host computer

  • Source_host: the hostname of the computer

  • Directory1: name of the directory containing the source file

  • Filename1: the name of files 1

  • filename2: the name of files 2

While typing the destination and source paths, ensure to include a space. Additionally, exercise caution when copying files with identical names on both hosts to prevent unintentional data overwrites.


In the given instances, assuming your username is “syna,” and you are currently logged into your account on the computer.

To copy a file named fortune.txt from your home directory to a directory named “star” in your account on the computer “,” input:

scp ~/fortune.txt

You will be prompted to input your password on the destination system, and the command will not execute successfully unless the correct password is provided.

Utilize scp with the -r option to copy a directory and all its associated files. This instructs scp to recursively copy the source directory and its contents.

To copy the directory from your account to the other account, input:

scp -r

You will need to enter your password, and the command will not proceed if the provided password is incorrect.

Users can employ wildcards such as “*”, “?” to copy multiple files within a directory. However, when using wildcards to copy more than two files from a remote system, it is mandatory to enclose the file names in quotes. This is necessary because the Unix shell script expands the unquoted wildcards, not the scp command.

SCP cmd pass password in linux 1

Nevertheless, to replicate all the ‘.txt’ files from one directory in your account to another directory, input the following command:

scp"star/.txt" ~/star/

You will be prompted to enter the password on your source system, and the command will not proceed if the provided password is incorrect.

Let’s consider a scenario where the user “syna” is logged into another computer, not, and has a different account. To copy luke.txt from the home directory to your “star” directory on, input:


You will be required to enter two passwords; the command will not proceed if either of the passwords is entered incorrectly.

  • One password is for the source system,
  • Other is for the destination server.

Safely Transfer Files Using SCP Command:

The straightforwardness of the SCP command line tool empowers users to securely copy files and directories between different Linux environments. This can be achieved from a desktop environment to a remote server environment or from one server setting to another remote server setting.

SCP cmd pass password in linux 2

The implementation of the SCP command enhances the security of password encryption to safeguard against potential leakage or interception during the transfer of files, whether it involves a single file or multiple files and directories. The conventional or widely understood method of utilizing the SCP command adheres to the following syntax.

$ scp [Username@SourceHost:]Path_to_File_to_Copy [Username@DestinationHost:]Path_to_Destination_of_Copied_File

As an example, it effectively replicates a file from a Linux desktop environment to either a single server or multiple remote servers using the following procedure:

$ scp linuxshelltips.txt ubuntu@18.118.208.xx:/home/ubuntu/

Executing the provided SCP command prompts the user to enter the password through the following password entry prompt:

ubuntu@18.118.208.xx's password:

This command will only advance upon the Linux user inputting a valid password, enabling the targeted file to be duplicated to its designated destination.

Is there an alternative one-liner command that eliminates the need for a password entry prompt, allowing for the instantaneous replication of files and directories to their specified destination?

Install SSHPass on Linux – Incorporate SSH Password Prompt into SCPCommand:

A user can utilize the Ack Command in Linux to integrate sshpass into their system. Install sshpass on Linux, as it is a straightforward and lightweight command-line utility that allows you to provide passwords to the command prompt. It proves valuable in a shell script when executing backups through a cron job.

To address the earlier question, combining SCP and SSHPASS tools into a single one-liner command is essential. Understanding the capabilities of the SSHPASS tool is crucial.

The SCP command is employed for copying files and directories from the host Linux machines to the destination machine. Relying solely on the password for the SCP command doesn’t suffice for password authentication when used in a one-line command, and it results in a subsequent password prompt for the OS user to input the login password.

By incorporating SSHPASS alongside SCP, it eliminates the need for a password prompt before the user can copy a file or directory, enhancing the efficiency of the process.

$ sudo apt-get install sshpass [On Debian, Ubuntu and Mint] $ sudo yum install sshpass [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/sshpass [On Gentoo Linux] $ sudo pacman -S sshpass [On Arch Linux] $ sudo zypper install sshpass [On OpenSUSE]

SCP cmd pass password in linux 3

How can I pass a password using the SCP command?

The fundamental structure for transmitting a password through the SCP command is outlined below:

sshpass -p "remote-user-password" scp filename user@remotehost:/dir/path/

As an example, if you wish to transfer a file named “file1.txt” to the remote server with the IP address, utilize the following Linux command:

sshpass -p "password" scp file1.txt root@

As demonstrated in the following example, utilizing the sshpass + SCP command enables you to transfer files and directories from one system to another using a single-line command.

How can I pass a password to the SCP command in Linux.

The non-interactive password authentication feature of SSHPASS can be employed in conjunction with SCP, as depicted by the following command syntax.


For example, if a user endeavors to transfer a file to a remote server using the following approach:

$ sshpass -p "REMOTE_USER_PASSWORD" scp linuxshelltips_v2.txt ubuntu@

In the aforementioned situation, if the user intends to copy multiple files and directories, they would utilize the -r option to recursively copy all the folders, sub-folders, and other files within the targeted directory.

Enter the following command:

$ sshpass -p "REMOTE_USER_PASSWORD" scp -r Some_Directory/ ubuntu@

As described, the combination of SSHPASS and SCP commands enables Linux users to accomplish a one-liner command for transferring the system or user’s password files and directories to the destination machine from the host machine.

The designated storage context of the directories and transferred files could be within a Linux desktop environment or a remote Linux server environment. SSHPASS + SCP tools facilitate quick, efficient, and effortless file copying, eliminating the need to wait for a password prompt before executing user-initiated actions.

Nevertheless, in a network crowded with numerous users, it is advisable to exercise caution when using SSHPASS, as a malicious user could potentially hijack your remote user password file.

To enhance SSH password protection with file permissions, the user should create an SSH key by:

ssh-keygen -t rsa -C ""

Then replicate the content:


Finally, append it to the remote machines.


Ensure that the remote machine has the appropriate permissions.

  • 0700 for ~./ssh folder

  • 0600 for ~/.ssh/authorized_keys

Frequently Asked Questions

To establish a secure connection using the SCP command, which securely copies files between a local and a remote system, authentication is necessary. If you’re connecting to a remote system that requires a password for authentication, you must provide the password to the SCP command.

Pass a password to the SCP command using the -r and -B options. For example, scp -r -B <password> /path/to/local/file user@remote:/path/to/destination/. Replace <password> with the password you want to use for authentication.


Avoid passing passwords to remote servers via the SCP command, as it is not recommended due to the potential risk of interception by malicious actors. Instead, it is advisable to use public key authentication or set up passwordless SSH to establish a secure connection between the local and remote systems.

Public key authentication is a technique that utilizes a public key and a private key to create a secure connection between the local and remote systems. The public key is stored on the remote system, while the private key is stored on the local system. When attempting to connect to the remote system, the SCP command employs the private key for authentication.

To establish public key authentication for SCP, you need to generate a public/private key pair on your local system and transfer the public key to the remote system. Subsequently, you can utilize the private key for authenticating your SCP connections. Numerous online tutorials offer comprehensive instructions on setting up public key authentication for SCP.

Passwordless SSH is an authentication method that enables you to create a secure connection between the local and remote systems without the need for a password. Instead, it relies on public key authentication to verify your SCP connections. This approach is considered more secure than transmitting passwords or SSH keys directly to the SCP command.