

SSH creates both interfaces automatically, but IP and routing should be configured after the connection is established. Once these files are created, enable them by restarting rvice.Īlso, you may manage tun interfaces with ip tunnel command. Group=network /etc/systemd/network/vpn.network See sshd_config(5) for details.Ĭreate tun interfaces using systemd-networkd /etc/systemd/network/vpn.netdev Setting yes enables forwarding for both point-to-point and ethernet tunnels. To enable forwarding for the TUN device, edit /etc/ssh/sshd_config and set PermitTunnel to yes, point-to-point or ethernet. It is also possible to create a layer 2/ethernet/TAP tunnel. Here, a layer 3/point-to-point/ TUN tunnel is described. OpenSSH has built-in TUN/TAP support using -w. Now all traffic (except for DNS and the SSH server itself) should go through tun0. # ip route add default via 10.0.0.2 metric 6 With all of that said, let us get to work: We also need a new default route with a lower metric than your old default route so that traffic goes into the tunnel at all. Apart from that, we need to set an explicit DNS route because tun2socks does not tunnel UDP (required for DNS). The idea behind setting the metrics specifically is because we need to ensure that the route picked to the SSH server is always direct because otherwise it would go back into the SSH tunnel which would cause a loop and we would lose the SSH connection as a result.
#GOLANG SSH TUNNEL CODE#
Here's simplified code that does just that: package main

Accept local connections and forward data to the remote connection.Make connection to the target ip:port from SSH connection.
#GOLANG SSH TUNNEL PASSWORD#
Establish SSH connection with remote server user pubkey or password authentication.Implementing ssh port forwarding programmatically takes a few steps:

There's a "third-party" package crypto/ssh maintainedīy Google ( docs): go get /x/crypto/ssh Here's how you can start a standard psql console: psql standard library has plenty of packages, but unfortunately it does not provide Format: port:host:hostportĮxample above was modeled for usage with PostgreSQL.
