How to Pass Password to SCP Command in Linux

How to Pass Password to SCP Command in Linux

Copying directories and copying files from a Linux system to another system is a common task performed by any system administrator. SCP, also known as a secure copy, is a Linux command line tool used to securely copy files or transfer files from one server to the other.

The SCP is a lightweight command line tool that authorizes you to securely copy a file or a directory from a local system to a remote system or from a remote system to a local system. You can also copy files between two remote systems from your local system.

When the SCP command is used, the user needs to share a remote user scp password before transferring a file. In the second file of this blog, you will learn how to pass passwords to the scp command in Linux.

as you progress to learn your Linux operating system user's password understanding, you will be hooked in Linux desktop to a server or Linux server to server enlargements. These techniques link to prioritized OS goals using Linux commands like directory copies and transfer files.

Requirements

  • A server running Linux on the Cloud Platform

  • A root password configured on your server

Copy Files Using the Command line to a Remote Server

The basic syntax to copy a file to the remote server or remote system from the local system is shown below:

scp filename user@remotehost:/directory/path

For example, copy a file name file1.txt to a remote system from a local system containing the IP address "192.168.1.100" inside /mnt directory. Run scp through the following command:

scp file1.txt [email protected]:/mnt/

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

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

For instance, to copy a file name "file1.txt" to the local system inside /opt directory from the remote system containing an IP address 192.168.1.100, run the following command:

scp [email protected]:/mnt/file1.txt /opt/

You will be asked to enter a remote password as shown below:

[email protected] password:

Syntax

The syntax for the scp command is shown below:

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

Specify the location of the source file, including:

  • 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

The user must include a space while typing the destination and source paths. Moreover, be careful while copying files with the same name on both hosts. Otherwise, you may overwrite the data you intend to secure.

Examples

For the following examples, let's say your username is "syna", and you are logged into your account on the computer

To copy a file called fortune.txt from your home directory to a directory called "star" in your account on the computer "livestar.com", type:

scp ~/fortune.txt [email protected]:~/star

You will be asked to enter your password on the destination system. The command will not work well unless you enter the correct password.

Use scp with the -r command to copy a directory and all its connected files. These guide scp to recursively copy the source directory and its contents. 

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

scp -r [email protected]:~/star~/star

You will be required to type your password. The command will not proceed if the password is incorrect.

Users can use wildcards to copy multiple files within a directory, for example, “or, ?”. However, using wildcards for copying more than two files from a remote system, you must type quotes symbols at the beginning and end of the file. It is compulsory as the Unix shell script expands the unquoted wildcards, not the scp command.

However, to copy all the ‘.txt’ files from the directory from your account to the other directory, type: 

scp [email protected]:"star/.txt" ~/star/

You will be asked to enter the password on your sources system. The command will not proceed if the password is incorrect. 

Assume that the user “syna” is logged in to another computer, not livestar.com or another account. To copy luke.txt from the home directory to your “star” directory on livestar.com, type:

scp [email protected]:~/luke.txt [email protected]:~/star

You will be asked to enter two passwords; the command will not proceed if both passwords are entered incorrectly. 

  • One for the source system 

  • One for the destination server

Securely Transfer Files Using SCP Command:

The clarity of the SCP command line tool makes it possible for users to remotely and remotely copy files and directories from one Linux environment to another. It could be from a desktop setting to a remote server setting or from a server setting to another remote server setting.

SCP command usage reinforces password encryption to secure it from leaking or being snooped during the file transfer or of multiple files and directory transfers. The traditional or comprehended approach of executing the SCP command attaches to the following syntax.

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

For instance, It will successfully copy a file to one server or remote servers from a Linux desktop environment in the following manner:

$ scp linuxshelltips.txt [email protected]:/home/ubuntu/

The implementation of the demonstrated SCP command directs to the following password entry prompt:

[email protected]'s password:

This command will proceed only after the Linux user enters a valid password that the targeted file is reproduced to its restricted destination.

What if there is a one-liner command where you do not have to hold off for a password entry prompt before your files and directories can be reproduced to their desired destination?

Install SSHPass in Linux – Add SSH Password prompt to SCPCommand:

A user can use Ack Command in Linux Install sshpass in Linux install sshpass. It is a simple and lightweight command-line tool that permits you to feed passwords to the command prompt. It is precious in a shell script when carrying backup via the cron job.

We must implement SCP and SSHPASS tools in one line command to brainstorm the above question. Let us understand what is offered by the SSHPASS tool.

The SCP command is to copy files and directories from the host Linux machines to the destination machine. The password to SCP command is not enough to adapt password authentication under one-line command usage as well as it leads to a subsequent password prompt for the OS user to type the login password. 

Alongside SCP, when SSHPASS is executed, it rejects the requirement for a password prompt before the user copies a file or directory.

$ 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]

How to Pass Password with SCP Command:

The basic syntax to pass a password with the SCP command is illustrated below:

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

For instance, if you desire to copy a file name file1.txt to the remote server with IP 192.168.1.100, use the following command in Linux:

sshpass -p "password" scp file1.txt [email protected]:/mnt/

As you witness in the following example, the sshpass SCP command will allow you to copy files and directories from one system to another, employing a one-line command.
How to pass Password to SCP Command in Linux.

SSHPASS non-interactive password authentication use subject can be executed alongside SCP as illustrated by the following command syntax.

$ sshpass -p "REMOTE_USER_PASSWORD" scp UserName@Remote_Host:/DESTINATION_PATH_TO_COPIED_FILES

For sample, if a user attempts to copy a file to a remote server using the following method:

$ sshpass -p "REMOTE_USER_PASSWORD" scp linuxshelltips_v2.txt [email protected]:/home/ubuntu/

In the above scenario, if the user wished to copy multiple files and directory files, he would use the -r alternative to recursively copy all the folders, sub-folders, and other files within the targeted directory.

Type the following command:

$ sshpass -p "REMOTE_USER_PASSWORD" scp -r Some_Directory/ [email protected]:/home/ubuntu/

As explained, SSHPASS SCP commands will support Linux users to achieve a one-liner command for copying the system or user's password files & directories to the destination machine from the host machine.

The targeted storage background of the directories and copied files could be a Linux desktop environment or a Linux remote server environment. SSHPASS SCP tools drive file copying quick, efficient, and effortless as you do not have to delay a password prompt before your user-initiated steps can execute.

However, it would be best to use SSHPASS on a network crowded with numerous users, as a hostile user could easily hijack your remote user password file.

To get ssh password protection a password with file permission, the user needs to generate an ssh key like:

ssh-keygen -t rsa -C "[email protected]"

Then copy the content:

~/.ssh/id_rsa.pub

Lastly, add it to the remote machines

~/.ssh/authorized_keys

Make sure the remote machine has the permissions

  • 0700 for ~./ssh folder

  • 0600 for ~/.ssh/authorized_keys

The author
Bruno Mirchevski

Bruno Mirchevski is a seasoned hosting industry professional. With years of experience, Bruno is dedicated to helping individuals and businesses find the best web hosting solutions. As a hosting expert at HostAdvice, Bruno provides valuable insights and guidance to users seeking reliable hosting services. He also owns The Logician trademark and manages his personal brand and HE Group. His passion for the industry is reflected in his dedication to speaking at events and publishing articles and reviews on various online platforms. You can check out the most reliable and up-to-date information about web hosting directly on our blog, where Bruno shares his expertise and insights. Social profiles are available here: Twitter @brunomirchevski, LinkedIn and Quora

Frequently Asked Questions

Why do I need to pass a password to the SCP command?

The SCP command securely copies files between a local and a remote system, but it requires authentication to establish a secure connection. You need to pass a password to the SCP command if you're connecting to a remote system that requires a password for authentication.

How do I pass a 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.

Is it secure to pass passwords to the SCP command?

Passing passwords to remote servers via the SCP command is not recommended, as malicious actors can intercept it. Instead, it would be best to use public key authentication or passwordless SSH to establish a secure connection between the local and remote systems.

What is public key authentication in SCP?

Public key authentication is a method that uses a public key and a private key to establish a secure connection between the local and remote systems. The public key is stored on the remote system, and the private key is stored on the local system. When you attempt to connect to the remote system, the SCP command uses the private key to authenticate you.

How do I set up public key authentication for SCP?

To set up public key authentication for SCP, you must generate a public/private key pair on your local system and copy the public key to the remote system. You can then use the private key to authenticate your SCP connections. There are many tutorials available online that provide detailed instructions on how to set up public key authentication for SCP.

What is passwordless SSH in SCP?

Passwordless SSH is a method of authentication that allows you to establish a secure connection between the local and remote systems without using a password. Instead, it relies on public key authentication to authenticate your SCP connections. This method is more secure than passing passwords or ssh keys to the SCP command.