在哪里可以找到SUDO_COMMAND环境变量?


8

我目前正在学习Ubuntu的Linux基础,并且有一些活动需要查找有关环境变量的信息。我已经找到了6/7信息,但找不到SUDO_COMMAND。清单是这样的:

SHELL=/bin/bash
USER=student
SUDO_COMMAND=
PWD=/home
HOME=/home/student
LOGNAME=student
OLDPWD=/home/student 

我注意到信息是按顺序排列的,并且SUDO_COMMAND在用户和密码之间。我在某个地方犯了错误吗?


1
你有sudo特权吗?
ravery

Answers:


11

SUDO_COMMAND是一个环境变量,sudo仅在由它启动(并由任何子进程继承)的进程的环境中设置。如果你运行sudo some-command arg1 arg2,然后SUDO_COMMAND将包含的绝对路径some-command,和arg1 arg2。如果您运行sudo -ssudo -i,则变量将设置为启动的外壳。无论如何,您可能不会在由开头的进程树之外看到它sudo

例如:

$ sudo sh -c 'echo $SUDO_COMMAND'
/bin/sh -c echo $SUDO_COMMAND

要么:

$ sudo env
HOME=/home/muru
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
TERM=xterm-256color
LANG=en_US.UTF-8
LC_NUMERIC=en_GB.UTF-8
LC_TIME=en_GB.UTF-8
LC_MONETARY=en_GB.UTF-8
LC_PAPER=en_GB.UTF-8
LC_NAME=en_GB.UTF-8
LC_ADDRESS=en_GB.UTF-8
LC_TELEPHONE=en_GB.UTF-8
LC_MEASUREMENT=en_GB.UTF-8
LC_IDENTIFICATION=en_GB.UTF-8
MAIL=/var/mail/root
LOGNAME=root
USER=root
USERNAME=root
SHELL=/bin/bash
SUDO_COMMAND=/usr/bin/env
SUDO_USER=muru
SUDO_UID=1000
SUDO_GID=1000

我注意到信息按顺序排列

我不知道哪个命令你使用,但你不能依靠的输出setdeclareenv或者printenv是在一些命令。


而且我认为sudo echo $SUDO_COMMAND可以用来创建不定式循环……:(
甜点

5

SUDO_COMMAND是一个环境变量,其设置为须藤运行命令

正如@muru所提到的-如果sudo执行新的shell- 这个环境变量将显示在这个新的shell中

更多信息

man sudo 提供以下详细信息:

ENVIRONMENT
 sudo utilizes the following environment variables.  The security policy
 has control over the actual content of the command's environment.

 EDITOR           Default editor to use in -e (sudoedit) mode if neither
                  SUDO_EDITOR nor VISUAL is set.

 MAIL             Set to the mail spool of the target user when the -i
                  option is specified or when env_reset is enabled in
                  sudoers (unless MAIL is present in the env_keep list).

 HOME             Set to the home directory of the target user when the -i
                  or -H options are specified, when the -s option is
                  specified and set_home is set in sudoers, when
                  always_set_home is enabled in sudoers, or when env_reset
                  is enabled in sudoers and HOME is not present in the
                  env_keep list.

 LOGNAME          Set to the login name of the target user when the -i
                  option is specified, when the set_logname option is
                  enabled in sudoers or when the env_reset option is
                  enabled in sudoers (unless LOGNAME is present in the
                  env_keep list).

 PATH             May be overridden by the security policy.

 SHELL            Used to determine shell to run with -s option.

 SUDO_ASKPASS     Specifies the path to a helper program used to read the
                  password if no terminal is available or if the -A option
                  is specified.

 SUDO_COMMAND     Set to the command run by sudo.

我认为您的示例实际上是不可复制的。SUDO_COMMAND它仅仅存在于开始的环境中sudo,如果SUDO_COMMAND实际在哪里/bin/ls,那么你就不会运行一个shell echo $SUDO_COMMAND
muru
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.