Skip to content

Instantly share code, notes, and snippets.

@sjmuniz
Created July 8, 2017 04:07
Show Gist options
  • Save sjmuniz/5033ad65e7ea69259f2d890d6a5b93d7 to your computer and use it in GitHub Desktop.
Save sjmuniz/5033ad65e7ea69259f2d890d6a5b93d7 to your computer and use it in GitHub Desktop.
autossh para remote access con remoteforward.
La idea es tener un tunel de acceso a un vps o servidor remoto usando un remoteforward.
Este ejemplo es con upstart porque tengo un ubuntu 14.x para 16.x hay que hacer lo mismo con systemd (fuera del scope de este gist)
Server laptop
1-ssh >----4444---->
2 configura un remoteforward, ej tcp/2200
3 <----2200----< ssh -p 2200 user@localhost
4 Profit!
En el server logeados con el usuario serveruser (de administracion) configuramos:
$ sudo apt install y autossh
$ sudo useradd -c "Autossh et call home" -m -r autossh
$ sudo su -
# su - autossh
$ ssh-keygen
$ ssh-copyid -p4444 userlaptop@laptop (para que esto funcione hay que configurar un port forward en el tcp/4444 de tu router)
$ ssh -p4444 userlaptop@laptop (probamos que funcone)
$ mkdir .ssh && chmod 400 .ssh
$ vi .ssh/config
$ cat .ssh/config
host *
ServerAliveInterval 60
ServerAliveCountMax 3
host laptop
hostname ippublicagatewaylaptop
port 4444
user userlaptop
RemoteForward 2200 localhost:22
$ ssh laptop (probamos que funcione).
$ autossh -M0 -4 -N laptop (probamos que autossh funcione también).
ctld d, ctld d
$ sudo vim /etc/init/autossh.conf
$ cat /etc/init/autossh.conf
description "autossh daemon for ssh tunnel"
start on net-device-up IFACE=eth0
stop on runlevel [01S6]
setuid autossh
respawn
respawn limit 5 60
script
export AUTOSSH_FIRST_POLL=30
export AUTOSSH_GATETIME=0
export AUTOSSH_POLL=60
autossh -M 0 -4 -N -o BatchMode=yes -o StrictHostKeyChecking=no laptop
end script
$ initctl status autossh
$ initctl start autossh
En la laptop:
-------------
Una vez que el autossh esté arriba tendremos un tunel en el tcp/2200 con el que podremos ssh hacia el server.
$ cat .ssh/config
host server
hostname localhost
port 2200
user serveruser
Espero que se entienda la idea.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment