How to configure a static IP address on CentOS 7 with VirtualBox
Tags: beginners, webdev, tutorial
Published: 2023-03-27 08:53:26

This article aims to explain how to configure a static IP address on a CentOS 7 virtual machine using VirtualBox.

We will go step by step, and at the end of this blog, you will be able to ssh into your virtual machine using a never-changing IP.

## What is a static IP?

Every computer has a random local IP address unless you have specified the contrary. These addresses are not fixed. It means that they could change.

In most cases, you don’t care about the IP address, but you usually do with virtual machines.

If you have a MySQL service running on a virtual machine, you would want to save the connection configuration once and re-use it every time. If the IP address changes, you have to modify the connection settings.

Another approach is to use port-forwarding. This approach is okay until you have 3 or more virtual machines with multiple services and ports to keep track of.

The following image shows the ideal local development environment with static IP addresses.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahu04972pqpyjlavwqky.png)

## Configure VirtualBox Networking

The app VirtualBox has some networking settings we have to set before changing the VM Linux configuration.

We want our VM to have the following:
- Access to the internet
- Access to our host computer

And we also want to be able to access the VM by IP.

### Step 1: Stop the VM

You have to stop the VM before doing the following steps.

### Step 2: Create Ethernet Adapter

Click on Tools - Networks and make sure you have an ethernet adapter created. Write down the IPv4 Prefix, because the static IP will be in this range.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oyc6ffvvuh602ax8diq.png)

In the image above:

- The gateway is 192.168.56.1 
- The network mask is 255.255.255.0 (24 bits)

The static IP of my VM will be in the 192.168.56.xx range.

### Step 3: Change adapters

Right-click on the virtual machine and choose the “Network” tab.
We are going to add 2 adapters:
1. The first one is going to be a NAT. This way the VM will have internet access.
2. The second one has to be a “Host-only Adapter” with the ethernet adapter of the previous step. This is the adapter that will have the static IP assigned.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdp3p5x88y5d4es10mmp.png)

## Configure VM Centos 7
Now that we have configured the VirtualBox networking, we will configure the inner VM networking settings.

### Step 1: Start the VM
Double-click on the VM or right-click and start.

### Step 2: Get the connection name
We know the static IP will be assigned to the second adapter, the Host-only Adapter. 

Let’s check the vm networking using `ip addr | head -n 20`:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/440kxqhw4ee5bwm2jtzp.png)

1. **loopback**
2. **enp0s3:** NAT Adapter
3. **enp0s8:** Host-Only Adapter

Now we know we have to assign an IP to the **enp0s8** device. To get its connection name, you have to execute the following statement:

`nmcli -p device`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3p04bn0zmqw1qak73k42.png)

The connection name is “Ethernet connection 1”.

### Step 3: Configure connection IP

There are two ways of doing it:
- Graphically with `nmtui`
- With bash statements and `nmcli`

In our case, we will execute some statements in the console.

```
connection_name="Ethernet connection 1"
ip=192.168.56.2
gateway=192.168.56.1
bits=24

nmcli con mod "$connection_name" ipv4.addresses $ip/$bits
nmcli con mod "$connection_name" ipv4.gateway $gateway
nmcli con mod "$connection_name" ipv4.method manual
nmcli con up "$connection_name"
```

If we execute `ip addr | head -n 20` again, we will see the previous IP address.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gerhssmowem6szxzwvfr.png)

### Step 4: Reboot

The last step is to reboot the VM

## Test the connection

Now we can connect to the VM using the command `ssh user@192.168.56.2`