Linux 将用户添加到组但在运行“id”时不反映

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7537197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:20:24  来源:igfitidea点击:

Add user to group but not reflected when run "id"

linuxunix

提问by matt_k

R creates a group called staff and I want to be able to update packages without starting R as sudo. So I added myself to staff using:

R 创建了一个名为 staff 的组,我希望能够在不将 R 启动为 sudo 的情况下更新包。所以我使用以下方法将自己添加到员工中:

sudo usermod -G adm,dialout,cdrom,plugdev,lpadmin,admin,sambashare,staff matt

(side question is there a way to add yourself to a group without listing every other group you're a member of?)

(附带问题有没有一种方法可以将自己添加到群组中,而无需列出您所属的所有其他群组?)

If i check /etc/groups i see

如果我检查 /etc/groups 我看到

staff:x:50:matt

and the same for /etc/shadow

和 /etc/shadow 一样

staff:*::matt

however if i run groups or id i'm not a member of staff. Also, I can't make changes to anything in /usr/local/lib/R.

但是,如果我经营团体或身份证,我就不是工作人员。此外,我无法对 /usr/local/lib/R 中的任何内容进行更改。

采纳答案by freiheit

Did you log the "matt" account out and back in after running the sudo usermodcommand? Changes to the groups a user is in under unix only take affect at login time.

运行sudo usermod命令后,您是否注销了“matt”帐户并重新登录?在 unix 下对用户所在组的更改仅在登录时生效。

回答by lxop

In answer to your side question, yes you can add a user to a group without listing them all. If you run a Debian based system, you can do it with

回答您的附带问题,是的,您可以将用户添加到组中,而无需列出所有用户。如果您运行基于 Debian 的系统,则可以使用

sudo adduser matt staff

sudo adduser matt staff

The adduserutility is just a friendly wrapper around useradd/usermod etc.

adduser实用程序只是围绕 useradd/usermod 等的友好包装。

If you don't have the adduserutility, you can still do it with usermod:

如果您没有该adduser实用程序,您仍然可以使用 usermod 来完成:

sudo usermod -a -G staff matt

sudo usermod -a -G staff matt

The -aflag means append (as opposed to overwrite).

-a标志表示追加(而不是覆盖)。

回答by voidstate

I know the original question is for Linux but OSX users can do the same with this command:

我知道最初的问题是针对 Linux,但 OSX 用户可以使用以下命令执行相同操作:

sudo dseditgroup -o edit -a newusertoadd -t user grouptobeaddedto

回答by kiiwii

回答by avivamg

Explanation: The operation succeeded - that's why your name appears in the right linux files on /etc/passwd& /etc/groupbut as soon as you open a new terminal process the bash will be updated with this setting and you can perform id mattas well.

说明:操作成功 - 这就是为什么您的名字出现在/etc/passwd&上正确的 linux 文件中,/etc/group但是一旦您打开一个新的终端进程,bash 将使用此设置进行更新,您也可以执行id matt

Clarification: You added yourself to additional group so you should have used append option -a(and not editing the all bunch of groups names to your user).

澄清:您将自己添加到其他组,因此您应该使用 append 选项-a(而不是将所有组名称编辑给您的用户)。

sudo usermod -aG staff matt