Open an SSH tunnel as a different user

Posted on

Problem :

I routinely open an SSH tunnel to access a service running on a remote machine:
/usr/bin/ssh -L portA:localhost:portB -N &. The problem I’m facing right now is that unlike usually, I need to access the service as a different user. When I SSH to and try accessing the service using sudo -u userX localhost:portB, it works fine as I have permissions over this user.

userX does not exist on my local machine and I essentially want to be able to access everything I can access on the remote host directly from my local machine. Is there any way to do that ?

PS: I can sudo as userX but don’t have their password (and shouldn’t have it). I’d like to be more accurate but my understanding of Linux permission model is a bit fuzzy in all fairness.

Solution :

Generate a key pair on your local machine for userX (ssh-keygen), ssh to the remote server, and add public key to the list of authorized keys. Then sudo in ssh config file, and uncomment public key authentication as allowed.
It shouldn’t ask about password then.

Try not to lock yourself out. Read some docs first if you are unsure.

Leave a Reply

Your email address will not be published. Required fields are marked *