LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Having a problem understanding sudo and setting environmental variables (https://www.linuxquestions.org/questions/linux-newbie-8/having-a-problem-understanding-sudo-and-setting-environmental-variables-4175674630/)

pwjohnston 05-05-2020 01:19 PM

Having a problem understanding sudo and setting environmental variables
 
Hello,

I'm having difficulty understanding sudo and environmental variables.

I'm currently setting up vagrant on a host so I can play with adding and removing linux VMs and try and learn Linux better. Host is CentOS 7.

When I run
Code:

$ sudo vagrant up
boxes are stored in
Code:

# ll /root/.vagrant.d/boxes
total 0
drwxr-xr-x. 3 root root 37 May  5 13:40 minimal-VAGRANTSLASH-centos7

The problem with this, however, is I only have 25 GB on that partition.
Code:

$ df -h /
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  25G  11G  14G  45% /

I have set up var to store data
Code:

$df -h /var
Filesystem                              Size  Used Avail Use% Mounted on
/dev/mapper/vg--1tb--mirror-var--mirror  197G  43G  145G  23% /var

Per this post I found that I can set the environmental variable for vagrant to put the Vagrant boxes somewhere else.
https://stackoverflow.com/questions/...he-home-folder

Code:

export VAGRANT_HOME=/path/to/vagrant
So I did
Code:

# cp -r ./.vagrant.d/ /var
then deleted the box file from
/root/.vagrant.d/boxes

and
Code:

$ export VAGRANT_HOME=/var/.vagrant.d
run
Code:

$ sudo vagrant up
and the box is still saved in
/root/.vagrant.d/boxes

So I'm thinking maybe sudo runs a command as root and not just with root permission? I do the same process over again

Code:

$su -
# vi ~/.bash_profile
add the line
export VAGRANT_HOME=/var/.vagrant.d
# cat ~/.bash_profile | grep VAGRANT
export VAGRANT_HOME=/var/.vagrant.d/

# source ~/.bash_profile

drop back into my user profile

Code:

$ sudo vagrant up
boxes are still being saved to

Code:

$ sudo ls -lah /root/.vagrant.d/boxes
[sudo] password for langenoir:
total 0
drwxr-xr-x. 3 root root  42 May  5 14:07 .
drwxr-xr-x. 7 root root 119 May  5 14:07 ..
drwxr-xr-x. 3 root root  37 May  5 14:07 minimal-VAGRANTSLASH-centos7

I really don't understand what is going on here. I keep reading that sudo elevates user permissions. I thought that means it gives my user root permissions temporarily to run a command. I'm guessing that is wrong.

shruggy 05-05-2020 01:39 PM

By default, sudo retains only a few select variables from the user environment. To change this, see
1) the sudo option -E / --preserve-env;
2) description of the env_keep setting in sudoers(5).

Turbocapitalist 05-05-2020 01:42 PM

Close. sudo runs the designated command as another user entirely for the duration of that command. If no use is designated at runtime then the default is root. You'll want to look more closely at some of the options you pass to sudo, such as --preserve-env or --preserve-env=... where ... is the list of variables to pass over to the new user. You may have to edit /etc/sudoers to list the variables allowed.

Code:

...
Defaults        env_keep+="FOO"
Defaults        env_keep+="BAR"
...


pwjohnston 05-05-2020 02:46 PM

That -E environment option seems to be what I was missing.

Code:

$ printenv | grep VAGRANT
VAGRANT_HOME=/var/.vagrant.d/

$ sudo -E vagrant up

$ ls -lah /var/.vagrant.d/boxes/
total 12K
drwxr-xr-x. 3 root root 4.0K May  5 15:40 .
drwxrwxrwx. 7 root root 4.0K May  5 15:39 ..
drwxr-xr-x. 3 root root 4.0K May  5 15:40 minimal-VAGRANTSLASH-centos7

Thank you shruggy and Turbocapitalist.


All times are GMT -5. The time now is 05:01 AM.