添加一个用户到指定用户组:

gpasswd –a 用户名 组名
usermod –G 组名 用户名

//第一种:gpasswd –a 用户名 组名

[root@localhost ~]# id user1

uid=501(user1) gid=501(user1) groups=501(user1)

[root@localhost ~]# gpasswd -a user1 user_group

Adding user user1 to group user_group

[root@localhost ~]# id user1

uid=501(user1) gid=501(user1) groups=501(user1),500(user_group)

//第二种:usermod –G 组名 用户名

[root@localhost ~]# id user1

uid=501(user1) gid=501(user1) groups=501(user1)

[root@localhost ~]# usermod -G user_group user1

[root@localhost ~]# id user1

uid=501(user1) gid=501(user1) groups=501(user1),500(user_group)

很显然,这两种方式得到的结果是一样的。其实对于上面两种方式正确来说是将用户组添加到指定用户的用户附加组中。

但是这个使用时候我们创建文件得到所属的组还是原来的user1组。可以使用newgrp来切换用户组,但是这个newgrp切换用户组只是临时切换的,当账号重新登录后就会变回去的。这个情况我们可以使用id和id 用户名 命令看出来。

[user1@localhost ~]$ touch file1

[user1@localhost ~]$ ls -l

total 0

-rw-rw-r--. 1 user1 user1 0 Jul  1 11:19 file1

[user1@localhost ~]$ id

uid=501(user1) gid=501(user1) groups=501(user1),500(user_group) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[user1@localhost ~]$ id user1

uid=501(user1) gid=501(user1) groups=501(user1),500(user_group)

[user1@localhost ~]$ newgrp user_group

[user1@localhost ~]$ touch file2

[user1@localhost ~]$ ls -l

total 0

-rw-rw-r--. 1 user1 user1      0 Jul  1 11:19 file1

-rw-r--r--. 1 user1 user_group 0 Jul  1 11:20 file2

[user1@localhost ~]$ id

uid=501(user1) gid=500(user_group) groups=501(user1),500(user_group) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[user1@localhost ~]$ id user1

uid=501(user1) gid=501(user1) groups=501(user1),500(user_group)

修改一个用户到指定用户组

[root@localhost ~]# id user1

uid=501(user1) gid=501(user1) groups=501(user1)

[root@localhost ~]# usermod -g user_group user1

[root@localhost ~]# id user1

uid=501(user1) gid=500(user_group) groups=500(user_group)

对于gpasswd -a命令和usermod -G添加用户到用户组同时会保留这个用户之前所在组信息因为是将用户添加到用户群组中修改的是groups,而usermod会将这个用户之前添加的组信息删除直接修改的是gid,所以如果想让指定用户临时加入指定组,可以使用gpasswd –a或者usermod -G命令。

linux中添加一个用户到指定用户组的两种方式,修改一个用户到指定用户组的一种方式的更多相关文章

  1. 在linux中添加ftp用户,并设置相应的权限

    在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...

  2. linux中添加ftp用户,并设置相应的权限

    在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...

  3. 如何在Linux中添加新的系统调用

    系统调用是应用程序和操作系统内核之间的功能接口.其主要目的是使得用户 可以使用操作系统提供的有关设备管理.输入/输入系统.文件系统和进程控制. 通信以及存储管理等方面的功能,而不必了解系统程序的内部结 ...

  4. Linux中添加管理员权限问题:xxx is not in the sudoers file. This incident will be reported.

    在各个不同版本的linux中添加拥有管理员权限账户有不同的简便方式. 问题: 今天遇见将新添用户添加到root用户组后,运行sudo仍然提示 ”xxx is not in the sudoers fi ...

  5. 在Windows Server 2008 R2中使用web方式修改域用户账户密码

    在Windows的domain环境下,加域的客户端修改账户密码是一件很easy的事情:即使没有加域的客户端如果组织中,使用Exchange邮件系统,借助Exchange的owa也可以轻松修改账户密码. ...

  6. Window通过Web方式修改域用户密码

    如何通过web方式修改域用户密码: 1.在Windows Server 2003上,系统默认提供了iisadmpwd作为一种修改域用户密码的方式 2.在Windows Server 2008上,可以提 ...

  7. Linux中添加、修改和删除用户和用户组

    宽为限 紧用功 功夫到 滞塞通 一.用户: 在创建用户时,需要为新建用户指定一用户组,如果不指定其用户所属的工作组,自动会生成一个与用户名同名的工作组.创建用户user1的时候指定其所属工作组user ...

  8. linux 中添加自己的库路径的方法 cannot open shared object file: No such file or directory

    本文转自:http://blog.csdn.net/maotianwang/article/details/44619197 库文档在连接(静态库和共享库)和运行(仅限于使用共享库的程式)时被使用,其 ...

  9. arm linux中添加开机启动

    微处理器:S5PV210操作系统:linux3.0.8 前言:    在产品中,基本上都要屏蔽arm开发板中linux系统的对外通信,只应该通过产品的相关APP做相关操作.    因此需要把该APP添 ...

随机推荐

  1. vi/vim打开文件提示Found a swap file by the name

    问题分析 有一次在远程连接主机时,用vi打开文件my.ini却提示:Found a swap file by the name ".my.ini.swp".百度了下才知道,原来在使 ...

  2. mongodb-Configuration

    命令行和配制后文件接口为Mongodb的管理者提供了大量的控制选项.在这篇文章中提供了对于一般应用场景的最佳实践配置. mongod --config /etc/mongod.conf mongod ...

  3. JMeter中的HTTPS套接字错误

    Apache JMeter对启用SSL的应用程序执行性能和/或负载测试时,SSL套接字错误可能是经常遇到的麻烦,严重阻碍了您的测试工作.本文重点介绍如何通过相应地配置和调优JMeter来克服这些与连接 ...

  4. 2017 Multi-University Training Contest - Team 1 Balala Power!

    Talented Mr.Tang has n strings consisting of only lower case characters. He wants to charge them wit ...

  5. Excel 通过pl/sql导入到数据库 文本导入器 odbc导入器

     Excel 通过pl/sql导入到数据库 第一种方法:文本导入器 1.准备Excel导入数据   jc.xls 2.把 jc.xls 文件 改为 jc.csv文件 3.在数据库里建一张jc表(FLH ...

  6. Executor等系列概念介绍

    这里对几个常见的的名词进行介绍 Executor 这是个接口,只声明了一个方法—— public interface Executor { void execute(Runnable command) ...

  7. Hibernate save()、saveOrUpdate()、merge()的区别

    一. update 和 merge的区别 首先在执行更新操作的时候,两者都必须要有id update是直接执行update 语句,执行后状态为持久化状态 而merge则不一样: 1. 如果sessio ...

  8. 从零开始利用vue-cli搭建简单音乐网站(四)

    上一篇文章中说到这一篇博客会实现音乐播放功能,只是令我意外的是,如果利用h5的audio标签,几行代码就实现了......先来看一下最终效果吧. 这里直接用了audio标签,样式没有怎么管,能获得音乐 ...

  9. 动画 iOS基础

    动画 iOS基础 1.     basic animation  基础动画 一个基础动画 在一个开始值和一个结束值之间运动   messageLabel.alpha=0.0; [UIView  ani ...

  10. 刷新本地DNS缓存的方法

    http://www.cnblogs.com/rubylouvre/archive/2012/08/31/2665859.html 常有人问到域名解析了不是即时生效的嘛,怎么还是原来的呢?答案就是在本 ...