差異處
這裏顯示兩個版本的差異處。
兩邊的前次修訂版 前次修改 下次修改 | 前次修改 | ||
tech:openvpn [2013/05/19 20:43] – [設定與啟動 Server 端] jonathan | tech:openvpn [2019/04/16 13:30] (目前版本) – jonathan_tsai | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== CentOS 6 安裝與設定 OpenVPN ====== | ||
+ | * OpenVPN 官方網站 : http:// | ||
+ | ===== Server 端 ===== | ||
+ | * CentOS 6.6 x86_64 | ||
+ | |||
+ | ==== 下載安裝最新版 OpenVPN 與相關 Lib ==== | ||
+ | <code sh> | ||
+ | su - root | ||
+ | rpm -ivh http:// | ||
+ | yum install kernel-devel openssl-devel gcc rpm-build | ||
+ | yum install lzo-devel pam-devel pkcs11-helper-devel openvpn easy-rsa | ||
+ | </ | ||
+ | ==== 設定虛擬網卡 tun0 與 NAT eth0 ==== | ||
+ | <code sh> | ||
+ | mknod / | ||
+ | modprobe tun | ||
+ | echo 1 > / | ||
+ | vi / | ||
+ | </ | ||
+ | : | ||
+ | # Controls IP packet forwarding | ||
+ | net.ipv4.ip_forward = 1 | ||
+ | : | ||
+ | </ | ||
+ | vi / | ||
+ | </ | ||
+ | *nat | ||
+ | -A POSTROUTING -o eth0 -j MASQUERADE | ||
+ | COMMIT | ||
+ | *filter | ||
+ | :INPUT ACCEPT [0:0] | ||
+ | :FORWARD ACCEPT [0:0] | ||
+ | -A FORWARD -i tun0 -j ACCEPT | ||
+ | -A FORWARD -o tun0 -j ACCEPT | ||
+ | :OUTPUT ACCEPT [0:0] | ||
+ | -A INPUT -m state --state ESTABLISHED, | ||
+ | -A INPUT -p icmp -j ACCEPT | ||
+ | -A INPUT -i lo -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT | ||
+ | -A INPUT -j REJECT --reject-with icmp-host-prohibited | ||
+ | -A FORWARD -j REJECT --reject-with icmp-host-prohibited | ||
+ | COMMIT | ||
+ | </ | ||
+ | service iptables restart | ||
+ | chkconfig iptables on | ||
+ | </ | ||
+ | |||
+ | ==== 建立主機相關憑證檔案 ==== | ||
+ | * 確認使用的 openssl 為 1.0.0< | ||
+ | [root@openvpn 2.0]# rpm -q openssl | ||
+ | openssl-1.0.0-20.el6_2.3.x86_64 | ||
+ | </ | ||
+ | * 建立 casrv 憑證管理者帳號與複製 easy-rsa 環境< | ||
+ | useradd casrv | ||
+ | passwd casrv | ||
+ | cp -a / | ||
+ | cd ~casrv/ | ||
+ | chown -R casrv:casrv easy-rsa/ | ||
+ | </ | ||
+ | * 建立 openssl.cnf 連結< | ||
+ | su - casrv | ||
+ | cd easy-rsa/ | ||
+ | ln -s openssl-1.0.0.cnf openssl.cnf | ||
+ | </ | ||
+ | * 編輯 vars 內容< | ||
+ | vi vars | ||
+ | </ | ||
+ | : | ||
+ | export KEY_COUNTRY=" | ||
+ | export KEY_PROVINCE=" | ||
+ | export KEY_CITY=" | ||
+ | export KEY_ORG=" | ||
+ | export KEY_EMAIL=" | ||
+ | export KEY_EMAIL=changeme | ||
+ | export KEY_CN=OpenVPN | ||
+ | export KEY_NAME=changeme | ||
+ | export KEY_OU=Tech | ||
+ | : | ||
+ | </ | ||
+ | * 產生 Root CA <code sh> | ||
+ | . ./vars | ||
+ | ./clean-all | ||
+ | ./build-ca | ||
+ | </ | ||
+ | [casrv@openvpn 2.0]% ./build-ca | ||
+ | Generating a 1024 bit RSA private key | ||
+ | : | ||
+ | : | ||
+ | Country Name (2 letter code) [US]:TW | ||
+ | State or Province Name (full name) [CA]:Taiwan | ||
+ | Locality Name (eg, city) [SanFrancisco]: | ||
+ | Organization Name (eg, company) [Fort-Funston]: | ||
+ | Organizational Unit Name (eg, section) [changeme]: | ||
+ | Common Name (eg, your name or your server' | ||
+ | Name [changeme]: | ||
+ | Email Address [[email protected]]: | ||
+ | |||
+ | </ | ||
+ | * 產生 Server CA <code sh> | ||
+ | ./ | ||
+ | </ | ||
+ | [casrv@openvpn 2.0]% ./ | ||
+ | Generating a 1024 bit RSA private key | ||
+ | : | ||
+ | : | ||
+ | Country Name (2 letter code) [US]:TW | ||
+ | State or Province Name (full name) [CA]:Taiwan | ||
+ | Locality Name (eg, city) [SanFrancisco]: | ||
+ | Organization Name (eg, company) [Fort-Funston]: | ||
+ | Organizational Unit Name (eg, section) [changeme]: | ||
+ | Common Name (eg, your name or your server' | ||
+ | Name [changeme]: | ||
+ | Email Address [[email protected]]: | ||
+ | : | ||
+ | A challenge password []: | ||
+ | An optional company name []: | ||
+ | : | ||
+ | Certificate is to be certified until Apr 4 06:21:30 2022 GMT (3650 days) | ||
+ | Sign the certificate? | ||
+ | : | ||
+ | 1 out of 1 certificate requests certified, commit? [y/n]y | ||
+ | Write out database with 1 new entries | ||
+ | Data Base Updated | ||
+ | </ | ||
+ | * 產生 Diffie Hellman 參數 <code sh> | ||
+ | ./build-dh | ||
+ | </ | ||
+ | [casrv@openvpn 2.0]% ./build-dh | ||
+ | Generating DH parameters, 1024 bit long safe prime, generator 2 | ||
+ | : | ||
+ | : | ||
+ | ..++*++*++* | ||
+ | </ | ||
+ | * 產生 TLS-Auth Key <code sh> | ||
+ | openvpn --genkey --secret keys/ta.key | ||
+ | </ | ||
+ | * 所有產生的 key file 都會存放在 < | ||
+ | ~casrv/ | ||
+ | </ | ||
+ | |||
+ | ==== 建立用戶憑證檔案 ==== | ||
+ | * Client CA <code sh> | ||
+ | su - casrv | ||
+ | cd easy-rsa/ | ||
+ | source ./vars | ||
+ | ./build-key client1 | ||
+ | : | ||
+ | : | ||
+ | ./build-key clientn | ||
+ | </ | ||
+ | [casrv@openvpn 2.0]% ./build-key client1 | ||
+ | Generating a 1024 bit RSA private key | ||
+ | : | ||
+ | writing new private key to ' | ||
+ | ----- | ||
+ | : | ||
+ | Country Name (2 letter code) [TW]: | ||
+ | State or Province Name (full name) [Taiwan]: | ||
+ | Locality Name (eg, city) [Taipei]: | ||
+ | Organization Name (eg, company) [Trysoft Corp.]: | ||
+ | Organizational Unit Name (eg, section) [Tech]: | ||
+ | Common Name (eg, your name or your server' | ||
+ | Name [changeme]: | ||
+ | Email Address [changeme]: | ||
+ | : | ||
+ | A challenge password []: | ||
+ | An optional company name []: | ||
+ | : | ||
+ | Certificate is to be certified until Apr 4 06:36:36 2022 GMT (3650 days) | ||
+ | Sign the certificate? | ||
+ | : | ||
+ | 1 out of 1 certificate requests certified, commit? [y/n]y | ||
+ | Write out database with 1 new entries | ||
+ | Data Base Updated | ||
+ | |||
+ | </ | ||
+ | * 所有產生的 key file 都會存放在 < | ||
+ | ~casrv/ | ||
+ | </ | ||
+ | * 已經產生 key 的清單可參考 index.txt< | ||
+ | V | ||
+ | V | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | ==== 廢止用戶憑證檔案 ==== | ||
+ | * 依照上一個程序先建立一個 client0 測試憑證然後再廢除 | ||
+ | * 廢除憑證的處理方式< | ||
+ | su - casrv | ||
+ | cd easy-rsa/ | ||
+ | source ./vars | ||
+ | ./ | ||
+ | </ | ||
+ | [casrv@openvpn CA]$ ./ | ||
+ | Using configuration from / | ||
+ | Revoking Certificate 03. | ||
+ | Data Base Updated | ||
+ | Using configuration from / | ||
+ | client0.crt: | ||
+ | error 23 at 0 depth lookup: | ||
+ | </ | ||
+ | * 每次處理廢止憑證後, | ||
+ | su - root | ||
+ | cp ~casrv/ | ||
+ | </ | ||
+ | su -root | ||
+ | cd / | ||
+ | ln / | ||
+ | </ | ||
+ | |||
+ | <note information> | ||
+ | * 如果啟動檢查 CRL, 在 OpenVPN 更新至 2.4 之後, 會發現用戶端可能就無法連線.. 在 Server Log 會看到訊息< | ||
+ | Fri Apr 21 08:08:18 2017 60.248.245.177: | ||
+ | Fri Apr 21 08:08:18 2017 60.248.245.177: | ||
+ | </ | ||
+ | </ | ||
+ | * 可透過以下語法重新建立 crl.pem <code sh> | ||
+ | su - casrv | ||
+ | cd easy-rsa/ | ||
+ | source ./vars | ||
+ | openssl ca -gencrl -keyfile keys/ca.key -cert keys/ | ||
+ | </ | ||
+ | * 所產生出來的 CRL 內容大致如下 <code sh> | ||
+ | openssl crl -in crl.pem -text | ||
+ | </ | ||
+ | Certificate Revocation List (CRL): | ||
+ | Version 1 (0x0) | ||
+ | Signature Algorithm: md5WithRSAEncryption | ||
+ | Issuer: / | ||
+ | Last Update: Apr 21 02:16:30 2017 GMT | ||
+ | Next Update: May 21 02:16:30 2017 GMT | ||
+ | Revoked Certificates: | ||
+ | Serial Number: 05 | ||
+ | Revocation Date: Jun 25 05:06:21 2012 GMT | ||
+ | : | ||
+ | Serial Number: 0A | ||
+ | Revocation Date: Dec 31 02:24:45 2015 GMT | ||
+ | Signature Algorithm: md5WithRSAEncryption | ||
+ | | ||
+ | : | ||
+ | | ||
+ | df:81 | ||
+ | -----BEGIN X509 CRL----- | ||
+ | MIIB3jCCAUcwDQYJKoZIhvcNAQEEBQAwgbExCzAJBgNVBAYTAlRXMQ8wDQYDVQQI | ||
+ | EwZUYWl3YW4xDzANBgNVBAcTBlRhaXBlaTEmMCQGA1UEChMdRXZlcnBsYXN0IE1h | ||
+ | : | ||
+ | vgzp3y49jtoXHn2YqioMaciGrOzCYxCrLcVWc/ | ||
+ | 34E= | ||
+ | -----END X509 CRL----- | ||
+ | </ | ||
+ | * 所以應該要加入 crontab 讓系統至少每個月能自動產生一份最新版的 crl.pem | ||
+ | |||
+ | ==== 設定與啟動 Server 端 ==== | ||
+ | * 安裝的 OpenVPN 版本為 2.3.6< | ||
+ | [root@openvpn openvpn]# rpm -q openvpn | ||
+ | openvpn-2.3.6-1.el6.x86_64 | ||
+ | </ | ||
+ | * 規劃好 Listen TCP/443, 分配給 Client 的 IP 為 192.168.221.101 ~ 150 | ||
+ | * 設定相關參數檔< | ||
+ | cd / | ||
+ | cp / | ||
+ | vi server.conf | ||
+ | </ | ||
+ | dev tun | ||
+ | proto tcp | ||
+ | port 443 | ||
+ | ca ca.crt | ||
+ | cert server.crt | ||
+ | key server.key | ||
+ | #crl-verify crl.pem | ||
+ | dh dh2048.pem | ||
+ | server 192.168.221.0 255.255.255.0 | ||
+ | ifconfig-pool-persist ipp.txt | ||
+ | persist-key | ||
+ | persist-tun | ||
+ | status openvpn-status.log | ||
+ | verb 3 | ||
+ | client-to-client | ||
+ | #push " | ||
+ | #push "route 192.168.11.0 255.255.255.0" | ||
+ | keepalive 10 120 | ||
+ | tls-auth ta.key 0 | ||
+ | cipher AES-128-CBC | ||
+ | comp-lzo | ||
+ | </ | ||
+ | cd / | ||
+ | cp ~casrv/ | ||
+ | cp ~casrv/ | ||
+ | cp ~casrv/ | ||
+ | cp ~casrv/ | ||
+ | cp ~casrv/ | ||
+ | service openvpn start | ||
+ | chkconfig openvpn on | ||
+ | </ | ||
+ | |||
+ | ===== 設定與啟動用戶端 ===== | ||
+ | ==== 安裝用戶端軟體 ==== | ||
+ | * 下載 http:// | ||
+ | * openvpn裝完後在電腦網路連線裡會自動新增一個設備是Tap-Win32 Adapter V9的區域連線 | ||
+ | |||
+ | ==== 用戶端憑證與設定檔 ==== | ||
+ | * 以下以 client1 為例 | ||
+ | * 在 OpenVPN 參數目錄 C:\Program Files\OpenVPN\config 內建立一個子目錄 ideas_tp | ||
+ | * 取得 CA Server 所產生的 ca.crt / client1.key / client1.crt / ta.key 放入 C:\Program Files\OpenVPN\config\ideas_tp | ||
+ | * 編輯 ideas_tp.ovpn < | ||
+ | # Specify that this is a client | ||
+ | client | ||
+ | |||
+ | # Bridge device setting | ||
+ | dev tun | ||
+ | proto tcp | ||
+ | |||
+ | # Host name and port for the server (default port is 1194) | ||
+ | # note: replace with the correct values your server set up | ||
+ | remote 175.98.155.2 443 # openvpn Server IP | ||
+ | remote-cert-tls server | ||
+ | |||
+ | # Client does not need to bind to a specific local port | ||
+ | nobind | ||
+ | |||
+ | # Keep trying to resolve the host name of OpenVPN server. | ||
+ | resolv-retry infinite | ||
+ | |||
+ | # Preserve state across restarts | ||
+ | persist-key | ||
+ | persist-tun | ||
+ | |||
+ | # SSL/TLS parameters - files created previously | ||
+ | ca ca.crt | ||
+ | cert client1.crt | ||
+ | key client1.key | ||
+ | |||
+ | # Since we specified the tls-auth for server, we need it for the client | ||
+ | # note: 0 = server, 1 = client | ||
+ | tls-auth ta.key 1 | ||
+ | |||
+ | # Specify same cipher as server | ||
+ | cipher AES-128-CBC | ||
+ | |||
+ | # Use compression | ||
+ | comp-lzo | ||
+ | |||
+ | # Log verbosity (to help if there are problems) | ||
+ | verb 3 | ||
+ | |||
+ | </ | ||
+ | |||
+ | < | ||
+ | **如果要同時連上多個 OpenVPN Server, 那就要建立多個 Tap-Win32 Adapter V9的區域連線設備** | ||
+ | - 在 Win7 以上需要使用 Administrator 的權限開啟命令提示字元(DOS 畫面) | ||
+ | - 每執行以下語法一次就會增加一個 TAP 虛擬網卡< | ||
+ | " | ||
+ | </ | ||
+ | 執行過程, | ||
+ | </ | ||
+ | |||
+ | * 也可以將憑證檔案內容直接放入設定檔內.. Exp.ideas_tp.ovpn < | ||
+ | # Specify that this is a client | ||
+ | client | ||
+ | |||
+ | # Bridge device setting | ||
+ | dev tun | ||
+ | proto tcp | ||
+ | |||
+ | # Host name and port for the server (default port is 1194) | ||
+ | # note: replace with the correct values your server set up | ||
+ | remote 175.98.155.2 443 # openvpn Server IP | ||
+ | remote-cert-tls server | ||
+ | |||
+ | # Client does not need to bind to a specific local port | ||
+ | nobind | ||
+ | |||
+ | # Keep trying to resolve the host name of OpenVPN server. | ||
+ | resolv-retry infinite | ||
+ | |||
+ | # Preserve state across restarts | ||
+ | persist-key | ||
+ | persist-tun | ||
+ | |||
+ | # Specify same cipher as server | ||
+ | cipher AES-128-CBC | ||
+ | |||
+ | # Use compression | ||
+ | comp-lzo | ||
+ | |||
+ | # Log verbosity (to help if there are problems) | ||
+ | verb 3 | ||
+ | |||
+ | key-direction 1 | ||
+ | # ca ca.crt | ||
+ | <ca> | ||
+ | -----BEGIN CERTIFICATE----- | ||
+ | ... | ||
+ | -----END CERTIFICATE----- | ||
+ | </ca> | ||
+ | #cert client1.crt | ||
+ | < | ||
+ | -----BEGIN CERTIFICATE----- | ||
+ | ... | ||
+ | -----END CERTIFICATE----- | ||
+ | </ | ||
+ | #key client1.key | ||
+ | <key> | ||
+ | -----BEGIN RSA PRIVATE KEY----- | ||
+ | ... | ||
+ | -----END RSA PRIVATE KEY----- | ||
+ | </ | ||
+ | #tls-auth ta.key 1 | ||
+ | < | ||
+ | -----BEGIN OpenVPN Static key V1----- | ||
+ | ... | ||
+ | -----END OpenVPN Static key V1----- | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== 用戶端開機自動連上 OpenVPN ==== | ||
+ | * 在 Windows 的「設定」-> | ||
+ | * 服務啟動後會自動掃描在 C:\Program Files\OpenVPN\config 目錄內的 *.ovpn 設定檔, 但不會掃描子目錄內的 *.ovpn, 因此如果之前透過子目錄來區隔多組 VPN 設定檔要將 *.ovpn 複製出來, | ||
+ | : | ||
+ | # SSL/TLS parameters - files created previously | ||
+ | ca ideas_tp/ | ||
+ | cert ideas_tp/ | ||
+ | key ideas_tp/ | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | ===== 參考網址 ===== | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | * 另外方案 **[[tech/ | ||
+ | |||
+ | {{tag> |