2

The configuration below is in my /etc/sudoers:

Defaults    !visiblepw
Defaults    always_set_home
Defaults    env_reset
Defaults    env_keep =  "COLORS EDITOR DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

When I sudo su my PS1 terminal changes to the system default; ignoring my .bashrc PS1 setting. How can I fix this so that the environment variables are not lost when sudo suing?

Weird; it's exporting EDITOR but it's not exporting PS1 for some reason; it's like it's being forcefully overridden; how can I stop this?

When I run sudo -V the variables to keep PS1 is listed; it's not listed in the variables to remove.

6

Don't use sudo su. It's an abuse/useless use of su. The proper equivalent is sudo -s.

If you want to preserve environment variables, you want sudo -Es.


If you find that $PS1 is still changing, you likely have a script doing it. You can find out where by doing sudo -Es $SHELL -x and reading through the output.

  • I've run into issues trying to use growisofs where it won't work with anything except sudo su -. – slm May 30 '14 at 17:48
  • @slm sudo su - equivalent would be sudo -i (login shell). If you're using SELinux, there are other options for that as well. – Patrick May 30 '14 at 18:11
  • 1
    @josten you can see what gets preserved by doing sudo -E env. Make sure that PS1 has even been exported (export PS1), as it's not required to be. – Patrick May 30 '14 at 18:14
  • 1
    @Patrick - yeah I've tried several of the sudo methods and they all result in this w/ growisofs. ... :-( growisofs is being executed under sudo, aborting! See NOTES paragraph in growisofs manual page for further details.... – slm May 30 '14 at 18:40
  • @slm Wow, that's just stupid in my opinion. It's not the job of destination program to prevent sudo from being abused. VI, bash, and any number of other utilities don't prevent themselves from being run under sudo. That's what sudoers is for. – Patrick May 30 '14 at 18:46
5

A year or two ago, the authors and package maintainers of the Unix system shells agreed that it is generally better to not export PS1 (due to vast syntax differences between the various shells combined with certain “explosive” behaviour of some shells when provided with the prompts of other shells). We all agreed on moving PS1 initialisation to the interactive shell startup files, that is, ~/.mkshrc, ~/.kshrc, ~/.bashrc etc.

Disclaimer: I'm the mksh developer.

  • The mksh dev? Is it that lonely a road? I do understand the problem you describe - i couldnt use $(printf \\t) in a bash $PS1 just yesterday because, as it happens, the common c-escape for <tab> is reserved by bash for special prompt-expansion. – mikeserv May 31 '14 at 7:37
  • @mikeserv just do tab=$'\t'; PS1='something'$tab'something'; unset tab then. (Or use mksh ☺) No, it's not lonely, the community/user feedback is amazing. Sure, sometimes I'd wish for a helping hand, but occasionally I do get patches, and very often good inspiration and even delightful bug reports (well written, with reproducer cases and suggestions for one or more ways of fixing it). – mirabilos May 31 '14 at 10:55
  • 1
    @mikeserv AIUI both AT&T ksh93 and GNU bash are developed by single persons as well (David Korn and Chet Ramey, respectively). – mirabilos May 31 '14 at 19:15
  • That's news! Thanks, @mirabilos. It was after I looked over your MirOS website that I deleted my last comment - I wouldn't like to assume you'd have the time - you're a busy guy. I'm curious about mksh and printf after reading through - but maybe I'll email... – mikeserv May 31 '14 at 20:02
1

PS1 is probably initialized at shell startup. I.e. the value is indeed preserved by sudo but afterwards overwritten by the shell.

1

Usually you can explicitly set the $ENV source file for a shell upon invocation. For instance, you might have some luck with the following:

sudo ENV=/home/$USER/.bashrc su

Failing that, you might also try something like:

sudo su -c "exec </dev/tty ENV="~/.bashrc" bash -i"

Your Answer

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