Connecting to a Linux Virtual Machine Over the Network
Difficulty
Intermediate
Duration
40m
Students
451
Ratings
5/5
Description

This brief course provides you with some extra pointers for working in Linux.

Transcript

The purpose of this lecture is to answer some questions about connecting to a Linux system over a network. Specifically, this lesson will cover connecting to a virtual machine over a network. If you're using a virtual machine where you are connecting directly to it, or you have physical access to a Linux system, or you were able to connect to your Linux system over the network successfully, feel free to skip this lesson.

In this lesson, you'll learn all the steps involved in connecting to a virtual machine that is running Linux over the network. We'll talk about the settings needed in VirtualBox to allow for network connections. You will also learn how to check if the Linux system is ready to accept network connections and if not, how to prepare it for those network connections. If you're trying to access a virtual machine that is running on your system, the easiest way is to simply start up the virtual machine and interact with it through VirtualBox.

For this course, there really is no need to connect to the virtual machine over the network. I really recommend just typing into the virtual machine directly for this course. Here's an example. I went ahead and started VirtualBox on my local machine or my host system, and I have a VirtualBox that is running. I'm simply gonna open this VirtualBox, and you can see that it's in a window, in a VirtualBox window. You can see the menu options from VirtualBox itself.

So what I'm going to do is simply click into this virtual machine and then interact with it as if it were a real physical Linux machine that I'm sitting in front of. You know, I can do things like start a terminal or start a web browser, and I can just type right into this system. And that's really how I recommend you go through this course. But again, this lesson is for those of you that want to connect to a virtual machine like this over the network. If you are one of those people that want to connect to the virtual machine that is running on your computer, you need to do a couple of things that you may or may not have been introduced to at this point.

Unless you have a compelling reason to do so, I would suggest waiting until you finish the other lessons in this course before continuing with this lesson. The first thing you need to check is the network configuration of the virtual machine and VirtualBox. Make sure that the virtual machine is stopped, otherwise you will not be able to change the configuration if you need to do so. I'm going to highlight the virtual machine and click on Settings. So here's the virtual machine that I'm working with, and I can click on Settings up here, or I can even right click and click Settings here.

What we want to do is make sure that the network adaptor is set to bridged. I'll go here and click on Network, and we can see that it says it's set to NAT, it's attached to NAT. So I'm just going to select Bridged Adapter. You can make this work with NAT in conjunction with port forwarding but it's just simpler to use bridged networking in most cases, especially if you're working on a typical home network. Bridged networking allows the virtual machine to exist on the network just like any other machine. It makes it most act like actual hardware.

So now that you have bridged network configured, you can go ahead and start the virtual machine. I'll click OK to save my changes and then right click and start. Unless auto log-in is configured, you'll have to enter a username and password to get access to the Linux system. So to connect to a Linux system over the network, you need to make sure that an SSH server is installed and running, and we can check that by looking at the process table.

Let's go ahead and open a terminal. We can look at the process table by using the ps command. In this example, I'm going to use ps -ef, I'm gonna pipe that to grep. And I'm looking for sshd, which is the SSH daemon or the SSH server process. So in this case, no sshd process is running. We just see the grep command that we just ran but no sshd process. Let's check to see if it's even installed. This is an Ubuntu system, so I'm going to use the dpkg command to look for an SSH server. I'm gonna -l to list the installed packages, and I'm going to grep for ssh-server.

If this were a Red Hat based system, I would run rpm -qa to query the packages that are installed and list them all and then grep for ssh-server. Since this is not a Red Hat based system, RPM isn't installed. But at any rate, when we listed the packages, we had no SSH server packages returned. So that means SSH is not installed. On an Ubuntu system, what I need to do first is run sudo apt-get updates. After I've done the apt-get update, I'm going to run sudo apt-get install openssh-server and openssh-client. I'll make sure that the SSH client is installed so I can connect to other systems from this system if I need or want to.

I'll go ahead and answer yes to any of these questions here. And in just a second, you'll see that it gets installed. If I were on a Red Hat based system, like Red Hat or CentOS or Fedora, I would use sudo yum and install openssh-server and openssh-clients. Remember that the important thing here is that you install software with root privileges. So if sudo isn't configured, you would need to su to the root user and then run the apt-get or yum commands.

So now, let's look at the process table and check for SSHD. Okay, now we can see that SSHD is in fact running. If it wasn't running, I would start it with sudo service ssh start. That's for an Ubuntu system. On Red Hat, it's slightly different. It's sshd start. But in our case, it's running, so I don't need to do this. So now that SSHD is installed and running, we need to know the IP address of this virtual machine.

To get that information, we can run the /sbin/ip addr command. In this example, there are two network interfaces. You can see one colon and then some information and then a line that starts with two colon, and that is the second network interface. The first network interface is a virtual network interface that will be present on all systems. It's called the loopback device. It allows the system to communicate with itself. Also, it always has the IP address of 127.0.0.1.

The other network interface is the one that we want to pay attention to. So we'll look at the inet line here, and what appears before the forward slash is the IP address. On my system, it's 10.0.0.6. On your system, it's probably going to be something else.

Finally, let's go ahead and confirm our username with a whoami command. And in this case, it's adminuser. Many times, SSH access is disabled for the root user for security reasons, so you'll have to connect to the system with a different user. So if you try to connect to a system as root and it doesn't work, this is probably why. You'll also need to know the password for the account, and this case, on my system, with the image I'm using, the username is adminuser and the password is adminuser as well.

Again, this username and password will vary from system to system, of course. My physical computer or my host computer is also running Linux. In your case, it will probably be Windows or Mac. I'm going to open up a terminal and attempt to connect to the Linux system that is running in VirtualBox. If you're on a Mac, this is the exact same process.

Let me go ahead and open up my terminal. I'm going to run the ssh command. I'm gonna run ssh and then provide the username on the remote system. So on my local system, my username is Jason, but that's not what is on the remote system, so I have to supply it explicitly. So I'm going to type in adminuser, use the @ symbol, and then I'm going to take the IP address that we got from the ip addr command. So in this example, it's 10.0.0.6. And of course, on your system, it will probably be something different. It prompts me to verify the key. I'm going to say yes. And then, I need to enter the admin user's password, and I hit Enter, and here I am.

I'm logged into the virtual machine. I'm going to go ahead and just exit out of the virtual machine, and I'm going to demonstrate the use of PuTTY for the people connecting with PuTTY on Windows machines. I'm just going to start PuTTY here. I have PuTTY installed on my Linux system. To connect via PuTTY, I'll just click in the Host Name section here and enter the IP address of our virtual machine.

In this example, it's 10.0.0.6. And I'll go ahead and click Open. Here, you can see that PuTTY is asking you to confirm the host key, and we'll go ahead and click Accept. And now, we need to type in our username, which is adminuser for my system, and then the password. And then, so now you can see that we've successfully logged into this virtual machine over the network using PuTTY.

In this lesson, you learned how to connect to a Linux virtual machine over the network. You learned that first you need to set the network adapter settings to bridged networking mode. You also learned how to make sure that an SSH server is installed and running. Next, you were able to get the IP address of the Linux system by using the ip addr command.

Keep in mind that you may need to log into the system using an account other than the root account. Finally, you used the desired account and IP address to connect to the system over the network using a command line utility, such as SSH on a Mac or Linux system or using PuTTY on a Windows system.

About the Author
Students
21097
Courses
61
Learning Paths
18

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.