VPN(Virtual Private Network) เป็นการขยาย network ภายในข้ามการใช้งานเครือข่ายภายนอก ให้เราสามารถส่งข้อมูลใดๆจาก public network ไปยังเครือข่ายภายในได้ โดยเครือข่าย VPN จะถูกทำเป็นท่อการเข้ารหัสระหว่างเรากับ VPN Server จากนั้นจะเป็นหน้าที่ของ VPN Server อีกทีว่าจะให้ traffic ดังกล่าวไปทางใด
โดยเราสามารถ setup VPN Server ใน Ubuntu 16.04 โดยทำดังนี้
1. ติดตั้ง Required Application
1 |
apt-get install openvpn easy-rsa |
2. สร้าง CA Directory
1 2 |
make-cadir ~/openvpn-ca cd openvpn-ca |
3. กำหนดค่าการสร้าง certificate ใน vars
1 2 3 4 5 6 |
export KEY_COUNTRY="..." export KEY_PROVINCE="..." export KEY_CITY="..." export KEY_ORG="..." export KEY_EMAIL="..." export KEY_OU="..." |
กำหนดชื่อของ CA ในการสร้าง
1 |
export KEY_NAME="server" |
4. สร้าง Certificate Authority
1 2 3 |
source vars ./clean-all ./build-ca |
5. สร้าง Server Certificate
1 |
./build-key-server server |
สร้าง Diffie-Hellman keys เพื่อใช้ขณะการแลกเปลี่ยน key
1 |
./build-dh |
สร้าง HMAC Signature เพื่อเพิ่มความแข็งแกร่งให้กับการตรวจสอบของ TLS
1 |
openvpn --genkey --secret keys/ta.key |
6. สร้าง Client Certificate และ key pair สำหรับใช้ authentication
1 2 3 4 5 |
cd ~/openvpn-ca source vars ./build-key client1 หากอยากให้มีการใช้ password ด้วยจะใช้เป็น ./build-key-pass client1 |
7. Copy Certificate และ key ต่างๆของ Server จาก ~/openvpn-ca/keys/ ไปไว้ที่ /etc/openvpn
1 |
cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn |
8. แตกไฟล์ sample openvpn configuration มาจาก /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz
1 |
gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf |
9. กำหนดใน server.conf ดังนี้
1 2 3 |
กำหนดใช้ interface เป็นแบบ tap dev tap ;dev tun |
1 2 3 |
กำหนด HMAC Authentication tls-auth ta.key 0 # This file is secret key-direction 0 |
1 2 |
กำหนด Cipher ที่ใช้ cipher AES-128-CBC |
1 2 |
กำหนด HMAC message digest algorithm auth SHA256 |
1 2 3 4 5 6 |
หากต้องการให้ redirect traffic ทั้งหมดออกมาทาง OpenVPN สามารถทำได้โดยกำหนด option push "redirect-gateway def1 bypass-dhcp" กำหนด DNS ให้กับ Client push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" |
10. กำหนด IP Forwarding และ disable IPv6 ใน sysctl.conf
1 2 |
net.ipv4.ip_forward=1 net.ipv6.conf.all.disable_ipv6 = 1 |
จากนั้นอ่านและใช้งานไฟล์ sysctl.conf
1 |
sudo sysctl -p |
11. Start VPN
1 |
systemctl restart openvpn@server |
12. ตรวจสอบสถานะของ VPN
1 |
systemctl status openvpn@server |
13. กำหนดให้ autostart openvpn server
1 |
systemctl enable openvpn@server |
14. สร้าง Configuration File ของ Client
1 2 3 |
mkdir -p ~/client-configs/files chmod 700 ~/client-configs/files ln -s ~/client-configs/ /etc/openvpn/ |
15. สร้าง Client Configuration จาก sample (/usr/share/doc/openvpn/examples/sample-config-files/client.conf)
1 |
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf |
16. กำหนดรายละเอียดภายใน Configuration
1 2 3 |
กำหนดใช้ interface เป็นแบบ tap dev tap ;dev tun |
กำหนด server ที่จะใช้ authentication
1 |
remote <Destination_Server> 1194 |
1 2 |
Protocol ที่ใช้ (ตามที่กำหนดใน server.conf) proto udp |
1 2 3 |
กำหนดการต่อเป็น low privilege user nobody group nogroup |
1 2 3 |
#ca ca.crt #cert client.crt #key client.key |
1 2 3 |
กำหนด Cipher และ Authentication cipher AES-128-CBC auth SHA256 |
กำหนด key-direction
1 |
key-direction 1 |
17. สร้าง script สำหรับการสร้าง client configuration สำหรับแต่ละ user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/bash # First argument: Client identifier KEY_DIR=~/openvpn-ca/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 |
18. Run script โดยใช้เป็น
1 2 3 4 |
chmod +x make_config.sh ./make_config.sh <ขื่อ user> เช่น ./make_config.sh client1 |
จะได้ไฟล์ configuration สำหรับการเชื่อมต่อโดยใช้ client1 เป็น
1 |
~/client-configs/files/client1.ovpn |
19. Copy Configuration client1.ovpn ไปให้กับ Client โดยเส้นทางใดๆ จากนั้นหากเป็น
Windows ให้ติดตั้ง OpenVPN จาก https://openvpn.net/index.php/open-source/downloads.html แล้วทำการนำไฟล์ client1.ovpn ไปไว้ที่
1 |
C:\Program Files\OpenVPN\config |
หากเป็น Linux จะใช้เป็น
1 2 3 |
Debian/Ubuntu sudo apt-get update sudo apt-get install openvpn |
1 2 3 |
CentOS/RHEL sudo yum install epel-release sudo yum install openvpn |
20. จากนั้นติดต่อไปยัง OpenVPN
Optional
1) ป้องกันการแชร์ cert ระหว่าง client ด้วยการ disconnect user ที่ login มาทีหลังในช่วงเวลาเดียวกัน โดยกำหนดใน Configuration แทนที่จะใช้
1 |
keepalive 10 120 |
กลายเป็น
1 2 3 4 |
ping 10 ping-restart 240 push "ping 10" push "ping-exit 60" |
2) หากต้องการกำหนด routing ให้กับ client เพิ่มเมื่อต่อ vpn ให้ใช้เป็น
1 |
push "route 10.10.10.0 255.255.255.0" |
3) ทำให้ OpenVPN Client ใดๆที่ต่อเข้ามาสามารถเข้าถึง internal network ได้
1 2 3 4 |
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -A FORWARD -i eth0 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT |
4) หากนำ Tutorial นี้ไปใช้ใน Docker จะติดปัญหา “ERROR: Cannot open TUN/TAP dev /dev/net/tun: No such file or directory (errno=2)” ให้รันเป็น
1 |
docker run -d -i -t -p 1194:1194/udp --privileged="true" <Name>:<Tag> /bin/bash |
5) หากต้องการ certificate ให้กับ client อื่นๆ ให้ทำซ้ำในข้อ 6 และ 18
Source:: DigitalOcean