last updated in
CentOS, Linux, OpenVPNIntroduction 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 server.
Advertisements
Procedure: CentOS 7 Set Up OpenVPN Server In 5 Minutes
The steps are as follows:
Step 1 – Update your system
Step 2 – Find and note down your IP address
Use the ip command as follows:
Another option is to run the following dig command/host command to find out your public IP address from Linux command line:
One can grab find IPv4 address using the dig and awk command:
For IPv6 version, try:
{vivek@centos7:~ }$ ip a
{vivek@centos7:~ }$ ip a show eth0
Another option is to run the following dig command/host command to find out your public IP address from Linux command line:
{vivek@centos7:~ }$ dig +short myip.opendns.com @resolver1.opendns.com
One can grab find IPv4 address using the dig and awk command:
{vivek@centos7:~ }$ dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'
For IPv6 version, try:
{vivek@centos7:~ }$ dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'
A note about IP address
Most 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:
Setup permissions using the chmod command
One can view the script using a text editor such as vim/vi :
{vivek@centos7:~ }$ wget https://raw.githubusercontent.com/Angristan/openvpn-install/master/openvpn-install.sh -O centos7-vpn.sh
Setup permissions using the chmod command
{vivek@centos7:~ }$ chmod +x centos7-vpn.sh
One can view the script using a text editor such as vim/vi :
{vivek@centos7:~ }$ vi centos7-vpn.sh
RUN CENTOS7-VPN.SH TO INSTALL OPENVPN SERVER
Now all you have to do is:
Sample session from AWS/Lightsail where my cloud server is behind NAT:
{vivek@centos7:~ }$ sudo ./centos7-vpn.sh
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.
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.
VPN client config
At the end of the installation, you need to provide a client name. We can choose client names such as desktop for desktop VPN client or ios for Apple iPhone and so on. We can also encrypt the private key with a password for added security. So every time you start a VPN session, you need to provide a password for added security reasons:
HOW DO I START/STOP/RESTART OPENVPN SERVER ON CENTOS 7 ?
{vivek@centos7:~ }$ sudo systemctl stop openvpn@server #<--- stop server
{vivek@centos7:~ }$ sudo systemctl start openvpn@server #<--- start server
{vivek@centos7:~ }$ sudo systemctl restart openvpn@server #<--- restart server
{vivek@centos7:~ }$ sudo systemctl status openvpn@server #<--- 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:
OR
Next, provide this file to your OpenVPN client to connect:
{vivek@centos7:~ }$ scp vivek@139.162.60.234:~/deskcop.ovpn .
OR
{vivek@centos7:~ }$ scp root@139.162.60.234:~/deskcop.ovpn .
Next, provide this file to your OpenVPN client to connect:
- Apple app store: Apple iOS client
- Google play store: Android client
- Apple MacOS (OS X) client
- Windows 8/10 client
LINUX DESKTOP: OPENVPN CLIENT CONFIGURATION
First, install the openvpn client for your desktop as per your distro.
CentOS/RHEL/Oracle Linux desktop
Fedora Linux desktop
Run the dnf command:
{vivek@centos7:~ }$ sudo dnf install openvpn
Debian/Ubuntu Linux destkop
We need use the apt command/apt-get command:
Next, copy desktop.ovpn as follows:
Test connectivity from the CLI:
Your Linux system will automatically connect when computer restart using openvpn script/service:
{vivek@centos7:~ }$ sudo apt install openvpn
Next, copy desktop.ovpn as follows:
{vivek@centos7:~ }$ sudo cp desktop.ovpn /etc/openvpn/client.conf
Test connectivity from the CLI:
{vivek@centos7:~ }$ sudo openvpn --client --config /etc/openvpn/desktop.conf
Your Linux system will automatically connect when computer restart using openvpn script/service:
{vivek@centos7:~ }$ sudo systemctl enable openvpn@client
{vivek@centos7:~ }$ sudo systemctl start openvpn@client
Step 5 – Verify/test the connectivity
Execute the following commands after connecting to OpenVPN server from your Linux desktop:
{vivek@centos7:~ }$ ping 10.8.0.1 #Ping to the OpenVPN server gateway
{vivek@centos7:~ }$ ip route #Make sure routing setup working
{vivek@centos7:~ }$ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com #Must return public IP address of OpenVPN server
Step 6 – How to add additional OpenVPN client on a CentOS 7
First, log in to your openvpn based CentOS 7 server using the ssh command:
Run downloaded centos7-vpn.sh script again:
Sample session:
ssh vivek@your-centos7-server-ip
Run downloaded centos7-vpn.sh script again:
sudo ./centos7-vpn.sh
Sample session:
We can now add a new VPN user or delete existing VPN user.
A note about trouble shooting OpenVPN server and client issues
Check OpenVPN server for errors:
{vivek@centos7:~ }$ journalctl --identifier openvpn
Is firewall rule setup correctly on your server? Use the cat command to see rules:
{vivek@centos7:~ }$ sudo cat /etc/iptables/add-openvpn-rules.sh
Another option is to run iptables command and sysctl command commands to verify NAT rule setup on your server:
Insert the rules if not inserted from/etc/iptables/add-openvpn-rules.sh
Is OpenVPN server running and port is open? Use the ss command or netstat command and pidof command/ps command:
If not running, restart the OpenVPN server:
Look out for errors:
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:
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.
{vivek@centos7:~ }$ sudo iptables -t nat -L -n -v
{vivek@centos7:~ }$ sysctl net.ipv4.ip_forward
Insert the rules if not inserted from/etc/iptables/add-openvpn-rules.sh
{vivek@centos7:~ }$ sudo sh /etc/iptables/add-openvpn-rules.sh
{vivek@centos7:~ }$ sudo sysctl -w net.ipv4.ip_forward=1
Is OpenVPN server running and port is open? Use the ss command or netstat command and pidof command/ps command:
{vivek@centos7:~ }$ netstat -tulpn | grep :1194 ## 1194 is the openvpn server port ##
{vivek@centos7:~ }$ ss -tulpn | grep :1194 ## 1194 is the openvpn server port ##
{vivek@centos7:~ }$ ps aux | grep openvpn ## is the openvpn server running? ##
{vivek@centos7:~ }$ ps -C openvpn ## is the openvpn server running? ##
{vivek@centos7:~ }$ pidof openvpn ## find the openvpn server PID ##
If not running, restart the OpenVPN server:
{vivek@centos7:~ }$ sudo systemctl restart openvpn@server
Look out for errors:
{vivek@centos7:~ }$ sudo systemctl status openvpn@server
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:
{vivek@centos7:~ }$ nc -vu 139.162.60.234 1194
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.
This entry is 7 of 10 in the OpenVPN Tutorial series. Keep reading the rest of the series:
- How To Setup OpenVPN Server In 5 Minutes on Ubuntu Server
- Install Pi-hole with an OpenVPN to block ads
- Update/upgrade Pi-hole with an OpenVPN
- OpenVPN server on Debian 9/8
- Import a OpenVPN .ovpn file with Network Manager
- Ubuntu 18.04 LTS Set Up OpenVPN Server In 5 Minutes
- CentOS 7 Set Up OpenVPN Server In 5 Minutes
- Pi-Hole and Cloudflare DoH config
- Debian 10 Set Up OpenVPN Server In 5 Minutes
- CentOS 8 OpenVPN server in 5 mintues
ADVERTISEMENTS
No hay comentarios:
Publicar un comentario