Check out a free preview of the full Full Stack for Front-End Engineers, v3 course

The "SSH Key Recap" Lesson is part of the full, Full Stack for Front-End Engineers, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Jem quickly reviews creating an SSH key and logging into a server with that key. Modifying the SSH config to define the identity file automatically and adding the private key to keychain are also covered in this segment.


Transcript from the "SSH Key Recap" Lesson

>> All right, so let's run through creating an ssh key one more time, just in case I lost anybody on the way. I know some people using Windows commands, should be the same actually, but it might look a little bit different. So what we did was, to we create a new ssh key, we went, We went into the ssh directory, so that's cd ~/.ssh.

Then we said ssh-keygen, And then we gave it a name, we called it full stack front end, already did that, so I won't do it again, but call it fsfe. And then once you're all done, I'm just gonna exit out of there. And you ls full stack front end, if you have a bunch of keys for some reason, remember we talked about using grep to find the actual thing we're looking for.

So you should have two, two keys in there, once the private key, the full stack front end, then the .pub which is your public key. So to get the public key, we need to paste that into Digital Ocean so it knows how to encrypt our ssh tunnel when we connect to our server.

And it'll put the public key onto the server for us as well. That's part of setting up the server. So we're gonna cat fsfe, the public key, .pub. We copy all of this, And then we went through the exercise of picking a datacenter, Ubuntu LTS by creating a new ssh key.

Then we paste our key in here, like so, we give it a name. And you can call this whatever you want, as long as you remember what it's tied to on your computer. Then you select a key, then you say we're creating one droplet, and then you give it a host name.

You can call it whatever you like. And then you hit Create Droplet, which I won't do cuz I already did one, and I don't wanna get confused myself with so many servers running. But you go ahead and create that droplet. So then you'll have a droplet that's getting spun up with your SSH key pre-installed, the public key installed, and you have the private key.

So once that's up and running, You should have something here that says droplet or something like that. And then you copy, or you can always select. So this is the IP address of your brand new server you just set up. So to log in to our server, we need to pass it the identity file.

So we're gonna say ssh, we'll use -i to specify where our private key is. And we say ~/ cuz it's an ssh, even though we're already there, it doesn't hurt to be specific. Your private key, so the one that does not end at .pub, and remember, the first user always created on a server is called root, indicates root access.

So we're gonna say root@ our IP address. And if we did everything right, We're now logged in. So you may run into something where it says something along the lines of, as we did earlier, the authenticity of the hosts, blah, blah, blah, your IP address can't be established.

That's essentially saying, hey, you've never ssh into this computer before. Are you sure it's the right one? And this is really important, because let's say you were sshing into How do I know that hasn't been hijacked by someone else? We don't, because the IP address can change underneath us.

And so we're assuming we're connecting to one host, but it's actually something completely different. The computer checks that using the server's SSH key and says, hey, we've never seen this before. Are you sure this is the right thing? So the first time you connect your server, you're gonna get a prompt like this.

The first time you connect to any new server that you've never connected before, you're gonna get a prompt like this. You can generally say yes. If this ever happens and it's not expected, do not say yes cuz then you have different problems at that point. And if you wanna check out, this isn't really necessary, but if you wanna see who all you've connected to in the past, we can look at the, oops, we can look at the known host file.

So I'm gonna exit out of my server. To exit out of your server, you just type exit. Probably you should put that somewhere in there, but you can guess, or you close your terminal, it'll close down the SSH connection, but then you have to make a new terminal, so let's type exit.

So we can look at the known hosts, so we can cat ~/.ssh/ n and auto tab, so something called the known_hosts file. So this just keeps track of every single computer we've ever used an SSH key to connect to, it just keeps a long, long history. And you can see I've connected a lot of computers.

And again, it's just an extra security precaution saying like, hey, are you sure you know what you're doing? Okay, we're gonna remember this for next time, and it won't bother you again. Like I said, if you're ever SSHing into your server and you get this prompt again, something is wrong, and you should double-check before you connect.

Okay, hopefully we're all there. Hopefully, we all successfully connected into our server. You know what I didn't like? I'm pretty lazy, and I wanna shortcut as much as I possibly can. I don't wanna have to specify my identity file every single time. I think SSH should pick that up by default.

So we can use that by modifying our SSH config. So let's go ahead and, you can follow along here. So we're already in SSH, but I like to be specific. So I'm gonna vi, And gonna go to SSH, the directory, and I'm gonna say config. And I wanna make sure that the AddKeysToAgent is true and that we're using the keychain.

So here's where I can specify whatever my default identity file is. I can lay them out explicitly. Or even better, I can just add it to the keychain, so it'll always automatically run through every single pair whenever I'm connecting to an SSH client. Really, really useful thing, especially if you're connecting to a bunch of servers at a time and you have a bunch of keys.

You add them to your identity file. SSH will try every single one of them until it gets the right one. There's no shame in trying all of them. But most importantly, we wanna make sure the AddKeysToAgent says yes. We want the UseKeychain also to say yes. And we're just gonnaclose vm because we're not, I didn't make any changes, you may have made a change.

So I'm just gonna say :q. So to add something to the keychain, we're gonna use ssh add, ssh-add. And this one's a little funky because Apple OSX has its own kind of strangeness, so we have to type in this specific command, --apple-use-keychain and the name of our key.

So we're putting our private key on the keychain. So it's --apple, -use-keychain and then name of our private key, which is fsfe, all right? And it's been added. So now, if I did things correctly, I should be able to SSH without having to specify that identity file. I have not memorized my IP, so I'm gonna jump back to DigitalOcean.

I'm gonna copy this again. So now we can SSH, @ our IP. And now we're in, and now we don't have to specify that identity file every single time, much more convenient and much faster. Because once you're working on the server in the back end, you'll be switching back and forth quite often.

So this just saves a couple of keystrokes. Someone was asking, vi, is it a shortcut for vim? Yes, you can say vim or vi. They both open Vim. Technically, Vim is vi improved, but it's like PHP. What do we say PHP stands for? PHP, Hypertext Preprocessor, yeah. So it's kind of the same story.

Someone's asking, is the command --apple/use-keychain? Yes, that's only for OSX computers. Windows will use a different command, I'm not sure, but I think it can just use the regular SSH. If you man SSH, you should be able to use the standard config. If there's not a config file in your SSH, you can just create one.

Just in your.ssh folder, just create config, and then put the config in there. And looks like someone's getting, can't connect on port 22 to their IP, they're getting a parse error. That usually means you copied the public key, or maybe you're missing a line or out of the space somewhere in there.

So just double check that and feel free to delete the key and readd it or create another key if you need. We can create as many SSH keys as we want. Just don't lose track of them because you'll be like, which one is it? It's a whole thing.

It's a whole thing.

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