try.directtry.direct
Article preview image

How to Check Whether Your Server Is Online Using a Linux Command Line

We often need to check connectivity to troubleshoot network issues, or want to check whether our server is online. We created this article to show you how to do this.

There are many handy Linux tools for this:


1. ping


Ping is one of the most widely used commands to check whether a server or remote host is up and responding. Ping sends ICMP ECHO_REQUEST to the server. If ICMP is blocked from the server-side or cloud provider, then the ping command may not work as expected (like AWS ec2 instance). Here, we assume that cloud providers firewalls do not block ICMP.


Syntax: ping <destination>


ping example.com

Shell


root@ubuntu:~# ping example.com
PING example.com(2606:2800:220:1:248:1893:25c8:1946 (2606:2800:220:1:248:1893:25c8:1946)) 56 data bytes
64 bytes from 2606:2800:220:1:248:1893:25c8:1946 (2606:2800:220:1:248:1893:25c8:1946): icmp_seq=1 ttl=59 time=2.04 ms
64 bytes from 2606:2800:220:1:248:1893:25c8:1946 (2606:2800:220:1:248:1893:25c8:1946): icmp_seq=2 ttl=59 time=1.60 ms
^C
--- example.com ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 1.443/1.698/2.044/0.220 ms

2. telnet


Telnet is a network protocol that allows you to connect to a remote host. Telnet is usually installed on most Linux OS by default. Otherwise, you can run the following command in the terminal:


For Redhat/CentOS/Rocky Linux


yum -y install telnet

Shell


For Ubuntu/Debian


apt-get install telnet

Shell


Using telnet, you can identify the availability of certain ports. You can use telnet to make sure no firewalls in between are blocking incoming connections to the server.

System or network administrators use Telnet to configure network devices like routers and switches.



telnet localhost 22

Shell


root@ubuntu:~# telnet localhost 22
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3

The above command shows that port 22 is open on the localhost.

Let’s now check whether the webserver port is open.


telnet example.com 443

Shell


root@ubuntu:~# telnet example.com 443
Trying 2606:2800:220:1:248:1893:25c8:1946...
Connected to example.com.
Escape character is ‘^]’.

If the port is firewalled on the remote host, then you will see something like this


telnet example.com 22

Shell


root@ubuntu:~# telnet example.com 22
Trying 2606:2800:220:1:248:1893:25c8:1946...


3. nslookup


Simply speaking, nslookup is used for DNS record checks. You can use nslookup to check if your website is connected to the internet. It is also used to troubleshoot DNS-related problems.


Syntax:


nslookup <domain>

Shell


It will show the IP address and other details


root@ubuntu:~# nslookup example.com
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
Name: example.com
Address: 93.184.216.34
Name: example.com
Address: 2606:2800:220:1:248:1893:25c8:1946

Otherwise you will see the following:


root@ubuntu:~# nslookup example.com1
Server: 127.0.0.53
Address: 127.0.0.53#53
** server can’t find example.com1: NXDOMAIN

By default, nslookup will show "A" records.


4. nmap


Nmap or Network Mapper is a powerful tool for finding open ports and network audits.

In case the nmap command is not available, you can install it executing the following command in terminal:


sudo apt install nmap

Shell


Nmap is also used to identify whether the host is up or not.


nmap example.com

Shell


This command will show open port and host status as below:


root@ubuntu:~# nmap example.com
Starting Nmap 7.80 ( https://nmap.org ) at 2021-11-05 14:48 UTC
Nmap scan report for example.com (93.184.216.34)
Host is up (0.0016s latency).
Other addresses for example.com (not scanned): 2606:2800:220:1:248:1893:25c8:1946
Not shown: 996 filtered ports

PORT STATE SERVICE
80/tcp open http
443/tcp open https
1119/tcp closed bnetgame
1935/tcp closed rtmp
Nmap done: 1 IP address (1 host up) scanned in 4.91 seconds

You can easily check whether your server is running using the following command:

Syntax:

nmap -sn <hostname>

Shell


root@ubuntu:~# nmap -sn example.com
Starting Nmap 7.80 ( https://nmap.org ) at 2021-11-05 14:54 UTC
Nmap scan report for example.com (93.184.216.34)
Host is up (0.0023s latency).

5. nc


Netcat is known as an nc command used for network scanning like Port Scanning, open Remote connections, Network debugging, read and write operation on the network, and many more.

You can easily check the reachability of remote ports using nc Command.


Syntax


nc -vz $HOSTNAME $PORT

Shell


nc -vz example.com 443

If port 443 is running on the server, we will get the following:

Connection to example.com 443 port [tcp/https] succeeded!


root@ubuntu:~# nc -vz example.com
nc: missing port number
root@ubuntu:~# nc -vz example.com 443
Connection to example.com 443 port [tcp/https] succeeded!
root@ubuntu:~# nc -vz example.com 80
Connection to example.com 80 port [tcp/https] succeeded!

6. wget


Wget is a command-line download tool that supports HTTP, HTTPS and FTP. Wget is very useful for those who want to check any network-related issue or test connection from a Linux terminal. It will help you in web-related issues debugging.


Syntax:

wget <URL>

Shell


root@ubuntu:~# wget example.com
--2021-11-05 17:48:52– http://example.com
Resolving example.com (example.com)... 2606:2800:220:1:248:1893:25c8:1946, 93.184.216.34
Connecting to example.com (example.com)| 2606:2800:220:1:248:1893:25c8:1946|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1256 (1.2K) [text/html]
Saving to: índex.html.2
index.html.2 100%[=========================>] 1.23K --.-KB\s in 0s2021-11-05 17:48:52 (74.6 MB/s) - ’index.html.2’saved [1256/1256]

Here we get the “200” response code from the web server and index.html file downloaded.


7. curl


You can use the curl command to transfer data either from or to a server. It supports proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, and many more.

You can use curl command to test TCP ports connectivity.

Let’s check our SSH port connection with CURL


Syntax:

curl -v telnet://127.0.0.1:22

Shell


root@ubuntu:~# curl -v telnet://127.0.0.1.22
* Trying 127.0.0.1:22...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)

Now let’s check whether our web server is running on port 80 using curl


root@ubuntu:~# curl -v telnet://example.com:80
* Trying 2606:2800:220:1:248:1893:25c8:1946:80
* TCP_NODELAY set
* Connected to example.com (2606:2800:220:1:248:1893:25c8:1946) port 80 (#0)

Let’s check the SMTP port


root@ubuntu:~# curl -v telnet://127.0.0.1:25
* Trying 127.0.0.1:25...
* TCP_NODELAY set
* Connected to 127.0.0.1 port 25 failed: Connection refused
* Failed to connect to 127.0.0.1 port 25: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 25: Connection refused

As you see, CURL is a useful command-line tool available in Linux to check the connection with the server and port.


8. Bash script

If you want your linux based laptop or a server to send a notification to your email when your another server is offline you can use the following bash script. First, ensure that the mail utility is installed and properly configured on your server. If not, you can install it using your package manager (e.g., sudo apt-get install mailutils for Debian-based systems or sudo yum install mailx for Red Hat-based systems):


#!/bin/bash
# URL of the server to check
URL="http://yourserver.com"

# Log file to store the server status
LOG_FILE="/path/to/your/logfile.log"

# Email configuration
TO_EMAIL="your-email@example.com"
SUBJECT="Server Offline Alert"

# Check the server status
STATUS=$(curl -s -o /dev/null -w "%{http_code}" $URL)

# Get the current date and time
DATE=$(date '+%Y-%m-%d %H:%M:%S')

# Log the status
if [ $STATUS -eq 200 ]; then
    echo "$DATE - Server is online. Status code: $STATUS" >> $LOG_FILE
else
    echo "$DATE - Server is offline. Status code: $STATUS" >> $LOG_FILE
    # Send an email notification
    echo "The server at $URL is offline as of $DATE. Status code: $STATUS" | mail -s "$SUBJECT" $TO_EMAIL
fi

Shell

9. Python command


Also known as one-liners.

python -c "from urllib.request import urlopen;resp=urlopen('http://www.google.com/');print(resp.status)"

Shell


We have examined the Linux command-line tools for checking server health and network connectivity in detail. These are essential tools for system and network administrators to troubleshoot any network-related issues.


Did not find the solution? Join our discord channel and consult with our tech experts 💬
Still don't know how to check? Get system administration help from our partners🆘