Networking Linux commands are used to manage, configure and troubleshoot network connections on a Linux system.
Some common networking Linux commands are:
ifconfig
"ifconfig" is a command line tool used to configure a network interface on Linux-based systems. It displays the configuration of the network interface(s), such as IP address, netmask, broadcast address, etc. You can also use it to bring up or down an interface or to set the IP address, netmask, etc. for an interface.
The command syntax is:
The "ifconfig" command in Linux is used to configure network interfaces. It can be used to display the status of current network interfaces, assign an IP address, configure the subnet mask, and broadcast address, and many other network-related tasks. Some common usage examples include:
Displaying all interfaces and their details: ifconfig
Displaying a specific interface: ifconfig <interface>
Assigning an IP address to an interface: ifconfig <interface> <ip_address> netmask <subnet_mask>
Enabling or disabling an interface: ifconfig <interface> down
or ifconfig <interface> up
Note that the exact usage and options of ifconfig may vary depending on the distribution of Linux you are using.
ip
The "ip" command is used to configure and manage network interfaces in Linux. With "ip", you can:
- Display network interface information and configuration
- Set network interface IP addresses and routes
- Add/remove network addresses from interfaces
- Display/manage the ARP table
- Display/manage routing table information
- Display/manage TCP/IP statistics
Example usage:
Display network interface information: "ip addr show"
Display routing table information: "ip route show"
Set IP address for an interface: "ip addr add 192.168.1.100/24 dev eth0"
Note: The "ip" command is more recent than "ifconfig" and provides more advanced features, so it is recommended to use "ip" instead of "ifconfig" in most cases.
ping
The ping command is a networking utility tool used to test the reachability of a network host and measure the round-trip time for packets to travel from the source host to the target host and back.
The basic syntax for using ping is
The following are some of the commonly used options with ping:
- -c count: specifies the number of echo request packets to be sent.
- -W timeout: sets the time-out value in seconds for waiting for a response.
- -I interface: specifies the network interface to be used.
- -s packetsize: sets the packet size in bytes.
- -v: verbose output.
Example usage:
Here's an example of pinging google.com :
The commands send a request to the server, and the server returns a response. "ping" keeps sending the request every second, by default, and will keep running until you stop it with ctrl + C unless you pass the number of times you want to try with the -c option:
Once the "ping" is stopped, it will print some statistics about the results: the percentage of packages lost, and statistics about the network performance.
As you can see the screen prints the host IP address, and the time that it took to get the response back. Not all servers support pinging, in case of the requests time out.
Sometimes this is done on purpose, to "hide" the server, or just to reduce the load. The ping packets
can also be filtered by firewalls.
ping works using the ICMP protocol (Internet Control Message Protocol), a network layer protocol
just like TCP or UDP. The request sends a packet to the server with the ECHO_REQUEST message and the server returns an ECHO_REPLY message. I won't go into details, but this is the basic concept.
Pinging a host is useful to know if the host is reachable (supposing it implements ping), and how
distant it is in terms of how long it takes to get back to you. Usually the nearest the server is geographic, the less time it will take to return back to you, for simple physical laws that cause a longer distance introduces more delay in the cables.
traceroute
Syntax:
Options:
- -I: use ICMP packets instead of UDP
- -m max_ttl: maximum number of hops to search for target
- -q nqueries: number of probes per hop
- -w wait_time: waiting time in seconds for the response
- -n: display hostnames and IP addresses as numbers, not hostnames.
Example:
This will show the hops, RTT, and IP addresses along the route to reach the destination google.com.
Not every router traveled returns our information. In this case, traceroute prints * * *. Otherwise, we can see the hostname, the IP address, and some performance indicators.
For every router, we can see 3 samples, which means the traceroute tries by default 3 times to get you a good indication of the time needed to reach it. This is why it takes this long to execute traceroute compared to simply doing a ping to that host.
You can customize this number with the -q option:
netstat
- -a: Display all active connections, including listening sockets.
- -t: Display only TCP connections.
- -u: Display only UDP connections.
- -n: Display IP addresses and port numbers in numerical form, instead of resolving them to hostnames and service names.
- -l: Display only listening sockets, which are sockets waiting for incoming connections.
- -r: Display the system routing table.
By default, netstat only displays established connections.
The output of netstat is useful for troubleshooting network issues, such as slow network performance, or for monitoring network usage.
nslookup
nslookup (name server lookup) is a command-line tool that helps to find the IP address of a domain name. It can be used to resolve the hostname to the IP address or vice versa. The complete usage of nslookup is as follows:
Options:
- -a: Display all information about the host.
- -d: Print debugging information.
- -r: Disable recursion.
- -v: Display verbose output.
- -t type: Specify the query type (e.g. -t MX, -t A, -t NS, etc.).
- -x address: Reverse lookup of an IP address.
Examples:
To resolve a domain name to an IP address: nslookup google.com
To find the name server of a domain: nslookup -t ns google.com
To perform a reverse lookup of an IP address: nslookup -x 8.8.8.8
route
the "route" is a Linux command used to view and manipulate the IP routing table of a system. The IP routing table is a data structure used by the kernel to determine the path that network packets should take when they leave the local host. The route command is used to view the current routing table, add or delete routing table entries, and modify existing entries.
Here are some standard options and usage of the route command:
- route add: Adds a new entry to the routing table
- route delete: Deletes an entry from the routing table
- route show: Displays the current routing table
- route -n: Shows the routing table without performing hostname resolution
- route -net: Shows the routing table for a specific network
The route command can also be used to view and set the default gateway for a system. The default gateway is the IP address of the router that connects the local network to the Internet.
For more information and usage examples of the route command, refer to the man pages or online documentation for your specific operating system.
hostname
The "hostname" command in Linux is used to display or set the hostname of the system.
Syntax:
Options:
- -a, --alias: Display the alias name (if used)
- -d, --domain: Display the name of the DNS domain
- -f, --fqdn, --long: Display the fully qualified domain name
- -A, --all-fqdns: Display all fully qualified domain names
- -i, --ip-address: Display the IP address(es) associated with the hostname
- -s, --short: Display the short hostname
Example usage:
To display the current hostname: hostname
To set a new hostname: hostname new_host_name
Note: Changing the hostname requires superuser privileges.
dig
"dig" is a network administration command-line tool used for querying the Domain Name System (DNS). It performs DNS lookups and displays the answers that are returned from the DNS server(s) that were queried.
Syntax:
Options:
- -a, --address: display the IP address(es) of the host(s)
- -A, --any: set query type to ANY
- -c, --class=CLASS: query for a specific class (IN, CH, HS)
- -C, --tcp: use TCP instead of UDP
- -d, --debug enable debug output
- -e, --edns=0|1: set EDNS version 0 or 1
- -f, --file=FILE: specify a file containing the query
- -h, --help: display usage and exit
- -i, --ignore: ignore truncation of responses
- -k, --checknames: check names in the reply
- -m, --msgarray: print out an array of raw message packets
- -M, --dnssec: request DNSSEC records
- -n, --ndots=NUMBER: set search NDOTS value
- -p, --port=PORT: set destination port number
- -q, --query=QUERY: set query type or class
- -Q, --quote: quote names in the reply
- -r, --recurse: set recursion desired
- -t, --type=TYPE: query for a specific type (A, MX, NS, ...)
- -u, --user=USER: set the username for TSIG
- -v, --verbose: enable verbose output
- -x, --reverse: reverse the lookups (PTR)
- -y, --key=KEY: set TSIG key -4,
- --ipv4: use IPv4 only -6,
- --ipv6: use IPv6 only
Examples:
dig example.com perform a lookup for example.com
- dig example.com MX perform an MX record lookup for example.com
- dig @8.8.8.8 example.com perform a lookup for example.com using the specified DNS server
- dig -x IP_ADDRESS performs a reverse lookup for an IP address
ssh
The client software establishes a secure connection with the server, authenticates the user, and then runs a shell session. SSH uses public-key cryptography to authenticate the remote computer and to secure the data that is being transmitted.
The ssh command is used to initiate an SSH connection in the terminal. You can connect to a remote host by specifying its hostname or IP address:
The scp command is used to securely copy files between hosts:
There are many other options and features available with SSH, including key-based authentication, port forwarding, X11 forwarding, and much more.