How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Reading Time: 4 minutes

Today, I will show you how to connect CentOS 7 to the Internet in Virtual Machine, and if you have challenges on how to install your first CentOS 7 server in Virtual Machine, you may check the article below: 

Install a CentOS 7 Minimal Server in Virtual Machine with screenshots



If you are ready, let us get started. Turn on your Virtual Machine, then login as root.

image-33 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

From here, go to the directory as shown below:

cd /etc/sysconfig/network-scripts/
image-34 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Let us backup our ifcfg-enp0s3 file first, by running:

cp ifcfg-enp0s3 ifcfg-enp0s3_backup
image-35 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

If you wish to see your back up file, run:

ls -lah | less

This should list files on our current directory as shown below:

image-37 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Now, we have our back up file, let us edit ifcfg-enp0s3 by:

vi ifcfg-enp0s3
image-38 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Then update the these lines below:

BOOTPROTO=static
ONBOOT=yes

Then add this line below:

IPADDR=192.168.2.100

IPADDR represents your desired IP for your Virtual Machine.

image-39 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Press ctr + c, the type :wq to save these changes.

Now, let us setup our gateway by going to sysconfig directory. To do so, run:

cd /etc/sysconfig
image-40 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Then, let us make a backup copy of our network file, by:

cp network network_backup
image-41 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

If you wish to see your backup file, run:

ls -lah | grep net

You should have these output below:

image-42 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Let us now add lines to network work file by:

vi network

You should have an empty file. Let us add these lines below:

NETWORKING=yes
HOSTNAME=centos7
GATEWAY=192.168.2.1

You should have this output below:

image-43 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Make sure to  press ctr + c, the type :wq to save your changes.

If you have challenges where to get your Default Gateway, you may open your cmd on your Windows host machine, then type:

ipconfig

Look for Default Gateway under your network adapter. In my case, I was using my Wireless Adapter. You should have these output below:

image-45 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

All set? Let us work with our name servers in resolv.conf file found in etc directory. To go about this, run:

cd /etc/
image-44 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

From here, make a backup copy of your resolv.conf file first, by:

cp resolv.conf resolv.conf_backup
image-46 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

You can view these changes by:

ls -lah | grep res
image-47 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

At this point, we should be able to add lines on our resolv.conf file. To do so, run:

vi resolv.conf 

This should let us edit our file using vi. For starters, vi is a Command Line Editor. If you wish to know more about vi, Ryan Chadwick made a simple explanation of how it works, see his work here

From here, add these links below:

nameserver 8.8.8.8
nameserver 8.8.4.4

These are Google Public DNS, and we will use them as our DNS resolver.

image-48 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Make sure to press ctr + c, the type :wq to save these changes.

We are almost done. Make sure to restart our network changes by running:

systemctl restart network
image-49 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Once completed, run :

ip addr

You should see your IP address as shown below:

image-50 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

One way to test if you have internet connection is to ping a site. In our case, I will ping google.com

image-51 How to connect CentOS 7 to the Internet in Virtual Machine with screenshots

Press ctrl + c to stop the command. At this point you should have a working CentOS 7 with internet connection. 

Great job.

Was this post helpful?

This Post Has 3 Comments

  1. SAnjeev Kumar

    very good, it worked for me

  2. elasticn00b

    It did not work for me. I re-checked the whole steps, but when I do a ping on google.com the response is Name or service not known.
    I am connected on ethernet. When I run IP ADDR, I see the desired IP address 192.168.1.20, the gateway is 192.168.1.1, but also see the dynamic IP of 10.0.2.15/24. What to do now?

    1. Kurt Obando

      Could you share the content inside ifcfg-enp0s3 ? For me to see what went wrong?

Leave a Reply