After installing a linux server and playing with the basic command lines, the first thing we should be able to do is configure our network, without which of course there is no Internet. Most of the latest linux distributions ship with easy to use GUI for configuration and testing of the network. But here we’ll be using all command lines. Remember, these commands directly talk to the responsible programs without any intermediary (GUI), so obviously it’s fast, more powerful and have many options that are not available in GUI.
This guide will help you to configure your network and troubleshoot simple network problems using command lines.
This command is used to configure or view the information of network interfaces. E.g. if you want to configure the interface eth0 with an ip address 192.168.1.45, you should use the following command:
The other useful and simple use of ifconfig is done with the switch -a, which displays the state of all the interfaces. It lists all the informations you need to know about your network card. In the following example you can see Hardware Address (Hwaddr), IP Address (inet addr), Broadcast Address (Bcast), Subnetmask (Mask) and many more.
The second thing we need to configure after the ip address is default route or gateway. The following command will add a default gateway 192.168.1.1, which is accessible via interface eth0.
The default gateway can be deleted just by substituting the add with del in the above command.
You can check the default gateway and the whole routing table by executing simple route command.
After setting up the ip address and the default gateway, final thing you need do is configure the DNS servers. The DNS server information is stored in a text file /etc/resolv.conf. You can use your favourite text editor (e.g. vi or emacs) to edit the file. Following is a sample resolv.conf file, where two (primary and secondary) DNS servers are listed:
That’s it, congratulations! You’ve configured the network of a linux server, which should allow to go to the Internet.
Sooner or later you’ll either need to check simple connectivity issues or troubleshoot serious network problems. Here are some very useful and basic diagnostic commands that can help you along the way.
I think ping is one of the most useful and widely used commands to check network connectivity. This command is used to test whether a particular host is rechable over an IP network. Ping uses ICMP packets to send an “echo request”, and the receiving host is required to answer back using “echo reply”. If you cannot reach the internet, the first thing you should do is ping your default gateway to make sure that you get an icmp reply.
While ping helps to check the connectivity between two hosts, traceroute actually show the route between them. It lists the nodes (hosts/routers) that your packet will travel through to reach the destination. If the ping shows some packet loss, looking at the output of traceroute we can figure out which hosts (routers) along the way have problems. This can be determined looking at the latency at each host. From the following traceroute we can see that to reach Google, the packet needs to travel through 5 hops (hosts/routers).
This command is very useful when you want to see where your server is connected to and also who is connected back to your server and services from outside. netstat prints the active network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. E.g. when it’s invoked with the switch -a, it lists all the active network connections and services that this server is listening to.
This command is used to test DNS servers and domain names. E.g when executed with switch -q=any, it provides the ip address of the domain name we’re querying about, the name servers responsible for this domain and other parameters such as TTL and contact email address of the administrator.