OpenVPN a Ubuntu
Contingut
Referències
La guia que hem seguit per instal.lar OpenVPN en un Ubuntu Server 12.04 LTS, i en el client:
D'aquest enllaç pots descarregar-te les claus i certificats que s'han d'instal.lar en el client, per tal de què et puguis connectar al servidor VPN:
Instal.lació i configuració del servidor
Treballem com a root en el servidor
$ sudo su # sudo apt-get install openvpn
Configuració de la infraestructura de clau pública
Seguim les instruccions de l'enllaç.
#mkdir /etc/openvpn/easy-rsa/ #cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0/* /etc/openvpn/easy-rsa/ export KEY_COUNTRY="CA" export KEY_PROVINCE="BC" export KEY_CITY="Barcelona" export KEY_ORG="Joan Quintana" export KEY_EMAIL="joanqc@gmail.com
Petit problema:
# cd /etc/openvpn/easy-rsa/ # source vars ************************************************************** No /etc/openvpn/easy-rsa/openssl.cnf file could be found Further invocations will fail **************************************************************
Mirem per resoldre el problema a:
You will now need to remove all instances of '[[:alnum:]]' from the file. This will allow it to properly detect the version of OpenSSL on your computer. if [ "$OPENSSL" ]; then if $OPENSSL version | grep -E "0\.9\.6" > /dev/null; then cnf="$1/openssl-0.9.6.cnf" elif $OPENSSL version | grep -E "0\.9\.8" > /dev/null; then cnf="$1/openssl-0.9.8.cnf" elif $OPENSSL version | grep -E "1\.0\.([[:digit:]])" > /dev/null; then cnf="$1/openssl-1.0.0.cnf" else cnf="$1/openssl.cnf" fi fi
i ja podem continuar:
# ./clean-all # ./build-ca Generating a 1024 bit RSA private key ...........................................++++++ ........................++++++ writing new private key to 'ca.key' # ./build-dh Generating DH parameters, 1024 bit long safe prime, generator 2 This is going to take a long time .................... # ./build-key-server myservername # ./build-dh # cd keys/ # cp myservername.crt myservername.key ca.crt dh1024.pem /etc/openvpn/
Certificats del client
# cd /etc/openvpn/easy-rsa/ # source vars # ./build-key client1 Generating a 1024 bit RSA private key ..........................................................++++++ ..........................++++++ writing new private key to 'client1.key' # ls -la -rw-r--r-- 1 root root 1375 gen 30 15:06 /etc/openvpn/ca.crt # ls -la /etc/openvpn/easy-rsa/keys/client1.crt -rw-r--r-- 1 root root 3985 gen 30 15:08 /etc/openvpn/easy-rsa/keys/client1.crt # ls -la /etc/openvpn/easy-rsa/keys/client1.key -rw------- 1 root root 912 gen 30 15:08 /etc/openvpn/easy-rsa/keys/client1.key
Aquests tres fitxers s'han de copiar al client. Estan disponibles a:
Configuració del servidor
# sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ # sudo gzip -d /etc/openvpn/server.conf.gz # joe /etc/openvpn/server.conf ca ca.crt cert myservername.crt key myservername.key dh dh1024.pem ... # Configure server mode and supply a VPN subnet # for OpenVPN to draw client addresses from. # The server will take 10.8.0.1 for itself, # the rest will be made available to clients. # Each client will be able to reach the server # on 10.8.0.1. Comment this line out if you are # ethernet bridging. See the man page for more info. server 10.8.0.0 255.255.255.0
I ara ja podem arrencar el servidor:
# /etc/init.d/openvpn start * Starting virtual private network daemon(s)... * Autostarting VPN 'server' # cat /var/log/syslog Jan 30 16:41:25 ubuntu ovpn-server[8332]: NOTE: your local LAN uses the extremely common subnet address 192.168.0.x or 192.168.1.x. Be aware that this might create routing conflicts if you connect to the VPN server from public locations such as internet cafes that use the same subnet. Jan 30 16:41:25 ubuntu ovpn-server[8332]: NOTE: the current --script-security setting may allow this configuration to call user-defined scripts Jan 30 16:41:25 ubuntu ovpn-server[8332]: Diffie-Hellman initialized with 1024 bit key Jan 30 16:41:25 ubuntu ovpn-server[8332]: TLS-Auth MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ] Jan 30 16:41:25 ubuntu ovpn-server[8332]: Socket Buffers: R=[180224->131072] S=[180224->131072] Jan 30 16:41:25 ubuntu ovpn-server[8332]: ROUTE default_gateway=192.168.1.1 Jan 30 16:41:25 ubuntu ovpn-server[8332]: TUN/TAP device tun0 opened Jan 30 16:41:25 ubuntu ovpn-server[8332]: TUN/TAP TX queue length set to 100 Jan 30 16:41:25 ubuntu ovpn-server[8332]: do_ifconfig, tt->ipv6=0, tt->did_ifconfig_ipv6_setup=0 Jan 30 16:41:25 ubuntu ovpn-server[8332]: /sbin/ifconfig tun0 10.8.0.1 pointopoint 10.8.0.2 mtu 1500 Jan 30 16:41:25 ubuntu ovpn-server[8332]: /sbin/route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.2 Jan 30 16:41:25 ubuntu ovpn-server[8332]: Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:0 EL:0 AF:3/1 ] Jan 30 16:41:25 ubuntu ovpn-server[8338]: UDPv4 link local (bound): [undef] Jan 30 16:41:25 ubuntu ovpn-server[8338]: UDPv4 link remote: [undef] Jan 30 16:41:25 ubuntu ovpn-server[8338]: MULTI: multi_init called, r=256 v=256 Jan 30 16:41:25 ubuntu ovpn-server[8338]: IFCONFIG POOL: base=10.8.0.4 size=62, ipv6=0 Jan 30 16:41:25 ubuntu ovpn-server[8338]: IFCONFIG POOL LIST Jan 30 16:41:25 ubuntu ovpn-server[8338]: Initialization Sequence Completed # ifconfig eth0 Link encap:Ethernet HWaddr 00:0b:6a:a6:f6:da inet addr:192.168.1.35 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20b:6aff:fea6:f6da/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14782 errors:0 dropped:0 overruns:0 frame:0 TX packets:11917 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1981881 (1.9 MB) TX bytes:3517056 (3.5 MB) ... tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:10.8.0.1 P-t-P:10.8.0.2 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Instal.lació i configuració del client
Hi ha vàries implementacions del client, sense i amb interfície gràfica. De moment ho farem sense interfície gràfica, amb el mateix executable que el servidor. Instal.lem en el client el mateix paquet que vam instal.lar en el servidor:
$ sudo apt-get install openvpn
Copiem el fitxer de configuració a la carpeta adient:
$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/
I copiem les claus i el certificat que vam crear en el servidor en la carpeta /etc/openvpn/. Pots descarregar-te un fitxer amb les tres claus a:
per ex, si desempaquetem a /tmp:
$ sudo cp /tmp/ca.crt /etc/openvpn/ $ sudo cp /tmp/client1.crt /etc/openvpn/ $ sudo cp /tmp/client1.key /etc/openvpn/
I ara editem el fitxer de configuració del client,
$ sudo joe /etc/openvpn/client.conf
canviar client.crt per client1.crt, client.key per client1.key:
ca ca.crt cert client1.crt key client1.key # Specify that we are a client and that we # will be pulling certain config file directives # from the server. client ... remote wiki.joanillo.org 1194
Hem d'especificar que és un client, i a quin servidor i port ens volem connectar. En una altra línia diem que el protocol de connexió és UDP (opció per defecte, que ha de ser la mateixa opció que hem definit en el servidor).
Arrenquem el OpenVPN client:
$ sudo /etc/init.d/openvpn start
I amb el ifconfig comprovem que s'ha creat la interface tun0:
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:10.8.0.6 P-t-P:10.8.0.5 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
i en el syslog comprovem que tot ha anat bé:
$ cat /var/log/syslog Jan 31 01:18:03 musica ovpn-client[3024]: OpenVPN 2.2.1 i686-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11] [eurephia] [MH] [PF_INET6] [IPv6 payload 20110424-2 (2.2RC2)] built on Feb 27 2013 Jan 31 01:18:03 musica ovpn-client[3024]: NOTE: the current --script-security setting may allow this configuration to call user-defined scripts Jan 31 01:18:03 musica ovpn-client[3024]: LZO compression initialized Jan 31 01:18:03 musica ovpn-client[3024]: Control Channel MTU parms [ L:1544 D:140 EF:40 EB:0 ET:0 EL:0 ] Jan 31 01:18:03 musica ovpn-client[3024]: Socket Buffers: R=[87380->131072] S=[16384->131072] Jan 31 01:18:03 musica ovpn-client[3024]: Data Channel MTU parms [ L:1544 D:1450 EF:44 EB:135 ET:0 EL:0 AF:3/1 ] Jan 31 01:18:03 musica ovpn-client[3024]: Local Options hash (VER=V4): '69109d17' Jan 31 01:18:03 musica ovpn-client[3024]: Expected Remote Options hash (VER=V4): 'c0103fa8' Jan 31 01:18:03 musica ovpn-client[3025]: Attempting to establish TCP connection with [AF_INET]81.35.105.162:1194 [nonblock] Jan 31 01:18:04 musica ovpn-client[3025]: TCP connection established with [AF_INET]81.35.105.162:1194 Jan 31 01:18:04 musica ovpn-client[3025]: TCPv4_CLIENT link local: [undef] Jan 31 01:18:04 musica ovpn-client[3025]: TCPv4_CLIENT link remote: [AF_INET]81.35.105.162:1194 Jan 31 01:18:04 musica ovpn-client[3025]: TLS: Initial packet from [AF_INET]81.35.105.162:1194, sid=c2ed3079 7c6308fa
I podem fer un ping al OpenVPN server (a través d'un túnel encriptat).
$ ping 10.8.0.1 PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data. 64 bytes from 10.8.0.1: icmp_req=1 ttl=64 time=40.0 ms 64 bytes from 10.8.0.1: icmp_req=2 ttl=64 time=41.2 ms
Recordem en què la gràcia de tot plegat és que el client i el servidor estan separats per la xarxa pública d'Internet, és a dir, no estan en la mateixa LAN ni en el mateix edifici. És realment una connexió remota.
Mirem l'enrutament:
$ sudo netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 10.8.0.1 10.8.0.5 255.255.255.255 UGH 0 0 0 tun0 10.8.0.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
Ara ja podem accedir a través del túnel a la màquina que fa de servidor VPN:
$ nmap -A 10.8.0.1 Starting Nmap 5.21 ( http://nmap.org ) at 2014-02-03 17:56 CET Nmap scan report for 10.8.0.1 Host is up (0.024s latency). Not shown: 999 closed ports PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 5.9p1 Debian 5ubuntu1.1 (protocol 2.0) | ssh-hostkey: 1024 28:ce:81:d7:73:d9:fe:45:87:d3:86:16:b2:85:0f:ed (DSA) |_2048 96:c5:13:4a:7a:7e:27:85:4c:b1:c2:7c:c8:a7:d5:2e (RSA) Service Info: OS: Linux
Veiem que hi ha un port obert. nmap amb l'opció -sU permet veure els ports UDP. Triga bastant, i descobreix el port 68/udp (dhcp). la descoberta de ports udp triga bastant. Tanmateix, el port del VPN no el descobreix (TBD).
Aquesta configuració tal com ha quedat és molt senzilla. Podem accedir al servidor VPN, però no podem accedir a la xarxa local on està aquest servidor. Més avall s'explica com fer-ho. Ara tot seguit farem la configuració del client amb interfície gràfica.
Configuració del client amb network-manager
(Linux Network-Manager GUI for OpenVPN, implemetacions del client)
El Network Manager és el programa que normalment utilitza les versions d'escriptori de l'Ubuntu per configurar la xara. Té un apartat que és per configurar xarxes VPN.
$ sudo apt-get install network-manager-openvpn $ sudo restart network-manager network-manager start/running, process 971
Open the Network Manager GUI, select the VPN tab and then the 'Add' button. Select OpenVPN as the VPN type in the opening requester and press 'Create'. In the next window add the OpenVPN's server name as the 'Gateway', set 'Type' to 'Certificates (TLS)', point 'User Certificate' to your user certificate, 'CA Certificate' to your CA certificate and 'Private Key' to your private key file. Use the advanced button to enable compression or other special settings you set on the server. Now try to establish your VPN.
- Nom de la connexió: wiki_joanillo_org
- Gateway: wiki.joanillo.org
- Tipus Autenticació: Certificats (TLS)
- certificat d'usuari: client1.crt
- CA certificate: ca.crt
- Private key: client1.key
- en opcions avançades no cal marcar res, senzillament assegurar-se de què ens volem connectar pel port 1194 (UDP).
Entrega
Recorda la normativa per entregar les pràctiques al Moodle: ASIX-M11-SAD#Normativa_d.27entrega_de_les_pr.C3.A0ctiques_al_Moodle
creat per Joan Quintana Compte, novembre 2011