GNU/Linux >> Linux の 問題 >  >> Linux

Linux での「su」および「sudo」によるユーザー環境変数

su および sudo を使用すると、別のユーザーでコマンドまたはシェルを実行できます。どのように呼び出されるかによって、環境変数が変化し、コマンドの結果が異なる可能性があります。

「su」と「sudo」の両方で、他のユーザーに代わってコマンドを実行できます。 su の使用は、root によって呼び出された場合を除き、「他の」ユーザーのパスワードを知っていることを意味します。ユーザーができることをあまり制御できません。アクセスが許可されている場合、制限はありません。

sudo では、ユーザーが実行できること、実行できるコマンドを細かく制御できます。 「他の」ユーザーのパスワードを知る必要はありません。権限は構成ファイルで設定されます。

注意 :sudo su [USER] の使用は避ける必要があります。これは、2 つのセキュリティ コンテキストの変更を使用するためです。 sudo -u [USER] を使用することをお勧めします。

su – 代替ユーザーとグループ ID でコマンドを実行

マニュアルページから:

su allows to run commands with substitute user and group ID.

When called without arguments su defaults to running an interactive
shell as root.

For backward compatibility su defaults to not change the current direc‐
tory and to only set the environment variables HOME and SHELL (plus
USER and LOGNAME if the target user is not root). It is recommended to
always use the --login option (instead it's shortcut -) to avoid side
effects caused by mixing environments.

例:

# su opc -c 'echo $PATH'
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# su - opc -c 'echo $PATH'
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/opc/.local/bin:/home/opc/bin

「su opc」では、コマンドの実行に使用される環境変数は、元の環境変数 (この場合はユーザー root 環境) です。コマンドが「」で呼び出された場合 」または「–ログイン 」環境は「TERM」を除いて「opc」ユーザーです。

マニュアルページで説明されているように:

-, -l, --login
  Starts the shell as login shell with an environment similar to a
  real login:

  o clears all environment variables except for TERM

  o initializes the environment variables HOME, SHELL,
  USER, LOGNAME, PATH

  o changes to the target user's home directory

  o sets argv[0] of the shell to '-' in order to make the
  shell a login shell

sudo – 別のユーザーとしてコマンドを実行

マニュアルページから:

sudo allows a permitted user to execute a command as the superuser or
another user, as specified by the security policy. The invoking user's
real (not effective) user ID is used to determine the user name with
which to query the security policy.

例:

# sudo -u opc bash -c 'echo $PATH'
/sbin:/bin:/usr/sbin:/usr/bin
# sudo -i -u opc bash -c 'echo $PATH'
/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/home/opc/.local/bin:/home/opc/bin

「sudo」では、/etc/sudoers で定義されているように、環境変数が元のセッションから「sudo」セッションに渡されます。

Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

定義された変数は保持されます。

「sudo -i」を使用すると、一部の変数がリセットされる可能性があります:

-i, --login
    Run the shell specified by the target user's password data‐
    base entry as a login shell. This means that login-specific
    resource files such as .profile, .bash_profile or .login will
    be read by the shell. If a command is specified, it is
    passed to the shell for execution via the shell's -c option.
    If no command is specified, an interactive shell is executed.
    sudo attempts to change to that user's home directory before
    running the shell. The command is run with an environment
    similar to the one a user would receive at log in. Note that
    most shells behave differently when a command is specified as
    compared to an interactive session; consult the shell's man‐
    ual for details. The Command environment section in the
    sudoers(5) manual documents how the -i option affects the
    environment in which a command is run when the sudoers policy
    is in use.

例、デフォルトの /etc/sudoers から、PS1 変数が保持されます:

# PS1="%: " sudo -u opc bash
%:
# PS1="%: " sudo -i -u opc bash
[opc@[HOSTNAME] ~]$

「-i」を使用すると、ログイン リソース ファイルが実行され、PS1 変数が /etc/bashrc に設定されているようにリセットされます。


Linux
  1. GNUacctを使用したLinuxでのユーザーステータスとアクティビティの監視

  2. Linuxで環境変数とシェル変数を設定/作成する方法

  3. Linuxでのsudoの8つのベストプラクティス–sudoの推奨事項と禁止事項

  1. RockyLinuxおよびCentOSでSudoユーザーを作成する方法

  2. Linux での sudo コマンドの例

  3. Linux 環境変数の設定

  1. Linuxでローカル、ユーザー、およびシステム全体の環境変数を設定および設定解除する方法

  2. Linuxでユーザーとグループを操作する方法

  3. ドキュメントのroot権限を持つLinuxユーザーを追加します