Networking has been a vital area for all the operating systems , applications and devices. Linux one of the most used operating system has a very strong networking environments. Many clients use Linux as their internet gateway and has been using it for several purpose. In this Ubuntu Networking tutorial we will cover all aspects of networking starting from basics , commands and advance level.
What is Localhost ?
Localhost is used to test our applications , networking on a particular our own system . This helps admins, developers to check their applications locally on their systems before deploying it on live environments. This gives chance to do quick smoke test of their application and fix issues if any. Lets talk now in technical term , localhost Interface or loopback Interface is also known as lo. This is automatically created by ubuntu during the installation.
Let us now see, how to check all interfaces on the machines.
ip address show # It shows your network Interfaces including loopback
ifconfig # It all shows your network Interfaces including loopback interface
Why do we called localhost as dummy interface or loopback interface? The reason is kernel sends network traffic to and from itself on the same machine because the interface doesn’t really exist. Localhost uses 127.0.0.1 Ip address which is defined automatically . In case the loopback interface is unreachable ie. 127.0.0.1 you can update it in /etc/hosts file , ifconfig command or by using ip addr command.
How to work with Network interfaces
- How to update the interface with /etc/hosts file
2. How to add or assign ip address to network interface
ip addr add 127.0.0.1/24 dev lo # Addition of Loopback Interface ip addr add 192.168.2.9 dev eth1 # Assign an IP address to a specific interface
3. How to create network interface using ip route add command
ip route add default 184.108.40.206 # To add a default Gateway ip route add 127.0.0.1/24 dev lo # To add a network interface
4. How to create network interface using ifconfig command
ifconfig eth0:1 192.168.1.6 # Creates New Ethernet interface ifconfig eth0 10.10.10.12 # Changes the IP address of network interface ifconfig eth0 netmask 255.255.255.0 # Changes the netmask of network interface ifconfig eth0 down/up # Mark the interface down or up
5. How to enable or disable your network interface
ip link set eth1 up # Mark the interface up ip link set eth1 down # Mark the interface down
How can you monitor your Network Connections
Ping: This tool helps in checking the connectivity. By running this command you can identify if particular application or server is working or not.
ping -c 3 localhost # Here -c is number of pings
Traceroute: This tool helps in identify the issue when a network packet travels a path. It checks entire connectivity starting from local machine till the network host or the destination. Let us see an example of our website from a ubuntu machine.
traceroute automateinfra.com # Use apt install traceroute : if already not installed
mtr: This command is used to combine both functionalities ie. ping and traceroute and give a well formatted output. The full form of mtr is my traceroute.
ip route : This command help in managing , deleting , adding or showing the route which packets will traverse.
ip route show
netstat : netstat is a command-line network tool which shows network connections for TCP, routing tables, and displays the number of network interface and network protocol statistics.
netstat # Displays the status of network ,services listed by sockets netstat -i # It will provide the list of interfaces configured netstat -v # It will list both active and inactive sockets netstat – e # It will list only active connections netstat -s # It will display the activity for each protocol
Network Configuration Files:
There are some file which helps in setting up or modifying the networking in ubuntu machines. Let us discuss some of the most important files.
/etc/hosts : In this directory you can define list of IP addresses and hostnames. It is required to map your IP address with the hostname so that it can resolve your application name or server name accordingly.
/etc/services: In this directory you can see number of services and their allocated ports on which they are working or on which port client can run a service.
/etc/nsswitch.conf: This file is used to check the order in which network services will be accessed. For Example if system needs to check your password for any reason it will search first in /etc/passwd file and later in /etc/group. Let us see image below to understand the priorities inside /etc/nsswitch.conf file.
/etc/resolv.conf : This is a dynamic resolv.conf file for connecting local clients to the internal DNS stub resolver of systemd-resolved. This file lists all configured search domains. For the very first time whenever Nameserver is searched or anything related to DNS , request checks this file and then work on the request.
/etc/host.conf: The /etc/host.conf file informs us about how names are resolved. The entries in the etc/host.conf file tell the resolver library what services to use, and in which order, to resolve names
This concludes the ubuntu networking tutorial , where we learnt a lot about localhost , monitoring the network , setting up interfaces and finally most important files and directories to work with while updating or configuring the networks. These tutorial will surely help you troubleshoot network , setup network and work with various applications. Hope this helps you and I would highly request you to share with your friends so that they are aware of this tutorial and can help others.