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

is there a way to just execute a command (or script) on a user (ssh) login and then exit? some kind of replacing the default shell with a custom script.

i want a user only be able to restart a service, but not to have regular shell-access


share|improve this question
up vote 3 down vote accepted

The way you want to do this is by using ssh keys, and set the command associated with the key in the user's .ssh/authorized_keys file. A quick google gives me this, specifically the "Forced command" section.

Edit: The second part of this is to passwd -l the user so that password-based login is impossible.

share|improve this answer

In Ubuntu I was able to create a script that restarted the service. Then in the user's /etc/passwd entry I set the script as the login shell (the last field).

from /etc/passwd:


share|improve this answer
In general: write a program that does exactly what the user needs to be able to do, and set it as the user's shell in /etc/passwd. I have used the same principle for CVS servers and other purposes. – reinierpost Jun 9 '10 at 13:14

Something Like that?

root@admin:~ # ssh root@www 'df -h'
Filesystem Size Used Avail Use% Mounted on
/dev/ubda 3.5G 2.1G 1.4G 61% /
tmpfs 96M 4.0K 96M 1% /dev/shm
share|improve this answer
yes, something LIKE that :-) in this case the user is able to really login. i just want him to be able to execute EXACTLY 1 command (always the same) and be loged out automaticaly. i do not want him to have shell access – bmaeser Jun 9 '10 at 12:54
maybe add the commands to the users .profile? – The Unix Janitor Jun 9 '10 at 13:16

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.