As you progressively graduate in your Linux operating system user experience, you are bound to either find yourself in a Linux desktop-to-server or Linux server-to-server entanglement. In most cases, these scenarios are related to prioritized OS objectives such as the transfer of useful files and directory copies.
[ You might also like: How to Pass Password to SSH Command in Linux ]
Securely Transfer Files Using Scp Command
The simplicity of the scp command-line tool makes it possible for users to easily and remotely copy files and directories from one Linux environment to another. It could be from a desktop environment to a server environment or from a server environment to another server environment.
SCP command usage supports password encryption to protect it from leaking or being snooped during files and directories transfer. The traditional or known approach of implementing the SCP command adheres to the following syntax.
$ scp [Username@SourceHost:]Path_to_File_to_Copy [Username@DestinationHost:]Path_to_Destination_of_Copied_File
For instance, I could successfully copy a file to one of my remote servers from my Linux desktop environment in the following manner:
$ scp linuxshelltips.txt email@example.com:/home/ubuntu/
The initial execution of the above demonstrated SCP command leads to the following password entry prompt:
It is only after the Linux user enters a correct user-associated password that the targeted file is copied to its specified destination.
What if there was a one-liner command where you don’t have to wait for a password entry prompt before your files and directories can be copied to their intended destination?
Install SSHPass in Linux – Add SSH Password to Command
To brainstorm the above question, we have to implement both SSHPASS and SCP tools in one command. First, let us understand what this SSHPASS tool offers.
The logic of the SCP command is to copy files and directories from a host machine to a destination machine. The SCP command; on its own, is not sufficient enough to accommodate password authentication under a one-line command usage and therefore leads to a successive password prompt for the OS user to enter the required login passcode.
When SSHPASS is implemented alongside SCP, it nullifies the need for a password prompt before a user’s files and directories can be copied.
$ 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 Add Password to SCP Command in Linux
SSHPASS non-interactive password authentication use case can be implemented alongside SCP as demonstrated by the following command syntax.
$ sshpass -p "REMOTE_USER_PASSWORD" scp UserName@Remote_Host:/DESTINATION_PATH_TO_COPIED_FILES
For instance, I could attempt copying a file to a remote server in the following manner:
$ sshpass -p "REMOTE_USER_PASSWORD" scp linuxshelltips_v2.txt firstname.lastname@example.org:/home/ubuntu/
In the above case, if we wanted to copy directory files, we would use the
-r option to recursively copy all the files, folders, and sub-folders within the targeted directory.
Consider the following command.
$ sshpass -p "REMOTE_USER_PASSWORD" scp -r Some_Directory/ email@example.com:/home/ubuntu/
As demonstrated, SSHPASS + SCP commands will help Linux users achieve a one-liner command usage for copying system/user files & directories from one host machine to another destination machine.
The targeted storage location of the copied files and directories could be a Linux server environment or a Linux desktop environment. SSHPASS + SCP tools make file copying fast, efficient, and manageable as you do not have to wait for a password prompt before your user-initiated actions can execute.
You should however be cautious with the use of SSHPASS on a network jammed with too many users as a malicious user could easily hijack your user password.