IntroductionTransferring files between computers can be a pain. FTP is somewhat clunky and old, and using online services isn't direct and is less than ideal for handling sensitive files. Git works well for code and text, but isn't the best for binary files and requires a repository to be configured. So, what's a good solution for sending files directly between computers? SFTP.
SFTP is a secure file transfer protocol that makes use of SSH to send files between computers. It is encrypted and direct. It allows you to use an existing service to send files, thereby reducing your attack surface, and it eliminates the need to rely on potentially vulnerable passwords for file transfers.
Before you go any further, you need to set up SSH keys for the machines you want to work with. If you don't know how, check out our guide on setting up SSH key based authentication.
The Case Against FTPFTP sucks. There's no way around it. FTP is susceptible to many security exploits, and continues to be a favorite target for would-be attackers. It also relies on password authentication, making for yet another possible way an attacker can destroy your system.
What makes all of this especially bad is the fact that FTPs purpose is to transfer files. That means that the mechanisms are already in place for an attacker to upload and execute malicious code on your machine.
If possible, avoid FTP.
ConnectingSFTP's syntax is very similar to SSH. Connecting to a server isn't very different using SFTP than it is with SSH.
You may need to use a different port for SSH/SFTP. In that case, specify it with the
Sending FilesFrom the SFTP shell, you can use SFTP's built-in commands to send and receive files. The commands for either direction are similar to
cpsyntax. To send a file, use the
putwill take a local file and place it into the current working directory of the remote machine.
sftp> put localfileThe
localfilewill be transferred to your current directory. Folders are similar. Like
cp, you can use the
-rflag to copy a directory and its content.
sftp> put -r localdir
Receiving FilesPulling files down works nearly the same with the
getcommand. In this case, it's not dependent on your current directory. You can specify a file path to copy and a location which you would like to copy it to.
sftp> get remotefileThis will just get the file and place it in your current directory.
sftp> get remotefile /path/to/localfileThe above will get the same remote file and place it in a specific local directory.
You can also use the
getto copy an entire directory.
sftp> get -r /path/do/remotedir path/to/localdirIf there is a need to preserve the exact permissions of the directory, add the
sftp> get -Pr /path/do/remotedir path/to/localdir