如何在我的linux系统下为每个人设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1641477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set environment variable for everyone under my linux system?
提问by TIMEX
Can I have certain settings that are universal for all my users?
我可以拥有对所有用户通用的某些设置吗?
采纳答案by Kieron
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
.
和/etc/profile
其他人提到的一样,一些 Linux 系统现在使用目录/etc/profile.d/
;.sh
那里的任何文件都将来自/etc/profile
. 将自定义环境内容保存在这些文件中比仅编辑/etc/profile
.
回答by DigitalRoss
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.
令人惊讶的是,Unix 和 Linux 实际上没有设置全局环境变量的地方。您能做的最好的事情是安排任何特定的 shell 进行特定于站点的初始化。
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.
如果你把它放进去/etc/profile
,这将为大多数兼容 posix 的 shell 用户处理事情。对于非关键目的,这可能“足够好”。
But anyone with a csh
or tcsh
shell won't see it, and I don't believe csh
has a global initialization file.
但是任何有 acsh
或tcsh
shell 的人都不会看到它,而且我不相信csh
有全局初始化文件。
回答by Pascal Thivent
Some interesting excerpts from the bash manpage:
bash 联机帮助页中的一些有趣摘录:
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
.
当 bash 作为交互式登录 shell 或作为具有
--login
选项的非交互式 shell 调用时,它首先从文件中读取并执行命令/etc/profile
(如果该文件存在)。读取该文件后,它会按该顺序查找~/.bash_profile
、~/.bash_login
、 和~/.profile
,并从第一个存在且可读的命令中读取和执行命令。--noprofile
当 shell 启动时可以使用该选项来禁止这种行为。
...
当一个不是登录 shell 的交互式 shell 启动时,bash 读取并执行来自/etc/bash.bashrc
和 的命令~/.bashrc
(如果这些文件存在)。这可以通过使用--norc
选项来禁止。这--rcfile
file 选项将强制 bash 从 file 而不是/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:
所以看看/etc/profile
or /etc/bash.bashrc
,这些文件是全局设置的正确位置。将这样的内容放入其中以设置环境变量:
export MY_VAR=xxx
回答by Tim Post
Every process running under the Linux kernel receives its own, unique environment that it inherits from its parent. In this case, the parent will be either a shell itself (spawning a sub shell), or the 'login' program (on a typical system).
在 Linux 内核下运行的每个进程都有自己独特的环境,它从其父进程继承而来。在这种情况下,父进程要么是一个 shell(产生一个子 shell),要么是“登录”程序(在一个典型的系统上)。
As each process' environment is protected, there is no way to 'inject' an environmental variable to every running process, so even if you modify the default shell .rc / profile, it won't go into effect until each process exits and reloads its start up settings.
由于每个进程的环境都受到保护,因此无法为每个正在运行的进程“注入”环境变量,因此即使您修改了默认的 shell .rc/profile,它也不会生效,直到每个进程退出并重新加载它的启动设置。
Look in /etc/ to modify the default start up variables for any particular shell. Just realize that users can (and often do) change them in their individual settings.
查看 /etc/ 以修改任何特定 shell 的默认启动变量。只需意识到用户可以(并且经常这样做)在他们的个人设置中更改它们。
Unix is designed to obey the user, within limits.
Unix 旨在在限制范围内服从用户。
NB: Bash is not the onlyshell on your system. Pay careful attention to what the /bin/sh symbolic link actually points to. On many systems, this could actually be dashwhich is (by default, with no special invocation) POSIXLY correct. Therefore, you should take care to modify bothdefaults, or scripts that start with /bin/sh will not inherit your global defaults. Similarly, take care to avoid syntax that only bashunderstands when editing both, aka avoiding bashisms
.
注意:Bash 不是您系统上唯一的shell。请仔细注意 /bin/sh 符号链接实际指向的内容。在许多系统上,这实际上可能是dash,这是(默认情况下,没有特殊调用)POSIXLY 正确的。因此,您应该注意修改这两个默认值,否则以 /bin/sh 开头的脚本将不会继承您的全局默认值。同样,在编辑两者时注意避免只有bash理解的语法,也就是avoiding bashisms
.
回答by ephemient
If all login services use PAM, and all login services have session required pam_env.so
in their respective /etc/pam.d/*
configuration files, then all login sessions will have some environment variables set as specified in pam_env
's configuration file.
如果所有登录服务都使用PAM,并且所有登录服务都session required pam_env.so
在其各自的/etc/pam.d/*
配置文件中,那么所有登录会话都将有一些环境变量设置为pam_env
的配置文件中指定的。
On most modern Linux distributions, this is all there by default -- just add your desired global environment variables to /etc/security/pam_env.conf
.
在大多数现代 Linux 发行版上,默认情况下这一切都在那里——只需将所需的全局环境变量添加到/etc/security/pam_env.conf
.
This works regardless of the user's shell, and works for graphical logins too (if xdm/kdm/gdm/entrance/… is set up like this).
这与用户的 shell 无关,也适用于图形登录(如果 xdm/kdm/gdm/entrance/... 像这样设置)。
回答by ephemient
Using PAM is execellent.
使用 PAM 非常好。
# 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
回答by tulipcomp1
If your LinuxOS has this file:
如果你的 LinuxOS 有这个文件:
/etc/environment
You can use it to permanently set environmental variables for all users.
您可以使用它为所有用户永久设置环境变量。
Extracted from: http://www.sysadmit.com/2016/04/linux-variables-de-entorno-permanentes.html
摘自:http: //www.sysadmit.com/2016/04/linux-variables-de-entorno-permanentes.html