LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2017, 12:09 PM   #1
Andrew256
LQ Newbie
 
Registered: May 2017
Posts: 12

Rep: Reputation: Disabled
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.

Last edited by Andrew256; 05-25-2017 at 07:57 AM.
 
Old 05-23-2017, 12:21 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
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.
 
Old 05-23-2017, 12:25 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You can set some environment related settings in the sudoers file. But you'll just have to use sudo with each command.
 
Old 05-23-2017, 12:32 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.4
Posts: 5,806

Rep: Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239Reputation: 2239
Quote:
Originally Posted by Andrew256 View Post
So, is there a way to switch to root with 'su' but preserve my own configs?
AFAIK, the short answer is: no.
 
Old 05-23-2017, 12:36 PM   #5
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 286

Rep: Reputation: 91
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.
 
1 members found this post helpful.
Old 05-23-2017, 04:23 PM   #6
Andrew256
LQ Newbie
 
Registered: May 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
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 View Post
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

Last edited by Andrew256; 05-23-2017 at 04:32 PM.
 
Old 05-23-2017, 04:57 PM   #7
Andrew256
LQ Newbie
 
Registered: May 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Andrew256 View Post
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...

Last edited by Andrew256; 05-23-2017 at 06:31 PM.
 
Old 05-23-2017, 09:46 PM   #8
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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!
 
Old 05-24-2017, 03:35 AM   #9
Andrew256
LQ Newbie
 
Registered: May 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
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?
 
Old 05-24-2017, 03:41 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,523
Blog Entries: 4

Rep: Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831Reputation: 3831
Quote:
Originally Posted by Andrew256 View Post
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.
 
Old 05-24-2017, 05:05 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,767

Rep: Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564
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
 
Old 05-25-2017, 08:06 AM   #12
Andrew256
LQ Newbie
 
Registered: May 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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?
 
Old 05-25-2017, 08:31 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,767

Rep: Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564Reputation: 7564
I would say sudo -E -s (or something similar) is enough, but probably I missed something.
 
Old 05-25-2017, 08:32 AM   #14
cynwulf
Senior Member
 
Registered: Apr 2005
Posts: 2,727

Rep: Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368Reputation: 2368
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] sudo: preserve non-std file descriptors DieZwiebel Linux - Software 1 02-27-2012 09:41 AM
sudo not getting target user's environment variables spyghost Linux - General 6 01-05-2012 07:24 AM
sudo loads wrong user environment wilibird Linux - Software 6 12-31-2009 12:54 PM
sudo requires current user's password robogymnast Linux - General 5 08-04-2008 08:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration