Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to run a script/command every time a user connects using ssh? Can it be configured globally (i.e run the script when any user login)?

I came across this question on Identica, but there is no answer yet and would like to know it anyway.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

For all users, or a particular user? For a single user, set it in their .bashrc file; for all users, check out pam_exec.

If the users are coming in from sshd, you'll want to add the following line to /etc/pam.d/sshd; other files depending on their source:

session optional pam_exec.so seteuid  /path/to/my/hook.sh

For testing purposes, the module is included as optional, so that you can still log in if it fails. After you made sure that it works, you can change optional to required. Then login won't be possible unless the execution of your hook script is successful.

Note: As always when you change the login configuration, leave a backup shell open in the background and test the login from a new terminal.

share|improve this answer
    
Thanks, that looks promising. Can you elaborate a bit more? I'm unfamiliar with this. –  phunehehe Feb 14 '11 at 16:05
    
I suppose that I should add session include pam_exec.so seteuid /path/to/script to the file /etc/pam.d/system-remote-login. Is that correct? –  phunehehe Feb 14 '11 at 16:07
    
Either system-remote-login or sshd, depending on how the user is coming in. –  Glen Solsberry Feb 14 '11 at 16:07
    
Awesome! It would be great if you edit the answer to include the information (you know, for others who search for it). –  phunehehe Feb 14 '11 at 16:13
add comment

there's another way which only influences users using ssh not local ones (which might be better in emergency situations)

see the snippets from the ssh man page below.

in this case the users can usually modify the files themselves (a bit like .bashrc)

 ~/.ssh/rc
         Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started.  See the sshd(8) manual page for more information.

and this is global and not modifiable by the normal user

 /etc/sshrc
         Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started.  See the sshd(8) manual page for more information.

HTH Marcel

share|improve this answer
    
But /etc/sshrc will be executed only if user do not have own ~/.ssh/rc, so this user can bypass –  fanosek Feb 7 '13 at 21:02
add comment

Your Answer

 
discard

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.