Tôi tìm biến môi trường SUDO_COMMAND ở đâu?


8

Tôi hiện đang tìm hiểu nền tảng của Linux với Ubuntu và có một hoạt động nhỏ mà tôi cần tìm thông tin về các biến môi trường. Tôi đã tìm thấy 6/7 thông tin nhưng không thể tìm thấy SUDO_COMMAND. đây là cách danh sách đi:

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

Tôi nhận thấy rằng thông tin có thứ tự và SUDO_COMMANDgiữa người dùng và pwd. tôi đã phạm sai lầm ở đâu đó?


1
Bạn có đặc quyền sudo?
ravery

Câu trả lời:


11

SUDO_COMMANDlà một biến môi trường được thiết lập sudochỉ trong môi trường của quy trình được bắt đầu bởi nó (và được kế thừa bởi bất kỳ quy trình con nào). Nếu bạn chạy sudo some-command arg1 arg2, sau đó SUDO_COMMANDsẽ chứa đường dẫn tuyệt đối đến some-commandarg1 arg2. Nếu bạn đã chạy sudo -shoặc sudo -i, thì biến sẽ được đặt thành shell đã được bắt đầu. Trong mọi trường hợp, bạn có thể sẽ không thấy nó bên ngoài cây quy trình bắt đầu bằng sudo.

Ví dụ:

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

Hoặc là:

$ 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

Tôi nhận thấy rằng thông tin đến theo thứ tự

Tôi không biết đó là lệnh bạn đang sử dụng, nhưng bạn không thể dựa vào đầu ra của set, declare, envhoặc printenvlà theo một trật tự.


Và tôi nghĩ rằng sudo echo $SUDO_COMMANDcó thể được sử dụng để tạo ra một vòng lặp vô tận Từ :(
tráng miệng

5

Đây SUDO_COMMANDlà một biến môi trường được đặt thành lệnh được chạy bởi sudo .

Như được đề cập bởi @muru - nếu sudothực thi shell mới - biến môi trường này sẽ được hiển thị trong shell mới này

Thêm thông tin

man sudo cung cấp các chi tiết sau:

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.

Tôi không nghĩ rằng ví dụ của bạn thực sự có thể tái tạo. SUDO_COMMANDchỉ tồn tại trong môi trường bắt đầu bởi sudo, nếu SUDO_COMMANDthực sự ở đâu /bin/ls, thì bạn sẽ không có vỏ để chạy echo $SUDO_COMMAND.
muru
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.