The Wayback Machine - https://web.archive.org/web/20240921004719/https://www.geeksforgeeks.org/ping-command-in-linux-with-examples/
Open In App

How to Check Network Connectivity in Linux | ping Command

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Ensuring a stable and reliable internet connection is crucial for seamless navigation and efficient communication in the world of Linux. The “ping” command is a powerful tool that allows users to check the status of their internet connection and diagnose network-related issues. In this article, we will explore how to use the ping command in Linux to verify internet connectivity and troubleshoot potential problems.

How to Check Network Connectivity Using Ping Command

The PING (Packet Internet Groper) command is used to check the network connectivity between the host and server/host. This command takes as input the IP address or the URL and sends a data packet to the specified address with the message “PING” and gets a response from the server/host this time is recorded which is called latency. Fast ping with low latency means a faster connection. Ping uses ICMP(Internet Control Message Protocol) to send an ICMP echo message to the specified host if that host is available then it sends an ICMP reply message. Ping is generally measured in milliseconds every modern operating system has this ping pre-installed. 

Basic Syntax of the Ping Command:

The basic syntax of the ping command is as follows: 

ping [options] host_or_IP_address

Here, “host_or_IP_address” represents the destination you want to ping.

Checking Internet Connection in Linux

To check your internet connection using the ping command, open the terminal and type the following command:

ping www.google.com

Replace “www.google.com” with the desired host or IP address you want to ping. If the connection is successful, you will see a series of responses displaying the round-trip time, indicating that your internet connection is working. To stop pinging we should use ctrl+c otherwise it will keep on sending packets.

testing internet connection using ping command

testing internet connection using ping command

Here,

  • min: minimum time to get a response
  • avg: average time to get responses
  • max: maximum time to get a response

The internet connection to www.google.com is working correctly with 0% packet loss, successful transmission and reception of 4 packets, and low round-trip time (RTT) values averaging 1.264 ms, indicating a responsive and stable connection.

1. How to Check PING Version

To get ping version installed on your system. 

 ping -V
To get ping version

To get ping version

2. How to Specify the Number of Pings

Earlier we did not define the number of packets to send to the server/host by using -c option we can do so. 

ping -c 2 www.geeksforgeeks.org
Specify the number of pings in Linux

Specify the number of pings in Linux

3. How to Controll the Size of Packets Send Using Ping Command

Earlier a default sized packets were sent to a host but we can send light and heavy packet by using -s option. 

 ping -s 40 -c 5 www.geeksforgeeks.org 

3

4. How to Change the Time Interval Using Ping Command

By default ping wait for 1 sec to send next packet we can change this time by using -i option.  

 ping -i 2 www.geeksforgeeks.org

Now, the ping interval will change to 2 seconds. 

Changing time interval in Ping Command

Changing time interval in Ping Command

5. How to Get Only Summary Using Ping Command 

To only get the summary about the network use -q option.

 ping -c 5 -q www.geeksforgeeks.org

6. How to Timeout PING

 The following command demonstrates how to set a timeout for the PING command using the -w option.

 ping -w 3 www.geeksforgeeks.org

This command instructs the system to stop pinging the specified website (www.geeksforgeeks.org) after waiting for 3 seconds.

7. Flooding with PING

To flood a network with PING packets for testing network performance, use the `-f` option with the PING command.

 ping -f www.geeksforgeeks.org

This command floods the network with PING packets directed at www.geeksforgeeks.org, providing a means to assess and test network performance.

folding with ping

folding with ping

8. Add Timestamp to PING

Timestamps record the current time of an event over a network. The ping command supports three timestamp options:

  1. tsonly (timestamp only)
  2. tsandaddr (timestamp and address)
  3. tsprespec (timestamp pre-specified for multiple hosts)
 ping -T tsonly -c 2 127.0.0.1
Adding timestamp to ping

Adding timestamp to ping

 ping -T tsandaddr -c 2 127.0.0.1
Adding timestamp to ping

Adding timestamp to ping

9. Time to Wait for Response

Set the time to wait for a response using the `-W` option with the `ping` command

 ping -c 5 -W 3 www.geeksforgeeks.org

This command configures the system to wait for a response for a maximum of 3 seconds while sending 5 PING requests to `www.geeksforgeeks.org`. 

10. Fill Packet with Data

Use the `-p` option to fill a PING packet with specific data. For example, `-p ff` fills the packet with ones.

 ping -c 5 -p ff www.geeksforgeeks.org

This command fills PING packets sent to `www.geeksforgeeks.org` with the specified data, in this case, ones (represented by `ff`).

Fill Packet with Data

Fill Packet with Data

11. Path MTU Discovery

Path MTU Discovery helps determine the maximum Transmission Unit (MTU) a TCP path can handle. Use the `-M` option with values like `do` (prohibit fragmentation), `want` (do PMTU discovery, fragment locally when packet size is large), or `dont` (do not set DF flag). 

 ping -c 5 -M want www.geeksforgeeks.org

This command initiates Path MTU Discovery for the specified website (`www.geeksforgeeks.org`) with the option to either prohibit fragmentation (`do`), perform PMTU discovery with local fragmentation when needed (`want`), or not set the DF flag (`dont`). 

Path Mtu Discovery

Path Mtu Discovery

12. Specify TTL (Time To Live)

The Time To Live (TTL) represents the maximum hops a packet can traverse before getting discarded. Use the `-t` option to specify the TTL value. 

ping -c 5 -t 64 www.geeksforgeeks.org

This command sends 5 PING requests to www.geeksforgeeks.org with a Time To Live (TTL) set to 64, restricting the packet’s maximum allowed hops. 

Specify TTL (Time To Live)

Specify TTL (Time To Live)

How to Check Network Connectivity in Linux | ping Command – FAQs

How do I use the ping command to check if my Linux system is connected to the internet?

You can use the ping command followed by a target host or IP address.

For example:

ping www.google.com. 

If successful, you’ll see round-trip time statistics indicating a successful network connection.

What does the “ping: unknown host” error mean, and how can I resolve it?

This error occurs when the specified host or domain is not recognized. Double-check the hostname or try using an IP address instead. If using a hostname, ensure DNS resolution is working, and you have a valid internet connection.

How can I use the ping command to continuously monitor network connectivity? 

The -c option in the ping command allows you to specify the number of packets to send. Use -c 4 to send four packets, or use the -f option for continuous ping.

For example:

ping -c 4 www.example.com

or

ping -f www.example.com

What does “Request timed out” or “Destination Host Unreachable” mean in the ping output?

“Request timed out” indicates that the target is not responding to ping requests. “Destination Host Unreachable” suggests that there is an issue reaching the destination network. Check your network configuration, firewall settings, and ensure the target is reachable.

Can I adjust the time interval between ping requests to reduce network load?

Yes, you can use the -i option followed by the desired interval in seconds.

For example:

ping -i 2 www.example.com

It will send ping requests every 2 seconds. Adjust the interval based on your testing requirements and to avoid unnecessary network load.

Conclusion

In this article we discussed the “ping” command which is a crucial tool in Linux for checking and maintaining a stable internet connection. This article provides a guide on using the ping command to verify connectivity, troubleshoot issues, and monitor network performance. It covers the basics of the command, important parameters, and various customization options. The inclusion of frequently asked questions addresses common concerns, making it easier for users to navigate and optimize their network connections in Linux. Ultimately, the ping command is a valuable resource for ensuring a responsive and reliable internet experience on Linux systems.



Previous Article
Next Article

Similar Reads

How to disable ping to a website using Linux?
In this article we are going to create a customized network setup, to restrict accessing specific sites. Suppose we want to access Google and don't want to access Facebook, for this we need to create a customized network setup by configuring some routing rules for our system router. So let's come to some terms regarding networking: Entries of an IP
2 min read
How to ping an Ip on windows macos and Linux
Ping is a command line networking utility that uses ICMP packets to check if a logical route is available to communicate with a specific host. A failure of the ping command doesn't necessarily mean that the host is down. There are many other possible reasons for the failure. Along with the primary purpose of ping, this command can also provide insi
8 min read
How to Block Ping (ICMP) Responses in Linux?
Ever heard of ICMP? It's like the internet's messenger, quietly ensuring messages flow smoothly between devices. Think of it as a network detective, reporting issues when something goes wrong. In the "ping" game, ICMP checks if hosts are reachable. This article explores ICMP's role and shows how to boost Linux security by blocking ICMP responses, u
8 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Iperf Command to Test Speed, Performance and Bandwidth of Network in Linux
There is a great degree of flex in how the packets are delivered and overall bit rate and packet payload size can be controlled. iperf is a tool that is used to perform network performance measurement and tuning. iperf is an open-source software which is written in C language. Jperf is a GUI version for doing the same job. The core concept work beh
2 min read
Manage Network Connections From the Linux Command Line with nmcli
Network connectivity is a critical aspect of modern computing, and Linux provides a robust command-line tool for managing network connections: nmcli. Network Manager Command-Line Interface (nmcli) is a powerful and versatile utility that allows users to control and configure network settings directly from the terminal. In this article, we will delv
5 min read
Command-Line Tools and Utilities For Network Management in Linux
If you are thinking of becoming a system administrator, or you are already a system admin, then this article is for you. As a system admin, your daily routine will include configuring, maintaining, troubleshooting, monitoring, securing networks, and managing servers within data centers. Network configuration and troubleshooting are some important t
8 min read
How to check the routing table in Linux | route Command
The IP/kernel routing table acts as a crucial map, determining how network packets are forwarded between different hosts and networks. By utilizing the route command, Linux administrators and users can establish static routes, enabling precise control over network connectivity and optimizing data transmission. In this comprehensive guide, we will e
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
du Command in LINUX
While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and disp
9 min read
dmesg command in Linux for driver messages
dmesg command also called as "driver message" or "display message" is used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg : When the computer boots up, there are lot of messages(log) generated during the system start-up. So you
5 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read