newgrp: Changing the Current User’s Group

Overview:
newgrp is a command used to change the group associated with the current user. It allows the user to switch to a different group, affecting file permissions and other operations. This command operates within the context of the currently logged-in user, meaning it cannot be used to change the group for other users.

Syntax:

newgrp [group_name]

Details:
newgrp works similarly to the login command. It allows the user to log in again under the same account but with a different group. The primary effect of running newgrp is that it switches the user’s effective group to the specified one, which will influence operations such as file access permissions.
If no group is specified, newgrp logs the user into the default group associated with the user’s username.
To use newgrp to switch groups, the user must be a member of the specified group. Otherwise, access to that group will be denied. Once a user has switched groups via newgrp, they can revert to their original group by using the exit command to close the current shell session.

Parameters:

  • group_name: The name of the group to switch to.

Example:
To add a user to the docker group:

$ sudo usermod -aG docker username

Replace username with the actual username. To add the current user to the docker group, run:

$ sudo usermod -aG docker $USER

After adding the user to the docker group, a re-login or system restart is required for the changes to take effect. Alternatively, use the following command to reload the user’s group memberships without logging out:

$ newgrp docker

newgrp更改当前用户所属的组

功能说明:更改当前用户所属的组。

语  法:newgrp [群组名称]

补充说明:newgrp指令类似login指令,它是以相同的帐号,使用另一个群组名称,再次登入系统。它的作用是将当前用户的有效组切换为指定的组,这样做会影响文件权限等操作,但它只能在当前登录的用户上下文中运行,不能用于切换其他用户的组。欲使用newgrp指令切换群组,你必须是该群组的用户,否则将无法登入指定的群组。若不指定群组名称,则newgrp指令会登入该用户名称的预设群组。一旦你通过 newgrp 切换了组,你可以通过exit命令退出当前 shell 会话来恢复原有的组。

参  数:

群组名称

    例:

将用户添加到 Docker 组:

$ sudo usermod -aG docker username

你需要将 username 替换为实际的用户名。可以使用以下命令添加当前用户:

$ sudo usermod -aG docker $USER

用户被添加到 docker 组后,需要重新登录或重启系统,才能使更改生效。也可以运行以下命令重新加载用户的组:

$ newgrp docker