0

I need to set a reverse ssh connection for an Ubuntu server 16.04. I searched different ways to write in a file with a variable but I didn't find one for my case :(

the command to be executed: autossh -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /home/myUser/.ssh/nopwd -R ${PORT_NUMBER}:localhost:22 myUser@example.com &

I use this script;

#!/bin/bash `
KEY_PATH=/home/myUser/.ssh/nopwd echo "generating your key..."
ssh-keygen -b 2048 -t rsa -f $KEY_PATH  -N ""
echo "the key is saved under: $KEY_PATH "
ssh-copy-id -i /home/myUser/.ssh/nopwd.pub myUser@example.com
echo "key sent..."
echo "enter a free port..."
read PORT_NUMBER
sed -i -e '$i \autossh -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /home/myUser/.ssh/nopwd -R $PORT_NUMBER:localhost:22 myUser@example.com &' /etc/rc.local

The idea is to set the port number of the rc.local command with this script.

All advices are welcomed since im very new to linux and bash.

  • use vpn instead – Ipor Sircer Dec 8 '16 at 15:20
  • What distro are you using for this? This seems better to create a service for, rc.local is deprecated in most cases. If the goal is a portable way to run a command on boot, then services are the way to go. – Centimane Dec 8 '16 at 16:09
  • @Centimane he can enable it sudo systemctl enable rc-local.service – GAD3R Dec 8 '16 at 22:36
  • I'm using Ubuntu server 16.04. Now I try to do that with a string that I assemble, it seems easier. @centiman – user204542 Dec 9 '16 at 7:22
  • @GAD3R enabling rc-local isn't the solution, you shouldn't add on to something deprecated and start using it, you should simply use the current method, which in Ubuntu I believe is upstart services. This is especially true for someone new to Linux, don't learn the old and busted, learn the new hotness. – Centimane Dec 9 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy