Assign static IP address to Centos/RHEL 7 Virtual machines in Oracle VirtualBox

Leave a Comment

Hello readers, in this post we will learn how we can set up static IP address to the Virtual machines created using Oracle Virtual Box.


In case you want to install Oracle Virtual Box and deploy a Linux VM like Ubuntu or Centos on it, please read old post with detailed steps.


When we created these virtual machines, they have IP address assigned which are from the bridged adaptor on my Wi-Fi router network. So, the ip address that my virtual machines are getting are assigned by the Wi-Fi router using dhcp. As the IPs assigned by the DHCP router are dynamic so they can change anytime when you reboot the machines.  Sometimes, when you reboot the machines, you will find that the IP address assigned got changed than the one they had before.


So, in case you want to have your virtual machines to have static IP address, this cannot be achieved using the bridged network, so a alternate way is to create another network that will be host only network. 


It will be a network on my Laptop and my laptop will have a virtual interface on that network and all of the virtual machines will have a virtual interface on that network and we can assign static IPs for the VMs on that network.






In this post, we have performed steps on CentOS 7.9 Linux VM.


Open Host Network Manager from the File menu or Network option from the Tools menu



Host only Ethernet Adapter

Here you will see list of Host only networks that are already created, sometimes you will one of the default as shown in screenshot below already created, else you can create new host only ethernet network.



In this case, i am going to use default one with IP address range 192.168.56.1/24  Make sure dhcp server option is unchecked.





Once you have identified the host only network, you need to attach this network to your virtual machines. Right click on machine and choose settings and go to network.

Adapter 1 with Bridged Network for internet access 



In the adapter one, you can see it is attached to Bridged network which is required for internet connectivity if you want to download any packages. 






Go to Adapter 2 and select attached to : Host-only adapter , leave everything else as default.







Now we have two adapter attached to the machine, one is the bridged router and second one is host only adapter. Click on OK and power on the Virtual machine. You will see a new interface is attached and verified using the ip a s command or ifconfig command,  however IP address is still not assigned to the interface.


Configure Static IP Address in CentOS 7 / RHEL 7




Perform below steps to create a new config file for the newly added interface enp0s8


# cd /etc/sysconfig/network-scripts# vi ifcfg-enp0s8

Add below to the ifcfg-enp0s8

NAME=enp0s8

DEVICE=enp0s8

BOOTPROTO=none

DEFROUTE=yes

ONBOOT=yes

PREFIX=24

IPADDR=192.168.56.2

save and quit the file.


Restart the network service using below command



# systemctl restart network



Now, you will see that IP address 192.168.56.2 given in the config file is plumbed on the interface enp0s8



You can reboot the machine and test ssh via IP, You have successfully assigned static IP address to your Centos 7 /RHEL Linux machine.



0 comments:

Post a comment