【问题标题】:Toggle for adding a user to and removing the user from sudo group?切换以将用户添加到 sudo 组和从 sudo 组中删除用户?
【发布时间】:2021-08-05 19:46:35
【问题描述】:

我知道如何将用户添加到 sudo 组或如何从中删除他/她:

sudo usermod -aG wheel $1 # $1 represents the username as an argument passed to the command
sudo gpasswd -d $1 wheel 

如何使它成为一个切换?如果用户在 sudo 组中,我会将其从组中删除,否则将其添加。这对于临时授予 sudo 权限很有用。

【问题讨论】:

  • if groups "$1" | cut -d: -f2 | grep -F wheel ; then echo good; else echo bad; fi ?
  • if getent group wheel | grep -Fq "$1"; then sudo gpasswd ...; else sudo usermod ... ; fi

标签: bash centos sudo sudoers


【解决方案1】:

使用if 检查用户当前是否在组中。 另见Check if a user is in a group,我借用了this check

if [[ " $(id -Gn "$1") " == *" wheel "* ]]; then
    sudo gpasswd -d "$1" wheel
else
    sudo usermod -aG wheel "$1 "
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多