Linux小推车

Linux Day13:useradd及相关

2018-10-19  本文已影响17人  泥人吴
  • 新增与移除使用者: useradd, 相关配置文件, passwd, usermod, userdel

useraddd

# 创建user1用户
root@VM-0-3-ubuntu:~# useradd user1
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user1:x:1000:1000::/home/user1:
root@VM-0-3-ubuntu:~# #在创建用户的时候,没有为用户指定组,将创造同用户相同UID的组。
# 如果两者不一样,则会出现特殊用户。
#创建新用户并加入到mygroup中
root@VM-0-3-ubuntu:~# useradd -g mygroup user2
useradd: group 'mygroup' does not exist
# 说名这个组要事先存在才行
# 将user2加入到刚刚创建的user1中去
root@VM-0-3-ubuntu:~# useradd -g user1 user2
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user2:x:1001:1000::/home/user2:
root@VM-0-3-ubuntu:~# 其实,这个user1的UID为1000,在创建新用户的时候,系统默认根据/etc/passwd的最后一位UID自动创建下一位,就是这里的1001.
# 创建user3,并制定其附加组为user1
root@VM-0-3-ubuntu:~# tail /etc/group
lxd:x:110:
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:
root@VM-0-3-ubuntu:~# useradd -G user1 user3
root@VM-0-3-ubuntu:~# tail /etc/group
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:user3
user3:x:1002:
# 指定名称和家目录
root@VM-0-3-ubuntu:~# useradd -c "wuyi" -d /home/wuyi user5
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user5:x:1004:1004:wuyi:/home/wuyi:
# 默认情况下创建的用户:
root@VM-0-3-ubuntu:~# echo $SHELL
/bin/bash

userdel:删除用户

#删除用户user4,默认情况下不会删除其家目录,要想删除-r
root@VM-0-3-ubuntu:~# userdel user4
# 删除user5及其家目录
root@VM-0-3-ubuntu:~# userdel -r user5
userdel: user5 mail spool (/var/mail/user5) not found
userdel: user5 home directory (/home/wuyi) not found
# user5创建了家目录的,为什么提示找不到?

id: 查看用户账号的属性

SYNOPSIS
       id [OPTION]... [USER]

DESCRIPTION
       Print  user  and group information for the specified USER, or (when USER omit‐
       ted) for the current user.

       -a     ignore, for compatibility with other versions

       -Z, --context
              print only the security context of the process

       -g, --group
              print only the effective group ID

       -G, --groups
              print all group IDs

       -n, --name
$ #查看当前用户的id信息
$ id
uid=1091(ywu) gid=1092(ywu) groups=1092(ywu),1019(student)
$ #id user1查看user1的账户信息
$ id user1
id: ‘user1’: no such user
$ id -n
id: cannot print only names or real IDs in default format
$ id -u -n ywu
ywu
$ id -G -n ywu
ywu student

finger 查看用户账户信息

root@VM-0-3-ubuntu:~# finger user1
Login: user1                    Name: 
Directory: /home/user1                  Shell: /bin/sh
Never logged in.
No mail.
No Plan.

修改账户信息:

usermod:

chsh:修改用户的默认的shell

root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell []: /bin/tcsh
chsh: Warning: /bin/tcsh does not exist
root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell [/bin/tcsh]: /bin/bash
root@VM-0-3-ubuntu:~# finger user2
Login: user2                    Name: 
Directory: /home/user2                  Shell: /bin/bash
Never logged in.
No mail.
No Plan.

chfn:修改注释信息

root@VM-0-3-ubuntu:~# chfn user2
Changing the user information for user2
Enter the new value, or press ENTER for the default
    Full Name []: wuyi^H^H^H^H
    Room Number []: chongqing
    Work Phone []: 5^H023
    Home Phone []: 15870000000
    Other []: nothing
chfn: name with non-ASCII characters: ''uyi
chfn: invalid work phone: '023'

密码管理:

passwd:

root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:!:17825:0:99999:7:::

root@VM-0-3-ubuntu:~# passwd user2
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:$6$TyWhVhuR$wDzvvJx8GlArSe9OigIgdNejH3M.dYsly79ebgtuLqsK9a2KSTBVb4rTvMydrnF.eWX0np7xP2zsLbdT45C4Z.:17825:0:99999:7:::

pwck:检查用户账号的完整性

root@VM-0-3-ubuntu:~# pwck
user 'lp': directory '/var/spool/lpd' does not exist
user 'news': directory '/var/spool/news' does not exist
user 'uucp': directory '/var/spool/uucp' does not exist
user 'www-data': directory '/var/www' does not exist
user 'list': directory '/var/list' does not exist
user 'irc': directory '/var/run/ircd' does not exist
user 'gnats': directory '/var/lib/gnats' does not exist
user 'nobody': directory '/nonexistent' does not exist
user 'systemd-resolve': directory '/run/systemd/resolve' does not exist
user 'syslog': directory '/home/syslog' does not exist
user '_apt': directory '/nonexistent' does not exist
user 'ntp': directory '/home/ntp' does not exist
user 'user1': directory '/home/user1' does not exist
user 'user2': directory '/home/user2' does not exist
user 'user3': directory '/home/user3' does not exist
pwck: no changes

chage:

-d :最近一次的修改时间
-E:过期时间
-I:非活动时间
-m:最短使用期限
-M: 最长使用权限
-W: 警告时间

组管理:

创建组:groupadd:

root@VM-0-3-ubuntu:~# groupadd -r wechat
root@VM-0-3-ubuntu:~# tail -1 /etc/group
wechat:x:999:
# -r, --system
           Create a system group.
# 按照马哥来说的话是<500,为什么这里为999?

newgrp:切换到新的组

root@VM-0-3-ubuntu:~# useradd hadoop
root@VM-0-3-ubuntu:~# su - hadoop
No directory, logging in with HOME=/
$ cd /tmp
$ touch a.hadoop
$ ls -l
total 12
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root   root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root   root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root   root   191 Oct 21 12:30 setRps.log
$ #一个用户创建的文件属于用户的基本组

$ id
uid=1003(hadoop) gid=1003(hadoop) groups=1003(hadoop)
$ #将一个文件的基本组切换到其他组去
$ newgrp user1
Password: 
# 如果想要回到原来的组,则使用exit
马哥作业.PNG

友情阅读推荐:

生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
请猛戳下面链接
B站链接:https://m.bilibili.com/space/338686099

YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

上一篇下一篇

猜你喜欢

热点阅读