Transfer Files Securely Using SCP in Linux

The most common way to get terminal access to a remote Linux machine is to use Secure Shell (SSH). To work, the Linux server needs to be running a SSH server (OpenSSH) and at the other end you need a SSH client, something like PuTTy in Windows or the ssh command line tool on Linux or other Unix-like operating systems such as FreeBSD.

The attraction of SSH is that the connection between the two machines is encrypted. This means that you can access the server from anywhere in the world safe in the knowledge that the connection is secure. However the real power of SSH is that the secure connection it provides can be used for more than just terminal access. Among its uses is the ability to copy files to and from a remote server.

To prepare the server, you need to install the openssh-server package. On Ubuntu, you can install it from the Ubuntu Software Center or using the command line:

sudo apt-get install openssh-server

Next, you need to discover the IP address of the server. On Ubuntu, the IP address is shown in the Network applet in System Settings or you can use the command line:

ifconfig

In the output, look for the line starting with inet under eth0. In this example the IP addres of the server is 192.168.1.101.

ifconfig-inet-address

To test the SSH connection, move to the Linux client machine and type:

ssh 192.168.1.101

Where 192.168.1.101 is the IP address of the server. Enter your username and password when prompted and you will be connected to the remote machine. If you get a question about the “authenticity of host can’t be established” just answer “yes” to the question. It is a security check designed to make sure that you are connecting to your actual server and not an impostor.

Now that you have tested the SSH connection, you can start to copy files between the two machines. Secure copying is achieved using the scp. The basic format of the scp command is:

scp /filepath/to/file/to/copy user@IP-address:localpath

For example, to copy the file “backup.zip” from the local machine to the “backups” folder in the home directory of user “gary” on the remote server with the IP address of 192.168.1.101, use:

scp backup.zip gary@192.168.1.101:backups/

Similar to when you connect using ssh, you will be prompted for the password. You won’t be prompted for the username as that was specified in the command.

You can also use wild cards like this:

scp *.zip gary@192.168.1.101:backups/

To copy a file from the remote server to the local machine, just reverse the parameters:

scp gary@192.168.1.101:backups/backup.zip .

Notice the dot at the end of the command which means “the current directory,” as it does with the standard cp or mv commands.

And the same with wild cards:

scp gary@192.168.1.101:backups/*.zip .

To recursively copy a directory to a remote server use the -r option:

scp -r backups/ gary@192.168.1.101:backups/

And to copy a recursively copy a directory from the remote server to the local machine use:

scp -r gary@192.168.1.101:backups/ .

If you don’t want to place the incoming files in the current directory (note the dot at the end) then you can specify a different directory name:

scp -r gary@192.168.1.101:backups/ backups-from-server/

scp is a powerful and yet convenient way to copy files to and from a server without have to setup FTP or other file sharing servers. It has the added bonus that it is secure (something that can’t be said for a default FTP installation). To progress further, try experimenting with the -C option, which enables compression during the copy or the -l option which limits the bandwidth during the copy.