5

I set these users' home folders through the /etc/passwd file, but when they log in I would like them to activate a python virtualenv. If they had a personal .bashrc file, I'd just include the activate command there, and the directory change as a post-activate hook with virtualenv.

However these users don't have home folders, and don't have personal .bashrc files.

How can I execute a different command on login for each of these users? (Only one command needs to be executed.)

These users are only logged into using su, never SSH. The machine is a VPS running Ubuntu 12.04.3 LTS.

2 Answers 2

8

You can add a file to the system's /etc/profiile.d directory that includes a if/then statement for each of the users that you want to run the virtualenv for.

Example

Say I create a file like this, /etc/profile.d/me.sh.

if [ "$USER" == "saml" ]; then
  touch /tmp/samsfile
fi

Make it executable:

$ chmod +x /etc/profile.d/me.sh

And then login as saml, using su:

$ su saml

If we check to see if the temp file was created, it was:

$ ls -l /tmp/samsfile 
-rw-rw-r-- 1 saml saml 0 Oct 14 00:31 /tmp/samsfile
2

If all you want to do is define environment variables, you can put them in ~/.environment in the users' home directory. You can put more complex shell snippets in ~/.profile.

All user accounts do have a home directory, even if it doesn't belong to them. While you can have accounts whose home directory doesn't exist, there isn't much point to doing that. You can use a single directory owned by root as the home directory of these users if you like.

Another possibility to execute a command at login time would be to add pam_exec to the PAM stack. But beware that this is quite tricky to use correctly, because the program runs as the original user, or as root, and not as the target user, and it runs in the environment chosen by the original user so it's hard to make it secure if it's running as root.

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.