Configuring OpenVPN server on Fedora 21

Posted: July 6, 2015 in Linux

OpenVPN is an open-source VPN application which allows you to create secure tunnels between machines that are not on the same local network.

In this article we will install OpenVPN server on Fedora 21 KVM host,with IP ADDRESS 192.168.0.43,hostname s1.mydomain.com

OpenVPN clients would be KVM host (CENTOS 7),it’s on the same network as Fedora,but it will be used to test connection

Another client will be my Win7 lap-top,also on the same network as Fedora.After testing VPN connection locally,lap top will be connected to different Wi-Fi network to test access from the internet

Installing OpenVPN server

yum install openvpn easy-rsa -y

Creating keys and certificates

We will create directory in which we will store certs,keys and certification scripts:

mkdir /etc/openvpn/easy-rsa/keys

Copy cert scripts to new folder:

cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa

Create predefined answers for script  to avoid typing it when we start to create certs/keys

vi /etc/openvpn/easy-rsa/vars
# NOTE: If you installed from an RPM,
# don't edit this file in place in
# /usr/share/openvpn/easy-rsa --
# instead, you should copy the whole
# easy-rsa directory to another location
# (such as /etc/openvpn) so that your
# edits will not be wiped out by a future
# OpenVPN package upgrade.

# This variable should point to
# the top level of the easy-rsa
# tree.
export EASY_RSA="`pwd`"

#
# This variable should point to
# the requested executables
#
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"
# This variable should point to
# the openssl.cnf file included
# with easy-rsa.
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`

# Edit this variable to point to
# your soon-to-be-created key
# directory.
#
# WARNING: clean-all will do
# a rm -rf on this directory
# so make sure you define
# it correctly!
export KEY_DIR="$EASY_RSA/keys"

# Issue rm -rf warning
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR

# PKCS11 fixes
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"

# Increase this to 2048 if you
# are paranoid. This will slow
# down TLS negotiation performance
# as well as the one-time DH parms
# generation process.
export KEY_SIZE=2048

# In how many days should the root CA key expire?
export CA_EXPIRE=3650

# In how many days should certificates expire?
export KEY_EXPIRE=3650

# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="SR"
export KEY_PROVINCE="Zemoon"
export KEY_CITY="Belgrade"
export KEY_ORG="my organization"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"

# X509 Subject Field
export KEY_NAME=s1.mydomain.com  #this is OpenVPN server host name

# PKCS11 Smart Card
# export PKCS11_MODULE_PATH="/usr/lib/changeme.so"
# export PKCS11_PIN=1234

Read and execute commands from vars:

cd /etc/openvpn/easy-rsa
source ./vars

Clean any previously created certs/keys

./clean-all

Build CA and press ENTER for any given questions (that’s why we edited vars script)

./build-ca

Now generate  key and certificate for OpenVPN server. Again,press ENTER for each questions, and  answer Y (yes) to commit the changes at the end

./build-key-server s1.mydomain.com

We must create a Diffie-Hellman key exchange file.

./build-dh

in easy-rsa folder,subfolder keys is generated by scripts above.In that folder certs/keys are stored.

We need to copy these keys to /etc/openvpn directory

cd /etc/openvpn/easy-rsa/keys
cp dh2048.pem ca.crt s1.mydomain.com.crt s1.mydomain.com.key /etc/openvpn

Our clients will also need certificates to be able to authenticate.These keys/certs need to be transferred to client machines

(ssh,scp,WinSCP,USB stick,shared folder….)

I named our client june

cd /etc/openvpn/easy-rsa
./build-key june

Files june.crt and june.key will be created.

june.crt,june.key and ca.crt files need to be copied to client computers,no matter what OS they’re using

We’ll do that later.

Now we will configure OpenVPN server:

vi /etc/openvpn/server.conf
# Which local IP address should OpenVPN
# listen on? (optional)
local 192.168.0.43  #OpenVPN server IP

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one. You will need to
# open up this port on your firewall.
port 1194 # You can change port here

# TCP or UDP server?
;proto tcp
proto udp

 
;dev tap
dev tun
 
ca /etc/openvpn/ca.crt                                 #our previously generated key/certs
cert /etc/openvpn/s1.mydomain.com.crt
key /etc/openvpn/s1.mydomain.com.key   # This file should be kept secret

 
dh /etc/openvpn/dh2048.pem

 
server 10.8.0.0 255.255.255.0  # pool of addresses which OpenVPN clients will be using

# Maintain a record of client <-> virtual IP address
# associations in this file. If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt


push "redirect-gateway def1 bypass-dhcp"  #instruct  the client to redirect all traffic through our OpenVPN.

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses. CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
#Provide DNS servers to the client, as it will not be able to use the default
# DNS servers provided by your ISP
push "dhcp-option DNS 8.8.8.8"


 

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

 
# You can uncomment this out on
# non-Windows systems.

Run OpenVPN with no privileges once it has started, 

user nobody 
group nobody 

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
;log-append  openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

Enable routing

For the server to be able to forward IPv4 packets between the interfaces we need to enable IPv4 forwarding.

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

Add the following firewall-cmd rules (open port 1194,add OpenVPN interface-tun1 to trusted zone,enable masquerading,

configure IP masquerading on the server for outgoing packets from VPN clients)

firewall-cmd --permanent --add-service openvpn
firewall-cmd --permanent --zone=trusted --add-interface=tun1
firewall-cmd --permanent --zone=trusted --add-masquerade
DEV=$(ip route get 8.8.8.8 | awk 'NR==1 {print $(NF-2)}')
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s  10.8.0.0/24 -o $DEV -j MASQUERADE
firewall-cmd --reload

Start OpenVPN:

systemctl start openvpn@server.service

Configuring Clients-CentOS 7
We will now transfer client certs/key to client machine

On Centos 7 client,create folder for files above:

mkdir /root/.openvpn

From OpenVPN server,transfer june.crt,june.key and ca.crt to .openvpn folder

Install OpenVPN package to CENTOS 7 client

We’ll need to install the Extra Packages for Enterprise Linux (EPEL) repository. This is because OpenVPN isn’t available in the default CentOS repositories.

yum install epel-release
yum install openvpn easy-rsa

in .openvpn folder,create config file june.ovpn (each user has to have it’s own ovpn file)

client
remote 192.168.0.43 1194  # It's my Fedora server's IP address
dev tun
proto udp
resolv-retry infinite
nobind
persist-key
persist-tun
verb 3
key june.key
ca ca.crt
cert june.crt
comp-lzo

Add s1.mydomain.com to hosts file

And finally,test connection:

openvpn --config ~/.openvpn/june.ovpn

You should see that connection is successful

Untitled6

<strongConfiguring Win 7client

For win7  i created shared folder on Fedora server,this share will be accessible from the internet.

My ISP blocked incoming connections for port 445,so i will use VPN connection to access this share

On VPN server install samba,create shared folder and configure samba

yum install samba* -y
  mkdir /share
  vi /etc/samba/smb.cnf
[global]

# ----------------------- Network-Related Options -------------------------
#
# workgroup = the Windows NT domain name or workgroup name, for example, MYGROUP.
#
# server string = the equivalent of the Windows NT Description field.
#
# netbios name = used to specify a server name that is not tied to the hostname.
#
# interfaces = used to configure Samba to listen on multiple network interfaces.
# If you have multiple interfaces, you can use the "interfaces =" option to
# configure which of those interfaces Samba listens on. Never omit the localhost
# interface (lo).
#
# hosts allow = the hosts allowed to connect. This option can also be used on a
# per-share basis.
#
# hosts deny = the hosts not allowed to connect. This option can also be used on
# a per-share basis.
#
# max protocol = used to define the supported protocol. The default is NT1. You
# can set it to SMB2 if you want experimental SMB2 support.
#
workgroup = MYGROUP
server string = Samba Server Version %v

; netbios name = MYSERVER

interfaces = lo enp2s0 #Server's interface
hosts allow = 127. 192.168.0.0/24 10.8.0.0/24  # allow access to VPN users

 
 [Shares]
 path = /shares/
 read only = no
 browseable = yes
 writable = yes
 public = yes
 valid users= root june

Create password for CentOS server user june

smbpasswd june

Start samba service,open firewall ports and configure SELinux to allow write to folder

systemctl start smb
firewall-cmd --add-service=samba --permanent
firewall-cmd --reload
chcon -t samba_share_t /shares

Same files we copied to CentOS 7 client,we need to copy to Win7,put them in /share folder so it can be accessible to win7 client.You won’t be able to copy june.key to win7 until change ownership of june.key to june

cp /etc/openvpn/easy-rsa/keys {ca.crt,june.crt,june.key} /shares/
chown june /shares/june.key

On win 7 install latest version of OpenVPN Windows client,add s1.mydomain.com to hosts file,

copy ca.crt,june.crt,june.key to C:\Program Files\OpenVPN\config.In insatall directory you have file client.ovpn,copy it to

config folder too as june.ovpn:

File june.ovpn:

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
;proto tcp
proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
;remote 192.168.0.43 1194
remote server.bigfirm.info 1194  # Public FQDN of OpenVPN server

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert june.crt
key june.key

# Verify server certificate by checking that the
# certicate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
#   digitalSignature, keyEncipherment
# and the extendedKeyUsage to
#   serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20

Untitled4

To test internet access,i bought bigfirm.info domain,put my current public IPAddress to GoDaddy DNS server and made my
Fedora OpenVPN server publicly accessible.

I connected my lap top to different Wi-Fi network,separated from my home network,and tested VPN connection:

Untitled1

Untitled3

Untitled5

I can now browse samba share from my lap-top

If you get error regarding mismatching password,from win 7 open june.key from samba share,copy all content and paste it

in june.key file in C:\Program Files\OpenVPN\config directory

Advertisements
Comments
  1. Thanks for publishing this awesome article. I’m a long time reader but I’ve
    never been compelled to leave a comment. I subscribed to your blog and shared this on my
    Twitter. Thanks again for this great post!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s