为什么新用户的 $PATH 中不包含`/sbin`?

Cha*_*ell 2 ifconfig paths

ifconfig位于/sbin/ifconfigifconfig我在 root 用户中发出命令没有问题。

当我创建一个新用户时,问题就开始了。

root@Ubuntu:~# useradd -m user
root@Ubuntu:~#

root@Ubuntu:~# passwd user
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@Ubuntu:~# 

root@Ubuntu:~# su user
user@Ubuntu:/root$ id
uid=1003(user) gid=1003(user) groups=1003(user)
user@Ubuntu:/root$ 
Run Code Online (Sandbox Code Playgroud)

ifconfig在新用户中找不到。

user@Ubuntu:~$ ifconfig
-su: ifconfig: command not found
user@Ubuntu:~$ 
Run Code Online (Sandbox Code Playgroud)

临时解决方法是从完整路径执行它/sbin/ifconfig

user@Ubuntu:~$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
user@Ubuntu:~$ 

user@Ubuntu:~$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr AA:AA:AA:AA:AA:AA
          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0
user@Ubuntu:~$
Run Code Online (Sandbox Code Playgroud)

ifconfig未找到,因为/sbin在新用户环境中不再配置。

user@Ubuntu:~$ echo $PATH
/home/user/bin:/home/user/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
user@Ubuntu:~$ 

user@Ubuntu:~$ echo $PATH | grep sbin
user@Ubuntu:~$ 
Run Code Online (Sandbox Code Playgroud)

/sbin默认情况下,我在 root 用户上没有看到此问题$PATH

root@Ubuntu:~# ifconfig 
eth0      Link encap:Ethernet  HWaddr AA:AA:AA:AA:AA:AA
          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0

root@Ubuntu:~# echo $PATH | grep sbin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
root@Ubuntu:~# 
Run Code Online (Sandbox Code Playgroud)
  1. 问题是为什么/sbin默认不包含在内?这不是让用户的事情变得更加困难吗?

  2. 如何确保/sbin自动添加而$PATH不是为每个用户手动修改?

Evg*_*nuk 5

/sbin目录用于系统维护和/或管理工具。因此,只有管理员才能使用它,并且您的 $PATH 中没有此目录。您可以在文件系统层次结构标准FHS中阅读更多内容。

$PATH变量添加新值的正确位置是用户~/.profile文件或系统/etc/profile文件。通常已经有一行,$PATH因此您只需添加/sbin到行的开头即可。或者只需在文件末尾添加这一行:

PATH="/sbin:$PATH"
Run Code Online (Sandbox Code Playgroud)

您也可以编辑/etc/skel/.profile文件。当您创建新文件时,该文件将被复制到用户的主目录。但如果您想为所有用户提供此功能,请更改/etc/profile.