Setting up networking (I can has internetz)

In my previously misleadingly titled “Setting up an environment” post, we created a brand new minimal CentOS 7 installation. The title is misleading in the sense that in order to set up an environment, I’ll need far more than a few posts to explore and configure an ideal environment.

In this post, we’ll configure networking and make sure we can reach the Internet!

Are we connected?

Fire up your virtual machine and login as root. ping cssigniter.com. Oh, unknown host! Is cssigniter.com down? Let’s try Google. ping google.com. Unknown host again… What wrong? Let’s read a bit.

Screenshot of failed ping attempts.
I would expect networking to work out of the box. But it doesn’t 🙁

Let’s Google the term centos 7 networking. It seems we are not alone here. The first result we get is a blog post that says “After installing Centos 7, you may not able to connect network in that machine“, and the second one is the FAQ section of the CentOS website.

Let’s read first the CentOS 7 FAQ as it’s more authoritative…
Did you read it? Yes? Good. It says networking is handled by NetworkManager, that network interfaces are disabled by default, and that we could have enabled Ethernet networking during the installation (which we didn’t know at the time, so now it’s too late). It also says how to enable it from a GUI after the installation, a GUI that we didn’t install. It goes on with instructions in case we don’t use NetworkManager, but we do (default installation, remember?).

Let’s go back to our first Google result, an article called How to Setup network on centos 7. Did you read it? So, it seems we can have the option of using a command line tool called nmcli, a text-based UI tool called nmtui, or directly editing some configuration files.

As I want to get a running system with the less fuss possible, I’ll just use the nmtui tool to get this over with. If you want to do it in any of the other ways, either follow the instructions on the article above, or consult the RedHat’s documention of nmcli. After all, CentOS is just a re-branded version of RHEL.

So, let’s run nmtui and select “Edit a connection”. We can move the cursor with the keyboard’s arrow keys. Once selected, press the keyboard’s Enter/Return key to enter that menu. Then select the appropriate interface (I only have one) and press the “Edit…” button. You might need to press Tab to move from the list of interfaces to the list of available actions. Once inside the “Edit Connection” dialog, move to “Automatically connect” and press the keyboard’s Space key to check that option. Next press the “OK” button and then the “Quit” button. Re-run the nmtui tool, but this time select the “Activate a connection” option. Select the interface and then press “Activate”. If you’re lucky, everything will be fine and the “Activate” button will be renamed to “Deactivate”. I got a “Could not activate connection: Activation failed” message.

That’s just great. I quit the tool and tried to ping google.com. Same luck as before. Just out of curiosity, I’m restarting the machine (by issuing the reboot command on the command line) to see if it will automagically work. Login. Retry pinging. Nope. No magic here.

Run nmcli d (d stands for device). On my enp0s3 interface (that would be my Ethernet adapter) the state reads as “connecting (getting IP configuration)”. Perhaps my router is slow? I re-run it after a while. Still the same. Re-run it a bit later. Now it just says it’s disconnected :'(

Screenshot of nmcli run repeatedly and showing disconnected states.
nmcli telling me it hates me.

As a last resort, I will restart both the VM and my router. I know it’s a bit unorthodox but my router is a piece of crap, handed to me free by my ISP, and it generally gives me crap every now and then. So I shutdown now the VM and physically turn of my router for a couple of minutes. I first turn the router back on, and once a connection is established with my laptop (so I’m sure I have internet access in the first place) I start the virtual machine as well.

Lo and behold, it works!

Screenshot of nmcli in connected state.
Networking works after a router restart.

Let’s actually confirm that it indeed works by pinging a few hosts (i.e. Google). Press Ctrl-c to stop pinging.

Screenshot of ping utility pinging google.com

Pinging google.com actually works!

At this point, I shut down my VM and my laptop as I needed to go out. When I came back and restarted everything, I couldn’t get an IP, even by restarting my router. I updated VirtualBox to the latest version (v5.0.20 at the time of writing) just in case it was a bug or something, and it now seems to be working perfectly, each and every time. The point here is: for most people on common home routers, the above steps should lead to a connected virtual machine. If not, then something is off, either with your hardware or your software. Updates and restarts are always a good start before troubleshooting.

That’s all for now. Next, we’re going to make our machine accessible without knowing its IP address.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *