大网数据广告活动
服务器运维问答

Allow users to use -D (--chdir) option with sudo

· 52CE 我爱测

I want to make user bob able to use the -D option in sudo (like sudo -D /home bash which would open a root shell on /home ). How can I do this and what are the security implications to this?

问题原文

原帖链接 · luihum · CC BY-SA 4.0

I want to make user bob able to use the -D option in sudo (like sudo -D /home bash which would open a root shell on /home).

How can I do this and what are the security implications to this?

已采纳解答

原帖链接 · HBruijn · CC BY-SA 4.0

That option is relatively new (the Changelog entry that introduced the feature is from 2020-09-01 and Ubuntu support was only introduced starting Ubuntu 21)

man sudo

 -D directory, --chdir=directory
             Run the command in the specified directory instead of the current working
             directory.  The security policy may return an error if the user does not have
             permission to specify the working directory.

The associated settings in /etc/sudoers are:

 runcwd        If set, sudo will use this value for the working directory when running a
               command.  The special value “*” will allow the user to specify the working
               directory via sudo's -D option.  See the Chdir_Spec section for more details.

  ....
 Chdir_Spec

 The working directory that the command will be run in can be specified using the CWD
 setting.  The directory must be a fully-qualified path name beginning with a ‘/’ or ‘~’
 character, or the special value “*”.  A value of “*” indicates that the user may specify the
 working directory by running sudo with the -D option.  By default, commands are run from the
 invoking user's current working directory, unless the -i option is given.  Path names of the
 form ~user/path/name are interpreted as being relative to the named user's home directory.
 If the user name is omitted, the path will be relative to the runas user's home directory.

 This setting is only supported by version 1.9.3 or higher

I don't have a suitable system at hand, but I think that you can allow users alice and bob permission to use -D with:

# /etc/sudoers or include from /etc/sudoers.d/
...
# Modify the defaults for the members of User_Alias USERLIST

Defaults:USERLIST   runcwd=*

# User alias specification

User_Alias  USERLIST = alice, bob

As to the security implications: I don't feel qualified to say if there are any risks, or if it offers new avenues of abuse. I couldn't find any recommendations in favour or against runcwd=* yet with a cursory search either.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。