Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm not sure if there is a specific name for this type of activity or not, but anyways:

When a user ssh's into machine1, is there someway to force them to automatically ssh to machine2?

For example, if user Bob ssh's into server1, can you set something up on server1 so that when Bob successfully logs in, he automatically ssh's to server2, so that he only has access to server2 and isn't really able to do anything on server1? Also, when he exits, it would disconnect him from server2 but also server1.

Does that make sense? Is that possible?

share|improve this question
up vote 2 down vote accepted

Is the real requirement here that Bob needs ssh access to machine2 but can't get at it directly because it's behind a firewall? If so, the cleanest solution is probably to port-forward a port on server1 to the ssh port (22) on server2, which you can do with iptables, see Then the user can

ssh -p <port number> server1 

to get into server2.

If you really want to force people logging in to server1 to go straight to server2 it might be possible to set the relevant user's shell on server1 to a script which runs "ssh server2" however the user will end up having to type a password twice (if you're using paswords) - once for server1 and again for server2 - and you'd also need to be careful to lock down the SSH server on server1 to prevent the user doing port forwarding etc through it (which you can do with SSH access regardless of what your shell is set to).

share|improve this answer

Add this to your .ssh/authorized_keys file in bob's home directory:

command="ssh host2" ...bob's key here...

This is called a 'Forced Command.' Google can give you more info.

share|improve this answer

To do exactly what you want to do, you should edit the user's .bash_profile to do a ssh into server 2, then the following line issue an exit command. Once his ssh session is over, the profile will kick off the "exit" command and log him off.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.