don't stop believing

openVPN 설치 (on Ubuntu 18.04) 본문

Tools/OpenVPN

openVPN 설치 (on Ubuntu 18.04)

Tongchun 2018. 8. 30. 10:55

사내에서 vpn을 한번 사용해 보더니 수요가 늘었습니다.

iptime T24000을 라우터로 사용하고 있는데 VPN을 다섯개만 지원합니다. 그래서 openVPN Server를 구성해 사용하겠습니다.


openVPN Server 구축에 참고한 링크는 아래와 같습니다.

https://www.digitalocean.com/community/tutorials/how-to-set-up-an-openvpn-server-on-ubuntu-18-04


위 링크에서는 openVPN 서버와 CA 서버를 구분해서 설명해 주고 있는데 저는 하나의 PC에 만들겠습니다.


apt를 업데이트하고 openvpn을 설치합니다.

$ sudo apt update
$ sudo apt install openvpn


그다음 인증서(CA)를 생성할 EasyRSA를 다운받고 압축을 해제합니다.

EasyRSA는 아래 링크에서 다운받을 수 있으며 현재 버전은 3.0.4 입니다.

https://github.com/OpenVPN/easy-rsa/releases

$ wget -P ~/ https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.4/EasyRSA-3.0.4.tgz
$ tar xvf EasyRSA-3.0.4.tgz

EasyRSA의 설정정보를 수정할 차례입니다.

EasyRSA-3.0.4 폴더 안에는 vars.example라는 예제파일이 있습니다. 이걸 vars라고 새로 복재한 후 vars에서 수정하겠습니다.

$ cd ~/EasyRSA-3.0.4/
$ cp vars.example vars
$ vim vars

수정해줄 부분은 회사(사용자) 정보 부분입니다.

#set_var EASYRSA_REQ_COUNTRY    "US"
#set_var EASYRSA_REQ_PROVINCE   "California"
#set_var EASYRSA_REQ_CITY       "San Francisco"
#set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"
#set_var EASYRSA_REQ_EMAIL      "me@example.net"
#set_var EASYRSA_REQ_OU         "My Organizational Unit"

주석을 제거하고 nGle 주소에 맞게 변경했습니다.

set_var EASYRSA_REQ_COUNTRY     "KR"
set_var EASYRSA_REQ_PROVINCE    "Gyeonggi-do"
set_var EASYRSA_REQ_CITY        "Seongnam"
set_var EASYRSA_REQ_ORG         "nGle"
set_var EASYRSA_REQ_EMAIL       "tongchun@ngle.co.kr"
set_var EASYRSA_REQ_OU          "PlatformQA"

수정했으면 저장하고 나옵니다.

EasyRSA-3.0.4 폴더 안에는 easyrsa 파일이 있습니다. 이걸 실행하면 vars에 수정한 정보로 CA 서버가 구축됩니다.

easyras의 초기화 옵션인 init-pki를 추가해 EasyRSA 서버를 구축합니다.

$ ./easyrsa init-pki

Note: using Easy-RSA configuration from: ./vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /home/ngle/EasyRSA-3.0.4/pki

이제 easyrsa로 인증서와 키 파일을 생성합니다.

매번 비번을 넣기 번거로우므로 nopass 옵션도 줍니다.

$ ./easyrsa build-ca nopass

명령을 실행하면 아래와 같이 Common Name을 묻습니다.

Common Name (eg: your user, host, or server name) [Easy-RSA CA]:


Common Name은 인증서를 만들때 참조할 인증기관을 말합니다. 특별히 입력할게 없다면 그냥 엔터를 치고 넘어갑니다. 그런 기본 인증기관이 지정됩니다.


이번에는 openVPN에서 사용할 인증서와 key파일을 만들겠습니다.

이번에는gen-req 옵션을 주고 바로 다음에 인증서를 사용할 장비의 이름을 넣습니다. 장비이름은 그냥 예제를 따라서 server라고 하겠습니다. nopass 옵션도 줍니다.

$ ./easyrsa gen-req server nopass

명령을 실행하면 인증서를 사용할 이름을 묻습니다. 옵션에 server라고 줬기 때문에 그냥 엔터를 누르면 server라고 생성됩니다.

Common Name (eg: your user, host, or server name) [server]:


결과가 아래와 같이 리턴되었습니다.

Keypair and certificate request completed. Your files are:

req: /home/ngle/EasyRSA-3.0.4/pki/reqs/server.req

key: /home/ngle/EasyRSA-3.0.4/pki/private/server.key


key 파일과 certificate request 파일이 생성되었습니다.

이제 openVPN 디렉터리에 key파일을 복사합니다.


openVPN 디렉터리는 /etc/openvpn/ 입니다.

$ sudo cp ~/EasyRSA-3.0.4/pki/private/server.key /etc/openvpn/

이제 인증서 파일을 만들 차례입니다.

easyras에 sign-req 옵션을 줍니다. request 타입은 client와 server가 있는데 server로 줍니다. 아래 명령에서 첫번째 server를 말합니다.

마지막에 오는 server는 key파일의 이름입니다.

$ ./easyrsa sign-req server server

명령을 실행하면 아래와 같이 리턴됩니다.


Note: using Easy-RSA configuration from: ./vars


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 3650 days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details:

Confirm request details에 yes라고 입력하고 엔터를 누릅니다.

인증서의 유효기간은 3650일 이네요.


실행이 완료되면 server.crt파일이 만들어 집니다.

Certificate created at: /home/ngle/EasyRSA-3.0.4/pki/issued/server.crt


생성된 server.crt 파일과 ca.crt 파일을 openVPN 폴더로 복사합니다.

$ sudo cp ~/EasyRSA-3.0.4/pki/issued/server.crt /etc/openvpn/
$ sudo cp ~/EasyRSA-3.0.4/pki/ca.crt /etc/openvpn/

이제 Diffie-Hellman key를 만들어 줍니다.

$ ./easyrsa gen-dh

명령을 실행하면 점들이 찍히며 완료하는데 몇 초 정도 걸립니다.

결과로 아래와 같이 pem 파일이 만들어 졌습니다.

DH parameters of size 2048 created at /home/ngle/EasyRSA-3.0.4/pki/dh.pem


이번엔 openvpn을 이용해 ta.key 파일을 만들겠습니다.

$ openvpn --genkey --secret ta.key

생성한 dh.pem 파일과 ta.key 파일을 openvpn 폴더로 복사합니다.

$ sudo cp ~/EasyRSA-3.0.4/ta.key /etc/openvpn/
$ sudo cp ~/EasyRSA-3.0.4/pki/dh.pem /etc/openvpn/

생성해야 할 파일들이 많네요. 서버에 대한 인증서는 이제 준비되었습니다.

이번엔 client에서 사용할 인증서 파일을 만들겠습니다.

client 인증서는 VPN 유저 1명당 발급해야 합니다. 따라서 vpn 계정이 여러개일 경우 아래 명령을 반복해야 합니다.

스크립트로 간단히 만들어 놓으면 좋을 것 같습니다.


먼저 client 인증서를 모아둘 폴더를 하나 만들겠습니다.

$ mkdir -p ~/client-configs/keys
$ chmod -R 700 ~/client-configs

이제 EasyRSA에서 vpn 계정에 대한 인증서를 만들어 줍니다.

vpn 계정을 tongchun으로 할겁니다. 그럼 easyrsa 명령의 gen-req 옵션에 tongchun 이라고 common name을 줍니다. nopass 도 옵션에 포함합니다.

$ cd ~/EasyRSA-3.0.4/
$ ./easyrsa gen-req tongchun nopass

명령을 실행하면 아래와 같이 Common Name을 다시한번 확인합니다.

Common Name (eg: your user, host, or server name) [tongchun]:

엔터를 치고 tongchun으로 계속 진행합니다.


Keypair 아 certificate request 파일이 아래와 같이 만들어 졌습니다.

req: /home/ngle/EasyRSA-3.0.4/pki/reqs/tongchun.req

key: /home/ngle/EasyRSA-3.0.4/pki/private/tongchun.key


생성한 tongchun.key 파일을 클라이언트 keys 폴더(~/client-configs/keys)에 복사합니다.

$ cp pki/private/tongchun.key ~/client-configs/keys/

tongchun.key 파일로 인증서를 만들어 줍니다. request type은 client로 줍니다.

$ ./easyrsa sign-req client tongchun

마찬가지로 인증서의 정보가 맞는지 확인합니다.

Note: using Easy-RSA configuration from: ./vars


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 3650 days:

subject=
    commonName                = tongchun


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details:

Confirm request details: 에 yes를 입력하고 엔터를 누릅니다.

그럼 아래와 같이 인증서 파일이 만들어 집니다.

Certificate created at: /home/ngle/EasyRSA-3.0.4/pki/issued/tongchun.crt


만들어진 인증서 파일(.crt)도 클라이언트 keys 폴더에 복사합니다.

$ sudo cp pki/issued/tongchun.crt ~/client-configs/keys/

ca.crt 와 ta.key 파일도 클라이언트 keys 폴더로 복사합니다.

$ sudo cp ~/EasyRSA-3.0.4/ta.key ~/client-configs/keys/
$ sudo cp /etc/openvpn/ca.crt ~/client-configs/keys/


이제 openVPN 계정으로 사용할 tongchun 계정의 인증서 준비가 되었습니다.


이번엔 openVPN에 대한 설정입니다.

먼저 openVPN에서 제공한 sample config files를 복사하고 압축을 해재합니다.

$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
$ sudo gzip -d /etc/openvpn/server.conf.gz

압축을 푼 server.conf 파일을 엽니다.

$ sudo vim /etc/openvpn/server.conf

HMAC Section의 tls-auth를 찾습니다.

tls-auth ta.key 0 이 주석이 제거되어 있는지 확인합니다. 그리고 바로 밑에 key-direction 0을 추가합니다.

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth ta.key 0 # This file is secret
key-direction 0

다음으로 cryptographic cipher Section의 cipher를 찾습니다.

cipher AES-256-CBC가 주석이 제거되어 있는지 확인합니다. 그리고 바로 밑에 auth SHA256를 추가합니다.

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
auth SHA256

이번에는 dh를 찾습니다.

dh의 파라메터에 dh2048.pem으로 되어 있을 것입니다. EasyRSA가 생성하는 .pem파일이 이전에는 dh2048.pem이었는데 최근 버전에서는 dh.pem 파일로 생성되는 파일 이름이 변경되었습니다. (앞서 인증서 생성할때 보셨을 겁니다.)

dh2040.pem에서 2048을 지우고 dh dh.pem 으로 변경합니다.

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh2048.pem 2048
dh dh.pem

마지막으로, user와 group을 찾습니다.

user와 group 앞에있는 주석을 제거합니다.

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nogroup

여기까지는 server.conf 파일의 기본 변경 설정이었으며 다음은 optional 설정입니다.

저는 아래 설명할 optional 설정도 모두 변경해 줬습니다.


(Optional) Push DNS Changes to Redirect All Traffic Through the VPN

VPN으로 접속한 Client의 DNS를 변경하게 합니다.

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
push "redirect-gateway def1 bypass-dhcp"

push "redirect-gateway def1 bypass-dhcp" 앞의 주석을 제거합니다.

그리고 바로 밑에있는 dhcp-option 옵션도 주석을 제거합니다.

# 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.
push "dhcp-option DNS 168.126.63.1"
push "dhcp-option DNS 168.126.63.2"

dhcp-option DNS에 opendns.com의 DNS 주소가 있었는데 저는 KT의 DNS를 사용했습니다.

이렇게 하면 LG나 다른 통신사 회선으로 VPN에 접속하더라도 클라이언트의 DNS를 config에 설정한 DNS로 변경하게 됩니다.


(Optional) Adjust the Port and Protocol

openVPN의 기본 port는 1194이고 UDP를 사용합니다.

이걸 Port는 443, 프로토콜은 TCP를 사용하도록 변경하겠습니다.

# 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 443

# TCP or UDP server?
proto tcp
;proto udp

그리고 tcp를 사용하도록 수정했다면 explicit-exit-notify도 변경해 줘야 합니다.

explicit-exit-notify의 값을 0으로 변경합니다.

# Notify the client that when the server restarts so it
# can automatically reconnect.
explicit-exit-notify 0


(Optional) Point to Non-Default Credentials

만약 앞서 서버 인증서를 만들때 server가 아닌 다른 이름으로 만들었다면 .crt와 .key파일의 이름을 변경해 줘야 합니다.

저는 server로 만들어 줘서 기본 설정인 server.crt와 server.key가 정상적인지만 확인하겠습니다.

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret

이제 수정해야 할 기본 설정은 되었습니다.

server.conf 파일을 저장합니다.


이번에는 openVPN server의 네트워크 설정을 변경해 줘야 합니다.

먼저 ip forwarding이 되도록 설정해야 합니다.

/etc/sysctl.conf 파일을 열어서 net.ipv4.ip_forward=1로 수정합니다.

$ sudo vim /etc/sysctl.conf

net.ipv4.ip_forward를 찾습니다. 주석처리 되어 있다면 주석을 해제합니다.

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

주석을 제거했다면 sysctl.conf 파일을 저장합니다.

그리고 현재 열려있는 session에서 변경 사항을 적용합니다.

$ sudo sysctl -p
net.ipv4.ip_forward = 1


이번에는 방화벽(Ubuntu) 설정입니다.

VPN 서버로 들어오는 불필요한 트래픽을 제거하는 목적입니다.

먼저 현재 네트워크의 인터페이스 이름을 확인해야 합니다.

아래 명령으로 확인할 수 있습니다.

$ ip route | grep default
default via 192.168.0.1 dev enp3s0 proto dhcp metric 100 

default gateway가 192.168.0.1로 되어 있네요. 인터페이스 이름은 dev 다음에 있습니다.

enp3s0가 네트워크 인터페이스 네임입니다.


이제 ufw(방화벽) 설정을 열고 openVPN에서 사용할 roule을 추가합니다.

$ sudo vim /etc/ufw/before.rules

UFW rule은 보통 ufw 명령을 사용해 추가합니다. 추가된 방화벽(ufw) rule은 before.rules 파일에 저장됩니다.

아래와같이 추가합니다.

# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward
#
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to enp3s0
-A POSTROUTING -s 10.8.0.0/8 -o enp3s0 -j MASQUERADE
COMMIT
# END OPENVPN RULES
#

VPN으로 접속했을때 클라이언트에서 받는 ip는 10.8.0.1 부터 10.10.255.254까지 입니다.

ip를 다르게 해보려고 했는데 openVPN의 server.conf 파일에 모두 10.8.0.0으로 설정되어 있어서 변경하려면 수정할 부분이 많아집니다.

그래서 그냥 쓰도록 하겠습니다.


추가되는 rule은 START OPENVPN RULES 부터 END OPENVPN RULES까지 내용입니다.

추가되었다면 저장합니다.


UFW에도 추가해야 합니다.

/etc/default/ufw 파일을 열고 DEFAULT_FORWARD_POLICY찾아 ACCEPT로 수정합니다.

$ sudo vim /etc/default/ufw

DEFAULT_FORWARD_POLICY는 DROP이라고 되어 있는 것을 ACCEPT로 변경합니다.

# Set the default forward policy to ACCEPT, DROP or REJECT.  Please note that
# if you change this you will most likely want to adjust your rules
DEFAULT_FORWARD_POLICY="ACCEPT"

ufw 파일을 저장합니다.


앞서 openVPN의 port와 프로토콜을 변경해 줬기 때문에 방화벽(ufw)에도 적용해야 합니다.

openSSH도 추가해 줍니다.

$ sudo ufw allow 443/tcp
$ sudo ufw allow OpenSSH

방화벽 설정이 다 되었다면 ufw disable enable을 실행해 재시작 해줍니다.

$ sudo ufw disable
$ sudo ufw enable

sudo ufw enable 명령을 실행할때 아래와 같은 메시지가 나왔다면 y를 입력합니다.

Command may disrupt existing ssh connections. Proceed with operation (y|n)? 


openVPN server를 시작할 준비는 거의 되었습니다.

이제 openVPN을 시작할 차례입니다. systemctl 명령으로 openVPN을 시작합니다. openVPN 실행시 설정 파일은 /etc/openvpn/server.conf 파일을 사용하기 위해 @server를 추가해 실행합니다.

$ sudo systemctl start openvpn@server

명령을 실행하면 리턴값이 없습니다. systemctl status 옵션으로 확인해 봅니다.

$ sudo systemctl status openvpn@server

status 옵션으로 명령을 실행하면 아래와 같이 결과가 나옵니다.

● openvpn@server.service - OpenVPN connection to server
   Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-08-30 16:08:10 KST; 6min ago
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO
 Main PID: 11225 (openvpn)
   Status: "Initialization Sequence Completed"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/system-openvpn.slice/openvpn@server.service
           └─11225 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid

openVPN의 tun0 인터페이스도 확인해 줍니다.

$ ip addr show tun0
3: tun0:  mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::ef81:ab40:f27b:a304/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

서버가 reboot되어도 자동으로 시작되도록 설정합니다.

$ sudo systemctl enable openvpn@server
Created symlink /etc/systemd/system/multi-user.target.wants/openvpn@server.service → /lib/systemd/system/openvpn@.service.

이제 openVPN이 모두 설정되었고 실행되었습니다.


여기서 끝이 아닙니다.

초기 사용자로 tongchun을 추가했었는데요. 사용자를 추가할때마다 인증서를 만들어줘야 하기 때문에 상당히 번거롭습니다.

그래서 Client 추가에 설정 구성을 잡도록 하겠습니다.


먼저 만들었단 clinet-configs 폴더 아래 files라고 하위 폴더를 만들어 줍니다.

$ mkdir -p ~/client-configs/files

 그리고 client configuration 샘플파일을 client-configs 폴더에 복사합니다.

$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

복사한 base.conf 파일을 열고 수정하겠습니다.

$ sudo vim ~/client-configs/base.conf

remote를 찾아 openVPN 서버의 ip와 port를 추가합니다. (ip는 public ip 입니다.)

port도 기본 포트가 아닌 443으로 변경했으니 443으로 잡아줍니다.

$ # The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote 120.130.170.120 443

이번엔 proto를 찾고 tcp로 변경합니다. 우리는 앞서 udp에서 tcp로 변경했습니다.

주석을 변경하면 됩니다.

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

다음으로 user와 group 앞의 주석을 제거합니다.

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

SSL/TLS parms. Section을 찾아 ca, cert, key 설정을 모두 주석 처리 합니다.

# 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 client.crt
#key client.key

cipher와 auth를 찾아 server의 config와 동일하게 수정합니다.

cipher는 AES-256-CBC 그대로이며 auth SHA256은 추가해 줍니다.

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
auth SHA256

이번에는 key-direction 1 을 추가해 줍니다. auth 밑에 추가해 주면 됩니다.

key-direction 1

이제 마지막으로 아래 항목을 주석처리 한 채 추가해 줍니다. 추가되는 항목은 리눅스 클라이언트에서 사용하는 항목들입니다

base.config 파일 재일 마지막 줄에 추가했습니다.

# script-security 2
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf

설정이 완료되었다면 파일을 저장합니다.


이제 simple script를 만들 차례입니다.인증서와 key 파일들을 구성하고 ~/client-configs/files 경로에 생성하게 해줍니다.

~/client-configs 폴더 안애 make_config.sh 라고 파일을 만들어 줍니다.

$ sudo vim ~/client-configs/make_config.sh

make_config.sh 파일안에 아래와 같이 추가합니다.

#!/bin/bash

# First argument: Client identifier

KEY_DIR=~/client-configs/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf

cat ${BASE_CONFIG} \
    <(echo -e '<ca>') \
    ${KEY_DIR}/ca.crt \
    <(echo -e '</ca>\n<cert>') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '</cert>\n<key>') \
    ${KEY_DIR}/${1}.key \
    <(echo -e '</key>\n<tls-auth>') \
    ${KEY_DIR}/ta.key \
    <(echo -e '</tls-auth>') \
    > ${OUTPUT_DIR}/${1}.ovpn

make_config.sh 파일을 저장하고 아래와 같이 권한을 줍니다.

$ sudo chmod 700 ~/client-configs/make_config.sh

이제 사용자를 구성할 설정이 완료되었습니다.

client-configs 폴더로 이동해 tongchun 계정을 openVPN 계정으로 만들어 보겠습니다.

$ cd ~/client-configs
$ sudo ./make_config.sh tongchun

make_config.sh <인증서를 만든 계정>을 실행하면 ~/client-configs/files 안에 계정 이름으로된 .ovpn 파일이 생성됩니다.

저는 tongchun.ovpn 파일이 생성되었습니다.

$ ls ~/client-configs/files/
tongchun.ovpn

.ovpn 파일은 client에서 vpn 접속시 필요합니다. VPN으로 접속하려는 로컬 컴퓨터나 모바일 디바이스에서 필요합니다.

.ovpn 파일은 암호화된 ftp(sftp)를 이용해 전달되어야 합니다.


리눅스나 맥에서 아래와 같이 파일을 가져올 수 있습니다.


[추가]

Windows를 제외한 OS에서 VPN 연결을 하려면 ta.key 파일도 필요합니다.

ta.key 파일도 client-configs/files/ 로 복사하고 권한을 변경해 줍니다.

$ sudo cp ~/client-configs/keys/ta.key ~/client-configs/files/
$ sudo chmod 644 ta.key 

openVPN 서버에서 나갑니다.

그리고 Mac에서 sftp를 이용해 ovpn 파일과 ta.key 파일을 다운로드 합니다.

$ sftp ngle@192.168.0.185:client-configs/files/ta.key ~/Downloads/
$ sftp ngle@192.168.0.185:client-configs/files/tongchun.ovpn ~/Downloads/

이제 openVPN 서버 구성은 완료되었습니다.

스크롤의 압박으로 클라이언트 접속과 추가 유저 생성에 대한 부분은 나눠서 Post 하겠습니다.


마지막으로 외부에서 내부 openVPN server로 접속할 수 있도록 라우터의 port forwarding 설정을 아래와 같이 합니다.

내부 openVPN server IP는 192.168.0.185입니다. 포트는 443으로 설정했죠.




Comments