如何在我的linux系统下为每个人设置环境变量?

时间:2022-09-24 23:29:26

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

我可以设置一些对所有用户都通用的设置吗?

9 个解决方案

#1


87  

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.com。将自定义环境的内容保存在这些文件中比仅仅编辑/etc/ profile更为整洁。

#2


35  

man 8 pam_env

人8 pam_env

man 5 pam_env.conf

男人5 pam_env.conf

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,并且所有登录服务都有会话,则需要pam_env。在它们各自的/etc/ pamp中。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/ entry /…是这样设置的)。

#3


17  

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

#4


16  

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.

但是任何有csh或tcsh shell的人都不会看到它,而且我认为csh没有全局初始化文件。

#5


5  

Some interesting excerpts from the bash manpage:

来自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.

当bash作为交互式登录shell调用时,或者作为具有——login选项的非交互式shell调用时,它首先从文件/etc/profile读取并执行命令,如果该文件存在的话。读了这个文件后,它查找~/。bash_profile、~ /。bash_login和~ /。配置文件,在该顺序中,并从存在且可读的第一个命令中读取和执行命令。当shell开始禁止这种行为时,可以使用noprofile选项。当启动非登录shell的交互式shell时,bash将从/etc/bash读取并执行命令bashrc,(~ /。如果这些文件存在的话。这可以通过使用-norc选项来抑制。rcfile选项将强制bash从文件中读取和执行命令,而不是/etc/bashbashrc和~ / . 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或/etc/bash。bashrc,这些文件是用于全局设置的正确位置。在里面放一些这样的东西来建立一个环境变量:

export MY_VAR=xxx

#6


3  

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

#7


2  

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),要么是“login”程序(在典型的系统上)。

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 only shell on your system. Pay careful attention to what the /bin/sh symbolic link actually points to. On many systems, this could actually be dash which is (by default, with no special invocation) POSIXLY correct. Therefore, you should take care to modify both defaults, or scripts that start with /bin/sh will not inherit your global defaults. Similarly, take care to avoid syntax that only bash understands when editing both, aka avoiding bashisms.

NB: Bash不是系统上唯一的shell。仔细注意/bin/sh符号链接所指向的内容。在许多系统中,这实际上可能是破折号(默认情况下,没有特殊调用),非常正确。因此,您应该注意修改这两个默认值,否则以/bin/sh开头的脚本不会继承全局默认值。类似地,要注意避免只有bash在编辑这两种语法时才理解的语法,也就是避免使用bashisms。

#8


-3  

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"

如果您正在使用ubuntu,请输入这个命令~/。bashrc(如果您正在使用gedit,您可以键入gedit ~/.bashrc),然后编写您希望在所有终端会话(如export变量=“2015”)中坚持的环境变量

#9


-4  

  1. Download jdk using wget
  2. 下载jdk使用wget
  3. 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
  4. 使用$定位java路径,它将向您显示java实际存储在何处。e /usr/lib/jvm/java-8-openjdk-amd64 / / bin / java
  5. Copy the above path i.e /usr/lib/jvm/java-8-openjdk-amd64/
  6. 复制上面的路径i。e /usr/lib/jvm/java-8-openjdk-amd64 /
  7. Now open the .bashrc using nano editor i.e nano .bashrc
  8. 现在使用nano编辑器i打开。bashrc。e nano . bashrc
  9. 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
  10. 在.bashrc文件i中添加路径。e JAVA_HOME使用export cammand并保存文件i。e导出JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/和导出路径=$JAVA_HOME/bin:$PATH。
  11. Run $ source ~/.bashrc
  12. 运行$ ~ / . bashrc来源
  13. $echo $PATH
  14. 回声路径美元

Your Path is set

你的路径设置

#1


87  

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.com。将自定义环境的内容保存在这些文件中比仅仅编辑/etc/ profile更为整洁。

#2


35  

man 8 pam_env

人8 pam_env

man 5 pam_env.conf

男人5 pam_env.conf

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,并且所有登录服务都有会话,则需要pam_env。在它们各自的/etc/ pamp中。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/ entry /…是这样设置的)。

#3


17  

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

#4


16  

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.

但是任何有csh或tcsh shell的人都不会看到它,而且我认为csh没有全局初始化文件。

#5


5  

Some interesting excerpts from the bash manpage:

来自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.

当bash作为交互式登录shell调用时,或者作为具有——login选项的非交互式shell调用时,它首先从文件/etc/profile读取并执行命令,如果该文件存在的话。读了这个文件后,它查找~/。bash_profile、~ /。bash_login和~ /。配置文件,在该顺序中,并从存在且可读的第一个命令中读取和执行命令。当shell开始禁止这种行为时,可以使用noprofile选项。当启动非登录shell的交互式shell时,bash将从/etc/bash读取并执行命令bashrc,(~ /。如果这些文件存在的话。这可以通过使用-norc选项来抑制。rcfile选项将强制bash从文件中读取和执行命令,而不是/etc/bashbashrc和~ / . 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或/etc/bash。bashrc,这些文件是用于全局设置的正确位置。在里面放一些这样的东西来建立一个环境变量:

export MY_VAR=xxx

#6


3  

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

#7


2  

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),要么是“login”程序(在典型的系统上)。

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 only shell on your system. Pay careful attention to what the /bin/sh symbolic link actually points to. On many systems, this could actually be dash which is (by default, with no special invocation) POSIXLY correct. Therefore, you should take care to modify both defaults, or scripts that start with /bin/sh will not inherit your global defaults. Similarly, take care to avoid syntax that only bash understands when editing both, aka avoiding bashisms.

NB: Bash不是系统上唯一的shell。仔细注意/bin/sh符号链接所指向的内容。在许多系统中,这实际上可能是破折号(默认情况下,没有特殊调用),非常正确。因此,您应该注意修改这两个默认值,否则以/bin/sh开头的脚本不会继承全局默认值。类似地,要注意避免只有bash在编辑这两种语法时才理解的语法,也就是避免使用bashisms。

#8


-3  

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"

如果您正在使用ubuntu,请输入这个命令~/。bashrc(如果您正在使用gedit,您可以键入gedit ~/.bashrc),然后编写您希望在所有终端会话(如export变量=“2015”)中坚持的环境变量

#9


-4  

  1. Download jdk using wget
  2. 下载jdk使用wget
  3. 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
  4. 使用$定位java路径,它将向您显示java实际存储在何处。e /usr/lib/jvm/java-8-openjdk-amd64 / / bin / java
  5. Copy the above path i.e /usr/lib/jvm/java-8-openjdk-amd64/
  6. 复制上面的路径i。e /usr/lib/jvm/java-8-openjdk-amd64 /
  7. Now open the .bashrc using nano editor i.e nano .bashrc
  8. 现在使用nano编辑器i打开。bashrc。e nano . bashrc
  9. 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
  10. 在.bashrc文件i中添加路径。e JAVA_HOME使用export cammand并保存文件i。e导出JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/和导出路径=$JAVA_HOME/bin:$PATH。
  11. Run $ source ~/.bashrc
  12. 运行$ ~ / . bashrc来源
  13. $echo $PATH
  14. 回声路径美元

Your Path is set

你的路径设置