Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 am trying to run a command/script that requires root privileges when a non-root user logs into ssh

This unfortunately means I cannot use ~/.bashrc or /etc/bashrc

I also tried /etc/ssh/sshrc but it has the same problem, the command executes as the logged in user.

I see /etc/profile.d/ does something but not sure how to use it or if it will have the same problem.

sshd does run as root so I am hoping it can be taught to do this somehow

I do need to know the ip of the login for the command.

Thanks for any ideas!

added: sudo is not possible because it's for a non-privileged user without sudo rights

share|improve this question
Please include the actual command you need to run (answers will be different for scripts and compiled binaries for example). – terdon Jul 8 '13 at 18:36
Setuo sudo. Use sudo ... where required. – Zoredache Jul 8 '13 at 18:38
up vote 1 down vote accepted

Use pam_exec as a PAM session module.

share|improve this answer
Oh wow, I never knew about PAM. Reading up now, looks very promising. Any ideas how to grab the remote ip since it doesn't look like it's included in the PAM environment? – ck_ Jul 8 '13 at 18:44
@ck_: It's in the PAM_RHOST 'PAM item', but pam_exec exports it as an environment variable ($PAM_RHOST) as well. – grawity Jul 8 '13 at 19:15

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.