Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  62] [ 0]  / answers: 1 / hits: 117787  / 1 Year ago, sun, april 23, 2023, 9:11:14

To create persistent environment variables, I add the script file to /etc/profile.d directory, for example:




# my script
export MY_VAR=var_value


This works properly for current user:




alex@alex-64:~$ echo $MY_VAR
var_value


Now I need the same environment variables for the root user, but /etc/profile.d script doesn't work for root:




alex@alex-64:~$ echo $MY_VAR
var_value
alex@alex-64:~$ sudo su
root@alex-64:/home/alex# echo $MY_VAR

root@alex-64:/home/alex#


How can I set the same variables for the root?


More From » root

 Answers
7

sudo does not normally preserve local environment variables. You should use it with the -E switch to do so, i.e. sudo -E su will preserve $MYVAR for root.



Alternatively, to create persistent variables that are truly system-wide, you should set them in /etc/environment.


[#36959] Monday, April 24, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ithriv

Total Points: 46
Total Questions: 115
Total Answers: 96

Location: Malaysia
Member since Wed, May 11, 2022
2 Years ago
ithriv questions
Tue, Mar 14, 23, 14:31, 1 Year ago
Wed, Oct 5, 22, 02:59, 2 Years ago
Fri, Mar 11, 22, 10:12, 2 Years ago
Thu, May 11, 23, 10:55, 1 Year ago
Sun, Sep 26, 21, 06:47, 3 Years ago
;