Line 237: |
Line 237: |
| The following is how to create a persistent SSH Tunnel between two systems. This is handy if you want to secure data flowing across networks, or even setup a tunnel without messing with VPN configuration. | | The following is how to create a persistent SSH Tunnel between two systems. This is handy if you want to secure data flowing across networks, or even setup a tunnel without messing with VPN configuration. |
| | | |
− | =Create User/Generate SSH key= | + | ==Create User/Generate SSH key== |
| First you will create the user you will use for the tunnel. This will allow you to forward non-privileged ports over 1024. | | First you will create the user you will use for the tunnel. This will allow you to forward non-privileged ports over 1024. |
| | | |
Line 257: |
Line 257: |
| </pre> | | </pre> |
| | | |
− | =Copy public key to target system= | + | ==Copy public key to target system== |
| You will need to copy '''''id_rsa.pub''''' file from '''''/home/autossh/.ssh/''''' to the '''''authorized_keys''''' file on the remote system you want to connect to for the tunnel. | | You will need to copy '''''id_rsa.pub''''' file from '''''/home/autossh/.ssh/''''' to the '''''authorized_keys''''' file on the remote system you want to connect to for the tunnel. |
| | | |
| ''Note: It is recommended that you also create a normal user on the remote system and not use root.'' | | ''Note: It is recommended that you also create a normal user on the remote system and not use root.'' |
| | | |
− | =Install autossh= | + | ==Install autossh== |
| You will need to install the autossh program on the system that will initiate the SSH tunnel. Autossh automatically restarts the SSH tunnel when it exits. | | You will need to install the autossh program on the system that will initiate the SSH tunnel. Autossh automatically restarts the SSH tunnel when it exits. |
| <pre> | | <pre> |
Line 268: |
Line 268: |
| </pre> | | </pre> |
| | | |
− | =Setup script= | + | ==Setup script== |
| Copy the following script, making the necessary changes as specified between the <> and place on the system that will initiate the tunnel (here we will save it as /opt/ssh-tunnel.sh): | | Copy the following script, making the necessary changes as specified between the <> and place on the system that will initiate the tunnel (here we will save it as /opt/ssh-tunnel.sh): |
| <pre> | | <pre> |
Line 302: |
Line 302: |
| You can also use -L to change the direction of the port forwarding from Remote to Local and have the initiating system forward data over the tunnel the the remote. | | You can also use -L to change the direction of the port forwarding from Remote to Local and have the initiating system forward data over the tunnel the the remote. |
| | | |
− | =Make script executable= | + | ==Make script executable== |
| Make sure you mark the script as executable with: | | Make sure you mark the script as executable with: |
| | | |
Line 308: |
Line 308: |
| chmod +x /opt/ssh-tunnel.sh | | chmod +x /opt/ssh-tunnel.sh |
| </pre> | | </pre> |
− | =Tunnel at startup= | + | ==Tunnel at startup== |
| To have the tunnel up when the system restarts, choose one of the following methods | | To have the tunnel up when the system restarts, choose one of the following methods |
| | | |
− | ==rc.local== | + | ===rc.local=== |
| Add a line to /etc/rc.local that calls the script. | | Add a line to /etc/rc.local that calls the script. |
| | | |
Line 320: |
Line 320: |
| | | |
| ''Note: You may have to enable rc.local on Ubuntu and Debian based systems via systemd. Refer to your distributions documentation for information on how to enable it.'' | | ''Note: You may have to enable rc.local on Ubuntu and Debian based systems via systemd. Refer to your distributions documentation for information on how to enable it.'' |
− | ==systemd== | + | ===systemd=== |
| To have the script start at boot with systemd, create the following file and add it to /etc/systemd/system/ssh-tunnel.service | | To have the script start at boot with systemd, create the following file and add it to /etc/systemd/system/ssh-tunnel.service |
| | | |
− | ===ssh-tunnel.service=== | + | ====ssh-tunnel.service==== |
| <pre> | | <pre> |
| [Unit] | | [Unit] |
Line 336: |
Line 336: |
| </pre> | | </pre> |
| | | |
− | ===Enable service=== | + | ====Enable service==== |
| To enable the service to run via systemd run: | | To enable the service to run via systemd run: |
| <pre> | | <pre> |