Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "SFTP" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that it is possible to transfer files using an SFTP connection with an SSH key, and demonstrates how to securely transfer files between a remote machine and a local one.


Transcript from the "SFTP" Lesson

>> So I'm going to exit this so that I'm back in my, this one is still in primary and this one is still on secondary, right? So I still have two VMs up and running. We're going to set up how to do SFTP. If you've been a web developer for long enough, you know that the joys and the pains of deploying your app via FTP, right?

I think, as soon as you say FTP to a developer, they're either going to like long for simpler days. It's like, why do I need all this like continuous integration and continuous deployment, and why I just liked connecting my client, and just dragging and dropping files across, right?

But then every developer that's had to do that has accidentally deleted the wrong file in production and brought down their entire production server, present company included. So I'm actually gonna show you how to do that, if you really wanna do that yourself. Like I'm gonna show you how to blow up your server.

[LAUGH] I'm just kidding. But I am gonna show you how to move files between servers, right? Like for example, if I had files that were on primary,and I needed to get them to secondary, I'm gonna show you how to do that connection via FTP, which is called File Transfer Protocol, but specifically a variation of FTP called SFTP, Secure File Transfer Protocol.

Okay, so all I can say is, buckle up. This is probably the hardest part of what we're going to set up today. If you're going to, assume you just did the previous section with me with the SSH keys and everything like that, you're just going to say exactly the same command, just change the SH to FTP and then hit Enter.

I'm just kidding. This is the easiest thing. We already set up the SSH part. And so if you set up SSH, you set up SFTP at the same time. So that's like one of the really, really nice parts about SFTP is like when you set up SSH, you are inherently setting up SFTP as well, because they use the same protocol, they use the same ports, like everything kind of works the same.

So that's a really nice feature. You can disable, like so that maybe you don't want people to be able to connect to your server via FTP. And you can disable it but by default, it'll just work out of the box as long as you make one or the other work.

And so just by saying SFTP like that, I am now connected to secondary via SFTP. So this isn't quite exactly the same but now everything works a little bit different like for example, tab completion does not work inside of this. But now I can say pwd, you can see the remote working directory that I'm in is /home/brian.

And then if I say, lpwd, the L standing for local, you can see that my local working directory is in Ubuntu, right? So let's say I wanted to put a file from my primary onto my secondary. So I'm going to say lls for local list, right? And I can see, here's all the files that I have here on my local computer, and just pick any file, it doesn't really matter.

I'm going to pick status.txt, right? So I wanna put my status.txt from my local computer to my remote computer. So I'm going to say, put status.txt. And you can see here, that's really all it takes is now I have put from my local computer to my remote computer, right?

Now there's something really nice you can do here. If I put exclamation point here, I can actually run commands on my local computer. If I forgot to do something, I can put I say, echo, hello this is Brian. Then I'm gonna put that into a file here called file-to-put.txt.

So I can say, exclamation point cat file-to-put.txt. And you can see, cool, I have this file here called hello this is Brian. So I want to put that file, so I'm gonna put put file-to-put.txt, and then I wanna rename it something else so I can say, putted-file.txt, okay?

So now if we go a little over here and look at secondary, if I ls here, and I get out of here, you can see I have putted file, hello this is Brian. So I'm moving files across the network here, right? So coming back here, a lot of this works like how you would expect, but if you forget, you can say help, and this will just kinda help you through all the various different things that you can do.

So you can do, I showed you that one, ls, you can cd, right? So, where am I right now? On my remote directory, I can say cd.., and now if I say, pwd, I'm in this directory. So I guess something to kind of keep in mind when you're doing SFTP.

Normally, when you're on one computer, you're navigating around, you're in one directory at one time, right? And when you're in SFTP, you're on two directories, so you have a local directory and a remote directory that you're manning all at the same time. So again, I have a local working where I'm in home, Ubuntu, and I can say lcd for local change directory.

And now they will both be in home if I look at them, so I'm gonna change back both of them to their home directories. That's interesting. All right, so the remote one I want is Brian. There we go. So now they're back in their home directories. So just like you can put as well, you can also say, let's say I wanted to get status.

And just like that you can say, get status.txt, and I will call it, getted-status.txt. And by that way, I can download files from the remote server onto my local computer as well. And that's really about it. This is where tar comes in very useful, the command I showed you earlier.

This is useful because you can bundle up a bunch of files and you can just get one file instead of having to go and get every individual one. But yeah, that's it. That's how SFTP works. This is another thing I use pretty frequently, especially when I'm working with remote servers of just moving files around.

It's pretty convenient. So I guess, yeah, it does bear mentioning. So I've just shown you SFTP, which I say these days is probably the most common way of doing, moving files around. There is FTP, just plain FTP, and that's an older variant. You have to set it up separately.

Whereas like, this is nice cuz you set up SSH and you can apparently set up SFTP. When you're setting up FTP, you have to set it up differently. It works on a different protocol. It requires different keys and that kind of stuff, and it's just less secure. Whereas SFTP, it's all locked down.

It's all going through SSH, which is a very secure communication protocol. So this includes your use for the secondary VM, right? So you can go ahead and shut it down. You can delete it, you can do whatever you want, but we're not going to use the secondary VM anymore.

So in this particular case, I'm just going to say, exit. And I'm going to, yeah, get out of there, our of SFTP. One of my favorite things is the way you get out of SFTP to say, bye, that's it, and it's done. You can also say, quit and exit.

There's a couple ways to say, but because by works, I feel compelled to do it that way.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now