LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Preserve current user environment for sudo su (https://www.linuxquestions.org/questions/linux-newbie-8/preserve-current-user-environment-for-sudo-su-4175606520/)

Andrew256 05-23-2017 12:09 PM

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.

rtmistler 05-23-2017 12:21 PM

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.

AwesomeMachine 05-23-2017 12:25 PM

You can set some environment related settings in the sudoers file. But you'll just have to use sudo with each command.

scasey 05-23-2017 12:32 PM

Quote:

Originally Posted by Andrew256 (Post 5714277)
So, is there a way to switch to root with 'su' but preserve my own configs?

AFAIK, the short answer is: no.

cepheus11 05-23-2017 12:36 PM

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.

Andrew256 05-23-2017 04:23 PM

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:

Originally Posted by cepheus11 (Post 5714295)
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.

Unfortunately I don't have such variable, but this gave me an idea. First, I can create a script in /usr/bin, like this:

Code:

echo $USER > /tmp/.origuser
sudo su -

Then I woud run this script to switch to root, and then add something like this into /root/.bashrc:

Code:

if [ ls -a /tmp | grep -o .origuser ]; then

    USERNAME=$(cat /tmp/.origuser)

    if ! [ -z "$USERNAME" ]; then
        cp ~/.vimrc ~/.vimrc.bak
        cp ~/.tmux.conf ~/.tmux.conf.bak
        cp /home/$USERNAME/.vimrc ~/.vimrc
        cp /home/$USERNAME/.tmux.conf ~/.tmux.conf
    fi

    rm /tmp/.origuser
else
    if [ ls -a ~ | grep -o .vimrc.bak ]; then
        mv ~/.vimrc.bak ~/.vimrc
    fi

    if [ ls -a ~ | grep -o .tmux.conf.bak ]; then
        mv ~/.tmux.conf.bak ~/.tmux.conf
    fi
fi

This will copy my configs temporaty to /root directory, and when someone else login into the shell using "sudo su -" the script would replace them back. It's not perfect of course, but it's something. However I have alot more configs. Is there a way to write something like this?:

Pseudocode:
Code:

while (/root contains file starting with ".") do
        append ".bak" to the file's name
        copy same file from /home/$USERNAME/ to /root/
done

and vice versa:
Code:

while (/root contains files ending with ".bak") do
        move ~/."$FILENAME.bak" to ~/.$FILENAME (i.e. remove ".bak")
done


Andrew256 05-23-2017 04:57 PM

Quote:

Originally Posted by Andrew256 (Post 5714413)
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

Hahaha, I feel so stupid. Looks like there is a native way. All I need to do is to set HOME=/home/$USERNAME after switching to root and all my configs would be read from there. And there I was going out of my way to write some stupid scrits... The variable is reset back to default each time close the session and it doesn't affect other users as far as I can tell. This is a perfect solution!

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...

AwesomeMachine 05-23-2017 09:46 PM

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!

Andrew256 05-24-2017 03:35 AM

Quote:

Originally Posted by AwesomeMachine (Post 5714487)
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!

Is there any reason why it's undesirable? If we run text editor and load the contents of the file into a virtual buffer (that what text editors do), the file stay unchanged until we save the changes, i.e. the same as owerwriting file with another one. And vim can warn you if the file was changed by someone else while you were working on it before saving. Is there a good reason why this should be avoided?

Turbocapitalist 05-24-2017 03:41 AM

Quote:

Originally Posted by Andrew256 (Post 5714413)
Honestly, even using sudo each time is not good,

It's definitely not a good way to edit.

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.

pan64 05-24-2017 05:05 AM

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

Andrew256 05-25-2017 08:06 AM

Quote:

Originally Posted by pan64 (Post 5714569)
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

Let's say I manage a remote server. I create a new user for myself and make some long secure random password I don't even want to remember. Than I create a SSH key to login without password. Here's the catch - after I login with my user, I want to have a full root privileges without bothering with either my or root password. So I add myself to sudoers file with NOPAASSWD: option, and then I do this:

Code:

sudo -E su -p
This preserves my environment across the whole switching process and I end up as root with my user's environment and configs. Sweet! Also I can add an alias in .bashrc, somthing like

Code:

alias suroot='sudo -E su -p'
So far I haven't found any problems with it. I understand this is probably not the best practice, but does it have critical flaws I don't know about? Should I abandon it?

pan64 05-25-2017 08:31 AM

I would say sudo -E -s (or something similar) is enough, but probably I missed something.

cynwulf 05-25-2017 08:32 AM

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.