4

I am on AIX 7.1.

I have a bunch of aliases defined in my personal .profile.

alias df='df -k'
alias cl=clear
alias h=history
alias ll='ls -al'
alias lt='ls -latr'
alias ls='ls -Fa'
alias psj='ps -ef | grep java'

If I issue a 'sudo su' or 'sudo su other_user' command, I am not able to use these aliases. I was under the impression that using a 'sudo su' without a '-' (sudo su -) would make me root while using my personal .profile?

jg10371@asdepdm1: /home/jg10371
$ ll
total 88
drwx------    3 jg10371  unxusers       4096 May 29 09:21 ./
drwxr-xr-x  154 bin      bin           12288 May 29 09:35 ../
-rw-------    1 root     system          200 Jul 04 2010  .bash_history
-rw-r--r--    1 jg10371  unxusers       1943 May 29 09:35 .profile
-rw-------    1 jg10371  unxusers       6944 May 29 09:36 .sh_history
drwx------    2 jg10371  unxusers        256 May 28 11:06 .ssh/
-rw-------    1 jg10371  unxusers         44 May 28 12:21 .vas_disauthcc_9168
-rwx------    1 jg10371  unxusers         28 May 28 12:21 .vas_logon_server*
-rwx------    1 jg10371  unxusers         18 Mar 28 18:06 .vi_history*
jg10371@asdepdm1: /home/jg10371
$ sudo su
Password:
jg10371@asdepdm1: /home/jg10371
$ ll
ksh: ll:  not found.
jg10371@asdepdm1: /home/jg10371
2
  • .profile is only read for login shells. You should define aliases in .bashrc not .profile.
    – jw013
    Commented May 29, 2015 at 15:57
  • @jw103: AIX uses ksh by default, so the hint about .bashrc will not work. Notice that the .bash_history in the excerpt is from 5 years ago.
    – doktor5000
    Commented May 31, 2015 at 13:48

4 Answers 4

3

Yes, actually logging into a different account (root in this case) will create a new session and the settings will be from the root's .bashrc or .kshrc file. You can see this in the following:

$ alias
$ ... output has over 100 aliases
$ sudo su
[sudo] password for durrantm: 
root@Castle2012:/tmp/user/1000/x# alias  # <- Only 7 aliases now!
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
root@Castle2012:/tmp/user/1000/x# cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
...
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
...

root@Castle2012:/tmp/user/1000/x# 

You can however export your environment - for that session only - with sudo -E.

You can make the alias available to ALL shells permanently by putting the alias definition in

/etc/bash.bashrc  # (You'll need to edit it with sudo!)
# /etc/profile for ksh

and you could also add conditional logic to only do that if the username matches root or the one you want.

2
1

Well, as you already noticed, it does not. The manpage entry says "sudo - execute a command as another user", which means, that aliases and bash variables will change accordingly. And when doing "sudo ll", the system notices, that user root does not know anything about ll.

If you want your aliases available as root, you can either copy the definitions to the roots profile file or create a separate alias file and include it using the source command. (It is possible to include the users profile file itself, but you may have some lines in it that shall not be adopted by root.)

1
  • Thanks all for the input. I simply do not have access to root .kshrc or .profile file so can't make this change. We do not own enough of the servers to be doing that.
    – hacket
    Commented Jun 1, 2015 at 13:36
1

It is easily possible to "fix" this partly for your use case, as long as you only run sudo some_command and not get a shell via sudo su or similar. It's based on the order of which the shell evaluates things, and pretty neat.
Check https://askubuntu.com/a/22043/329633 or https://serverfault.com/questions/61321/how-to-pass-alias-through-sudo and add that sudo alias to your ~/.kshrc
(I'm assuming you use the default AIX shell, as you don't have a ~/.bashrc and the ~/.bash_history in your excerpt is 5 years old ... If you do use bash, you need to adapt.)

If you want to add an alias for one specific user and if you use the default shell which is ksh and not bash then put the aliases into ~/.kshrc file of that user and not into ~/.bashrc.

You can also configure sudo to keep the environment of your user, check e.g. keep aliases when I use sudo bash for details.

Or if the commands and aliases should be available to all users, you could also add it to /etc/environment.

0

If you want to be able to execute another user's aliases as root without having to do any extra work (e.g., copying the alias definitions to root's profile file, converting the user's aliases into shell scripts), then you can extract the alias command and pipe it to the shell.

Example:

sudo su - otheruser -s /bin/bash -c "alias ll | sed -E \"s/.+='(.+)'/\1/\" | bash"

Or you can use my generic "run-as" function:

runas() {
  if [ $# -eq 2 ]; then
    # Check if user exists
    id -u $1 > /dev/null 2>&1
    if [ $? -gt 0 ]; then
      echo "$FUNCNAME: no such user"
      return 1
    fi
    # Check if alias exists
    sudo su - $1 -s /bin/bash -c "alias $2" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      # Alias is defined; execute $2 as alias
      sudo su - $1 -s /bin/bash -c "alias $2 | sed -E \"s/.+='(.+)'/\1/; s/'\\\'//g\" | bash"
    else
      # Alias is not defined; execute $2 as command
      sudo su - $1 -s /bin/bash -c "$2"
    fi
  else
    echo "Execute a command or alias as another user"
    echo "Usage: $FUNCNAME USER ALIAS"
    return 2
  fi
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .