Skip to content

Instantly share code, notes, and snippets.

@Benoss
Created November 9, 2018 02:45
Show Gist options
  • Save Benoss/3c2371f69f695ff43ee8d3d453329889 to your computer and use it in GitHub Desktop.
Save Benoss/3c2371f69f695ff43ee8d3d453329889 to your computer and use it in GitHub Desktop.
AutoSSH Tunnel Config

Create a new local tunnel user to keep the .ssh/config clean

sudo useradd -g nogroup -s /bin/false -m tunnel
sudo -u tunnel mkdir -p ~tunnel/.ssh  # and copy your private key here
sudo -u tunnel nano ~tunnel/.ssh/config  # add host and key configs here and a myhostsshconf (or other name)
sudo -u tunnel ssh myhostsshconf  # just make sure to add your host to `known_hosts`

Create a new systemd service with the same name than an entry in the .ssh/config (myhostsshconf in this example)

systemctl start [email protected]
# Check that everything is all good
systemctl status [email protected]
# /etc/systemd/system/[email protected]
# %i represent the instance of this service
# systemctl start [email protected] will start autossh for the entry myhostsshconf in /home/tunnel/.ssh/config
[Unit]
Description=Keeps an ssh tunnel to %I open
After=network-online.target ssh.service
[Service]
User=tunnel
# no monitoring
Environment="AUTOSSH_PORT=0"
# Disable gatetime behaviour
Environment="AUTOSSH_GATETIME=0"
RestartSec=3
Restart=always
# -NT Just open the connection and do nothing (not interactive, no tty alloc)
# use /usr/bin/ssh instead of autossh is good as well
ExecStart=/usr/bin/autossh -NT -o "ExitOnForwardFailure=yes" %i
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target
# /home/tunnel/.ssh/config
Host myhostsshconf
HostName benserver.com
User ben
# RemoteForward remoteport localinterface:localport
# Here we Forward the port 2221 on the remote machine to our local ssh port
RemoteForward 2221 127.0.0.1:22
# Localforward localport remoteip after ssh:port on this ip
# Here we forward mysql running on the local remote machine to our local port 3307
# LocalForward 3306 127.0.0.1:3306
ForwardAgent yes
IdentityFile ~/.ssh/id_rsa
# /etc/ssh/sshd_config
# This is an example of a configuration file for the server you want to connect to
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Cipher suites
Ciphers [email protected],[email protected]
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
X11Forwarding no
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
# Disable passwords, requiring keys
PasswordAuthentication no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment