Preserve current user environment for sudo su
Hi guys
This applies to Debian, but basically I'd like to know this for linux in general. From what I heard doing 'sudo su -' switch user and change the environment, while doing 'sudo su' switch user, but preserve the environment of the original user. However this doesn't seem to apply to config files. Here's the situation - I'm having my own account on the server and I configure everything to my needs (like saving my own .vimrc and .tmux.conf files in my ~ directory). However when I switch to root with 'sudo su', all configuration is read from /root directory, but I can't just overwrite root's configs because other people are also using root and have their own preferences. I know I can place 'sudo' before each command but I would prefer to work from root. So, is there a way to switch to root with 'su' but preserve my own configs? Sorry for noob question. |
Some distributions you are always root, however many are not this way.
Either case, it sounds as if this system you are referring too has numerous users. I would recommend you stick with the practice of using sudo. |
You can set some environment related settings in the sudoers file. But you'll just have to use sudo with each command.
|
Quote:
|
With "sudo su" on ubuntu, I can see a variable $SUDO_USER in the environment. Check if you habe this too. If yes, and if vim/tmux etc. support includes in their config files, you could check for existence and include "/home/${SUDO_USER}/.vimrc in /root/.vimrc etc.
|
Honestly, even using sudo each time is not good, because if I want to edit a protected file, I woud start vim with sudo, and vim would not load my configs then...
Also, the fact that if I ask google "linux switch to root but preserve configs", this post is the 4th result, means not many had asked this. I can't believe there isn't a native way to do something so immensibly usefull as preserve your configs while elevating yourself to root. You don't have such problems in windows while running something as Administrator. I'm confused... Quote:
Code:
echo $USER > /tmp/.origuser Code:
if [ ls -a /tmp | grep -o .origuser ]; then Pseudocode: Code:
while (/root contains file starting with ".") do Code:
while (/root contains files ending with ".bak") do |
Quote:
Thank you everyone who replied! If anyone knows if anything bad can happen if I would change $HOME variable all the time, please let me know. So far there doesn't seem to be any problems... UPD: Even better solution, we can use "sudo -E su -p" to preserve environemt fullym including your .bashrc and all dotted files! If only I knew from the beginning... |
Normally, if you want to edit a file that requires root privileges, you copy the file, edit it as a user, and copy it back with the sudo command. Launching a text editor as root is radical!
|
Quote:
|
Quote:
Look at sudoedit. It will launch your editor under your unprivileged account and work on a copy of the target file. If you save and exit then the file is copied over the original. |
sudo su is definitely bad practice, you use either sudo or su, but why both?
return to the original question: see man sudo, especially -E |
Quote:
Code:
sudo -E su -p Code:
alias suroot='sudo -E su -p' |
I would say sudo -E -s (or something similar) is enough, but probably I missed something.
|
The main problem with "sudo su" is that it makes running sudo essentially pointless. Once you issue "su" you are root, regardless, without sudo's logging or access control...
|
All times are GMT -5. The time now is 12:28 PM. |