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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-23-2017, 12:09 PM
|
#1
|
LQ Newbie
Registered: May 2017
Posts: 12
Rep:
|
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.
|
|
|
05-23-2017, 12:21 PM
|
#2
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
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.
|
|
|
05-23-2017, 12:25 PM
|
#3
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
You can set some environment related settings in the sudoers file. But you'll just have to use sudo with each command.
|
|
|
05-23-2017, 12:32 PM
|
#4
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.4
Posts: 5,806
|
Quote:
Originally Posted by Andrew256
So, is there a way to switch to root with 'su' but preserve my own configs?
|
AFAIK, the short answer is: no.
|
|
|
05-23-2017, 12:36 PM
|
#5
|
Member
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 286
Rep:
|
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.
|
05-23-2017, 04:23 PM
|
#6
|
LQ Newbie
Registered: May 2017
Posts: 12
Original Poster
Rep:
|
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
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.
|
|
|
05-23-2017, 04:57 PM
|
#7
|
LQ Newbie
Registered: May 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by Andrew256
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.
|
|
|
05-23-2017, 09:46 PM
|
#8
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
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!
|
|
|
05-24-2017, 03:35 AM
|
#9
|
LQ Newbie
Registered: May 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by AwesomeMachine
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?
|
|
|
05-24-2017, 03:41 AM
|
#10
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,523
|
Quote:
Originally Posted by Andrew256
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.
|
|
|
05-24-2017, 05:05 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,767
|
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
|
|
|
05-25-2017, 08:06 AM
|
#12
|
LQ Newbie
Registered: May 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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:
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?
|
|
|
05-25-2017, 08:31 AM
|
#13
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,767
|
I would say sudo -E -s (or something similar) is enough, but probably I missed something.
|
|
|
05-25-2017, 08:32 AM
|
#14
|
Senior Member
Registered: Apr 2005
Posts: 2,727
|
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 02:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|