In this post I'm going to describe how to tunnel all traffic, regardless of it's type, from one machine to some other. In a way, I'll show how to create VPN networks using SSH. Actually, there are three ways to do that:
- Tunneling using ppp protocol on top of SSH
- Tunneling using tun devices natively supported by newer versions of ssh on, at least, Linux.
- Tunneling using Ethernet-like tap devices, also supported on a Linux OS.
Link-layer setup
The basic goal of this step is to provide network device on which network layer will work.
Using ppp program and protocol
For this method there is a very good small howto document. Here I'll only repeat some relevant bits in case you don't want to read the whole document. First, you have to configure passwordless authentication to a remote host. This is easy to do and there are a plenty of references on the Internet. Later, maybe I write one, too. :) Anyway, in the following text I'll assume that you are using root account on both machines, i.e. you are root on a local machine and you are connecting to a remote machine under the username root. Beware that this is very bad security practice, but for a quick test or in a lab environment it'll do.
Ok, after you configured passwordless login for a root user, run the following command (note: this is a single line up to and including IP addresses) but it is broken into more lines because of a formatting in a browser!):
# pppd updetach noauth passive pty 'ssh REMOTE_HOST -o Batchmode=yes /usr/sbin/pppd nodetach notty noauth' 10.0.0.1:10.0.0.2As you can see, you got some information about interface and your prompt is immediately back. You need to have ppp package installed on both machines, if it is not then there will be an error message, something like command not found, and no connection will be established. Anyway, in this case everything was successful and we are notified that the remote side has address 10.0.0.2 and local side has address 10.0.0.1. To verify that everything works, try to ping remote side:
Using interface ppp0
Connect: ppp0 <--> /dev/pts/12
Deflate (15) compression enabled
local IP address 10.0.0.1
remote IP address 10.0.0.2
#
# ping -c 3 10.0.0.2If you see the output like the one above, that means that the link is working and the next thing to do is to setup network layer parameters (apart from IP addresses).
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_req=1 ttl=64 time=19.3 ms
64 bytes from 10.0.0.2: icmp_req=2 ttl=64 time=17.5 ms
64 bytes from 10.0.0.2: icmp_req=3 ttl=64 time=18.7 ms
--- 10.0.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 17.503/18.534/19.380/0.777 ms
So, what happened? SSH created a communication channel between the two machines. This channel is then used by pppd processes on both ends to create ppp interfaces (one per each side). Then, this interface is used to transfer IP packets via protocol PPP. What we are (mis)using here is that pppd requires direct link between two points and through this link it then sends IP packets. Direct link is some sort of a serial interface, for example direct cable, UMTS/EDGE or similar connection, etc. The exact means by which two endpoints communicate is unimportant from pppd's perspective, what only matters is that whatever is sent on one end is received by other, and vice versa. So, because of this we could place SSH between two pppd processes.
The way the things are exectuted is as follows:
- Local pppd starts (the first pppd in a line), without authentication phase (option noauth), in a passive mode (option passive) meaning it is waiting for someone to connect. It also allocates one master/slave pty (option pty) pair in which he controls master side, while slave side is connected to ssh proces (argument to pty option). There are also two ip addresses at the end of the command line that are actually arguments to this instance of PPP. They instruct local ppp process to assign to itself address 10.0.0.1 and to the remote end 10.0.0.2 address.
- ssh process connects to REMOTE_HOST in batch mode (option -o BatchMode=yes). Basically batch mode tells ssh not to use password/passphrase because there is no user to enter necessary parameters. The rest of the command until closing single quote is the command that ssh has to execute after successfully connecting to remote host.
- ssh that connected to the remote machine runs there second pppd process. The options instruct that pppd process to use stdin/stdout instead of a termina (option notty), to not detach from the controlling terminal (option nodetach), and to not require authentication (option noauth).
- Create separate account that will be used to connect to a remote machine.
- Configure sudo so that it allows that new account to run pppd binary without entering the password.
- Modify invocation of ssh command so that remote user is specified and pppd program is executed via sudo command.
For this mode you need to have the following directive in the server configuration (/etc/ssh/sshd_config):
Tunnel anyAfter the change don't forget to reload ssh configuration. What this configuration option tells ssh deamon is to allow tunneling using tun and tap devices. In other words, we can make our tunnel looks like ethernet interface, or like point-to-point interface on the 3rd (network) layer. Option any allows both. In case you want to restrict to a certain kind use either ethernet or point-to-point, depending on which one you need.
In this case we want Ethernet-like functionality, so assuming that you provided either any or ethernet parameter to Tunnel option, as a root user run ssh client as follows:
ssh -f -N -w any -o Tunnel=ethernet root@remotehostchange remotehost part with the host name (or IP address) you are connecting to. It is necessary to specify Tunnel option in ssh command because default value is point-to-point. The -w option requests from ssh to do tunneling using tap or tun device (depending on the value of Tunnel option) and in our case it is tap. After successfully logging to a remote machine, check interfaces with ip command (of ifconfig). You should see on both hosts that there is tap0 interface. If you already had tap interfaces, than the new ones will probably have highest number. Options -f and -N cause ssh to go into background after performing authentication, since command line is not necessary for tunneling.
To stop tap device, send a SIGTERM signal to ssh (using kill command, of course).
Using tun with only network layer functionality
As with the ethernet like functionality, you have to enable this mode in sshd configuration file, and also, you need to do this as a root on both sides of the connection.
The procedure to create point-to-point tunnels is similar to creating the Ethernet ones, only the argument is point-to-point instead of ethernet. Since point-to-point type is default, you don't have to specify Tunnel option, i.e.
ssh -f -N -w any root@remotehostAfter logging into the remote host, ssh will go into background and you'll see new tun device created.
4 comments:
Hi
I create an ssh command in ubuntu to make tap interface , but it away make tun interface . Can you give me some advice , here is the command :
ssh -o Tunnel=ethernet -w 1:1 ServerIP
Is it possible to send UDP broadcast through this tunnel?
@song: How do you know that you've got a tun type interface? Based on what? Note that if the interface is called tun it doesn't have to mean it has a type tun. Also, check server configuration, maybe it is prohibiting a client to create tap interface.
@Unguran: I don't know, even though I suspect that it doesn't allow. Basically, it boils down to the question if Ethernet broadcasts are forwarded, and I think not because of the ARP. Anyway, have you tried it? You can also try to ping broadcast network address, that is essentially the same situation (-b option to ping). Currently, I don't have a setup to try this.
Note that in recent versions of SSH, the line:
Tunnel any
is spelled like this:
PermitTunnel yes
Post a Comment