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

Upon any ssh login/connection (anybody logging in through ssh), is there a way to have a script executed? Using OpenSSH server.

The script should not be modifiable by any of the users, -- of course --, so this should be a system-wide script.


share|improve this question
up vote 1 down vote accepted

If you're looking to do it non-intrusively to the user's experience, take a look at a syslog substitute (e.g. syslog-ng). You can trigger the command on the log messages sent to syslog by the SSH server when someone logs in.

This approach is a little rube-goldbergian, and depends on syslog working correctly. YMMV, IMHO, SLATFATF

share|improve this answer
This seems the only solution that does not depend on the user's shell, can you tell me more about how to achieve this? – Weboide Jun 22 '10 at 11:14 <-- This person is doing something almost the same with his/her "Email certain logs" configuration. Matching log entries on a string, and executing a program in response. The sample program is to email, but yours can do whatever you want. – Slartibartfast Jun 23 '10 at 2:29

You already have a script that's being executed every time someone logs in: /etc/profile

You can modify that script to perform whatever task it is that you have in mind, or have it call your script directly. On Debian derivatives, you can just drop your custom script (ending in .sh) in /etc/profile.d and it will be called automatically.

NOTE: Everything I just said only applies to Bourne shell derivatives, like the default /bin/sh, ash, bash, ksh, etc. Most users stick with bash, so the above directions are likely all you need. Other families of shells use other startup scripts. Tcsh, for example, will execute /etc/csh.cshrc on login.

share|improve this answer
upboat for use of have in italics :) – troyengel Jun 22 '10 at 8:36
+1 for re-use of /etc/profile -- standard on almost every environment I use :) – James Mills Jan 14 '14 at 8:51

See ForceCommand in sshd_config(5):

Forces the execution of the command specified by ForceCommand,
ignoring any command supplied by the client and ~/.ssh/rc if
present.  The command is invoked by using the user's login shell
with the -c option.  This applies to shell, command, or subsystem
execution.  It is most useful inside a Match block.  The command
originally supplied by the client is available in the
SSH_ORIGINAL_COMMAND environment variable.  Specifying a command
of ``internal-sftp'' will force the use of an in-process sftp
server that requires no support files when used with
share|improve this answer

You could change a user's shell such that a custom script was executed upon login. It could be the default shell for any user that needs it (I'd recommend keeping an admin account with bash to fix problems).

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.