How do I copy a file to a Linux server remotely?

How do I copy a file to a Linux server remotely?

Technology Specialist

July 10, 2021

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 :/home/ar/.certs
$ scp :/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 :/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 :/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/ :/home/ar/.certs

SSH or Secure Shell is a protocol that allows secure access to remote computers. SSH also comes with scp utility for transferring file between remote computers. It uses the SSH protocol and has the syntax almost similar to the cp command.

Other file transfer applications such as sftp and rsync can also utilize SSH to secure their file transfers. These applications allow us to copy our files from local to remote servers and to copy files from remote servers to our local machine.

How do I copy a file to a Linux server remotely?

Examples for the following file transfer methods are based on the above setup.

Make sure you have SSH access to the remote server with adequate permission to the remote files and folders.

Methods for remote file transfer using SSH:

Transfer file using scp

The easiest of these is scp or secure copy. While cp is for copying local files, scp is for remote file transfer. The main difference between cp and scp is that, you'll have to specify the remote host's DNS name or IP address and provide a login credential for the command to work when using scp. You can scp files from local to remote and from remote to local.

  1. Copy single file from local to remote using scp.

    $ scp myfile.txt remoteuser@remoteserver:/remote/folder/

    If the target folder (/remote/folder/) is not specified, it will copy the file to the remote user's home directory.

  2. scp from remote to local using a single file .

    $ scp remoteuser@remoteserver:/remote/folder/remotefile.txt  localfile.txt

    Using . as the copy target (replacing localfile.txt will copy the remote file to the current working directory using the same filename (remotefile.txt)

  3. Copy multiple files from local to remote using scp.

    $ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/

  4. Copy all files from local to remote using scp.

    $ scp * remoteuser@remoteserver:/remote/folder/

  5. Copy all files and folders recursively from local to remote using scp.

    $ scp -r * remoteuser@remoteserver:/remote/folder/

    remoteuser need to exist and have write permission to /remote/folder/ in the remote system.

    GUI programs such WinSCP can also be used to transfer files between local and remote host using scp methods.

Transfer file using sftp

sftp or Secure FTP, on the other hand works almost exactly like ftp but with a secure connection. Most of the commands are similar and can be used interchangeably. The following sftp example will work exactly as ftp would.

$ sftp 
Connected to 192.168.1.10.
sftp> dir
file1      file2  file3   
sftp> pwd
Remote working directory: /home/user
sftp> get file2
Fetching /home/user/file2 to file2
/home/user/file2                         100% 3740KB 747.9KB/s   00:05    
sftp> bye
$ 

WinSCP can also be used for file transfer using SFTP protocol, but with graphical interface. The other popular tool is FileZilla.

Transfer file using rsync

You can also use ssh to secure your rsync session. To do this, use --rsh=ssh or -e “ssh” with your normal rsync commands. The following 2 commands will work exactly the same;

$ rsync -av --delete --rsh=ssh /path/to/source remote:/remote/folder/
$ rsync -av --delete -e "ssh" /path/to/source remote:/remote/folder/

If these options are not specified, rsync will first try to connect to rsyncd but will automatically fall back to SSH if rsyncd is not running in the remote system.

Mount remote filesystem locally

Remote filesystems could be mounted to the local host and accessed as a local filesystem. Mounting remote filesystem requires SSH access to the remote host and using sshfs.

How do I copy a file to a Linux server remotely?

Discuss the article:

Comment anonymously. Login not required.

How copy Windows file to Linux remote?

Copy files from Windows to Linux and vice versa.
Share network folders. Refer to SMB protocol and Samba..
Transfer files by using FTP. One of the best-known FTP servers in Linux is PureFTPd..
Securely copy files through SSH. ... .
Share data by using sync software. ... .
Use shared folders if Linux is running as a VM in your VM..

How copy file from local to Linux server?

Using SCP (SSH) With the SCP command, you can transfer files from your computer to your Linux server and vice versa. As this utility uses SSH to move files, you'll need the SSH credential of your server to transfer files.

Can I copy files via SSH?

A client can use an SCP to upload files to a remote server safely, download files, or even transfer files via SSH across remote servers.