First of all, you need a server with ssh access in the cloud, you can purchase a free one from amazon aws, let's call it mycloud
I want access my home pc (myhome) from my job pc (myjob).
First let's make myhome always connect to mycloud everytime it is connected to the internet. Since it's debian based linux, I write a script that I will call connect2mycloud and save it into /etc/network/if-up.d/ directory
inside /etc/network/init.d/connect2mycloud type the following and save it
ssh -R 12345:localhost:22 clouduser@mycloud
#su -c "autossh -f -N -R 12345:localhost:22 clouduser@mycloud -oStrictHostKeyChecking=no" myhomeuser
#su -c "autossh -f -N -R 12345:localhost:22 clouduser@mycloud -oStrictHostKeyChecking=no" myhomeuser
For RedHat based create a file called 15-connect2mycloud in the directory /etc/NetworkManager/dispatcher.d/
and inside /etc/NetworkManager/dispatcher.d/15-connect2mycloud type the following and save it
if [ "$2" = "up" ]; then
ssh -R 12345:localhost:22 clouduser@mycloud
#su -c "autossh -f -N -R 12345:localhost:22 clouduser@mycloud -oStrictHostKeyChecking=no" myhomeuser
fi
ssh -R 12345:localhost:22 clouduser@mycloud
#su -c "autossh -f -N -R 12345:localhost:22 clouduser@mycloud -oStrictHostKeyChecking=no" myhomeuser
fi
Doing this every time myhome is connected to the internet it will create a ssh reverse connection from mycloud. So once I am logged into mycloud I can just do ssh -p 12345 localhost, then I will connect to myhome
Ok, now I'm at myjob pc, and want to connect to myhome directly with only one command
To achieve this you must create an entry in your ~/.ssh/config file with a proxy command
so in myjob I add the following entry in ~/.ssh/config file like this
Host myhome
User myuser
ControlMaster auto
ControlPath /tmp/ssh-%r@%h:%p
ProxyCommand ssh myuser@mycloud nc localhost 12345 2> /dev/null
Now to connect to myhome, I just run
# ssh myhome
It will ask me mycloud and after myhome password in sequence, or nothing if I use public key
If you want to execute screen on remote host you need to use -tt (alocate tty) like this
# ssh -tt myhome screen -dR
If you don't want to edit ssh config file use the command
# ssh -tt myuser@mycloud 'ssh -tt localhost -p 12345'
you can also call screen reattach
# ssh -tt myuser@mycloud 'ssh -tt localhost -p 12345' screen -dR
No comments:
Post a Comment