Nazaudy, a spark in your curious mind

Change IP addresses in Linux from DHCP to Static

This is a quick guide about how to Change IP addresses in Linux from DHCP to Static using the most popular Linux distros

First, issue any of these commands to find out the version of Linux that you're running:

cat /etc/os-release

 

For Debian and Ubutu family

For Debian we got Ubuntu, Xubuntu, Kubunto, Lubuntu, Mint, etc. Have a look at the file /etc/network/interfaces and you'll notice first the loopback and then your lan on DHCP by default

Version 18.04, Bionic Beaver

This is how the interfaces file looks for DHCP

Debian etc network interfaces command 

Needless to say, you already know in Ubuntu there are NO root account, you achieve root privileges but issues "sudo" in front of the vi command to edit it. If you want to set a Static IP, set the interfaces file as below, memorise the entries

auto eth0   #ensures the interface comes on automagically
iface eth0 inet static  #sets interface eth0 to static
    address 192.168.1.18
    netmask 255.255.255.0
    gateway 192.168.1.1
#from ubuntu 12.04 the /etc/resolv.conf is not use anymore for DNS, use the below instead
    dns-nameservers 192.168.0.100
    dns-search nazaudy.com

For your changes to take effects, restart the LAN by issuing these commands, though it is recommended to reboot the server (just to ensure that the new settings 'survive' a reboot)

ifdown eth0
ifup eth0

You can also use:

ifconfig eth0 down
ifconfig eth0 up

 If you want to change the mac addresses of your box, create the files "/etc/iftab" and add the mac for your Ethernet with this format: 

#/etc/iftab
eth0 mac 11:22:33:44:55:66

 

Port binding

For port binding you can do it in Linux using mod 4 (if your switch supports LACP) or mode 6 if it doens't and is using balance-alb (adaptive load balancing). In either case, start by installing the app needed to creating the binding sudo apt install ifeenslave ,then configure the interfaces files as below

auto eth0               #ensure the eth0 comes on automatically
iface eth0 inet manual  #set it to manual
bond-master bond0       #bind the iface to virtual iface called bond0

auto eth1
iface eth1 inet manual
bond-master bond0

auto bond0
iface bond0 inet static
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x
bond-mode 6       #vmware workstation doesn't support LACP, so we use mode 6
bond-miimon 100   #check the bonding link every 100 milisecods
bond-slaves none  #doesn't claim any slaves, as we already submitted them on inet0 and inet1

 To test the bonding, rung the following command and check that the traffic is split between interfaces

ifconfig | more

Uncomplicated Firewall

ufw (uncomplicated firewall) is disable by default in Ubuntu, but you can enable by doing sudo ufw enable ,the rules for the firewall are stored in /etc/ufw

sudo ufw allow apache  #allows apache in the firewall
sudo ufw allow ssh
sudo ufw status        #check the ports and services that are allowed
sudo ufw disable       #turn it off, if you are nuts and want to use iptables

 

While in CentOS the goodies go to /etc/sysconfig, in Debian/Ubuntu the stuff goes to /etc/default. The version of Ubuntu are name with the year of release plus the month, being the month normally either April (04) or October (10), and you should always have a LTS (Long Term Support) release installed, like on my box 18.04, as LTS are supported for 5 years. Funny thing to know as well, every release is also name with an adjective + name of an animal; run cat /etc/os-release to find out yours

 

 

For CentOS 7

Issue nmcli device to see the name of the card in use

Edit the "ifcfg-ens192" file or the "ifcfg-eth0", depending on the hardware of your OS

sudo vi /etc/sysconfig/network-scripts/ifcfg-ens192

Then add this linne so that Network Manager (nmcli) doesn't control that interface, and we can do all the config using the scripts

 NM_CONTROLLED=no 

For the DNS entries, edit the resolv.conf file

vi /etc/resolv.conf

On a machine with DHCP configure, as default, the file will be similar to this one:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO="none"  #this line is a change on the default script
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
UUID=6b452737-f177-4676-8ee5-0040f5c16309
NAME=ens192
ONBOOT="yes"

 

To configure a static IP address, modify the file and add this lines, changing as well the BOOTPROTO entry from "DHCP" to none

BOOTPROTO=none

IPADDR=172.16.10.10
PREFIX=24
GATEWAY=172.16.10.1
DNS1=192.168.0.10
DNS2=8.8.8.8
ZONE=trusted

IPV6_PRIVACY=no
DEFROUTE=yes

 

For Ubuntu

Visit the file /etc/network/interfaces, if your machine is on DHCP it should look like this:

 

For CentOS 8

Install the network script package, which for some stupid reason is not installed by default with CentOS 8, just imagine that you need the Internet in order to install these scripts, and the scripts need to be there to configure the Internet, hello CentOS community? wake up! Install the scripts by default, please, so at least we can connect the VM to the Internet the way we want it

dnf install network-scripts

 

References