Linux用户相关操作

一,用户组操作

1,创建组

[root@VM_171_69_centos ~]# groupadd  
test增加一个test组

2,修改组

[root@VM_171_69_centos ~]# groupmod -n test2  
test将test组的名子改成test2

3,删除组

[root@VM_171_69_centos ~]# groupdel test2
删除 组test2

4,查看组

a),查看当前登录用户所在的组 groups,查看apacheuser所在组groups apacheuser

[root@VM_171_69_centos ~]# groups
[root@VM_171_69_centos ~]# root

b),查看所有组 cat /etc/group

[root@VM_171_69_centos ~]# cat /etc/group
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
postgres:x:500:
boss:x:888:
sexy:x:889:
imooc:x:503:
test:x:891:

有的linux系统没有/etc/group文件的,这个时候看下面的这个方法
cat /etc/passwd |awk -F [:] '{print $4}' |sort|uniq | getent group |awk -F [:] '{print $1}'
这里用到一个命令是getent,可以通过组ID来查找组信息,如果这个命令没有的话,那就很难查找,系统中所有的组了.


二,用户操作

[root@VM_171_69_centos ~]# useradd --help  
Usage: useradd [options] LOGIN

Options:
 -b, --base-dir BASE_DIR       设置基本路径作为用户的登录目录  
 -c, --comment COMMENT         对用户的注释  
 -d, --home-dir HOME_DIR       设置用户的登录目录  
 -D, --defaults                改变设置  
 -e, --expiredate EXPIRE_DATE  设置用户的有效期  
 -f, --inactive INACTIVE       用户过期后,让密码无效  
 -g, --gid GROUP               使用户只属于某个组  
 -G, --groups GROUPS           使用户加入某个组  
 -h, --help                    帮助  
 -k, --skel SKEL_DIR           指定其他的skel目录  
 -K, --key KEY=VALUE           覆盖 /etc/login.defs 配置文件  
 -m, --create-home             自动创建登录目录  
 -l,                           不把用户加入到lastlog文件中  
 -M,                           不自动创建登录目录  
 -r,                           建立系统账号  
 -o, --non-unique              允许用户拥有相同的UID  
 -p, --password PASSWORD       为新用户使用加密密码  
 -s, --shell SHELL             登录时候的shell  
 -u, --uid UID                 为新用户指定一个UID  
 -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

1,增加用户

[root@VM_171_69_centos ~]# useradd test
[root@VM_171_69_centos ~]# passwd test
更改用户 test 的密码 。
新的 密码:
无效的密码: 它没有包含足够的不同字符
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
增加用户test,有一点要注意的,useradd增加一个用户后,不要忘了给他设置密码,不然不能登录的。

2,修改用户

[root@VM_171_69_centos ~]# usermod -d /home/test -G test2 test
将test用户的登录目录改成/home/test,并加入test2组,注意这里是大G。

[root@VM_171_69_centos ~]# gpasswd -a test test2
Adding user test to group test2
 将用户test加入到test2组
 
[root@VM_171_69_centos ~]# gpasswd -d test test2
Removing user test from group test2
 将用户test从test2组中移出

3,删除用户

[root@VM_171_69_centos ~]# userdel test
将test用户删除

4,查看用户

a),查看当前登录用户

[root@VM_171_69_centos ~]# w
 21:15:27 up 6 days, 23 min,  4 users,  load average: 0.10, 0.03, 0.01
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    125.211.30.81    18:54    1:51m  0.00s  0.00s -bash
root     pts/1    125.211.30.81    19:32    1:00m  0.01s  0.01s -bash
root     pts/2    125.211.30.81    20:16    9:11   0.04s  0.04s -bash
root     pts/3    117.136.7.235    21:07    0.00s  0.01s  0.00s w


[root@VM_171_69_centos ~]# who
root     pts/0        2017-01-30 18:54 (125.211.30.81)
root     pts/1        2017-01-30 19:32 (125.211.30.81)
root     pts/2        2017-01-30 20:16 (125.211.30.81)
root     pts/3        2017-01-30 21:07 (117.136.7.235)

b),查看自己的用户名

[root@VM_171_69_centos ~]# whoami
root

c),查看单个用户信息

[root@VM_171_69_centos ~]# id root
uid=0(root) gid=0(root) 组=0(root)

d),查看用户登录记录

[root@VM_171_69_centos ~]# last
root     pts/3        117.136.7.235    Mon Jan 30 21:07   still logged in   
root     pts/2        125.211.30.81    Mon Jan 30 20:16   still logged in   
root     pts/1        125.211.30.81    Mon Jan 30 19:32   still logged in  
reboot   system boot  2.6.32-504.30.3. Wed Jan 25 04:51 - 21:17 (5+16:25)   
root     pts/0        125.211.30.116   Tue Jan 24 20:44 - down   (00:07)    
reboot   system boot  2.6.32-504.30.3. Wed Jan 25 04:44 - 20:51  (-7:-52)   
root     pts/0        125.211.30.116   Tue Jan 24 20:02 - down   (00:41)    

wtmp begins Wed Jan 25 04:00:15 2017
查看登录成功的用户记录



[root@VM_171_69_centos ~]# lastb
root     ssh:notty    41.200.31.17     Tue Jan 24 22:43 - 22:43  (00:00)    
admin    ssh:notty    41.200.31.17     Tue Jan 24 22:43 - 22:43  (00:00)    
admin    ssh:notty    41.200.31.17     Tue Jan 24 22:43 - 22:43  (00:00)    
root     ssh:notty    14.118.84.90     Tue Jan 24 22:43 - 22:43  (00:00)    
root     ssh:notty    14.118.84.90     Tue Jan 24 22:43 - 22:43  (00:00)    
root     ssh:notty    14.118.84.90     Tue Jan 24 22:43 - 22:43  (00:00)    
root     ssh:notty    14.118.84.90     Tue Jan 24 22:43 - 22:43  (00:00)    

btmp begins Tue Nov 21 07:36:32 1905
查看登录不成功的用户记录

e),查看所有用户

[root@VM_171_69_centos ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody

或者
[root@VM_171_69_centos ~]# cat /etc/passwd |awk -F \: '{print $1}'
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
vcsa
abrt

欢迎来到我的IT 博客http://www.nightsong.cc

QQ:1623231307
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值