2

Not sure what I'm doing wrong here.

User2 sources a file in it's .bash_profile to set environment specific aliases.

# .bash_profile
source $HOME/set_environment_shortcuts

Inside $HOME/set_environment_shortcuts (there are many aliases in here). Example:

alias startservices="verylongcommand"

Now I would like to 'startservices from another user.

[User1@server1 ~]$ sudo su -l User2 -c '. ~/.bash_profile; startservices'
-bash: startservices: command not found

The runuser command produces the same result.

[User1@Server1 ~]$ sudo runuser -l User2 -c '. ~/.bash_profile; startservices'
-bash: startservices: command not found

Do aliases not work in this way?

Note, when bypassing the alias entirely, the command works.

  • Is startservices defined in the other user's ~/.bash_profile? – Satō Katsura Sep 25 '17 at 20:03
  • 2
    You realize that ~ means home directory (as does $HOME), and that different users have different home directories, right?  Please edit your question to be a whole lot clearer about what is defined in whose file, and which user is executing which command. – Scott Sep 26 '17 at 1:02
5

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set with shopt -s expand_aliases.

Aliases are a shortcut tool for use interactively. For any kind of scripting, use a shell function instead:

startservices () {
    # commands go here
}

Shell functions are a lot more flexible than aliases in many ways. They are able to take arguments like a shell script does, for starters:

startservices () {
    user="$1"
    service="$2"
    # code to start service "$service" as user "$user"
}

You should not have to source the other user's .bash_profile explicitly. Use sudo -i instead. This will start a login shell, which will read .bash_profile when starting:

$ sudo -i -u User2 startservices

This requires startservices to either be a script or other external utility in the $PATH of User2, a shell function defined in the shell startup files of User2, or an alias (with the shell running with expand_aliases set) defined in the shell startup files of User2.

See also Is there ever a good reason to run sudo su?

  • I see, thanks. I'll need to rebuild the script that sets the alias, to instead set functions. I will test using functions shortly. As for "sudo -i". It creates a different problem. I tried it earlier and the command prompts User1 for a password even though the user is a sudoer. – Motorahead Sep 25 '17 at 21:43
  • So I ended up using expand_aliases. It seems to be a cleaner approach than rebuilding the entire script. The script sets aliases based on three factors, user/host/environment. Within the alias there are variables that pick up strings based on those factors as well. Assigning the aliases to a function that applies to many user environments made most sense with my knowledge of scripting. Is shopt -s expand_aliases considered acceptable use, and not really a band-aid approach? What are the pros/cons to this? – Motorahead Sep 25 '17 at 23:17
  • @Motorahead I would honestly do a function for this, it sounds like a lot of logic and parameters could be built into the function, which would make it self-contained. I'm guessing you now have a lot of logic tests that sets of some variation of the alias based on the environment. This logic could be moved into the function. – Kusalananda Sep 26 '17 at 8:37
  • Yeah there a bunch of logic tests that determine which function to call, thus setting the aliases. I'll have to look at containing that logic into a function as you mentioned. Thanks! – Motorahead Sep 26 '17 at 21:27
0

The alias is defined in user1's .bash_profile . When you use "su -l user2" it actually clears all the environment variables for user1 and loads user2's variables instead, where no "startservices' alias is defined.

You can use "su -p " option which will preserve the current environment, but in that case user2 has to have read permission on user1's .bashrc file.

  • Sorry no, the aliases are defined in User2. – Motorahead Sep 25 '17 at 22:08

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.