Difference between revisions of "OpenVPN: Simple Server using Script"
Onnowpurbo (talk | contribs) (Created page with "sumber: https://www.cyberciti.biz/faq/howto-setup-openvpn-server-on-ubuntu-linux-14-04-or-16-04-lts/ I am a new Ubuntu Linux server user. How do I setup an OpenVPN Server o...") |
Onnowpurbo (talk | contribs) |
||
Line 13: | Line 13: | ||
Verify your connectivity | Verify your connectivity | ||
− | Find your public IP address | + | ==Find your public IP address== |
Use any one of the following command to find out your IPv4 public address. If your internface name is eth0 or eth1, enter: | Use any one of the following command to find out your IPv4 public address. If your internface name is eth0 or eth1, enter: | ||
− | $ ip addr show eth0 | + | |
+ | $ ip addr show eth0 | ||
OR | OR | ||
− | $ ip addr show eth1 | + | $ ip addr show eth1 |
Or use the host command or dig command as follows: | Or use the host command or dig command as follows: | ||
− | $ host myip.opendns.com resolver1.opendns.com | + | |
+ | $ host myip.opendns.com resolver1.opendns.com | ||
OR | OR | ||
− | $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | + | $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com |
Sample outputs: | Sample outputs: | ||
− | Fig.01: Find out your public IPv4 address using the CLI | + | |
− | Fig.01: Find out your public IPv4 address using the CLI | + | Fig.01: Find out your public IPv4 address using the CLI |
+ | Fig.01: Find out your public IPv4 address using the CLI | ||
Note down the public IP address 139.59.1.155 i.e. public ip address of your OpenVPN server. | Note down the public IP address 139.59.1.155 i.e. public ip address of your OpenVPN server. | ||
Line 36: | Line 39: | ||
Type the following wget command or curl command: | Type the following wget command or curl command: | ||
− | |||
− | + | $ wget https://git.io/vpn -O openvpn-install.sh | |
− | + | openvpn-install.sh | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Run openvpn-install.sh to install OpenVPN server | Run openvpn-install.sh to install OpenVPN server | ||
− | + | $ sudo bash openvpn-install.sh | |
− | $ sudo bash openvpn-install.sh | ||
− | + | That is all. Your OpenVPN server has been configured and ready to use. You can see added firewall rules /etc/rc.local file: | |
− | |||
− | |||
− | + | $ cat /etc/rc.local | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | $ cat /etc/rc.local | ||
Sample outputs: | Sample outputs: | ||
− | iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT | + | iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT |
− | iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT | + | iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT |
− | iptables -I INPUT -p udp --dport 1194 -j ACCEPT | + | iptables -I INPUT -p udp --dport 1194 -j ACCEPT |
− | iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to 139.59.1.155 | + | iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to 139.59.1.155 |
You can view your openvpn server config file generated by the script as follows (do not edit this file by hand): | You can view your openvpn server config file generated by the script as follows (do not edit this file by hand): | ||
− | $ sudo more /etc/openvpn/server.conf | + | |
− | $ sudo vi /etc/openvpn/server.conf | + | $ sudo more /etc/openvpn/server.conf |
+ | $ sudo vi /etc/openvpn/server.conf | ||
Sample outputs: | Sample outputs: | ||
− | port 1194 | + | port 1194 |
− | proto udp | + | proto udp |
− | dev tun | + | dev tun |
− | sndbuf 0 | + | sndbuf 0 |
− | rcvbuf 0 | + | rcvbuf 0 |
− | ca ca.crt | + | ca ca.crt |
− | cert server.crt | + | cert server.crt |
− | key server.key | + | key server.key |
− | dh dh.pem | + | dh dh.pem |
− | tls-auth ta.key 0 | + | tls-auth ta.key 0 |
− | topology subnet | + | topology subnet |
− | server 10.8.0.0 255.255.255.0 | + | server 10.8.0.0 255.255.255.0 |
− | ifconfig-pool-persist ipp.txt | + | ifconfig-pool-persist ipp.txt |
− | push "redirect-gateway def1 bypass-dhcp" | + | push "redirect-gateway def1 bypass-dhcp" |
− | push "dhcp-option DNS 8.8.8.8" | + | push "dhcp-option DNS 8.8.8.8" |
− | push "dhcp-option DNS 8.8.4.4" | + | push "dhcp-option DNS 8.8.4.4" |
− | keepalive 10 120 | + | keepalive 10 120 |
− | cipher AES-128-CBC | + | cipher AES-128-CBC |
− | comp-lzo | + | comp-lzo |
− | user nobody | + | user nobody |
− | group nogroup | + | group nogroup |
− | persist-key | + | persist-key |
− | persist-tun | + | persist-tun |
− | status openvpn-status.log | + | status openvpn-status.log |
− | verb 3 | + | verb 3 |
− | crl-verify crl.pem | + | crl-verify crl.pem |
How do I start/stop/restart OpenVPN server on Ubuntu Linux 16.04/18.04 LTS? | How do I start/stop/restart OpenVPN server on Ubuntu Linux 16.04/18.04 LTS? | ||
Type the following command stop the OpenVPN service: | Type the following command stop the OpenVPN service: | ||
− | $ sudo systemctl stop openvpn@server | + | $ sudo systemctl stop openvpn@server |
Type the following command start the OpenVPN service: | Type the following command start the OpenVPN service: | ||
− | $ sudo systemctl start openvpn@server | + | $ sudo systemctl start openvpn@server |
Type the following command restart the OpenVPN service: | Type the following command restart the OpenVPN service: | ||
− | $ sudo systemctl restart openvpn@server | + | $ sudo systemctl restart openvpn@server |
How do I start/stop/restart OpenVPN server on Ubuntu Linux 14.04 LTS? | How do I start/stop/restart OpenVPN server on Ubuntu Linux 14.04 LTS? | ||
Type the following command stop the OpenVPN service: | Type the following command stop the OpenVPN service: | ||
− | $ sudo /etc/init.d/openvpn stop | + | $ sudo /etc/init.d/openvpn stop |
Type the following command start the OpenVPN service: | Type the following command start the OpenVPN service: | ||
− | $ sudo /etc/init.d/openvpn start | + | $ sudo /etc/init.d/openvpn start |
Type the following command restart the OpenVPN service: | Type the following command restart the OpenVPN service: | ||
− | $ sudo /etc/init.d/openvpn restart | + | $ sudo /etc/init.d/openvpn restart |
{Optional} How to configure and use the ufw firewall rules for the OpenVPN server | {Optional} How to configure and use the ufw firewall rules for the OpenVPN server | ||
The default rules added to the /etc/rc.local file should work out of the box. However, if you have complicated firewall settings or prefer ufw to control all firewall settings on Ubuntu Linux server, try the following. First, edit the /etc/rc.local file using a text editor and comment out all firewall rules added by the script. Type the following ufw command to open port 1194 and 22 (ssh): | The default rules added to the /etc/rc.local file should work out of the box. However, if you have complicated firewall settings or prefer ufw to control all firewall settings on Ubuntu Linux server, try the following. First, edit the /etc/rc.local file using a text editor and comment out all firewall rules added by the script. Type the following ufw command to open port 1194 and 22 (ssh): | ||
− | $ sudo ufw allow 1194/udp | + | $ sudo ufw allow 1194/udp |
− | $ sudo ufw allow 22/tcp | + | $ sudo ufw allow 22/tcp |
Edit the file /etc/ufw/before.rules, enter: | Edit the file /etc/ufw/before.rules, enter: | ||
− | $ sudo vi /etc/ufw/before.rules | + | $ sudo vi /etc/ufw/before.rules |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Save and close the file. Next edit the /etc/ufw/sysctl.conf file, enter: | Save and close the file. Next edit the /etc/ufw/sysctl.conf file, enter: | ||
− | $ sudo vi /etc/ufw/sysctl.conf | + | $ sudo vi /etc/ufw/sysctl.conf |
Find and uncomment the following line to allow this host to route packets between interfaces | Find and uncomment the following line to allow this host to route packets between interfaces | ||
− | net/ipv4/ip_forward=1 | + | net/ipv4/ip_forward=1 |
Save and close the file. Enable ufw or reload if already running: | Save and close the file. Enable ufw or reload if already running: | ||
− | $ sudo ufw enable | + | $ sudo ufw enable |
OR | OR | ||
− | $ sudo ufw reload | + | $ sudo ufw reload |
Verify new firewall rules: | Verify new firewall rules: | ||
− | $ sudo ufw status | + | $ sudo ufw status |
− | $ sudo iptables -t nat -L -n -v | + | $ sudo iptables -t nat -L -n -v |
− | $ sudo iptables -L FORWARD -n -v | + | $ sudo iptables -L FORWARD -n -v |
− | $ sudo iptables -L ufw-before-forward -n -v | + | $ sudo iptables -L ufw-before-forward -n -v |
− | Client configuration | + | |
+ | ==Client configuration== | ||
On server your will find a client configuration file called ~/iphone.ovpn. All you have to do is copy this file to your local desktop using the scp and provide this file to your OpenVPN client to connect: | On server your will find a client configuration file called ~/iphone.ovpn. All you have to do is copy this file to your local desktop using the scp and provide this file to your OpenVPN client to connect: | ||
− | $ scp vivek@139.59.1.155:~/iphone.ovpn . | + | $ scp vivek@139.59.1.155:~/iphone.ovpn . |
Next, you need to download OpenVPN client as per your operating system: | Next, you need to download OpenVPN client as per your operating system: | ||
Line 289: | Line 161: | ||
You can ping to OpenVPN server private IP: | You can ping to OpenVPN server private IP: | ||
− | $ ping 10.8.0.1 | + | $ ping 10.8.0.1 |
Linux OpenVPN client configuration | Linux OpenVPN client configuration | ||
First, install the openvpn client, enter: | First, install the openvpn client, enter: | ||
− | $ sudo yum install openvpn | + | $ sudo yum install openvpn |
OR | OR | ||
− | $ sudo apt install openvpn | + | $ sudo apt install openvpn |
Next, copy iphone.ovpn as follows: | Next, copy iphone.ovpn as follows: | ||
− | $ sudo cp iphone.ovpn /etc/openvpn/client.conf | + | $ sudo cp iphone.ovpn /etc/openvpn/client.conf |
Test connectivity from the CLI: | Test connectivity from the CLI: | ||
− | $ sudo openvpn --client --config /etc/openvpn/client.conf | + | $ sudo openvpn --client --config /etc/openvpn/client.conf |
Your Linux system will automatically connect when computer restart using /etc/init.d/openvpn script: | Your Linux system will automatically connect when computer restart using /etc/init.d/openvpn script: | ||
− | $ sudo /etc/init.d/openvpn start | + | $ sudo /etc/init.d/openvpn start |
For systemd based system, use the following command: | For systemd based system, use the following command: | ||
− | $ sudo systemctl start openvpn@client | + | $ sudo systemctl start openvpn@client |
Test the connectivity: | Test the connectivity: | ||
− | $ ping 10.8.0.1 #Ping to OpenVPN server gateway | + | $ ping 10.8.0.1 #Ping to OpenVPN server gateway |
− | $ ip route #Make sure routing setup | + | $ ip route #Make sure routing setup |
− | $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com #Make sure your public IP set to OpenVPN server | + | $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com #Make sure your public IP set to OpenVPN server |
FreeBSD OpenVPN client configuration | FreeBSD OpenVPN client configuration | ||
First, install the openvpn client, enter: | First, install the openvpn client, enter: | ||
− | $ sudo pkg install openvpn | + | $ sudo pkg install openvpn |
Next, copy iphone.ovpn as follows: | Next, copy iphone.ovpn as follows: | ||
− | $ mkdir -p /usr/local/etc/openvpn/ | + | $ mkdir -p /usr/local/etc/openvpn/ |
− | $ sudo cp iphone.ovpn /usr/local/etc/openvpn/client.conf | + | $ sudo cp iphone.ovpn /usr/local/etc/openvpn/client.conf |
Edit /etc/rc.conf and add the following: | Edit /etc/rc.conf and add the following: | ||
− | openvpn_enable="YES" | + | openvpn_enable="YES" |
− | openvpn_configfile="/usr/local/etc/openvpn/client.conf" | + | openvpn_configfile="/usr/local/etc/openvpn/client.conf" |
Start the OpenVPN service: | Start the OpenVPN service: | ||
− | $ sudo /usr/local/etc/rc.d/openvpn start | + | $ sudo /usr/local/etc/rc.d/openvpn start |
Verify it: | Verify it: | ||
− | $ ping 10.8.0.1 #Ping to OpenVPN server gateway | + | $ ping 10.8.0.1 #Ping to OpenVPN server gateway |
− | $ | + | $ |
− | $ netstat -nr #Make sure routing setup | + | $ netstat -nr #Make sure routing setup |
− | $ | + | $ |
− | $ drill myip.opendns.com @resolver1.opendns.com #Make sure your public IP set to OpenVPN server | + | $ drill myip.opendns.com @resolver1.opendns.com #Make sure your public IP set to OpenVPN server |
How do I add a new client? | How do I add a new client? | ||
For demo purpose I added a new device called iphone. Let us add one more device called googlephone by running the script again: | For demo purpose I added a new device called iphone. Let us add one more device called googlephone by running the script again: | ||
− | $ sudo bash openvpn-install.sh | + | $ sudo bash openvpn-install.sh |
Sample outputs: | Sample outputs: | ||
Line 353: | Line 225: | ||
4) Exit | 4) Exit | ||
Select an option [1-4]: | Select an option [1-4]: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Revision as of 09:22, 16 February 2019
sumber: https://www.cyberciti.biz/faq/howto-setup-openvpn-server-on-ubuntu-linux-14-04-or-16-04-lts/
I am a new Ubuntu Linux server user. How do I setup an OpenVPN Server on Ubuntu Linux version 14.04 or 16.04/18.04 LTS server to shield my browsing activity from bad guys on public Wi-Fi, and more?
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 geo-blocked site and increase your privacy or safety online. This tutorial provides step-by-step instructions for configuring an OpenVPN “road warrior” server on Ubuntu Linux v14.04/16.04 LTS version including ufw/iptables firewall configuration. The steps are as follows:
Find and note down your public IP address Download openvpn-install.sh script Run openvpn-install.sh to install OpenVPN server Connect an OpenVPN server using IOS/Android/Linux/Windows client Verify your connectivity
Find your public IP address
Use any one of the following command to find out your IPv4 public address. If your internface name is eth0 or eth1, enter:
$ ip addr show eth0
OR
$ ip addr show eth1
Or use the host command or dig command as follows:
$ host myip.opendns.com resolver1.opendns.com
OR
$ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
Sample outputs:
Fig.01: Find out your public IPv4 address using the CLI Fig.01: Find out your public IPv4 address using the CLI
Note down the public IP address 139.59.1.155 i.e. public ip address of your OpenVPN server.
Download openvpn-install.sh script to set up OpenVPN server in 5 minutes on Ubuntu
Type the following wget command or curl command:
$ wget https://git.io/vpn -O openvpn-install.sh openvpn-install.sh
Run openvpn-install.sh to install OpenVPN server
$ sudo bash openvpn-install.sh
That is all. Your OpenVPN server has been configured and ready to use. You can see added firewall rules /etc/rc.local file:
$ cat /etc/rc.local
Sample outputs:
iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT iptables -I INPUT -p udp --dport 1194 -j ACCEPT iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to 139.59.1.155
You can view your openvpn server config file generated by the script as follows (do not edit this file by hand):
$ sudo more /etc/openvpn/server.conf $ sudo vi /etc/openvpn/server.conf
Sample outputs:
port 1194 proto udp dev tun sndbuf 0 rcvbuf 0 ca ca.crt cert server.crt key server.key dh dh.pem tls-auth ta.key 0 topology subnet server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" keepalive 10 120 cipher AES-128-CBC comp-lzo user nobody group nogroup persist-key persist-tun status openvpn-status.log verb 3 crl-verify crl.pem
How do I start/stop/restart OpenVPN server on Ubuntu Linux 16.04/18.04 LTS?
Type the following command stop the OpenVPN service:
$ sudo systemctl stop openvpn@server
Type the following command start the OpenVPN service:
$ sudo systemctl start openvpn@server
Type the following command restart the OpenVPN service:
$ sudo systemctl restart openvpn@server
How do I start/stop/restart OpenVPN server on Ubuntu Linux 14.04 LTS?
Type the following command stop the OpenVPN service:
$ sudo /etc/init.d/openvpn stop
Type the following command start the OpenVPN service:
$ sudo /etc/init.d/openvpn start
Type the following command restart the OpenVPN service:
$ sudo /etc/init.d/openvpn restart
{Optional} How to configure and use the ufw firewall rules for the OpenVPN server
The default rules added to the /etc/rc.local file should work out of the box. However, if you have complicated firewall settings or prefer ufw to control all firewall settings on Ubuntu Linux server, try the following. First, edit the /etc/rc.local file using a text editor and comment out all firewall rules added by the script. Type the following ufw command to open port 1194 and 22 (ssh):
$ sudo ufw allow 1194/udp $ sudo ufw allow 22/tcp
Edit the file /etc/ufw/before.rules, enter:
$ sudo vi /etc/ufw/before.rules
Save and close the file. Next edit the /etc/ufw/sysctl.conf file, enter:
$ sudo vi /etc/ufw/sysctl.conf
Find and uncomment the following line to allow this host to route packets between interfaces
net/ipv4/ip_forward=1
Save and close the file. Enable ufw or reload if already running:
$ sudo ufw enable
OR
$ sudo ufw reload
Verify new firewall rules:
$ sudo ufw status $ sudo iptables -t nat -L -n -v $ sudo iptables -L FORWARD -n -v $ sudo iptables -L ufw-before-forward -n -v
Client configuration
On server your will find a client configuration file called ~/iphone.ovpn. All you have to do is copy this file to your local desktop using the scp and provide this file to your OpenVPN client to connect:
$ scp vivek@139.59.1.155:~/iphone.ovpn .
Next, you need to download OpenVPN client as per your operating system:
Download OpenVPN client for Apple IOS version 6.x or above and install it. Download OpenVPN client for Android and install it. Download OpenVPN client for Apple MacOS (OS X) and install it. Download OpenVPN client for Windows 8/10 and install it.
MacOS/OS X OpenVPN client configuration
Just double click on iphone.ovpn file and it will open in your tunnelblick client > Click on the “Only me” to install it. Fig.03: MacOS / OS X openvpn client configuration Fig.03: MacOS / OS X openvpn client configuration
Once installed click on Connect button and you will be online. Use the following command on MacOS client to verify that your public IP changed to the VPN server IP: $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
You can ping to OpenVPN server private IP:
$ ping 10.8.0.1
Linux OpenVPN client configuration
First, install the openvpn client, enter:
$ sudo yum install openvpn
OR
$ sudo apt install openvpn
Next, copy iphone.ovpn as follows:
$ sudo cp iphone.ovpn /etc/openvpn/client.conf
Test connectivity from the CLI:
$ sudo openvpn --client --config /etc/openvpn/client.conf
Your Linux system will automatically connect when computer restart using /etc/init.d/openvpn script:
$ sudo /etc/init.d/openvpn start
For systemd based system, use the following command:
$ sudo systemctl start openvpn@client
Test the connectivity:
$ ping 10.8.0.1 #Ping to OpenVPN server gateway $ ip route #Make sure routing setup $ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com #Make sure your public IP set to OpenVPN server
FreeBSD OpenVPN client configuration
First, install the openvpn client, enter:
$ sudo pkg install openvpn
Next, copy iphone.ovpn as follows:
$ mkdir -p /usr/local/etc/openvpn/ $ sudo cp iphone.ovpn /usr/local/etc/openvpn/client.conf
Edit /etc/rc.conf and add the following:
openvpn_enable="YES" openvpn_configfile="/usr/local/etc/openvpn/client.conf"
Start the OpenVPN service:
$ sudo /usr/local/etc/rc.d/openvpn start
Verify it:
$ ping 10.8.0.1 #Ping to OpenVPN server gateway $ $ netstat -nr #Make sure routing setup $ $ drill myip.opendns.com @resolver1.opendns.com #Make sure your public IP set to OpenVPN server
How do I add a new client?
For demo purpose I added a new device called iphone. Let us add one more device called googlephone by running the script again:
$ sudo bash openvpn-install.sh
Sample outputs:
Looks like OpenVPN is already installed
What do you want to do?
1) Add a cert for a new user 2) Revoke existing user cert 3) Remove OpenVPN 4) Exit
Select an option [1-4]: