Configure IP Address and Routes on Linux

Loading

In this blog, we will configure a static IP address on Linux-based systems.

Configure Static IP on Ubuntu

The netplan config files are located at /etc/netplan and the default configuration file is /etc/netplan/01-netcfg.yaml

Open the network config file with an editor such as nano:

vi /etc/netplan/01-netcfg.yaml

network:
version: 2
renderer: networkd
ethernets:
ens35:
dhcp4: no
dhcp6: no
addresses: [192.168.20.1/24]
gateway4: 192.168.20.21
nameservers:
addresses: [1.1.1.1,8.8.8.8]

Once all your changes have been made, you can apply them by running:

sudo netplan apply

Configuring a DHCP address with Netplan

If you’d like to configure your Ubuntu server to run on DHCP this is how we will do it.

network:
version: 2
renderer: networkd
ethernets:
ens35:
dhcp4: yes
dhcp6: yes

Once all your changes have been made, you can apply them by running:

sudo netplan apply

Configure Static IP on Redhat/ Ubuntu

For RedHat-based systems, all net config info is under /etc/sysconfig/network-scripts/


# Locate all adapters on system
nmcli con show
nmcli con device

# Locate all existing IP Address
ip a

# To assign a IP address

nmcli con add con-name ens224 type ethernet ifname ens224 ipv4.method manual ipv4.addresses 172.16.99.102/24
Connection 'ens224' (6723bfce-5f84-4401-b995-c9fc4457e8b2) successfully added.

# To assign a gateway
 nmcli con mod ens224 ipv4.gateway 192.168.99.253

# To configure a static IP
nmcli con mod ens224 ipv4.method manual

#To add DNS entry
 nmcli con mod ens224 ipv4.dns "192.168.11.10"
 
# Reload the interface
nmcli con up ens224

# To restart network services
systemctl restart network
or
systemctl restart network.service

Configure Secondary IP on Redhat/ Ubuntu

# To add a secondary ip to the same interface using nmcli command
  nmcli con mod ens224 +ipv4.addresses "172.16.99.103/24"

Disable Network Interfaces on Redhat using NMCLI

# To Shut an interface using if up and down
ifup ens160
ifdown ens160


# To Show All network connections

[root@san ~]# nmcli con show
NAME                UUID                                  TYPE      DEVICE 
ens160              9ecab589-8a09-3e57-8895-a0d118b39be0  ethernet  ens160 
lo                  2156d16c-52b9-4bb6-a05c-83240ecf43cc  loopback  lo     
Wired connection 1  f945af43-4358-34ad-91ce-b50c8c0d6444  ethernet  ens224 
[root@san ~]# 

# To Shut an interface

[root@san ~]# nmcli connection down Wired\ connection\ 1 
Connection 'Wired connection 1' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@san ~]# 

# To Show All network connections

[root@san ~]# nmcli device status 
DEVICE  TYPE      STATE                   CONNECTION 
ens160  ethernet  connected               ens160     
lo      loopback  connected (externally)  lo         
ens224  ethernet  disconnected            --         
[root@san ~]# 

# To Up an interface

[root@san ~]# nmcli connection up Wired\ connection\ 1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@san ~]# 
[root@san ~]# 

# To Add a GW to interface
route add -net 192.168.11.0 netmask 255.255.255.0 gw 192.168.11.253

Check Network Usage Statistics

ethtool is an excellent utility for checking the network card status

# Install ethtool 
  yum install -y ethtool

# View card stats
  ethtool -S ens224 

# Check Auto-negotiation, RX and TX of a Particular Interface on Linux
  ethtool -a ens224

# Change speed
  ethtool -s ens224 speed 100
  ip link set ens224 up


#  Enable/Disable Auto-Negotiation for Ethernet Device
  ethtool -s ens224 autoneg off
  ethtool -s ens224 autoneg on

#  Making changes persistent will need an entry as shown

vi /etc/sysconfig/network-scripts/ifcfg-ens224
ETHTOOL_OPTS="speed 1000 duplex full autoneg off"

Check Port Usage Statistics

netstat -a | more

netstat -ltnp | grep -w ':80' 	
# netstat -o state established '( sport = :http or sport = :https )'

To scan all open/listening ports in your Linux system, run the following command (which should take a long time to complete).
$ sudo nmap -n -PN -sT -sU -p- localhost

fuser 80/tcp

1. Viewing the Network Routing Table
# netstat -nr

2. Display Network Interface Statistics
# netstat -ai

3. Show Network Connections
# netstat -ant

4. Show Network Services
netstat -pnltu
(Visited 133 times, 1 visits today)

By Ash Thomas

Ash Thomas is a seasoned IT professional with extensive experience as a technical expert, complemented by a keen interest in blockchain technology.

Leave a Reply