If the restriction is for a number of users I would set up a Match
block in /etc/sshd_config
, to apply restrictions for that group of users. If the restriction is for a particular ssh key I would consider taking your suggested route and blocking commands in the ~/.ssh/authorized_keys
file.
Match
block in /etc/sshd_config
Read the documentation for Match
, and as usual when changing how a connection subsystem such as ssh
works, ensure you have a root login available on the remote system to revert or adjust any changes.
# Add users to the UNIX group "restrictedusers"
Match Group restrictedusers
AllowTCPForwarding yes
X11Forwarding no
AllowAgentForwarding no
ForceCommand echo No logins permitted
Keys in ~/.ssh/authorized_keys
Not only force a command
but also disable X11Forwarding, agent forwarding, etc. by prefixing access control options to an existing or new key entry. Ensure you have a login available on the remote system to revert or adjust any changes to the file.
restrict,port-forwarding,command="echo No logins permitted" ssh-ed25519 ed25519-public-key-code-goes-here...
In both cases, once this is set up the caller needs to set up the reverse callback for you. For example, this one connects the remoteHost
's local port 50123/tcp
to the caller's local port 123/tcp
ssh -fN -R 50123:localhost:123 remoteHost
Best of all, this can all be prepared in the caller's ~/.ssh/config
for remoteHost
, so that they would then just need ssh remoteHost
to start the callback:
Host remoteHost
ForkAfterAuthentication yes
SessionType none
# listenPort targetHost:targetPort
RemoteForward 50123 localhost:123