Connecting to the Virtual Machine via SSH7:18 with Jay McGavren
Thus far, we've been controlling the virtual Linux server as if we were seated in front of it at its dedicated keyboard. Servers out on the network will expect you to connect via SSH. Let's configure SSH now.
So far we've been controlling the server via a virtual monitor provided by 0:00 Virtual Box. 0:04 Everything we type is going directly to the server. 0:05 But to connect servers out on the Internet, 0:08 you would use the Secure Shell program, or SSH. 0:09 SSH connects you to a terminal on a remote computer, and it encrypts everything you 0:13 do so no one can eavesdrop on the passwords and commands you're sending. 0:18 From now on we're going to want to connect via SSH. 0:23 So let's set that up now. 0:26 SSH usually listens for network traffic on port 22, 0:28 and the SSH on our Virtual Server will be no different. 0:31 We can tell Virtual Box to open a port on our local computer, and send all 0:35 network traffic that it receives on that port to a port on your Virtual Server. 0:39 So we're going to open port 2222, that's 2,222, 0:44 on our computer and forward all traffic to port 22 on our Virtual Server. 0:48 When we use the SSH command to connect to port 2222 on our computer, 0:54 we'll wind up talking to the SSH service on our Virtual Server. 0:59 So here in the main Virtual Box app we're going to select our Guest computer, 1:03 And we're gonna choose settings for it, and we're gonna click on the Network tab. 1:11 Here for Adapter 1 we're going to expand the Advanced options set and 1:18 we're gonna click on Port Forwarding. 1:22 This is what we're going to use to forward port 2222 traffic to port 22 on the Guest. 1:24 We'll click the plus icon over here to add a new rule, and 1:30 we can name it whatever we want but we're going to call it SSH so 1:35 that we know what service we're forwarding. 1:39 The protocol should be TCP. 1:42 We're gonna leave the host IP blank so 1:43 that it will forward traffic from any host to any guest. 1:46 And we're going to set the host port to 2222 so 1:52 that we don't conflict with the existing SSH service on our host. 1:55 We'll forward all traffic that we get on port 2222 to port 22 on the Guest, 2:00 that'll be the guest SSH service. 2:05 Since we're probably going to be running a web server on this virtual server, 2:09 we're also going to want to forward port 80. 2:13 Again, this might conflict with an existing web server 2:16 running on port 80 on our host. 2:19 So we're going to forward traffic from port 8080 to port 80 on the Guest. 2:21 So let's click the plus sign to add a new rule. 2:27 We're going to label this one HTTP, protocol is still TCP. 2:29 We'll leave the host IP blank again, and 2:36 we'll forward from host port 8080 to guest port 80 2:39 Click OK when you're done, and click OK to save the settings changes. 2:46 Now SSH isn't actually installed on our guest OS yet. 2:53 We're going to need to install that now. 2:56 We'll use Ubuntu's Package Manager to automatically download and 2:59 install the software for us. 3:02 So we're going to start with the commands sudo, that stands for super user do. 3:05 Basically sudo is going to give administrative access on this computer, 3:10 since we need it in order to install new software. 3:14 Then we'll run the command for the Package Manager, that's apt-get, space. 3:18 We'll run the install sub command to apt-get, and then 3:24 we need to provide the name of the actual software package we want to install. 3:30 That's called openssh, all one word, 3:34 followed by -server, since this is going to be an SSH server not a client. 3:38 Once all that's entered press Enter to run the command, and it'll ask for 3:44 your password. 3:49 So enter the password that you set up when installing Ubuntu. 3:51 It'll ask if you wanna continue, and the default will be Yes, so 3:56 just hit Enter And 3:59 then it will download and install the software automatically and 4:05 put you back to the system prompt when it's done. 4:08 Now we're ready to switch back to a terminal on our host computer, and 4:12 try connecting to our guest computer via SSH. 4:17 Now we would have to type some complicated command like SSH, our user name, 4:21 127.0.0.1 to specify that we wanna connect to our local host. 4:27 And then we'd have to specify the port number with a flag-p, port 2222. 4:32 That's what we would have to type the way things are set up now. 4:40 But that's a complicated command to have to remember. 4:43 So, instead, what we're going to do is edit our SSH configuration 4:47 to make it easier to connect. 4:51 We're going to accomplish this by editing the SSH configuration on our host. 4:54 SSH configuration is stored within a directory in your user's home folder 4:59 named SSH. 5:03 So let's create that directory right now in case it doesn't exist. 5:04 So mkdir ~/.ssh. 5:08 And now we're going to open the SSH folder within our favorite editor. 5:13 And we'll create a new file within it named config. 5:17 There should be no extension on the config file name. 5:23 Within that config file, we're going to create an entry for a new host. 5:27 Note that this doesn't mean the host to our virtual machine. 5:30 This means host as in a host computer out there somewhere on the Internet. 5:34 We're going to call this Host hostcom, same name as our guest machine. 5:39 The host name can be anything we want, but it's better to have it match the name of 5:43 the virtual machine just to avoid confusion. 5:47 Then in the line immediately under that, 5:51 we're going to add a HostName configuration value. 5:52 And this is going to be the actual address that we attempt to connect to 5:57 to talk to this host. 6:00 Now since this is a virtual machine running on our local computer, 6:03 we're going to connect to local host. 6:07 That's at the IP address 127.0.0.1, that's the standard IP address for local host. 6:10 We're also going to add a port entry, and 6:18 specify the port that it should connect to. 6:20 Let's save this and then we can quit out of our editor. 6:26 Now that our SSH configuration is complete, we can go back into 6:30 our terminal on our host computer and type the command SSH again. 6:33 But this time all we have to type is our user name and 6:37 the name of the host entry that we set up in our configuration, hostcom. 6:41 It'll look that entry up in the configuration and 6:47 realize that it wants to connect to 127.0.0.1 port 2222. 6:51 Since this is the first time connecting to our guest computer via SSH, 6:57 it's going to ask if we're sure we want to continue connecting. 7:00 We'll go ahead and type yes. 7:03 Now I'll be asked to enter the password that we set up while configuring Ubuntu. 7:05 And that's it, we're now connected to a shell on our guest 7:12 Linux computer from a terminal on our host. 7:15
You need to sign up for Treehouse in order to download course files.Sign up