1. Introduction

In this tutorial, we’ll see how to determine from the Linux command line if a device is connected to a VPN. We’ll discuss three different methods, including checking a device’s public IP address, using the traceroute command, and checking the routing table of the device with the route command.

We’ll discuss the steps for each method and understand the results that indicate a successful VPN connection.

2. Checking if Logged in via a VPN

There are many ways to check if we are logged in with a VPN with the command line, and we will take a look at some of the most common. Firstly we look at the curl command. This command is useful for getting our IP address and allowing manual checks.

2.1. curl ifconfig.co

$ curl ifconfig.co
34.201.93.33

First, this command will retrieve the device’s current public IP address. We then compare the IP address of our device to the IP address of a known VPN server. Finally, if the IP addresses match, it means we are using a VPN connection. However, if they don’t match, it means we’re not using a VPN.

Note that some websites that display our public IP address may not be accurate or up-to-date, so it’s best to check multiple sources to ensure we have the correct information.

2.2. traceroute Google.com

Furthermore, by utilizing the traceroute command, we can easily verify our VPN connection. This command traces the addresses we covered and gives us a more clear view of our VPN usage.

We open a terminal and run the traceroute command. This will display the path a packet of data takes from our device to a destination on the internet. If the first hop in the traceroute is the VPN server, this signifies a successful VPN connection. In essence, the traceroute command provides a quick and straightforward way to check if our device is connected to a VPN:

$ traceroute google.com
traceroute to google.com (172.217.163.14), 30 hops max, 60 byte packets
 1  10.8.0.1 (10.8.0.1)  29.102 ms  29.589 ms  30.069 ms
 2  10.7.0.1 (10.7.0.1)  30.435 ms  30.902 ms  31.349 ms
 3  172.217.163.14 (172.217.163.14)  31.803 ms  32.288 ms  32.741 ms

As we run the traceroute command to access Google.com, we’ll notice that the output clearly showcases that we are on a VPN network. The journey begins from our device with an IP address of 10.8.0.1 and takes two more hops, first to 10.7.0.1 and then finally to Google.com with an IP address of 172.217.163.14.

Significantly, the presence of private IP addresses starting with 10.x.x.x serves as an indicator of our VPN connection. In fact, this type of IP address is commonly used in VPN configurations, confirming our VPN status.

2.3. route

We can use the route command to examine our routing table and determine our IP address interactions, in addition to utilizing the traceroute command.

We can also use the route command to check our VPN connection. We can view and control the routing table through this command. The routing table contains information about the transmission and receipt of data packets over the internet:

$ route print

Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0    192.168.1.1    192.168.1.104     25
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
    192.168.1.0    255.255.255.0         On-link     192.168.1.104    281
  192.168.1.104  255.255.255.255         On-link     192.168.1.104    281
    192.168.1.255  255.255.255.255         On-link     192.168.1.104    281
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link     192.168.1.104    281
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link     192.168.1.104    281
===========================================================================
Persistent Routes:
  Network Address          Netmask  Gateway Address  Metric
          0.0.0.0          0.0.0.0    192.168.1.1  Default
===========================================================================

This will display the routing table of our device, including information about the network interfaces, the IP addresses, and the gateway addresses.

Next, we look for the entry with the 0.0.0.0 destination and the 0.0.0.0 mask. This entry represents the default gateway of our device. Afterwards, we’ll check the Gateway column for the IP address of the default gateway. This is the IP address of the device that our device uses to connect to the internet.

Finally, we check the IP address of the default gateway and compare it to the IP address of a recognized VPN server to determine our VPN connection.

It’s worth noting that this method may not work for all VPNs, especially those that use advanced routing configurations. However, it’s still a simple and effective way to check our VPN connection status if we use a Windows or Linux device.

3. Checking VPN Status With Python

We can confirm their VPN connection status by utilizing Python code:

import socket
def is_connected_to_vpn():
    vpn_ip = socket.gethostbyname("vpn.example.com")
    current_ip = socket.gethostbyname(socket.gethostname())
    if current_ip == vpn_ip:
        return True
    else:
        return False

We should see true if we are connected to a VPN:

>>> is_connected_to_vpn()
True

In this example, the code assesses the connection status of the device. The assessment includes evaluating the device’s connection to a VPN. The comparison of the device’s IP address with that of a known VPN server takes place.

In this case, checking the device’s public IP address and comparing it to the IP address of a known VPN server is the key to determining a VPN connection. If the IP addresses match, the function returns True, confirming the device’s VPN connection. But if they don’t match, the function returns False, indicating that the device isn’t connected to a VPN.

4. Conclusion

In this article, we covered different methods to confirm a VPN connection. We demonstrated how to use Python, run the curl ifconfig.co command in the Linux terminal, trace data routes with traceroute, and view the routing table on Windows/Linux devices with route.

By using these methods, we can garner information about our VPN connection and ensure that it’s running smoothly.

Comments are closed on this article!