2023-03-03

Network Related Commands in Linux

Network-related Commands in Linux

Linux has a vast array of network-related commands that can help system administrators troubleshoot and manage network connections. These commands allow users to gather information about the network configuration, diagnose network issues, and interact with remote servers. In this article, I will cover some of the most commonly used network-related commands in Linux.

host

The host command in Linux is used to perform DNS lookups and obtain information about domain names and IP addresses. It is often used to troubleshoot DNS-related issues and verify DNS settings.

bash
$ host [options] [domain name/IP address]

Some common options of the host command are:

  • -t: specifies the record type to query for (e.g. A, MX, NS, etc.)
  • -a: shows all information for the specified domain
  • -c: sets the query class (e.g. IN for Internet)

Here is an example of host command.

bash
$ host google.com

Output will be as follows.

bash
google.com has address 172.217.3.174
google.com has IPv6 address 2607:f8b0:4004:809::200e
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.

This command performs a DNS lookup on the domain name "google.com" and returns its IP address and mail server information.

ping

The Linux ping command is a utility used to test the connectivity between a source and a destination host on a network. It is a powerful tool that allows users to verify if a remote host is reachable and to measure the latency of the network connection.

Here is an example of how to use the ping command:

bash
$ ping google.com

This command will send ICMP packets to the Google website and display the response time and packet loss statistics. By default, the ping command sends packets indefinitely until it is interrupted by the user.

Another useful option of the ping command is -c, which allows users to specify the number of packets to send.

bash
$ ping -c 4 google.com

This command will send 4 ICMP packets to the Google website and display the response time and packet loss statistics.

The ping command also provides options to specify the interval between packets, the size of the packets, and the type of ICMP packets to send.

bash
$ ping -i 2 -s 100 -t 10 google.com

This command will send ICMP packets to the Google website every 2 seconds with a packet size of 100 bytes and a timeout of 10 seconds.

traceroute

The Linux traceroute command is a utility used to trace the path of packets between a source and a destination host on a network. It is a powerful tool that allows users to determine the network route taken by the packets, identify network issues such as network congestion or packet loss, and troubleshoot network connectivity issues.

Here is an example of how to use the traceroute command:

bash
$ traceroute google.com

This command will trace the path of packets from the source host to the Google website and display the IP addresses and response times of each network hop along the way. By default, the traceroute command sends packets with increasing TTL (Time-to-Live) values until it reaches the destination host or the maximum TTL value is reached.

Another useful option of the traceroute command is -n, which disables the reverse DNS lookup and displays IP addresses instead of hostnames.

bash
$ traceroute -n google.com

This command will trace the path of packets to the Google website and display the IP addresses of each network hop instead of hostnames.

The traceroute command also provides options to specify the maximum TTL value, the number of packets to send per TTL value, and the type of packets to send.

bash
$ traceroute -m 20 -q 5 -T google.com

This command will trace the path of TCP packets with a maximum TTL of 20 and send 5 packets per TTL value to the Google website.

ifconfig

The ifconfig command is a Linux utility used to configure and display the network interface parameters of a system. It is commonly used to view the IP address, netmask, and status of the network interfaces, as well as to configure IP addresses and network parameters.

Here is an example of how to use the ifconfig command:

bash
$ ifconfig

This command will display the configuration of all network interfaces available on the system. The output will include information such as the interface name, IP address, netmask, and status.

To configure a network interface, the ifconfig command can be used with various options. For example, to configure the IP address of an interface, the following command can be used:

bash
$ ifconfig eth0 192.168.1.10 netmask 255.255.255.0

This command will set the IP address of the eth0 interface to 192.168.1.10 with a netmask of 255.255.255.0.

The ifconfig command can also be used to enable or disable network interfaces, configure network aliases, and set various network parameters such as the MTU (Maximum Transmission Unit) size and the broadcast address.

nslookup

The nslookup command is a network administration tool used to query the Domain Name System (DNS) to obtain domain name or IP address mapping information. This can be helpful in troubleshooting network connectivity issues, verifying DNS configurations, and more.

Here's an example of how to use the nslookup command:

bash
$ nslookup example.com

This will return the IP address associated with the domain name example.com, along with other information such as the DNS server being used to perform the lookup.

The nslookup command also supports other options such as specifying a particular DNS server to use, querying for a specific record type (such as MX or TXT records), and more. You can view a full list of options and syntax by typing nslookup -h or man nslookup in the terminal.

netstat

The netstat command is a Linux utility used to display active network connections, routing tables, and various network statistics. It is commonly used to troubleshoot network connectivity issues and monitor network activity on a Linux system.

Here is an example of how to use the netstat command:

bash
$ netstat -a

This command will display all active network connections on the system, including listening sockets and established connections. The -a option specifies that both TCP and UDP connections should be displayed.

The netstat command can also be used to display various network statistics and routing tables. For example, the following command can be used to display the network routing table:

bash
$ netstat -r

This command will display the current routing table for the system, including information about the default gateway, network destinations, and routing interfaces.

The netstat command can also be used with various options to display specific types of network connections or statistics. For example, the following command can be used to display all TCP connections:

bash
$ netstat -at

This command will display all active TCP connections on the system. The -t option specifies that only TCP connections should be displayed.

ssh

The ssh command is a Linux utility used to securely connect to a remote system over a network. It is commonly used to manage remote servers, transfer files, and execute commands on remote systems.

Here is an example of how to use the ssh command:

bash
$ ssh user@remote_host

In this command, user is the username on the remote system that you want to connect to, and remote_host is the hostname or IP address of the remote system. When you run this command, you will be prompted to enter the password for the specified user on the remote system.

Once you have successfully connected to the remote system, you can execute commands on the remote system by simply typing them into the terminal window. For example, the following command can be used to display the current directory on the remote system:

bash
$ pwd

This command will display the current directory on the remote system.

The ssh command also supports various options that can be used to customize the connection and improve security. For example, the -X option can be used to enable X11 forwarding, which allows you to run graphical applications on the remote system and display them on your local system.

scp

The scp command in Linux stands for "secure copy" and is used to securely transfer files between local and remote hosts over a network.

Here is an example of how to use the scp command:

bash
$ scp file.txt user@remote:/path/to/destination

In this example, file.txt is the name of the file you want to transfer, user is the username of the remote host, remote is the hostname or IP address of the remote host, and /path/to/destination is the path to the directory where you want to transfer the file.

When you run this command, the scp command will securely copy the file from your local machine to the remote machine.

The scp command can also be used to transfer files from a remote machine to a local machine by reversing the source and destination arguments:

bash
$ scp user@remote:/path/to/file.txt /path/to/destination

In this example, user is the username of the remote host, remote is the hostname or IP address of the remote host, /path/to/file.txt is the path to the file you want to transfer, and /path/to/destination is the path to the directory where you want to transfer the file on your local machine.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!