Tuesday, September 19, 2017

Set Java environment variables for all users --- Difference between `su` & `su -`

1.  gedit /etc/profile
2.  add:  export JAVA_HOME=/home/birdonwire/Develop/jdk1.8.0_144/
           export PATH=$JAVA_HOME/bin:$PATH
3.  restart or reopen shell or use source /etc/profile to apply changes immediately

Difference between `su` & `su -`:

Using `su` won't load /etc/profile

birdonwire@win-u5n0t6:~$ su
Password:
root@win-u5n0t6:/home/birdonwire# $PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
root@win-u5n0t6:/home/birdonwire#


However `su -` will load /etc/profile

birdonwire@win-u5n0t6:~$ su -
Password:
root@win-u5n0t6:~# $PATH
-su: /home/birdonwire/Bash/com:/home/birdonwire/Develop/jdk1.8.0_144/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin: No such file or directory
root@win-u5n0t6:~#



Q:   Then how to set persistent environment variables for root or all users?
A:   To create persistent variables that are truly system-wide, you should set them both in /etc/environment and /etc/profile .

If you only set environment variables in /etc/environment, `su - <root or other users>` won't  load /etc/environment, but will load /etc/profile , although `su` will load /etc/environment, won't load /etc/profile . Therefore, you have to set environment variables both in /etc/environment and /etc/profile to make sure all users are covered.


@reference_1_stackoverflow.com
How to set JAVA_HOME in Linux for all users
@reference_2_superuser.com
Adjusting $PATH in /etc/profile does not affect root
How do I set persistent environment variables for root?

No comments:

Post a Comment