
I am a new CentOS Linux 7.0 server user. How do I set up an OpenVPN Server on CentOS Linux version 7.0 server to shield my browsing activity from bad guys on public Wi-Fi, and more?
Introduction OpenVPN is a full-featured SSL VPN (virtual private network). It implements OSI layer 2 or 3 secure network extension using the SSL/TLS protocol. It is an open source software and distributed under the GNU GPL. A VPN allows you to connect securely to an insecure public network such as wifi network at the airport or hotel. VPN is also required to access your corporate or enterprise or home server resources. You can bypass the geo-blocked site and increase your privacy or safety online. This tutorial provides step-by-step instructions for configuring an OpenVPN server on CentOS Linux 7.0 server.
Procedure: CentOS 7.0 Set Up OpenVPN Server In 5 Minutes
The steps are as follows:
Step 1 – Update your system
Run the yum command :
sudo yum update {[email protected]:~ }$
Step 2 – Find and note down your IP address
Use the ip command as follows:
ip a {[email protected]:~ }$
ip a show eth0
{[email protected]:~ }$
Another option is to run the following dig command/host command to find out your public IP address from Linux command line:
dig +short myip.opendns.com @resolver1.opendns.com {[email protected]:~ }$
OR
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'”‘ ‘{ print $2}’ {[email protected]:~ }$

A note about IP address
Most CentOS Linux cloud servers have two types of IP address:
- Public static IP address directly assigned to your box and routed from the Internet. For example, Linode, Digital Ocean, and others gives you direct public IP address.
- Private static IP address directly attached to your server and your server is behind NAT with public IP address. For example, AWS EC2/Lightsail give you this kind of NAT public IP address.
The script will automatically detect your networking setup. All you have to do is provide correct IP address when asked for it.
Step 3 – Download and run centos7-vpn.sh script
I am going to use the wget command:
wget https://raw.githubusercontent.com/Angristan/openvpn-install/master/openvpn-install.sh -O centos7-vpn.sh {[email protected]:~ }$
Setup permissions using the chmod command
chmod +x centos7-vpn.sh {[email protected]:~ }$
One can view the script using a text editor such as vim/vi :
vi centos7-vpn.sh {[email protected]:~ }$
Run centos7-vpn.sh to install OpenVPN server
Now all you have to do is:
sudo ./centos7-vpn.sh {[email protected]:~ }$
Sample session from AWS/Lightsail where my cloud server is behind NAT:
Sample session from Linode/DO server where cloud server has direct public IPv4 address:
To avoid problem always choose DNS as 1.1.1.1 or Google DNS. Those are fast DNS server and reached from anywhere on the Internet.
How do I start/stop/restart OpenVPN server on CentOS 7.0 ?
sudo systemctl stop [email protected] {[email protected]:~ }$
sudo systemctl start [email protected] # <--- stop server
{[email protected]:~ }$
sudo systemctl restart [email protected] # <--- start server
{[email protected]:~ }$
sudo systemctl status [email protected] # <--- restart server
{[email protected]:~ }$ # <--- get server status
Step 4 – Connect an OpenVPN server using IOS/Android/Linux/Windows client
On server your will find a client configuration file called ~/desktop.ovpn. All you have to do is copy this file to your local desktop using the scp command:
scp [email protected]:~/deskcop.ovpn . {[email protected]:~ }$
Next, provide this file to your OpenVPN client to connect:
- Apple iOS client
- Android client
- Apple MacOS (OS X) client
- Windows 8/10 client
Linux Desktop: OpenVPN client configuration
First, install the openvpn client for your desktop, enter:
sudo yum install openvpn {[email protected]:~ }$
OR
sudo apt install openvpn {[email protected]:~ }$
Next, copy desktop.ovpn as follows:
sudo cp desktop.ovpn /etc/openvpn/client.conf {[email protected]:~ }$
Test connectivity from the CLI:
sudo openvpn –client –config /etc/openvpn/desktop.conf {[email protected]:~ }$
Your Linux desktop system will automatically connect when computer restart using openvpn script/service:
sudo systemctl enable [email protected] {[email protected]:~ }$
sudo systemctl start [email protected]
{[email protected]:~ }$
Step 5 – Verify/test the connectivity
Execute the following commands after connecting to OpenVPN server from your Linux desktop:
ping 10.8.0.1 {[email protected]:~ }$
ip route #Ping to the OpenVPN server gateway
{[email protected]:~ }$
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com #Make sure routing setup working
{[email protected]:~ }$ #Must return public IP address of OpenVPN server
A note about trouble shooting OpenVPN server and client issues
Check OpenVPN server for errors:
journalctl –identifier openvpn {[email protected]:~ }$

Is firewall rule setup correctly on your server? Use the cat command to see rules:
cat /etc/iptables/add-openvpn-rules.sh {[email protected]:~ }$
#!/bin/sh iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE iptables -A INPUT -i tun0 -j ACCEPT iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT iptables -A INPUT -i eth0 -p udp --dport 1194 -j ACCEPT |
Another option is to run iptables command and sysctl command commands to verify NAT rule setup on your server:
sudo iptables -t nat -L -n -v {[email protected]:~ }$
sysctl net.ipv4.ip_forward
{[email protected]:~ }$
Insert the rules if not inserted from /etc/iptables/add-openvpn-rules.sh
sudo sh /etc/iptables/add-openvpn-rules.sh {[email protected]:~ }$
sudo sysctl -w net.ipv4.ip_forward=1
{[email protected]:~ }$
Is OpenVPN server running and port is open? Use the ss command or netstat command and pidof command/ps command:
netstat -tulpn | grep :1194 {[email protected]:~ }$
ss -tulpn | grep :1194 ## 1194 is the openvpn server port ##
{[email protected]:~ }$
ps aux | grep openvpn ## 1194 is the openvpn server port ##
{[email protected]:~ }$
ps -C openvpn ## is the openvpn server running? ##
{[email protected]:~ }$
pidof openvpn ## is the openvpn server running? ##
{[email protected]:~ }$ ## find the openvpn server PID ##
If not running, restart the OpenVPN server:
sudo systemctl restart [email protected] {[email protected]:~ }$
Look out for errors:
sudo systemctl status [email protected]{[email protected]:~ }$
Can the Linux desktop client connect to the OpenVPN server machine? First you need to run a simple test to see if the OpenVPN server port (UDP 1194) accepts connections:
nc -vu 139.162.60.234 1194 {[email protected]:~ }$
Connection to 139.162.60.234 1194 port [udp/openvpn] succeeded!
If not connected it means either a Linux desktop firewall or your router is blocking access to server. Make sure both client and server using same protocol and port, e.g. UDP port 1194.
Conclusion
Congratulations. You successfully set up an OpenVPN server on CentOS Linux 7.0 server running in the cloud. See the OpenVPN website here and script site here for additional information.
Add Comment