I have created a linux user and changed his default shell to /bin/true
. I want to allow this user to set up a port forwarding via SSH, but not execute anything on the server.
How can I do that?
I have created a linux user and changed his default shell to /bin/true
. I want to allow this user to set up a port forwarding via SSH, but not execute anything on the server.
How can I do that?
By setting the user's shell to /bin/true
, you already prevented the user from executing anything on the server. The problem is, if the user connects "normally" to the server via ssh
, after entering the password he/she is immediately disconnected, so it's not possible to use the port forwarding as well.
Your user should use -N
parameter to ssh
, like this:
ssh -N -L 8000:localhost:80 youruser@yourserver
This creates a port forwarding from the port 8000 of the machine the user is connecting from to the port 80 on server. The -N
parameter tells ssh
to only establish the connection, but to not execute the user's default shell (/bin/true
in your case). So the user stays connected and the connection will "hang" until interrupted by Ctrl-C. During this time the ports are forwarded.
You can also make ssh
to go to background before "hanging" (so the terminal is not blocked) by adding -f
before -N
. In this case, to quit the port forwarding later, you have to find the ssh
process in the process list and kill it.
/bin/true
(as stated in the question), they can't execute anything anyway. The problem is, however, that if they connect with ssh
"normally", without -N
, they are immediately disconnected, so they can't use port forwarding (which OP wants). The -N
is the way for them to stay connected, so they can actually use port forwarding.
– raj
Sep 28 '23 at 13:25
sftp
, reading (and potentially writing) files. Other service applications may also accept the username/password credentials as sufficient authentication without referencing the shell defined in /etc/passwd
– Chris Davies
Sep 28 '23 at 14:37
/bin/true
is not in /etc/shells
, then using sftp is not possible. Just tested it on my server. May be configuration dependent, but with standard configuration it works like that.
– raj
Sep 28 '23 at 15:55
/etc/shells
here either, but with internal-sftp
set for sshd
I can definitely log in
– Chris Davies
Sep 28 '23 at 16:40
internal-sftp
is not a default setting, someone must have knowingly set that.
– raj
Sep 29 '23 at 11:46
restrict ssh
in the search box here. What have you found, which documentation have you read, what have you tried so far? – Marcus Müller Sep 28 '23 at 12:06/bin/true
– you want to restrict a user to only open port forwardings. I'm fixing your question to reflect that. – Marcus Müller Sep 28 '23 at 12:17