Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

A server is an essential part of most Internet-based systems, be it a tiny web service or a complex cloud-computing platform.

For such systems, it’s crucial to track if the server is running correctly.

In this tutorial, we’ll study Linux commands to check if the server is up and running.

First, we’ll achieve that using the ping command. Then, we’ll focus on the nc tool. After that, we’ll discuss the wget command. Finally, we’ll learn the curl command to see if the server is up.

2. Using the ping Command

Ping is a widely-used tool to test the network connection.

We can use it to test if the server is up and running.

Let’s use a DNS server with IP address 8.8.8.8 as an example.

To ping the server 8.8.8.8, we can use the following ping command:

$ ping -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=22.8 ms
...

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 22.698/22.735/22.809/0.052 ms

Here, we’ve used the option -c to limit the number of pings to three.

As can be seen, all three packets have been received by the server.

3. Using the nc Command

nc is based on the NetCat utility which is a powerful tool for analyzing network packets.

nc allows us to rapidly check if a specific port is reachable on the server.

For example, let’s check if the Redis port 6379 is available on our local machine:

$ nc -4 -d -z -w 1 127.0.0.1 6379 &> /dev/null; echo $?
0

Here, the output 0 means that the port responds correctly.

Let’s look at the above command:

  • -4 is to use IPv4
  • -d is to not read from stdin
  • -z is to listen only and not to send data
  • -w 1 is a one second waiting time
  • 127.0.0.1 6379 shows the localhost IP address name and the Redis service port number
  • echo $? is to print the nc command exit status code.

In case the server port doesn’t respond correctly, the nc command returns 1For example, let’s try to reach a closed local port 8000:

$ nc -4 -d -z -w 1 127.0.0.1 8000 &> /dev/null; echo $?
1

As expected, the command’s output is 1.

4. Using the wget Command

If we need to check an HTTP server, we can use the wget command.

For instance, let’s see how to check Google’s HTTP server:

$ wget --timeout=1 --server-response --max-redirect=0 google.com:80
http://google.com/
Resolving google.com (google.com)... 216.58.215.78, 2a00:1450:4017:814::200e
Connecting to google.com (google.com)|216.58.215.78|:80... connected.
...

Here we can see the connected keyword in the output. This means that the server is running correctly.

In the above command, we’ve used the following options:

  • –timeout 1 to limit the waiting time to 1sec
  • –server-response is to check if the server responds correctly
  • –max-redirect=0 is to limit a page redirect

Let’s now try to access the server with an incorrect port 8000:

$ wget --timeout=1 --server-response --max-redirect=0 google.com:8000
--2024-11-02 16:22:59--  http://google.com:8000/
Resolving google.com (google.com)... 216.58.215.78, 2a00:1450:401b:808::200e
Connecting to google.com (google.com)|216.58.215.78|:8000... failed: Connection timed out.
Connecting to google.com (google.com)|2a00:1450:401b:808::200e|:8000... failed: Network is unreachable.

As a result, the command failed to connect to the server using port 8000.

Specifically, the keywords “Connection timed out” show that the server has reached its time limit to respond.

5. Using the curl Command

Curl is a popular tool for transferring data across networks.

It allows us to check if an HTTP server is running correctly by looking at its HTTP response.

For example, let’s check a Google server response:

$ curl -s -w "%{http_code}\n" -L "google.com:80/" -o /dev/null
200

As can be seen, the result HTTP status code is 200 which means success.

Let’s look at the above command in more detail:

  • -s means a silent mode. It skips unrelated packet information
  • -w “%{http_code}\n” is to set up the output format as HTTP code
  • -L is to define the location in the HTTP request
  • “google.com:80/” is the DNS name of a Google server. This can also be an IP address. 80 is an optional port number
  • -o /dev/null is to hide unrelated output information such as the HTML code of a web page.

In case, the server has a problem, curl should return the 000 status code.

For example, let’s see the result if we provide an incorrect server name:

$ curl -s -w "%{http_code}\n" -L "google.c/" -o /dev/null
000

As we expect, curl has shown a 000 output.

If we’re curious about what exactly caused the problem, we can check the curl command exit status code by printing the $? operator after the command:

$ curl -s -w "%{http_code}\n" -L "google.c/" -o /dev/null; echo $?
000
6

As a result, the number 6 has appeared in the output. It means that curl can’t resolve the hostname.

6. Conclusion

In this article, we studied how to check if our server is running correctly.

First, we used the ping command for that. Then, we learned how the nc command can check the server’s ports. After that, we used the wget command to check the HTTP server. Finally, we learned the curl command to track the HTTP status codes from the server.