A script that requires root privileges needs to be executed when a non-root privileged user signs in/out. How do I handle this?


Use the PAM session hooks module.

  • Thats useful. Now, some of operations in which I want to apply should be done before the memory is allocated for user account. And, the operations need to be done after the user account memory is fully deallocated. When does PAM actually trigger the scripts in concern with user account memory allocation/deallocation? One operation I intend to run is sdmem, which cleans the available ram. I need users who sign out to be cleaned up this way. – bambuntu Feb 7 '12 at 1:18
  • Linux doesn't allocate memory "for a user account" or at login time. Memory is allocated as new processes start. If you run this script from pam hooks, it will start before the user is able to run any programs. – poolie Feb 7 '12 at 1:59
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mitch Jul 8 '13 at 11:23

lightdm, the display manager in Ubuntu allows running session setup scripts as root if you add appropriate entry to /etc/lightdm/lightdm.conf file (Note, that this file doesn't exist as of 14.04, you have to manually create it).

Example of how it might look like

# session-setup-script = Script to run when starting a user session (runs as root)
# session-cleanup-script = Script to run when quitting a user session (runs as root)
  • Serg, apparently yr answer only applies to gui sessions (running lightdm). Can it be made to work just before power-down independently of any gui session ? I have two use-cases of interest: --- 1) when any user logs out from either gui or an interactive login shell with no gui --- 2) when a box runs headless and is about to power down with no user session (interactive login or gui). --- This might deserve a new question. – Cbhihe May 17 '16 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.