在任何 shell 或 sudo、pam.d 更改之前,Linux 中设置的用户 PATH 在哪里?

Tom*_*Tom 5 linux environment-variables

我注意到,在 Raspbian 中,如果我在不使用 shell 的情况下通过 ssh 运行命令,我会得到以下信息$PATH

$ ssh pi@raspberrypi env | grep PATH
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
Run Code Online (Sandbox Code Playgroud)

我找不到这条路径的实际设置位置。

我假设 或/etc/profile/etc/bashrc没有来源,因为它不是登录,也不是交互式 shell。看起来它可能会访问bashrc,但不会走得太远;

# cat /etc/bash.bashrc 
# System-wide .bashrc file for interactive bash(1) shells.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return
Run Code Online (Sandbox Code Playgroud)

pam
但是看起来会话将通过 pam 进行处理,并且表明/etc/pam.d/sshd/etc/environment/etc/security/pam_env.conf用于/etc/default/locale设置环境变量;

# Read environment variables from /etc/environment and
# /etc/security/pam_env.conf.
session    required     pam_env.so # [1]
# In Debian 4.0 (etch), locale-related environment variables were moved to
# /etc/default/locale, so read that as well.
session    required     pam_env.so user_readenv=1 envfile=/etc/default/locale
Run Code Online (Sandbox Code Playgroud)

但是,在这台计算机上,这些文件都不包含任何对设置路径的引用。例如

# cat /etc/default/locale
#  File generated by update-locale
LANG=en_GB.UTF-8

# cat /etc/environment
root@raspberrypi:~# 

# cat /etc/security/pam_env.conf | grep "^[^#;]"
root@raspberrypi:~# 
Run Code Online (Sandbox Code Playgroud)

login.defs
我唯一能找到类似于的路径集的地方

PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
Run Code Online (Sandbox Code Playgroud)

位于 /etc/login.defs 文件中;

login.defs:ENV_PATH        PATH=/usr/local/sbin:/usr/local/bin: \
/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
Run Code Online (Sandbox Code Playgroud)

看起来后一条路径的元素顺序正确,但组件sbin被删除。然而,文档login.defs表明该文件大部分已被弃用

过去由影子密码套件提供的大部分功能现在由 PAM 处理。因此,passwd(1) 不再使用 /etc/login.defs,login(1) 和 su(1) 也较少使用 /etc/login.defs。请参阅相应的 PAM 配置文件。

/home/pi/.bashrc
pi 用户是使用一些框架文件创建的,但对于非交互式、非登录 shell 来说,它们似乎都不会被读取;

# cat /home/pi/.bashrc 
# ~/.bashrc: executed by bash(1) for non-login shells.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
Run Code Online (Sandbox Code Playgroud)

/home/pi/.profile

# cat /home/pi/.profile 
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
Run Code Online (Sandbox Code Playgroud)

原始的 $PATH 是否硬编码到某个二进制文件中,或者我遗漏了什么?

Gil*_*il' 4

它被硬编码在sshd二进制文件中。

$ strings /usr/sbin/sshd |grep /usr/local/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
Run Code Online (Sandbox Code Playgroud)

在 OpenSSH 可移植代码中,这对于 root 和非 root_PATH_STDPATH都是默认的。/usr/bin:/bin:/usr/sbin:/sbinDebian构建规则将其设置/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin为 root、/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesUbuntu 上的非 root、/usr/local/bin:/usr/bin:/bin安装实时媒体 (udeb) 以及/usr/local/bin:/usr/bin:/bin:/usr/games其他情况下的非 root。