The Secure Transfer Problem
You are a web developer working on your local MacBook. You have just finished writing a new configuration file (config.json) that needs to be deployed to your production Linux server hosted in the cloud.
In the past, you might have opened a clunky FTP client, typed in the server credentials, dragged the file across a graphical interface, and waited for it to upload. Not only is FTP slow to set up, but standard FTP is completely unencrypted, meaning anyone intercepting the network traffic can read your configuration file in plain text.
If you only need to transfer a single file (or a small handful of files) securely, setting up a complex backup tool like rsync is overkill, and using FTP is insecure. Instead, you should use the scp command. Short for “Secure Copy,” scp is a command-line utility built directly on top of the SSH protocol. It allows you to instantly, securely, and simply copy a file from your local computer to a remote server (or vice versa) using a single line of text in your terminal.
The Syntax of Secure Copy
The scp command follows the exact same logical structure as the standard Linux copy command (cp), which is: Copy [This File] to [That Destination].
Because it operates over SSH, the “Destination” part of the command is formatted exactly like an SSH login (username@ip_address:/path/).
If you want to copy the file config.json from your local Documents folder directly to the /var/www/ directory on a remote server with the IP address 192.168.1.50, you would type:
scp ~/Documents/config.json [email protected]:/var/www/
When you press Enter, the terminal will ask for the remote server’s SSH password (or silently accept your SSH key if you have one configured). The file will then be securely encrypted, transferred over the internet, and deposited precisely in the /var/www/ folder.
Downloading Files from the Server
The beauty of scp is that it works in both directions. If a server log file has crashed the website and you need to pull it down to your local laptop to read it, you simply reverse the order of the command.
The syntax becomes: Copy [That Remote File] to [This Local Destination].
To download error.log from the remote server to your local Desktop, you would type:
scp [email protected]:/var/log/error.log ~/Desktop/
Copying Entire Directories (-r)
By default, scp is designed to copy individual files. If you point it at an entire folder, it will throw an error and refuse to transfer the data.
To copy a folder and all of its contents (including sub-folders), you must use the -r (recursive) flag.
scp -r ~/Documents/website_code/ [email protected]:/var/www/
This will securely upload the entire directory structure to the remote machine.
Conclusion
Stop installing unencrypted, graphical FTP clients for simple file transfers. By utilizing the Linux scp command, you can leverage the encryption of SSH to quickly and securely push and pull files to any remote server directly from your command line.