How to set environment variable for everyone under

2019-01-04 19:48发布

Can I have certain settings that are universal for all my users?

标签: linux unix bash
9条回答
乱世女痞
2楼-- · 2019-01-04 20:01

If you are working on ubuntu type this command ~/.bashrc (if you are using gedit you could type gedit ~/.bashrc) then write the environment variable that you want to persist across all terminal sessions eg export variable="2015"

查看更多
小情绪 Triste *
3楼-- · 2019-01-04 20:04
  1. Download jdk using wget
  2. locate the java path using $which java it will show you where JAVA is actually stored i.e /usr/lib/jvm/java-8-openjdk-amd64//bin/java
  3. Copy the above path i.e /usr/lib/jvm/java-8-openjdk-amd64/
  4. Now open the .bashrc using nano editor i.e nano .bashrc
  5. Add the path in .bashrc file i.e JAVA_HOME using export cammand and save the file i.e export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ and export PATH=$JAVA_HOME/bin:$PATH
  6. Run $ source ~/.bashrc
  7. $echo $PATH

Your Path is set

查看更多
Deceive 欺骗
4楼-- · 2019-01-04 20:06

Some interesting excerpts from the bash manpage:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

So have a look at /etc/profile or /etc/bash.bashrc, these files are the right places for global settings. Put something like this in them to set up an environement variable:

export MY_VAR=xxx
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-04 20:08

Amazingly, Unix and Linux do not actually have a place to set global environment variables. The best you can do is arrange for any specific shell to have a site-specific initialization.

If you put it in /etc/profile, that will take care of things for most posix-compatible shell users. This is probably "good enough" for non-critical purposes.

But anyone with a csh or tcsh shell won't see it, and I don't believe csh has a global initialization file.

查看更多
孤傲高冷的网名
6楼-- · 2019-01-04 20:09

As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It's slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.

查看更多
SAY GOODBYE
7楼-- · 2019-01-04 20:11

Using PAM is execellent.

# modify the display PAM
$ cat /etc/security/pam_env.conf 
# BEFORE: $ export DISPLAY=:0.0 && python /var/tmp/myproject/click.py &
# AFTER : $ python $abc/click.py &
DISPLAY  DEFAULT=${REMOTEHOST}:0.0 OVERRIDE=${DISPLAY}
abc   DEFAULT=/var/tmp/myproject
查看更多
登录 后发表回答