当前位置: 首页>>技术问答>>正文


设置PATH使其适用于所有用户,包括root/sudo

, ,

问题描述

How do I set PATH variables for all users on a server?中的指令用于为所有’normal’用户设置PATH。但是,如果我执行sudo -s,然后执行printenv PATH,则不会显示更新的路径。我已经看了例如Setting TeX Live path for root,但这对我来说似乎没有意义,也许因为我来自Windows背景。有没有办法设置真正的system-wide路径,这样条目绝对是在系统上运行的每个进程继承的?

最佳解决方法

更新:

设置全局环境变量仍应在/etc/environment中执行,但正如您所指出的,使用sudo -s具有这些变量的效果是”vanished”。

原因是sudo有重置环境和设置安全路径的策略。它默认启用:

/etc/sudoers:

Defaults  env_reset
Defaults  secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

因此,路径的/etc/environment中设置的任何内容都被sudo覆盖。

sudoers的手册页指出:

   env_reset       If set, sudo will reset the environment to only contain
                   the LOGNAME, MAIL, SHELL, USER, USERNAME and the SUDO_*
                   variables.  Any variables in the caller's environment
                   that match the env_keep and env_check lists are then
                   added.  The default contents of the env_keep and
                   env_check lists are displayed when sudo is run by root
                   with the -V option.  If the secure_path option is set,
                   its value will be used for the PATH environment
                   variable.  This flag is on by default.

作为解决方法,您可以使用sudo su,它将为shell提供root权限,但包含正确的PATH。


原始答案

您应该在/etc/environment中进行设置。

尝试sudo YOUR_TEXT_EDITOR /etc/environment(确保先创建备份)。

有关更多信息:EnvironmentVariables

System-wide environment variables

Environment variable settings that affect the system as a whole (rather then just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

/etc/environment – This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Not recommended:

/etc/profile – This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.

/etc/bash.bashrc – This is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/1962.html,未经允许,请勿转载。