We sometimes want Ubuntu machine to conduct some tasks on startup. In my case, I wanted my Ubuntu machine to create a reverse SSH tunnel on startup so that I don't have to create one manually, or that I could connect to machine after rebooting it remotely. The command was like
As a result, it was . Ubuntu executes script on startup, if exists. On Ubuntu 18.04 there is no such pre-existing script file, different from its predecessors, but I could manually create one instead. Note that we need to end the script file with the code like , like below:
# /etc/rc.local autossh ~~ exit 0
And you may have to make the file executable by the command .
But still I could not connect to the machine after rebooting. The problem was, I wanted to connect to the machine as a user, not root, when the command was executed by root. Fortunately, the solution was easy. I just could execute the command as a user, using the code like . So the final result was like below:
# /etc/rc.local su USER -c 'autossh ~~' exit 0
Now, I don't have to worry about opening the tunnel every time after rebooting machine, and can reboot the machine remotely, just in case. Cool.