How to Secure Copy Files Over SSH in Linux

If you are managing a remote Ubuntu server, you will eventually need to transfer files between your local machine and that remote environment. While you could set up a complex FTP server, there is a much simpler, faster, and more secure method built directly into almost every Linux distribution. Learning how to secure copy files over SSH in Linux using the scp command is an absolute necessity for any system administrator or developer.

In this guide, we will break down the syntax of the Secure Copy Protocol (SCP) and show you how to quickly move files in both directions across your network.

What is the SCP Command?

The scp command stands for Secure Copy. It uses the same underlying technology as SSH (Secure Shell) to establish an encrypted connection between two computers before transferring any data. This means that even if someone is monitoring your network traffic, they cannot intercept or read the files you are transferring.

How to Transfer a File to a Remote Server

The basic syntax for the command is always: scp [source] [destination].

To copy a file from the computer you are currently typing on (your local machine) to a remote server, use the following structure:

scp /path/to/local/file.txt username@remote_ip_address:/path/to/remote/directory/

Example:

Let’s say you have a configuration file named nginx.conf on your laptop, and you want to upload it to the /etc/nginx/ directory on your web server (IP: 192.168.1.50) using the username “admin”.

scp nginx.conf [email protected]:/etc/nginx/

You will be prompted to enter the “admin” password. Once authenticated, the file will securely upload.

How to Download a File from a Remote Server

If you need to pull a log file down from your server to analyse it locally, you simply reverse the source and destination in your command.

Example:

Let’s download the error.log file from the server to our local Downloads folder.

scp [email protected]:/var/log/nginx/error.log ~/Downloads/

How to Transfer an Entire Directory

If you have a folder containing dozens of web development files, you do not need to transfer them one by one. You can use the -r (recursive) flag to copy the entire directory structure at once.

scp -r /local/website_folder/ [email protected]:/var/www/html/

Frequently Asked Questions

What if my SSH server uses a non-standard port?

By default, SCP connects over port 22. If your server administrator has changed the SSH port (for example, to port 2222) for security reasons, you must specify this using a capital -P flag.

scp -P 2222 local_file.txt [email protected]:/remote/dir/

Can I use SSH keys instead of a password?

Yes. If you have already set up SSH key authentication with the remote server, scp will automatically use your keys, allowing you to transfer files without typing a password every time.

By mastering this single command, you will significantly speed up your deployment and server maintenance workflows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.