Yongbok Blog

리눅스 OpenVPN 구축

VPN?
가상 사설망(Virtual private network)은 공중 네트워크를 통해 한 회사나 몇몇 단체가 내용을 바깥 사람에게 드러내지 않고 통신할 목적으로 쓰이는 사설 통신망을 뜻합니다. 보통 해외에서 몇몇 사이트 차단으로 접근 불가능한 곳을 뚫는데 사용하기도 합니다. 더 자세한건 위키피디아를 참고하세요.

http://ko.wikipedia.org/wiki/VPN

1. OpenVPN 설치
필자는 CentOS 5.4 이므로 yum 을 통해 설치를 진행했습니다.
yum 으로 설치하기 위해서는 rpmforge 저장소를 추가 해줘야 합니다.

[root@ruo91 ~]# wget http://mirror.yongbok.net/repoforge/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

[root@ruo91 ~]# rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm

OpenVPN 을 설치합니다.

[root@ruo91 ~]# yum -y install openvpn

2. OpenVPN 서버 키(key) 추가
설치가 완료되면 vars 파일 내용 중 맨 아래 부분을 사용자 환경에 맞게 편집을 해줍니다.

[root@ruo91 ~]# cd /usr/share/doc/openvpn-2.0.9/easy-rsa/2.0/
[root@ruo91 2.0]# vi vars
# 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=”KR”
export KEY_PROVINCE=”NA”
export KEY_CITY=”Seoul”
export KEY_ORG=”OpenVPN-ruo91″
export KEY_EMAIL=”ruo91@yongbok.net”

편집한 vars 의 환경변수를 적용 해줍니다.

[root@ruo91 2.0]# chmod +rwx *
[root@ruo91 2.0]# source ./vars
[root@ruo91 2.0]# ./clean-all

ca.crt, ca.key 를 생성을 해주면 현재 디렉토리에 keys 디렉토리가 생성 됩니다.
(그냥 엔터 눌러서 생성하면 됩니다.)

[root@ruo91 2.0]# ./build-ca
Generating a 1024 bit RSA private key
..++++++
……….++++++
writing new private key to ‘ca.key’
—–
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.
—–
Country Name (2 letter code) [KR]:
State or Province Name (full name) [NA]:
Locality Name (eg, city) [Seoul]:
Organization Name (eg, company) [OpenVPN-ruo91]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) [OpenVPN-ruo91 CA]:
Email Address [ruo91@yongbok.net]:

서버로 사용될 키를 생성 해줄때 마지막 물어보는 2가지 항목에서 y 를 눌러 동의 하고 넘어 갑니다.

[root@ruo91 2.0]# ./build-key-server server
Generating a 1024 bit RSA private key
……..++++++
…….++++++
writing new private key to ‘server.key’
—–
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.
—–
Country Name (2 letter code) [KR]:
State or Province Name (full name) [NA]:
Locality Name (eg, city) [Seoul]:
Organization Name (eg, company) [OpenVPN-ruo91]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) [server]:
Email Address [ruo91@yongbok.net]:

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /usr/share/doc/openvpn-2.0.9/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject’s Distinguished Name is as follows
countryName :PRINTABLE:’KR’
stateOrProvinceName :PRINTABLE:’NA’
localityName :PRINTABLE:’Seoul’
organizationName :PRINTABLE:’OpenVPN-ruo91′
commonName :PRINTABLE:’server’
emailAddress :IA5STRING:’ruo91@yongbok.net’
Certificate is to be certified until Nov 5 20:33:41 2019 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

3. OpenVPN 클라이언트 사용자 키 추가
VPN 을 사용할 클라이언트의 사용자 키를 만들어 줍니다. 이것 역시 서버키 생성과 마찬가지로 마지막 물어보는 2가지 항목에서 y 를 눌러 동의 하고 넘어 갑니다.

[root@ruo91 2.0]# ./build-key ruo91
Generating a 1024 bit RSA private key
………++++++
……………………………………………………………………….++++++
writing new private key to ‘ruo91.key’
—–
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.
—–
Country Name (2 letter code) [KR]:
State or Province Name (full name) [NA]:
Locality Name (eg, city) [Seoul]:
Organization Name (eg, company) [OpenVPN-ruo91]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) [ruo91]:
Email Address [ruo91@yongbok.net]:

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /usr/share/doc/openvpn-2.0.9/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject’s Distinguished Name is as follows
countryName :PRINTABLE:’KR’
stateOrProvinceName :PRINTABLE:’NA’
localityName :PRINTABLE:’Seoul’
organizationName :PRINTABLE:’OpenVPN-ruo91′
commonName :PRINTABLE:’ruo91′
emailAddress :IA5STRING:’ruo91@yongbok.net’
Certificate is to be certified until Nov 5 20:38:27 2019 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

이제 최종적으로 pem 파일을 만들어 줍니다.

[root@ruo91 2.0]# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
…………+……………….+……………………………………+…………………………..+………+…………………
…………………………………………………+………………………..+………………………………………….+…
…………………….+………………………………+…………………………………………………………………….
………………+…………………………………….+…..+……………………………………………………………….
…………+………………………+………………………………………………………………………………….+…..
………..+………………….+…………………………..+……………………………………..++*++*++*

위에서 생성한 키 파일들 확인 해봤습니다.

[root@ruo91 2.0]# ls -al ../keys/
합계 76
drwx—— 2 root root 4096 11월 8 2009 .
drwxr-xr-x 3 root root 4096 11월 8 2009 ..
-rw-r–r– 1 root root 3889 11월 8 2009 01.pem
-rw-r–r– 1 root root 3767 11월 8 2009 02.pem
-rw-r–r– 1 root root 1237 11월 8 2009 ca.crt
-rw——- 1 root root 887 11월 8 2009 ca.key
-rw-r–r– 1 root root 245 11월 8 2009 dh1024.pem
-rw-r–r– 1 root root 209 11월 8 2009 index.txt
-rw-r–r– 1 root root 20 11월 8 2009 index.txt.attr
-rw-r–r– 1 root root 21 11월 8 2009 index.txt.attr.old
-rw-r–r– 1 root root 105 11월 8 2009 index.txt.old
-rw-r–r– 1 root root 3767 11월 8 2009 ruo91.crt
-rw-r–r– 1 root root 668 11월 8 2009 ruo91.csr
-rw——- 1 root root 887 11월 8 2009 ruo91.key
-rw-r–r– 1 root root 3 11월 8 2009 serial
-rw-r–r– 1 root root 3 11월 8 2009 serial.old
-rw-r–r– 1 root root 3889 11월 8 2009 server.crt
-rw-r–r– 1 root root 668 11월 8 2009 server.csr
-rw——- 1 root root 887 11월 8 2009 server.key

4. OpenVPN 설정파일 추가
openvpn.conf 환결 설정 파일을 생성합니다.

[root@ruo91 2.0]# pwd
/usr/share/doc/openvpn-2.0.9/easy-rsa/2.0

[root@ruo91 2.0]# cd ../../
[root@ruo91 openvpn-2.0.9]# vi openvpn.conf

# vpn 서버로 사용할 포트를 지정 합니다.
port 1194
# TCP 프로토콜을 사용합니다.
proto tcp
dev tun
# 위에서 생성한 key 파일의 경로를 지정해줍니다.
ca easy-rsa/2.0/keys/ca.crt
cert easy-rsa/2.0/keys/server.crt
key easy-rsa/2.0/keys/server.key
dh easy-rsa/2.0/keys/dh1024.pem
# VPN 클라이언트가 사용할 아이피 대역을 지정해줍니다.
# 10.10.10.0 ~ 10.10.10.254 까지 DHCP로 할당 됩니다.
server 10.10.10.0 255.255.255.0
# 클라이언트에 아이피를 할당시 DNS 서버를 지정 해줍니다.
# VPN 자체의 DNS 서버가 있다면 더욱 좋습니다.
push “dhcp-option DNS 168.126.63.1”
push “dhcp-option DNS 168.126.63.2”
# 만약 VPN 클라이언트의 네트워크 환경이 10.10.10.0 대역의 아이피가 존재 한다면
# 자동으로 192.168.100.0 대역으로 변경해서 할당 받게 해줍니다.
push “route 192.168.100.0 255.255.255.0”
# 머무르는 시간입니다.
keepalive 10 120
comp-lzo
# 사용자/그룹을 nobody 로 VPN 서버를 실행합니다.
user nobody
group nobody
persist-key
persist-tun
# VPN 서버 로그를 활성화 합니다.
status openvpn-status.log
log openvpn.log
log-append openvpn.log
verb 3
client-to-client
duplicate-cn

환경 설정파일을 수정하기 쉽도록 /etc 디렉토리에 심볼릭 링크를 샤방하게 걸어줍니다.

[root@ruo91 ~]# ln -s /usr/share/doc/openvpn-2.0.9 /etc/openvpn

5. OpenVPN 서버 실행
OpenVPN 서버를 서비스에 등록 후 실행 합니다.

[root@ruo91 ~]# chkconfig –add openvpn
[root@ruo91 ~]# /etc/init.d/openvpn start
openvpn을 시작 중: [ OK ]

[root@ruo91 ~]# netstat -ant | grep 1194
tcp 0 0 0.0.0.0:1194 0.0.0.0:* LISTEN

6. OpenVPN 클라이언트 설정
윈도우용 GUI OpenVPN 클라이언트를 아래 사이트에서 다운로드 받고 설치를 합니다.

http://openvpn.se/download.html

설치시에 아래와 같은 윈도우 호환 테스트 메세지가 나오면 계속을 눌러 나머지 설치를 진행 합니다.

설치가 완료 되면 윈도우 트레이부분에 OpenVPN 클라이언트와 연결되지 않는 로컬영역 연결이 보입니다.

리눅스 서버에서 생성한 사용자 키 ca.crt, ruo91.crt, ruo91.key 3가지 파일을 안전하게 윈도우로 다운로드 하여 윈도우용 OpenVPN 이 설치된 경로인 C:\Program Files\OpenVPN\config 에 넣어 줍니다.
클라이언트 설정파일을 C:\Program Files\OpenVPN\sample-config 에 가서 client.ovpn 파일을 config 폴더로 복사 해옵니다.

복사된 client.ovpn 파일을 메모장이나 아래와 같이 윈도우 시계쪽 트레이 부분에 OpenVPN 클라이언트에 마우스 오른쪽 클릭하여 Edit Config 를 눌러서 클라이언트 설정을 편집을 해줍니다.


아래는 클라이언트 설정 파일인 client.ovpn 내용입니다. 사용자 환경에 맞게 수정 해주시면 됩니다.

# 클라이언트로 설정
client
#
dev tun
# TCP 프로토콜 사용
proto tcp
# VPN 서버의 도메인주소 또는 아이피주소 와 포트를 설정
# remote VPN서버 포트
remote yongbok.net 1194

# 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

# Try to preserve some state across restarts.
persist-key
persist-tun
# 사용자 키 파일 경로 지정
ca ca.crt
cert ruo91.crt
key ruo91.key
#
comp-lzo
#
# Set log file verbosity.
verb 3

윈도우 시계쪽 트레이 부분에 OpenVPN 클라이언트를 더블클릭 하게 되면 자동으로 VPN 서버로 접속이 됩니다.
서버 접속 상황을 보여주다가 연결이 성립되면 자동으로 사라집니다.

VPN 서버와 정상적으로 연결된 화면입니다.

연결된 상태 확인

VPN 서버와 클라이언트 간의 핑 테스트를 해봅니다.

참고
http://wiki.kldp.org/wiki.php/OpenVPN
http://www.howtoforge.com/openvpn-server-on-centos-5.2
http://openvpn.net/index.php/open-source/documentation/howto.html#install

Exit mobile version