웹사이트 검색

Debian 10에서 OpenVPN 서버를 설치하고 구성하는 방법


이 페이지에서

  1. 요구 사항\n
  2. OpenVPN 설치
  3. 서버 인증서 및 키 생성
  4. 클라이언트 인증서 및 키 생성
  5. OpenVPN 서버 구성
  6. OpenVPN 클라이언트 설치 및 구성

OpenVPN은 신뢰할 수 없는 네트워크에 연결된 경우 인터넷에 안전하게 액세스하는 데 사용할 수 있는 오픈 소스 소프트웨어입니다. OpenVPN을 사용하면 암호화된 서버를 통해 온라인 데이터를 터널링하여 안전하게 유지할 수 있습니다. OpenVPN은 키 교환을 위해 SSL/TLS를 사용하고 네트워크 주소 변환기를 통과할 수 있습니다. 시장에는 많은 VPN 소프트웨어가 있지만 모두 비용이 많이 들고 설치 및 관리가 까다롭습니다. OpenVPN은 무료이며 설정, 구성 및 관리가 간단합니다.

이 튜토리얼에서는 Debian 10 서버에서 OpenVPN 서버를 설정하는 방법을 설명합니다.

요구 사항

  • Debian 10을 실행하는 두 대의 서버.\n
  • 고정 IP 주소 192.168.0.103은 VPN 서버에 구성되고 192.168.0.102는 VPN 클라이언트에 구성됩니다.
  • 루트 암호는 두 서버 모두에서 구성됩니다.\n

OpenVPN 설치

먼저 네트워크 패킷을 제대로 전달하려면 IP 전달을 활성화해야 합니다. /etc/sysctl.conf 파일을 편집하여 이를 수행할 수 있습니다.

nano /etc/sysctl.conf

다음 줄을 변경합니다.

net.ipv4.ip_forward=1

완료되면 파일을 저장하고 닫습니다. 그런 다음 다음 명령을 실행하여 새 설정을 적용합니다.

sysctl -p

다음으로 다음 명령을 실행하여 OpenVPN 패키지를 설치합니다.

apt-get install openvpn -y

설치가 완료되면 다음 단계로 진행할 수 있습니다.

서버 인증서 및 키 생성

먼저 EasyRSA 디렉토리를 /etc/openvpn/에 복사해야 합니다. 다음 명령으로 수행할 수 있습니다.

cp -r /usr/share/easy-rsa /etc/openvpn/

다음으로 디렉터리를 easy-rsa로 변경하고 vars.example 파일의 이름을 바꿉니다.

cd /etc/openvpn/easy-rsa
mv vars.example vars

다음으로 vars 파일을 엽니다.

nano vars

다음 줄을 추가합니다.

export KEY_COUNTRY="INDIA"
export KEY_PROVINCE="CA"
export KEY_CITY="Junagadh"
export KEY_ORG="Howtoforge"
export KEY_EMAIL=""
export KEY_OU="OpenVPN"

완료되면 파일을 저장하고 닫습니다. 그런 다음 다음 명령을 사용하여 PKI를 초기화합니다.

./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: /etc/openvpn/easy-rsa/pki

다음으로 아래와 같이 암호 없이 CA를 빌드합니다.

./easyrsa build-ca nopass

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019
Generating RSA private key, 2048 bit long modulus (2 primes)
...................................+++++
..............+++++
e is 65537 (0x010001)
Can't load /etc/openvpn/easy-rsa/pki/.rnd into RNG
140449484268672:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:98:Filename=/etc/openvpn/easy-rsa/pki/.rnd
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:server

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easy-rsa/pki/ca.crt

다음으로 다음 명령을 사용하여 서버 키를 생성합니다.

./easyrsa gen-req server nopass

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019
Generating a RSA private key
...+++++
................................................................................................................+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/server.key.uQ7rqU8ryK'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [server]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/server.req
key: /etc/openvpn/easy-rsa/pki/private/server.key

그런 다음 다음 명령을 사용하여 서버 인증서에 서명합니다.

./easyrsa sign-req server server

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019


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 1080 days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'server'
Certificate is to be certified until Sep  5 15:43:29 2022 GMT (1080 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt

다음으로 다음 명령을 사용하여 Diffie-Hellman 키 교환을 빌드합니다.

./easyrsa gen-dh

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...................+.............................................+..........................................................................................................................................................................................................................................................+.......+................................................................................+................+....................................+..........................+........................................+............................................................................................+.......................................................+............................+......................................................................................................+...................................................................................+.................+............+.+............................+...............................................................................................................................................+............+...............................................+................................................................................................................................................................................+.....................................................................................................................+...................................................................................................................................................................................................+.............................................+..................................................................................................................................+......................................................................................................................................+....................................+..................................................................................................................................................................................+................................................................................................+..............................................................................................+............................................................................................................................................................................................+...........+.................+.....+..........................................................................................................+..........................................................+............+......................................+............................................................................................................................................................................................................................................................................................................+..................................+.................................................................................+.............................+.....................................................................................................................................................................................................................+..........................+.......................................................+......................+.................................+..............................................................+.............................................................................................................................................................+........................................................................+...............................+...............................................................................................................+..............................................+......................................................+.......................+......................................................................................................................................................................................................................+............................................................................................................................+..........................+......................................................................................................................................................................+..........................................................................................+..........................................................++*++*++*++*

DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem

다음으로 다음 명령을 사용하여 HMAC 서명을 생성합니다.

openvpn --genkey --secret ta.key

마지막으로 모든 인증서와 키를 /etc/openvpn 디렉터리에 복사합니다.

cp ta.key /etc/openvpn/
cp pki/ca.crt /etc/openvpn/
cp pki/private/server.key /etc/openvpn/
cp pki/issued/server.crt /etc/openvpn/
cp pki/dh.pem /etc/openvpn/

클라이언트 인증서 및 키 생성

다음으로 다음 명령을 사용하여 클라이언트 인증서를 생성합니다.

./easyrsa gen-req client nopass

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019
Generating a RSA private key
..........................................+++++
...............+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/client.key.wU45j6E0Dt'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [client]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/client.req
key: /etc/openvpn/easy-rsa/pki/private/client.key

그런 다음 다음 명령을 사용하여 클라이언트 인증서에 서명합니다.

./easyrsa sign-req client client

다음 출력이 표시되어야 합니다.

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

Using SSL: openssl OpenSSL 1.1.1c  28 May 2019


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 1080 days:

subject=
    commonName                = client


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'client'
Certificate is to be certified until Sep  5 12:28:25 2022 GMT (1080 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client.crt

다음으로 모든 클라이언트 인증서와 키를 /etc/openvpn/client/ 디렉터리에 복사합니다.

cp pki/ca.crt /etc/openvpn/client/
cp pki/issued/client.crt /etc/openvpn/client/
cp pki/private/client.key /etc/openvpn/client/

OpenVPN 서버 구성

이제 서버와 클라이언트에 필요한 모든 인증서와 키가 생성되었습니다. 다음으로 OpenVPN 구성 파일을 생성해야 합니다. 다음 명령으로 만들 수 있습니다.

nano /etc/openvpn/server.conf

다음 콘텐츠를 추가합니다.

port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
keepalive 10 120
tls-auth ta.key 0 # This file is secret
cipher AES-256-CBC
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
log         /var/log/openvpn/openvpn.log
log-append  /var/log/openvpn/openvpn.log
verb 3
explicit-exit-notify 1

파일을 저장하고 닫습니다. 그런 다음 다음 명령으로 OpenVPN 서비스를 시작합니다.

systemctl start 

다음으로 다음 명령을 사용하여 OpenVPN 서버를 확인합니다.

systemctl status 

산출:

?  - OpenVPN connection to server
   Loaded: loaded (/lib/systemd/system/; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-09-21 08:46:47 EDT; 6s ago
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO
 Main PID: 5040 (openvpn)
   Status: "Initialization Sequence Completed"
    Tasks: 1 (limit: 1138)
   Memory: 1.7M
   CGroup: /system.slice/system-openvpn.slice/
           ??5040 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --config /etc/openvpn/server.

Sep 21 08:46:47 debian systemd[1]: Starting OpenVPN connection to server...
Sep 21 08:46:47 debian systemd[1]: Started OpenVPN connection to server.

OpenVPN 클라이언트 설치 및 구성

다음으로 OpenVPN 클라이언트 시스템에 로그인하고 다음 명령을 사용하여 OpenVPN 패키지를 설치합니다.

apt-get install openvpn -y

설치가 완료되면 OpenVPN 클라이언트에 대한 새 구성 파일을 만듭니다.

nano /etc/openvpn/client.conf

아래와 같이 서버 IP 주소와 클라이언트 인증서 파일을 정의합니다.

client
dev tun
proto udp
remote 192.168.0.103 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC
verb 3

파일을 저장하고 닫습니다. 그런 다음 다음 명령을 사용하여 OpenVPN 서버에서 OpenVPN 클라이언트 시스템으로 모든 클라이언트 인증서 및 키 파일을 복사합니다.

scp :/etc/openvpn/ta.key /etc/openvpn/

다음으로 다음 명령을 사용하여 OpenVPN 클라이언트 서비스를 시작합니다.

systemctl start 

이제 다음 명령을 사용하여 OpenVPN 서버에서 할당한 새 IP 주소를 볼 수 있습니다.

ifconfig

다음 출력이 표시되어야 합니다.

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.102  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::a00:27ff:fe99:dc40  prefixlen 64  scopeid 0x20
        ether 08:00:27:99:dc:40  txqueuelen 1000  (Ethernet)
        RX packets 447  bytes 42864 (41.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 334  bytes 47502 (46.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 57  bytes 9754 (9.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 57  bytes 9754 (9.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
        inet 10.8.0.6  netmask 255.255.255.255  destination 10.8.0.5
        inet6 fe80::52b5:a1d2:fa23:f51e  prefixlen 64  scopeid 0x20
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 100  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9  bytes 472 (472.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

그런 다음 OpenVPN 서버 시스템으로 이동하여 다음 명령을 사용하여 OpenVPN 로그를 확인합니다.

tail -f /var/log/openvpn/openvpn.log

다음 출력이 표시되어야 합니다.

Sun Sep 22 19:46:08 2019 192.168.0.103:45700 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, 2048 bit RSA
Sun Sep 22 19:46:08 2019 192.168.0.103:45700 [_] Peer Connection Initiated with [AF_INET]192.168.0.103:45700
Sun Sep 22 19:46:08 2019 _/192.168.0.103:45700 MULTI_sva: pool returned IPv4=10.8.0.6, IPv6=(Not enabled)
Sun Sep 22 19:46:08 2019 _/192.168.0.103:45700 MULTI: Learn: 10.8.0.6 -> _/192.168.0.103:45700
Sun Sep 22 19:46:08 2019 _/192.168.0.103:45700 MULTI: primary virtual IP for _/192.168.0.103:45700: 10.8.0.6
Sun Sep 22 19:46:09 2019 _/192.168.0.103:45700 PUSH: Received control message: 'PUSH_REQUEST'
Sun Sep 22 19:46:09 2019 _/192.168.0.103:45700 SENT CONTROL [_]: 'PUSH_REPLY,redirect-gateway def1 bypass-dhcp,dhcp-option DNS 208.67.222.222,dhcp-option DNS 208.67.220.220,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM' (status=1)
Sun Sep 22 19:46:09 2019 _/192.168.0.103:45700 Data Channel: using negotiated cipher 'AES-256-GCM'
Sun Sep 22 19:46:09 2019 _/192.168.0.103:45700 Outgoing Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key
Sun Sep 22 19:46:09 2019 _/192.168.0.103:45700 Incoming Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key

축하합니다! Debian 10에 OpenVPN 서버와 클라이언트를 성공적으로 설치하고 구성했습니다.