Note: Because this is an ongoing series, there is no certificate of completion available for this course.
Skill Level Intermediate
- [Instructor] As we work at the command line, we might want to transfer files from our local machine to the remote machine or vice versa. The SSH protocol gives us two methods of doing this: SCP and SFTP. SCP is short for Secure Copy and it's a command that you run on a local machine. It uses SSH credentials to open an encrypted connection of the remote machine and copy a file between the two. The syntax for it is to use the command scp and a source and a destination. It works pretty much like the regular CP or copy command except that the remote end of the connection is specified a little bit differently than the local one.
Let's say we wanted to copy a notes file from a remote server to our local system. The command would look like this: scp remoteuser@hostname:notes and then a space and then the destination, in this case, the documents folder. The remote portion of the command has a username@thehostname and will be prompted to enter that user's password when we run the command. The other part of the remote session is the colon. This represents the user's home folder. Any file that you want to copy from the remote end needs to be expressed relative to this or with an absolute path.
As it's written, this command will look for the file called notes in the remote user's home folder. So to copy, say, syslog from the var log folder, I would write a command like this instead. And that would copy the syslog file into the home local user documents folder on my local system, as long as the user has permission to do so. We can switch around which of the arguments is remote to send a file from our local machine to a remote machine. This would send the other notes file from the local system to the documents folder in the remote user's home folder.
On my client's system here, I'll create a file with the truncate command. I'll write truncate -s 30M and we'll call it myfile. That will make a 30 megabyte file. And now let's copy that to the server. I'll write scp myfile and then my username at the IP address of my server, colon. That'll copy it to my user's home folder on the server. I'll type in my password and the file's been copied. We can use wildcards with the SCP command as well, in case you need to copy a series of files all at once.
The other method of moving files around that SSH gives us, is called SFTP, or SSH File Transfer Protocol. It's not really related to the much older SFT protocol, but it emulates the commands of FTP over an SSH connection between two systems. FTP is notoriously insecure, so if at all possible, you should look into replacing it with SFTP wherever you can. The other method of moving files around that SSH gives us, is called SFTP, or SSH File Transfer Protocol. It's not really related to the much older FTP protocol, but it emulates the commands of FTP over an SSH connection between two systems.
FTP is notoriously insecure, so if at all possible, you should look into replacing it with SFTP wherever you can. Whereas the SCP command is a one-off command line sort of thing, SFTP allows a client to connect to a server with an FTP application and drag and drop files easily. This can be helpful for slightly more advanced management of files. Or as a transparent replacement for clients working with files on a web server. There are many client programs out there that support SFTP, some free and some paid. FileZilla is a free application that supports all of the major operating systems, so it's a popular choice for organizations to standardize on.
Unfortunately, the download side is full of ads and the installer may also include so-called bundled offers that you'll want to probably look out for and deselect. And that's a shame. I'd recommend getting the regular version, not the pro version, from filezilla-project.org. Here on their website, I'll choose to download the FileZilla client, and then I'll save it and I'll close my browser. Here in my downloads folder, I'll extract the archive and I'll run FileZilla.
Here in the host box, I'll type the IP address or host name of my remote server, in this case, 10.0.1.51, my user and my password. And then I'll set the port to 22. I'm prompted to accept the key or fingerprint, so I'll always trust this host, and I'll press OK. Now, here on the left side of the screen, is a listing of local files, and here on the right side, are the remote files on my server. In fact, if I scroll down, I can see my file, which we uploaded just a few minutes ago.
To copy files back and forth between the client and the server, just navigate where you want to go and drag and drop. SFTP also has a command line mode, which gives you a series of commands to navigate and transfer files. We can start that up with the sftp command and then a user @hostname. The SFTP prompt here has a few basic commands, but it's nowhere near as powerful as the regular shell. To see these commands, we can type help.
The basic commands for moving files around are get and put. I can also use the cd command to change directory and pwd to print the working directory. My remote working directory is the home folder, so let's take a look what's in there with ls. And here's my file. I'll download that with get myfile. If I wanted to send something to the server, I would use put instead. When I'm done using the command line interface, I'll type bye to exit. And I can see that myfile has been downloaded to the client machine.
That's SCP and SFTP, both helpful tools we get for free when using an SSH server. They make it really easy to move files back and forth without having to set up a separate service with separate credentials and so forth.