How to copy files to and from remote Linux server in Terminal
In this article, let us see how to copy files from a local machine to a remote Linux server and vice-versa in Terminal. With this approach, you may not require an FTP tool like Filezilla to perform a file transfer. Instead, you can very well perform the same file transfer operation in the terminal (command prompt) itself.
As a prerequisite, ensure that you are able to successfully login into the remote server with SSH Key or with a password. In the below code, I am copying a certificate file to a remote server and copying back the same file to the local folder with a different file name.
Basic SCP Command
The below command would request the password of the username included as part of the destination target. On successful authentication, the file can be moved to or from the remote Linux server.
# scp [src file] [userid]@[remote-server]:[dest folder/file]
$ scp ./.certs/ngx.lan-key.pem ar@10.10.30.5:/home/ar/.certs
$ scp ar@10.10.30.6:/home/ar/.certs/ngx.lan.pem ./lnx.pem
Other Common SCP Options
In the below code, let us see how to assign an SSH key for authentication and also connect to a remote server running SSH-Server in a non-standard port ( other than port 22 )
# scp -i [ssh key] [src file] [userid]@[remote-server]:[dest folder/file]
$ scp -i ~/.ssh/srv_id_rsa ./.certs/ngx.lan-key.pem ar@10.10.30.5:/home/ar/.certs
# If SSH server is running at port 33 in server
$ scp -P 33 -i ~/.ssh/srv_id_rsa ./.certs/ngx.lan-key.pem ar@10.10.30.5:/home/ar/.certs
Recursively Copy Folders With SCP
-r
allows to recursively copy the folder and its content and -p
allows to preserve the file attributes.
# Recursively copy folder contents
$ scp -P 33 -i ~/.ssh -rp ./.certs/ ar@10.10.30.5:/home/ar/.certs
Leave a comment