{"id":780,"date":"2017-10-18T16:55:26","date_gmt":"2017-10-18T08:55:26","guid":{"rendered":"http:\/\/www.xiaobo.li\/?p=780"},"modified":"2017-10-18T18:46:18","modified_gmt":"2017-10-18T10:46:18","slug":"centos-6-x-openvpn","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/780","title":{"rendered":"CentOS 6.x openvpn"},"content":{"rendered":"<p>OpenVPN \u4e0b\u8f7d\u5730\u5740\uff1a http:\/\/openvpn.net<\/p>\n<p>\u67e5\u770b\u7cfb\u7edf\u7248\u672c<br \/>\ncat \/etc\/redhat-release<br \/>\nCentOS release 6.5 (Final)<\/p>\n<p><strong>\u67e5\u770b\u5185\u6838\u548ccpu\u67b6\u6784<\/strong><br \/>\nuname -rm<br \/>\n2.6.32-431.el6.x86_64 x86_64<\/p>\n<p><strong>\u67e5\u770bip<\/strong><br \/>\nifconfig<br \/>\neth0 Link encap:Ethernet HWaddr 08:00:27:5E:DF:74<br \/>\ninet addr:xxx.xxx.xxx.xxx Bcast:xxx.xxx.xxx.255 Mask:255.255.255.0<br \/>\ninet6 addr: fe80::a00:27ff:fe5e:df74\/64 Scope:Link<br \/>\nUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1<br \/>\nRX packets:457 errors:0 dropped:0 overruns:0 frame:0<br \/>\nTX packets:55 errors:0 dropped:0 overruns:0 carrier:0<br \/>\ncollisions:0 txqueuelen:1000<br \/>\nRX bytes:37261 (36.3 KiB) TX bytes:6438 (6.2 KiB)<\/p>\n<p>lo Link encap:Local Loopback<br \/>\ninet addr:127.0.0.1 Mask:255.0.0.0<br \/>\ninet6 addr: ::1\/128 Scope:Host<br \/>\nUP LOOPBACK RUNNING MTU:16436 Metric:1<br \/>\nRX packets:0 errors:0 dropped:0 overruns:0 frame:0<br \/>\nTX packets:0 errors:0 dropped:0 overruns:0 carrier:0<br \/>\ncollisions:0 txqueuelen:0<br \/>\nRX bytes:0 (0.0 b) TX bytes:0 (0.0 b)<br \/>\n#\u4f7f\u7528\u5355\u7f51\u5361<\/p>\n<p><strong>\u6dfb\u52a0epel\u6e90<\/strong><br \/>\nrpm -ivh http:\/\/dl.fedoraproject.org\/pub\/epel\/5\/i386\/epel-release-5-4.noarch.rpm<\/p>\n<p>\u5b89\u88c5openvpn\u548ceasy-rsa<br \/>\nyum -y install openvpn easy-rsa<\/p>\n<p><strong>\u62f7\u8d1d\u6587\u4ef6<\/strong><br \/>\ncp \/usr\/share\/doc\/openvpn-2.3.10\/sample\/sample-config-files\/server.conf \/etc\/openvpn<br \/>\ncp -r \/usr\/share\/easy-rsa\/2.0\/* \/etc\/openvpn\/<\/p>\n<p><strong>\u8fdb\u5165openvpn\u76ee\u5f55<\/strong><br \/>\ncd \/etc\/openvpn\/<\/p>\n<p><strong>\u914d\u7f6ePKI<\/strong><br \/>\nvi vars<\/p>\n<p><strong>\u7f16\u8f91\u4e0b\u9762\u5185\u5bb9\uff0c\u81ea\u5b9a\u4e49\u3002<\/strong><br \/>\nexport KEY_COUNTRY=\"CN\"<br \/>\nexport KEY_PROVINCE=\"guangdong\"<br \/>\nexport KEY_CITY=\"guangzhou\"<br \/>\nexport KEY_ORG=\"xxx\"<br \/>\nexport KEY_EMAIL=\"xxx@qq.com\"<br \/>\nexport KEY_OU=\"xxx\"<\/p>\n<p><strong>\u521d\u59cb\u5316vars<\/strong><br \/>\nsource vars<\/p>\n<p><strong>\u6e05\u7406\u539f\u6709\u8bc1\u4e66<\/strong><br \/>\n.\/clean-all<\/p>\n<p><strong>\u751f\u6210ca\u8bc1\u4e66<\/strong><br \/>\n.\/build-ca<br \/>\n#\u4e00\u8def\u6309\u56de\u8f66\u952e<\/p>\n<p><strong>\u751f\u6210\u670d\u52a1\u5668\u5bc6\u94a5\u8bc1\u4e66<\/strong><br \/>\n.\/build-key-server server<br \/>\n#\u4e00\u8def\u6309\u56de\u8f66\u952e\uff0c\u4e24\u6b21\u6309y<\/p>\n<p><strong>\u751f\u6210DH\u9a8c\u8bc1\u6587\u4ef6<\/strong><br \/>\n.\/build-dh<\/p>\n<p><strong>\u751f\u6210\u5ba2\u6237\u7aef\u5bc6\u94a5\u8bc1\u4e66<\/strong><br \/>\n.\/build-key xxx<br \/>\n#\u4e00\u8def\u6309\u56de\u8f66\u952e\uff0c\u4e24\u6b21\u6309y<\/p>\n<p><strong>\u751f\u6210ta.key\u6587\u4ef6<\/strong><br \/>\n<code>openvpn --genkey --secret \/etc\/openvpn\/keys\/ta.key<\/code><\/p>\n<p>\u7f16\u8f91\u670d\u52a1\u914d\u7f6e\u6587\u4ef6server.conf<br \/>\nvi \/etc\/openvpn\/server.conf<\/p>\n<p><strong>\u4e3b\u8981\u4fee\u6539\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/strong><br \/>\nport 1194<br \/>\nproto udp<br \/>\ndev tun<br \/>\nca keys\/ca.crt<br \/>\ncert keys\/server.crt<br \/>\nkey keys\/server.key<br \/>\ndh keys\/dh2048.pem<br \/>\nserver 10.8.0.0 255.255.255.0<br \/>\npush \"dhcp-option DNS 8.8.8.8\"<br \/>\npush \"dhcp-option DNS 8.8.4.4\"<br \/>\nlog-append \/var\/log\/openvpn\/openvpn.log<br \/>\n#\u6211\u91c7\u7528\u7684\u662f\u76f8\u5bf9\u8def\u5f84<\/p>\n<p><strong>\u65b0\u5efa\u65e5\u5fd7\u76ee\u5f55<\/strong><br \/>\nmkdir -p \/var\/log\/openvpn<\/p>\n<p><strong>\u542f\u52a8openvpn\u670d\u52a1<\/strong><br \/>\nservice openvpn start<\/p>\n<p>\u65b0\u624b\u4e00\u822c\u90fd\u4f1a\u5728\u8fd9\u4e00\u6b65\u542f\u52a8\u4e0d\u4e86\uff0c\u4f46\u662f\u91cd\u8981\u7684\u662f\u8981\u5b66\u4f1a\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6392\u9519\u3002<br \/>\ncat \/var\/log\/openvpn\/openvpn.log<\/p>\n<p><strong>\u6dfb\u52a0openvpn\u5230\u540e\u53f0\u542f\u52a8<\/strong><br \/>\nchkconfig openvpn on<\/p>\n<p>\u5f00\u542f\u8def\u7531\u8f6c\u53d1\u529f\u80fd<br \/>\nsed -i '\/net.ipv4.ip_forward\/s\/0\/1\/' \/etc\/sysctl.conf<br \/>\nsysctl -p<\/p>\n<p><strong>\u914d\u7f6e\u9632\u706b\u5899\u8ba9openvpn\u7aef\u53e3\u901a\u8fc7<\/strong><br \/>\niptables -A INPUT -p UDP --dport 1194 -j ACCEPT<br \/>\niptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT<br \/>\niptables -t nat -A POSTROUTING -o eth0 -s 10.8.0.0\/24 -j MASQUERADE<br \/>\nservice iptables save<\/p>\n<p><strong>\u5ba2\u6237\u7aef\u6587\u4ef6\u914d\u7f6e<\/strong><br \/>\ncp \/usr\/share\/doc\/openvpn-2.3.10\/sample\/sample-config-files\/client.conf \/etc\/openvpn\/keys\/client.ovpn<br \/>\nvi \/etc\/openvpn\/keys\/client.ovpn<\/p>\n<p><strong>\u4e3b\u8981\u4fee\u6539\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/strong><br \/>\ndev tun<br \/>\nproto udp<br \/>\nremote xxx.xxx.xxx.xxx 1194 #ip\u4e3a\u670d\u52a1\u5668\u7f51\u5361\u7684ip<br \/>\nca ca.crt<br \/>\ncert xxx.crt<br \/>\nkey xxx.key<br \/>\n#\u4ee5\u4e0a\u8bc1\u4e66\u8def\u5f84\u5747\u4e3a\u76f8\u5bf9\u8def\u5f84<\/p>\n<p>\u5c06\u5ba2\u6237\u7aef\u6240\u9700\u7684\u4e94\u4e2a\u6587\u4ef6\u4e0b\u8f7d\u5230\u672c\u5730<br \/>\nsz \/etc\/openvpn\/keys\/{ca.crt,xxx.crt,xxx.key,ta.key,client.ovpn}<\/p>\n<p><strong>\u4e0b\u8f7dwindows\u5ba2\u6237\u7aef\u5e76\u5b89\u88c5<\/strong><br \/>\nhttps:\/\/openvpn.net\/index.php\/open-source\/downloads.html<\/p>\n<p>\u5c06\u4e4b\u524d\u4e0b\u8f7d\u7684\u4e94\u4e2a\u6587\u4ef6\u62f7\u8d1d\u5230C:\/Program Files\/OpenVPN\/config\u76ee\u5f55\u4e0b\uff0c\u53cc\u51fb\u684c\u9762openvpn\u56fe\u6807\u5c31ok\u4e86\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u65b0\u7528\u6237<\/strong><\/p>\n<p>\u4ee5\u4e0a\u4ec5\u751f\u6210\u4e86\u4e00\u4e2a xxx \u7528\u6237\uff0c \u4ec5\u4e00\u7528\u6237\u53ef\u7528\uff0c\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6dfb\u52a0\u5176\u5b83\u7528\u6237\u3002<\/p>\n<p>1: cd \/etc\/openvpn\/easy-rsa\/2.0<br \/>\n2: source vars<\/p>\n<p><em>PS\uff1a\u5982\u679c\u4e0d\u6267\u884c\u8fd9\u6b65\uff0c\u6267\u884c\u7b2c\u4e09\u6b65.\/build-key \u4f1a\u5bfc\u81f4\u5982\u4e0b\u95ee\u9898\uff1a<\/em><\/p>\n<p>Please edit the vars script to reflect your configuration,<br \/>\nthen source it with \"source .\/vars\".<br \/>\nNext, to start with a fresh PKI configuration and to delete any<br \/>\nprevious certificates and keys, run \".\/clean-all\".<br \/>\nFinally, you can run this tool (pkitool) to build certificates\/keys.<\/p>\n<p>3: .\/build-key client2<br \/>\n\u6267\u884c\u5b8c\u8fd9\u6b65\u540e\uff0c\u4f1a\u5728keys\u76ee\u5f55\u4e0b\u751f\u6210client2.crt client2.key\u4e24\u4e2a\u6587\u4ef6\u3002<\/p>\n<p>\u540c\u7b2c\u4e00\u4e2a\u7528\u6237\u4e00\u6837\uff0c\u5c06\u6b64\u4e24\u4e2a\u6587\u4ef6\u4e0eca.crt,ta.key\u6587\u4ef6\u4e00\u540c\u62f7\u8d1d\u5230\u4f7f\u7528\u8005\u8bbe\u5907\u4e0a\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<p>4: \u521b\u5efa\u5bf9\u5e94\u7684 client2.ovpn<br \/>\n\u53ef\u76f4\u63a5\u62f7\u8d1d\u7b2c\u4e00\u4e2a\u751f\u6210\u7684ovpn\u6216\u4ece\u793a\u4f8b\u4e2d\u590d\u5236\uff0c \u66ff\u6362\u5176\u4e2d\u7684 client2.crt \u4e0e client2.key \u5373\u53ef\u3002<\/p>\n<p><strong>\u6ce8\u9500\u4e00\u4e2a\u7528\u6237<\/strong><\/p>\n<p>\u4f8b\u5982\u4e00\u4f4d\u540c\u4e8b\u79bb\u804c\u7b49\u60c5\u51b5\u4e0b\u9700\u8981\u7981\u7528\u6216\u5220\u9664\u4e00\u4e2a\u7528\u6237\u3002<tt>\u4f7f\u7528revoke-full\u547d\u4ee4\u6765\u6ce8\u9500\u5176\u8bc1\u4e66\u3002\u82e5\u670d\u52a1\u5668\u4e0a\u65e0\u7528\u6237\u8bc1\u4e66\u9700\u8981\u5c06\u8be5\u540c<\/tt><tt>\u4e8b<\/tt><tt>\u8bc1\u4e66\u6587\u4ef6\u653e\u5230\u5bf9\u5e94\u7684keys\u4e0b\u3002<\/tt><br \/>\n\u7b2c1,2\u6b65\u540c\u6dfb\u52a0\u7528\u6237<\/p>\n<p>1: cd \/etc\/openvpn\/easy-rsa\/2.0<br \/>\n2: source vars<br \/>\n3: <tt>revoke-full client1<\/tt><br \/>\n\u6ce8\u9500common name\u4e3aclient1\u7684\u7528\u6237<tt><\/tt><\/p>\n<p><tt><em><br \/>\n<tt>revoke-full client2<\/tt> <\/em><\/tt><br \/>\n<em>\u6ce8\u9500common name\u4e3aclient2\u7684\u7528\u6237<tt><br \/>\n<tt>revoke-full xxxx<\/tt> <\/tt><\/em><br \/>\n<em>\u6ce8\u9500common name\u4e3axxxx\u7684\u7528\u6237<\/em><\/p>\n<p>4: \u5c06\u751f\u6210\u7684<tt>crl.pem\u6587\u4ef6\u653e\u5230\u76f8\u5e94\u7684\u914d\u7f6e\u76ee\u5f55config\u4e0b\uff0c\u7136\u540e\u5728\u914d\u7f6e\u6587\u4ef6\u52a0\u5165\u5982\u4e0b\u53c2\u6570\uff0c\u91cd\u542f\u6216\u91cd\u65b0\u8f7d\u5165openvpn\u670d\u52a1\u5668<\/tt><tt><\/tt><\/p>\n<p>crl-verify keys\/crl.pem<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenVPN \u4e0b\u8f7d\u5730\u5740\uff1a http:\/\/openvpn.net \u67e5\u770b\u7cfb\u7edf\u7248\u672c  &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/780\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-780","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=780"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/780\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}