DEV Community

Cover image for [Linux] Network Services (Part 1)
Waji
Waji

Posted on • Edited on

[Linux] Network Services (Part 1)

I have drafted some of the Network status related tools/commands in Linux along with some hands-on

👉 Just a note that I will be using CentOS7


✨Commands for checking network status

  • ifconfig
ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.129  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::f19a:98c6:d308:da7d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b1:be:7a  txqueuelen 1000  (Ethernet)
        RX packets 199  bytes 18537 (18.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 177  bytes 18785 (18.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Enter fullscreen mode Exit fullscreen mode

We can use ifconfig ens32 down to deactivate the ens32

ifconfig ens32 down
Enter fullscreen mode Exit fullscreen mode

using up instead of down will change the status to active again

  • netstat
netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1306/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1146/sshd           
tcp        0    396 192.168.1.129:22        192.168.1.1:61202       ESTABLISHED 1407/sshd: root@pts 
tcp        0     76 192.168.1.129:22        192.168.1.1:49870       ESTABLISHED 1589/sshd: root@pts 
tcp6       0      0 ::1:25                  :::*                    LISTEN      1306/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1146/sshd 
Enter fullscreen mode Exit fullscreen mode

the 't' option makes it to show only the tcp packets

✨Command for checking the routing table

route

route -n 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.2     0.0.0.0         UG    100    0        0 ens32
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens32
Enter fullscreen mode Exit fullscreen mode

We can also check ssh status

netstat -antp | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1146/sshd           
tcp        0    100 192.168.1.129:22        192.168.1.1:49870       ESTABLISHED 1589/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      1146/sshd 
Enter fullscreen mode Exit fullscreen mode

ESTABLISHED status shows that we are connected to this Linux system using SSH

✨Ping testing command

ping

ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=41.9 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=42.1 ms
Enter fullscreen mode Exit fullscreen mode

we can add the -c + 'no of times you want' to limit how many times the ping message displays

✨Checking Address Resolution Protocol

arp

arp -a
gateway (192.168.1.2) at 00:50:56:e6:7d:7d [ether] on ens32
? (192.168.1.1) at 00:50:56:c0:00:08 [ether] on ens32
Enter fullscreen mode Exit fullscreen mode

✨Checking the DNS name

nslookup

nslookup
> www.google.com
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   www.google.com
Address: 172.217.24.228
Name:   www.google.com
Address: 2404:6800:4005:80c::2004
Enter fullscreen mode Exit fullscreen mode

if the nslookup command doesn't work initially, we will have to install the bind-utils package

We can also use another format

dig www.google.com
Enter fullscreen mode Exit fullscreen mode

✨Checking the network daemon

systemctl status network
â—Ź network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: active (exited) since Mon 2023-02-06 10:28:01 KST; 2min 14s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1015 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)

Feb 06 10:28:00 Linux-2 systemd[1]: Starting LSB: Bring up/down networking...
Feb 06 10:28:01 Linux-2 network[1015]: Bringing up loopback interface:  [  OK  ]
Feb 06 10:28:01 Linux-2 network[1015]: Bringing up interface ens32:  [  OK  ]
Feb 06 10:28:01 Linux-2 systemd[1]: Started LSB: Bring up/down networking.
Enter fullscreen mode Exit fullscreen mode

Some Hands-on

- âś” Editing the ifcfg-ens32 NIC config file

cd /etc/sysconfig/network-scripts
vi ifcfg-ens32

TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens32
UUID=dcfd3416-5a10-4265-9102-294e578c015e
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.1.129
PREFIX=24
GATEWAY=192.168.1.2
DNS1=8.8.8.8
Enter fullscreen mode Exit fullscreen mode

removed all of the IPv6 settings as we aren't using it

we can notice that we can also change the IPaddress, Gateway information and DNS

A small test we can perform is that we can edit our IP address here,

IPADDR=192.168.1.150
Enter fullscreen mode Exit fullscreen mode

If we restart the daemon, we will be kicked out of the SSH as the IP was set differently.

If we SSH using the .150 IP,

Connecting to 192.168.1.150:22...
Connection established.
Enter fullscreen mode Exit fullscreen mode

we can see that the connection was successful


- 👉 setting up a new network interface

Created a new network adapter inside the VM and checked the network manager inside the cli,

nmcli connection
NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  3fe514bc-907a-362c-b85d-b999e6450132  ethernet  ens33  
ens32               dcfd3416-5a10-4265-9102-294e578c015e  ethernet  ens32  
Enter fullscreen mode Exit fullscreen mode

we can confirm a new ens33 device

We will be copying this UUID

Now, we can duplicate the ifcfg file inside the network-scripts directory for the ens33 adapter

cd /etc/sysconfig/network-scripts
cp ifcfg-ens32 ifcfg-ens33
Enter fullscreen mode Exit fullscreen mode

Editing inside this file,

vi ifcfg-ens33

TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens33
UUID=3fe514bc-907a-362c-b85d-b999e6450132
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.130
PREFIX=24
GATEWAY=192.168.1.2
DNS1=8.8.8.8
Enter fullscreen mode Exit fullscreen mode

replaced the UUID with the one that we copied before

After saving this file, we can restart the daemon

systemctl restart network 
Enter fullscreen mode Exit fullscreen mode

Finally, checking the ens33 adapter

ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.130  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:feb1:be84  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b1:be:84  txqueuelen 1000  (Ethernet)
        RX packets 35  bytes 2100 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 309  bytes 49826 (48.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Enter fullscreen mode Exit fullscreen mode

now we can SSH via this network adapter IP address as well

There is another way to configure using the nmtui command that comes with a rather simple GUI

nmtui GUI


- 👉 Using IP Binding

  • Providing multiple IP addresses to one NIC
  • Used mostly in Linux systems that run different services so that each service can get a unique IP address

We will copy the config file for the ens33 interface

cd /etc/sysconfig/network-scripts
cp ifcfg-ens33 ifcfg-ens33:0
Enter fullscreen mode Exit fullscreen mode

Editing the new config file

vi ifcfg-ens33:0

TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens33:0
UUID=3fe514bc-907a-362c-b85d-b999e6450132
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.1.131
PREFIX=24
GATEWAY=192.168.1.2
DNS1=8.8.8.8
Enter fullscreen mode Exit fullscreen mode

only the IP address and the name + device section was altered

Now if we restart the daemon and check the network details,

systemctl restart network

ifconfig
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.131  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:b1:be:84  txqueuelen 1000  (Ethernet)
Enter fullscreen mode Exit fullscreen mode

ens33 is a single network interface but we have given a **virtual* IP to that interface*

đź’ˇ We can add more virtual interfaces by creating the config files using :1, :2 and so on after the ens33 interface name

We can also remove virtual interfaces by simply deleting the config file

rm -rf ./ifcfg-ens33:0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)