I think we can all agree that it’s great when a VPN connection automatically connects when you log in. However, that doesn’t always happen and sometimes you have to specifically connect to the service from the command line.
In this case, you are going to need to know how to connect to a VPN from the Linux command line and in this article, we will teach you how.
Before You Begin
Before you begin, you should probably know what type of connection you have. Does the VPN service support OpenVPN? If so, then you can directly connect to it using the OpenVPN configuration file. If not, then you are going to have to configure the VPN manually, using the command line.
Also, make sure you are running a supported version of Linux. If you are not sure what version of Linux you are running, then type the following command at the command line:
This will print out the version of Linux you are running. If the output does not start with a version number, then you are running an unsupported version and you should probably upgrade soon (unless you like pain).
Step One: Connect To The Internet
To connect to the internet, you are going to need to type the following command at the command line:
This will print out all of the IP addresses bound to the interface (in this case, eth0).
You need to determine which IP address to use. To do this, you can either pick a dynamic IP address from your ISP (most likely a.4 address), or you can use the RANDOM IP address generator. The.4 address is usually not accessible from outside your home network and the RANDOM IP address generator gives you a completely unique address that you can use anywhere on the internet.
So, in our example, the IP address is 192.168.0.100 and the command is:
ip addr 192.168.0.100
Now, you need to type the following command to test that you’ve successfully connected to the internet:
ping -c 4 google.com
This will ping (-c option) google.com four times, quickly.
The output should look similar to this:
PING google.com (188.8.131.52): 56 data units PING google.com (184.108.40.206): 56 data units PING google.com (220.127.116.11): 56 data units PING google.com (18.104.22.168): 56 data units
As you can see, the output shows that the four ping requests were successfully completed.
Step Two: Connect To Your VPN
To connect to your VPN, you are going to need to use the connect command along with a couple of options. For example, if you are connecting to a PPTP VPN, then you would type the following command:connect google.com:443 pptp
Where 443 is the standard port for PPTP and pptp is the protocol you are using (in this case, Google's OpenVPN protocol).
You can also use the connect command with:
- a specific username and password
- a specific VPN server
- a specific VPN protocol
- an IP address or a DNS name
So, in our example, we are going to use the connect command with these options:
- -u root is an option to connect with the root user
- -p mypassword is an option to connect with a specific user with the username "root" and the authentication password "mypassword"
- connect google.com:443
- is a command to connect to Google's OpenVPN service
Now, after you type this command, you will see the following information:
Trying to connect to google.com:443
To confirm that you've connected successfully, you can type the following command:
This will print out all of the active VPN connections along with a status indicating whether the connection is enabled or disabled. In our example, you can see that the status is shown as enabled. If this is not the case, then you need to manually intervene and fix the issue (most likely by deactivating or disconnecting and then reactivating the connection).
Step Three: Disable Any Anti-Virus Protection
Some VPN services are much more susceptible to malware than other VPN services and it's important that you know how to protect yourself from becoming infected. One good way to do this is by disabling any anti-virus protection before you start using the command line.
If you have Windows 10, then you can disable Windows Defender SmartScreen by following these instructions:
- Open the Windows Task manager by pressing Ctrl+Alt+Delete
- Click "More Settings" from the context menu
- Under "General" click on "Defaults"
- In the dialog box that appears, remove the check mark in front of Quick Heal and Windows Defender
- Click "Apply"
- Type your password when prompted
This process is similar in Mac OS X. In fact, you can do most of the steps above using the Terminal.
Step Four: Configure Your Proxy Settings
A VPN connection should not be controlled by your ISP (or any other entity for that matter). In most cases, you should be able to choose which countries you want to allow the VPN to reach and which countries you don't want it to. To do this, you will need to set up a proxy server between you and the internet.
In our example, we are going to use the nmcli command to set up a tunnel for accessing the internet via a SOCKS5 proxy. To do this, first you will need to install the SOCKS5 proxy utility:
- sudo apt install socksproxy
Next, you will need to edit the /etc/apt/sources.list file and add the proxy's IP address to the end of the line that looks like this:
deb http://192.168.0.1:9050/ubuntu xenial main restricted
- 192.168.0.1 is the IP address of the proxy server
- 9050 is the port on which the proxy server is listening
- Xenial is the codename of the Ubuntu 16.04 Linux distribution
- main restricted is the status of the package, in this case it's enabled but not installed yet
Now you can verify that the proxy is functioning correctly by pinging 22.214.171.124. If you see the IP address of the proxy server printed out at the command line, then it's working correctly and you should be able to use the internet via the proxy.
Step Five: Test That The Connection Is Up And Running
Once you've successfully connected to your VPN server, you can test that it's working correctly by pinging 126.96.36.199. If you don't see any errors, then the connection is most likely up and running.