Linux Network Administration with ping and ip Commands – POFTUT

Linux Network Administration with ping and ip Commands


Linux network stack is very powerful. Network stack provides a lot of functionalities where most of the enterprise network products can not offer. Here we will look basic and everyday network administration commands.

Check Network and Internet Connectivity

Network connectivity can be checked easily with ping and telnet tools. One method to check internet connectivity is ping to Google.

# ping google.com 
PING google.com (216.58.212.46) 56(84) bytes of data. 
64 bytes from sof02s18-in-f46.1e100.net (216.58.212.46): icmp_seq=1 ttl=54 time=45.0 ms 
64 bytes from sof02s18-in-f46.1e100.net (216.58.212.46): icmp_seq=2 ttl=54 time=45.1 ms
  • ping sends icmp packets
  • google.com is the destination of the icmp packets
  • line 3 and4 shows that google.com responds to our icmp packets
  • In line 2 216.58.212.46 is the ip address of the google.com and seeing this means our system can resolve domain names

Some  times ping can not be a feasible method to check internet connectivity. Because some network administrators disables icmp traffic in the network for security reasons. Here our alternative is telnet

# telnet google.com 80 
Trying 216.58.212.14... 
Connected to google.com. 
Escape character is '^]'. 

  • telnet is the tool to make tcp connection
  • google.com is the destination system
  • 80 is port number of the system
  • Line 3 means there is no problem to make tcp connection to the google.com

If we see other than Line 3 it means there is a problem.

List Network Interfaces

Linux system can have multiple interfaces. Listing them can be done different ways but most used and modern way is using ip commands.

# ip link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
3: br-b7a05d7f1b53: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default  
    link/ether 02:42:32:dc:12:91 brd ff:ff:ff:ff:ff:ff 
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff 
6: vethbc15dcd@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default  
    link/ether 22:fe:82:84:d4:d5 brd ff:ff:ff:ff:ff:ff link-netnsid 0

We issue ip link command and get all interfaces. Interfaces are numbered to be easily indentified.

LEARN MORE  Linux Lsof Command Tutorial With Examples

After the interface name came like ens .

After interface name the status of the interface cames. There are different options but most important is UP and DOWN option. If there is DOWN status it shows there is a problem or the interface is not set UP .

And there is MAC address like 52:54:00:ea:3f:9c .

Setting Interface UP/DOWN

We can set an interface with ip link command by using set .

# ip link set down dev docker0  
# ip link show docker0 
4: docker0: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff

We have set down the interface and than list the status of the interface. Now we can set up the interface again. Keep in mind if an interface is set down the network communication about the interface will be down.

# ip link set up dev docker0      
# ip link show docker0        
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff

Good, everything works as what is expected. We simple used up argument instead of down . If there is an dhcp server in the interface network it will get an ip address as soon as possible.

List IP Adress

Listing ip address is easy. All interfaces ip addresses will be listed.

# ip address show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
    inet 127.0.0.1/8 scope host lo 
       valid_lft forever preferred_lft forever 
    inet6 ::1/128 scope host  
       valid_lft forever preferred_lft forever 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3399sec preferred_lft 3399sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever 
3: br-b7a05d7f1b53: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default  
    link/ether 02:42:32:dc:12:91 brd ff:ff:ff:ff:ff:ff 
    inet 172.18.0.1/16 scope global br-b7a05d7f1b53 
       valid_lft forever preferred_lft forever 
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff 
    inet6 fe80::42:19ff:fe73:829a/64 scope link  
       valid_lft forever preferred_lft forever 
6: vethbc15dcd@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default  
    link/ether 22:fe:82:84:d4:d5 brd ff:ff:ff:ff:ff:ff link-netnsid 0

We may want to list specific ip address. Because in enterprise systems there will be a lot of interface and ip addreses.

# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3293sec preferred_lft 3293sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

Setting IP Address

We want to set ip address statically. If dhcp is enable in the interface network it will get automatically an ip address.

# ip address add 192.168.122.107/24 dev ens3 
# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 1130sec preferred_lft 1130sec 
    inet 192.168.122.107/24 scope global secondary ens3 
       valid_lft forever preferred_lft forever 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

Our interface has all ready an ip address.

  • add specifies that we want to add an ip address
  • 192.168.122.107/24 is ip address and netmask
  • dev ens3 is the interface we want to set ip address
LEARN MORE  What Is /etc/hosts File and How To Create and Edit It?

Remove IP Address

Removing ip address is similar to adding just add command is substituted with del command.

# ip address del 192.168.122.107/24 dev ens3    
# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3065sec preferred_lft 3065sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

As we see previously added ip address is removed.

List Routing Table

Routing table is a crucial part of the networking because the network packets are sent to the destination according to routing table. We can check routing table like below

# ip route show 
default via 192.168.122.1 dev ens3  proto static  metric 100  
172.18.0.0/16 dev br-b7a05d7f1b53  proto kernel  scope link  src 172.18.0.1 linkdown  
192.168.122.0/24 dev ens3  proto kernel  scope link  src 192.168.122.106  metric 100
  • route means we will use routing submodule
  • show lists the routing table content

Add Static Routing

Static route is a new route that is inserted by system administrator. Normally routing entries are inserted by system itself.

# ip route add 10.0.0.0/24 via 192.168.122.1 dev ens3
  • add used to add new route entry
  • 10.0.0.0/24 specifies destination network
  • via 192.168.122.1 sets next hoop to destination network
  • dev ens3 is the network interface where packet go out for the destination network

Remove Static Route

Removing static route is easier than adding it.

# ip route del 10.0.0.0/24
  • del issued for removel
  • 10.0.0.0/24 specifies the destination network which route will be removed

Adding Default Gateway

Default gateway is a route too. But default gateway is special because if there is no entry on the route table about destination network, packets are forwarded through default gateway.

# ip route add default via 192.168.122.1
  • default means the destination is all networks
  • via 192.168.122.1 specifies default gateway ip address
LEARN MORE  How To Choose USB Wi-Fi Adapter and Best Wi-Fi Adaptors

Removing Default Gateway

Default gateway can be removed easyli just changing add in the adding command to del like below

# ip route del default via 192.168.122.1
  • del delete this route
  • default route is default gateway
  • via 192.168.122.1 the ip address of default gateway

Leave a Comment