01、创建用户
Linux在创建新用户时,用到的是useradd命令。useradd命令使用系统的默认值及命令行参数来设置创建后的账户。我们可以通过 -D 来查看都有哪些默认值:
[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
各行参数对应的含义为:
新用户会被添加到GID为100的公共用户组
新用户的HMOE目录将会位于/home/用户名 目录下
新用户的账户密码在过期后不会被禁用
新用户账户未被设置过期日期
新用户将bash shell作为默认shell
系统会将/etc/skel 目录下的内容复制到用户的HOME目录下
系统为该账户在mail目录下创建一个用于接收邮件的文件
来看下/etc/skel 目录下都有哪些文件
[root@localhost ~]# ls -al /etc/skel
total 24
drwxr-xr-x. 2 root root 62 Apr 11 00:59 .
drwxr-xr-x. 85 root root 8192 Aug 29 11:11 ..
-rw-r--r--. 1 root root 18 Apr 10 20:53 .bash_logout
-rw-r--r--. 1 root root 193 Apr 10 20:53 .bash_profile
-rw-r--r--. 1 root root 231 Apr 10 20:53 .bashrc
[root@localhost ~]#
在上一篇《
linux技术手册-定义环境变量
》已经介绍,这些都是系统启动文件。
有些Linux系统默认不会创建HOME目录,我们可以通过 -m 命令参数来使其创建HOME目录。
[root@localhost ~]# useradd -m test1
[root@localhost home]# ls -al /home/test1
total 12
drwx------. 4 test2 test2 91 Aug 29 11:44 .
drwxr-xr-x. 4 root root 32 Aug 29 11:44 ..
-rw-r--r--. 1 test2 test2 18 Apr 10 20:53 .bash_logout
-rw-r--r--. 1 test2 test2 193 Apr 10 20:53 .bash_profile
-rw-r--r--. 1 test2 test2 231 Apr 10 20:53 .bashrc
drwxrwxr-x. 3 test2 test2 18 Aug 29 11:44 .cache
drwxrwxr-x. 3 test2 test2 18 Aug 29 11:44 .config
在创建新用户时,需要以root用户登陆来运行创建命令。如上所示,我们创建了test1用户后,在 /home/test1 目录下也生成了默认的系统文件。
如果在创建用户时想修改某个指定的默认值,可以添加如下常用参数。
-c 给新用户添加备注
-e 更改默认账户的过期日期,格式为:YYY-MMM-DD
-f 更改默认新用户从密码过期到账户被禁用的天数;0标识密码过期就禁用,-1表示不禁用
-g 更改默认的组名或者GID
-p 为用户指定默认密码
-s 更改默认的登陆shell
例如,创建一个登陆账户在 2018年10月10日过期的账户
[root@localhost test3]# useradd -e '2018-10-10' test2
通过chage命令(接下来会讲到)查看用户 test2 信息,过期时间已生效
[root@localhost test5]# chage -l test2
Last password change: Aug 29, 2018
Password expires: never
Password inactive: never
Account expires: Oct 10, 2018
Minimum number of days between password change: 0
Maximum number of days between password change: 99999
Number of days of warning before password expires: 7
02、修改用户
修改用户主要用到以下命令:
usermod 修改用户账户的属性
passwd 修改指定用户的密码
chpasswd 从文件中读取登录名密码对,并更新密码
chage 修改密码的过期日期
chfn 修改用户账户的备注信息
chsh 修改用户账户的默认登陆shell
最常用的是usermod和passwd命令
a) usermod
usermod命令是非常实用的一个命令,它可以实现修改登录名、锁定账户、修改密码等,命令如下:
-l 修改用户账户的登录名
-L 锁定账户,使用户无法登陆
-U 解除锁定账户,使其可以正常登陆
例如,我们将test2账户进行锁定
[root@localhost ~]# usermod -L test2
b) passwd
passwd可直接修改用户的密码,比如我们修改test2用户的密码
[root@localhost ~]# passwd test2
Changing password for user test2.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
03、删除用户
如果从Linux系统中删除某个用户,可以用userdel命令。
默认情况,userdel命令只会删除/etc/passwd文件中的用户信息,而不会删除系统中属于该账户的任何内容。
在删除用户时,可以加上 -r 参数,来删除创建用户时,HOME目录下信息。
[root@localhost home]# userdel -r test2
[root@localhost home]#
[root@localhost home]# ls test2
ls: cannot access test2: No such file or directory
[root@localhost home]#
这时候,test2用户在HOME目录下的信息已经不存在了。
领取专属 10元无门槛券
私享最新 技术干货