We can easily copy the files from one Linux machine to another using SSH. Here is an example of it.
Copy Files Over SSH
We can use the secure copy command that is very easy to use. Here is the format of the same command.
scp [options] original_file destination_file
You will need to use the remote username and destination path while copying the file.
[email protected] Address:path/to/file
You can use the server IP Address or the resolvable hostname. Here is the complete command to migrate a file from one linux machine to a remote Linux machine.
scp –P 22 /home/test.txt [email protected] Address:/home/user/test.txt
Here, is the explanation of the above command.
scp : Secure Copy
-P : port number. Here, we have used the default port 22 for the SSH. If you have configured SSH to another port, you will need to use the same port
vpshost : It is a user of the remote server.
/home/user/ : Destination where we will move the file on the remote server.
You can easily copy the files to the destination remote server using the above command but you will need to enter the remote server password to complete the copying.
Copy Files Over SSH without Password
- Run the below command at source machine. You can refer to generate SSH key for more details.
- You can run ls command to .ssh directory to view the generated file. id_rsa is your private key and id_rsa.pub is your private key. Copy the public file to the remote server using scp command. Before that create a directory called authorizedkey_2 at remote server inside the /.ssh directory.
scp –P 22 /root/.ssh/id_rsa.pub [email protected] Address:/root/.ssh/
- Now run the below command at your remote server.
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
- Give the below permission to .ssh/authorized_keys folder.
chmod 600 .ssh/authorized_keys
- Finally try to test copy files without password from source to remote server and you will see it will not ask for the password.
scp –P 22 /home/test.txt [email protected] Address:/home/test.txt