Super User is a question and answer site for computer enthusiasts and power users. 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 possible way that I can execute a script, say; as root user on every login to my system via SSH?

I've gone through a similar question which says to add the script execution to .bashrc file. This is not much helpful as I'd have to add it to every users' config file. Also, they'd still have ability to remove it.

Execution as root is not as important as denying the users' the power to stop its execution. The OS is debian, if that helps in any way.

share|improve this question

You could add the call into /etc/bash.bashrc - this is processed on every Bash login, so assuming your users are using Bash as their shell, it should run the script.

share|improve this answer
.bashrc is only sourced when Bash is run as a non-login shell, so you might want to use /etc/profile? – slhck Jul 14 '13 at 12:23

OpenSSH supports the ForceCommand configuration variable which might be set to the pathname of a script which

  1. Does something administrative — see below for details.
  2. Calls




    after that to actually perform the task the user intended to carry out.

Now the problem is that the forced command is executed with the credentials of the logged in user. To combat this, it's sufficient to use some sort of IPC to communicate with a daemon process, running as root. Unix-domain sockets appears to be a best bet for me as it actually allows transferring of the credentials before the actual data exchange takes place (by using the getsockopt(SO_PREERCRED) call). The downside is that you'll have to write a pair of tools to do that (well, the client part could be handled by socat).

See also this thread.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .