Any system administrator will frequently copy directories and files from a Linux system to another one. A Linux command line tool called SCP, commonly referred to as a secure copy, is used to safely copy or transfer files from one server to another.
The SCP is a lightweight command line program that permits 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. Also, from your local system, you can copy files between two remote systems.
When transferring a file while using the SCP command, the user must share a remote user’s SCP password. You may find instructions on passing passwords to the Linux SCP command in the second file of this blog.
You will become connected to a Linux desktop to a server or a Linux server-to-server expansion as you advance in learning how to decipher your Linux operating system user’s password. These methods use Linux commands like directory clones and file transfers to connect to OS priorities.
Requirements
-
-
- a Linux server on the Cloud Platform
- A root password configured on your server
-
Using the command line, copy files to a remote server:
The following basic syntax demonstrates how to copy a file from the local system to a distant server or system:
scp filename user@remotehost:/directory/path
Copy a file with the name file1.txt from a local system with the IP address “192.168.1.100” inside the /mnt directory to a distant machine, for instance. Use the following command to run SCP :
scp file1.txt [email protected]:/mnt/
The following simple syntax is shown to copy a file from a remote host or remote system to a local system:
scp user@remotehost:/file/path local/path
For instance, run the command below to copy the file “file1.txt” from the distant system with the IP address 192.168.1.100 to the local system’s /opt directory:
scp [email protected]:/mnt/file1.txt /opt/
The following remote password will be required of you:
[email protected] password:
Syntax
The following is the SCP command’s syntax:
scp [options] username1@source_host:directory1/filename1
username2@destination_host:directory2/filename2
Indicate the source file’s location, mentioning:
-
-
- 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
-
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.
Examples
Let’s assume that you are logged into your account on the computer and that your username is “syna” for the purposes of the following demonstrations.
Type the following to move a file called fortune.txt from your home directory to the “star” directory in your account at livestar.com:
scp ~/fortune.txt [email protected]:~/star
On the destination system, you will be prompted to enter your password. The command won’t function properly unless you use the right password.
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.
Type: to transfer the directory from one account to the other.
scp -r [email protected]:~/star~/star
Your password must be entered manually. If the password is entered incorrectly, the command will not execute.
Users can use wildcards, like “or,?” 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.
However, enter the following command to copy all of the “.txt” files from your account’s directory to the other directory:
scp [email protected]:"star/.txt" ~/star/
You will be required to enter the password on your sources system. If the password is entered incorrectly, the command will not execute.
Assuming that livestar.com or any other account is not where the user “syna” is currently logged in. On livestar.com, type: to copy luke.txt from the home directory to your “star” directory.
scp [email protected]:~/luke.txt [email protected]:~/star
Two passwords will be required from you, and if one or both of them are entered wrong, the program will not run.
-
-
- One for the source system
- One for the destination server
-
Using the SCP Command to Transfer Files Securely:
Users can remotely copy files and directories from one Linux environment to other thanks to the SCP command line tool’s simplicity. The change could be made from a desktop to a remote server or from one server to another remote server.
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.
$ scp [Username@SourceHost:]Path_to_File_to_Copy [Username@DestinationHost:]Path_to_Destination_of_Copied_File
For instance, it will successfully copy a file from a Linux desktop environment to one server or remote servers in the way as follows:
$ scp linuxshelltips.txt [email protected]:/home/ubuntu/
When the SCP command is used as indicated, the following password-entering screen appears:
[email protected]'s password:
The targeted file will only be replicated to its restricted destination if the Linux user enters a valid password for this command to continue.
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?
Adding the SSH Password prompt to SCPCommand after installing SSHPass on Linux:
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.
To answer the aforementioned query, we must use the SCP and SSHPASS utilities in a single line of code. Let’s examine the features that the SSHPASS utility provides.
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.
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.
$ 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]
Password Passing using SCP Command:
The following example shows the fundamental syntax for supplying a password using the SCP command:
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/
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.
How to use Linux’s SCP command while passing a password.
The following command syntax shows how SSHPASS non-interactive password authentication use subject can be used in conjunction with SCP.
$ sshpass -p "REMOTE_USER_PASSWORD" scp UserName@Remote_Host:/DESTINATION_PATH_TO_COPIED_FILES
For instance, if a user uses the next technique to copy a file to a distant server:
$ sshpass -p "REMOTE_USER_PASSWORD" scp linuxshelltips_v2.txt [email protected]:/home/ubuntu/
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.
Enter the command as follows:
$ sshpass -p "REMOTE_USER_PASSWORD" scp -r Some_Directory/ [email protected]:/home/ubuntu/
As previously said, the SSHPASS + SCP commands will enable Linux users to copy system or user password files & folders to the destination machine from the host machine with a one-liner command.
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.
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.
The user must create an ssh key in the following ways to obtain password-protected SSH access:
ssh-keygen -t rsa -C "[email protected]"
Then duplicate the text:
~/.ssh/id_rsa.pub
Lastly, add it to the remote machines
~/.ssh/authorized_keys
Ensure the remote computer has the necessary permissions.
-
-
- 0700 for ~./ssh folder
- 0600 for ~/.ssh/authorized_keys
-