Monday, October 9, 2017

Examining network connections on Linux systems

There are a lot of commands available on Linux for looking at network settings and connections. In today's post, we're going to run through some very handy commands and see how they work.

One very useful command is the ifquery command. This command should give you a quick list of network interfaces. However, you might only see something like this -- showing only the loopback interface:

$ ifquery --list  lo  

If this is the case, your /etc/network/interfaces file doesn't include information on network interfaces except for the loopback interface. You can add lines like the last two in the example below -- assuming DHCP is used to assign addresses -- if you'd like it to be more useful.

# interfaces(5) file used by ifup(8) and ifdown(8)  auto lo  iface lo inet loopback  auto eth0  iface eth0 inet dhcp  

The related ifup and ifdown commands can be used to bring network connections up and shut them down as needed provided this file has the required descriptive data. Just keep in mind that "if" means "interface" in these commands just as it does in the ifconfig command, not "if" as in "if I only had a brain".

The ifconfig command, on the other hand, doesn't read the /etc/network/interfaces file at all and still provides quite a bit of useful information on network interfaces -- configuration data along with packet counts that tell you how busy each interface has been. The ifconfig command can also be used to shut down and restart network interfaces (e.g., ifconfig eth0 down).

$ ifconfig eth0  eth0      Link encap:Ethernet  HWaddr 00:1e:4f:c8:43:fc            inet addr:  Bcast:  Mask:            inet6 addr: fe80::b44b:bdb6:2527:6ae9/64 Scope:Link            UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1            RX packets:60474 errors:0 dropped:0 overruns:0 frame:0            TX packets:33463 errors:0 dropped:0 overruns:0 carrier:0            collisions:0 txqueuelen:1000            RX bytes:43922053 (43.9 MB)  TX bytes:4000460 (4.0 MB)            Interrupt:21 Memory:fe9e0000-fea00000  

The RX and TX packet counts in this output are extremely low. In addition, no errors or packet collisions have been reported. The uptime command will likely confirm that this system has only recently been rebooted.

The broadcast (Bcast) and network mask (Mask) addresses shown above indicate that the system is operating on a Class C equivalent network (the default) so local addresses will range from to

The netstat command provides information on routing and network connections. The netstat -rn command displays the system's routing table.

$ netstat -rn  Kernel IP routing table  Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface         UG        0 0          0 eth0     U         0 0          0 eth0   U         0 0          0 eth0  

That entry in the above output is only necessary if you are using or planning to use link-local communications. You can comment out the related lines in the /etc/network/if-up.d/avahi-autoipd file like this if this is not the case:

$ tail -12 /etc/network/if-up.d/avahi-autoipd  #if [ -x /bin/ip ]; then  #       # route already present?  #       ip route show | grep -q '^[[:space:]]' && exit 0  #  #       /bin/ip route add dev $IFACE metric 1000 scope link  #elif [ -x /sbin/route ]; then  #       # route already present?  #       /sbin/route -n | egrep -q "^[[:space:]]" && exit 0  #  #       /sbin/route add -net netmask dev $IFACE metric 1000  #fi  

The netstat -a command will display all network connections. To limit this to listening and established connections (generally much more useful), use the netstat -at command instead.

$ netstat -at  Active Internet connections (servers and established)  Proto Recv-Q Send-Q Local Address           Foreign Address         State  tcp        0      0 *:ssh                   *:*                     LISTEN  tcp        0      0 localhost:ipp           *:*                     LISTEN  tcp        0      0 localhost:smtp          *:*                     LISTEN  tcp        0    256      ESTABLISHED  tcp6       0      0 [::]:http               [::]:*                  LISTEN  tcp6       0      0 [::]:ssh                [::]:*                  LISTEN  tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN  tcp6       0      0 ip6-localhost:smtp      [::]:*                  LISTEN  

The netstat -rn command displays the system's routing table. The address is the local gateway (Flags=UG).

$ netstat -rn  Kernel IP routing table  Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface         UG        0 0          0 eth0   U         0 0          0 eth0  

The host command works a lot like nslookup by looking up the remote system's IP address, but also provides the system's mail handler.

$ host has address mail is handled by 10  

The nslookup also provides information on the system (in this case, the local system) that is providing DNS lookup services.

$ nslookup  Server:  Address:    Non-authoritative answer:  Name:  Address:  

The dig command provides quitea lot of information on connecting to a remote system -- including the name server we are communicating with and how long the query takes to respond and is often used for troubleshooting.

$ dig    ; <<>> DiG 9.10.3-P4-Ubuntu <<>>  ;; global options: +cmd  ;; Got answer:  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28679  ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1    ;; OPT PSEUDOSECTION:  ; EDNS: version: 0, flags:; udp: 512  ;; QUESTION SECTION:  ;                 IN      A    ;; ANSWER SECTION:          78146   IN      A    ;; Query time: 37 msec  ;; SERVER:  ;; WHEN: Mon Oct 09 13:26:46 EDT 2017  ;; MSG SIZE  rcvd: 58  

The nmap command is most frequently used to probe remote systems, but can also be used to report on the services being offered by the local system. In the output below, we can see that ssh is available for logins, that smtp is servicing email, that a web site is active, and that an ipp print service is running.

$ nmap localhost    Starting Nmap 7.01 ( ) at 2017-10-09 15:01 EDT  Nmap scan report for localhost (  Host is up (0.00016s latency).  Not shown: 996 closed ports  PORT    STATE SERVICE  22/tcp  open  ssh  25/tcp  open  smtp  80/tcp  open  http  631/tcp open  ipp    Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds  

Linux systems provide a lot of useful commands for reviewing their network configuration and connections. If you run out of commands to explore, keep in mind that apropos network might point you toward even more.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Let's block ads! (Why?)

see source

No comments:

Post a Comment