Linux学习之用户管理命令

1、groupadd命令:- create a new group

groupadd命令用于创建一个新的用户组

(1)使用格式

	groupadd [options] group

(2)option
groupadd -g:(gid)指定GID——默认是上一个组的GID+1
groupadd -r:指定创建系统组
补充:

(1)、运行中的系统:就是在后台运行了很多的进程,kenel和许多线程,服务管理进程
(2)、后台进程:即开机启动的程序
(3)、对文件的访问:都是在该用户权限下,后台进程对文件的访问
(4)、命令发起的权限一定是在该用户的权限之内
(5)、开机启动程序以谁的权限进行启动?
		开机启动程序程序就是以一般普通用户身份进行启动
(6)、系统用户:不用登陆系统;仅仅只是开启一些开机启动程序(以非管理员全线运行)
(7)、安全上下文:
		进程以其发起者身份运行
		进程对文件的访问权限,取决于发起此进程的用户权限

2、groupmod命令:- modify a group definition on the system

groupmod命令用于修改组的属性

(1)使用格式

groupmod [options] GROUP

(2)option
groupmod -g:修改组的GID——不要与现有的组GID相同

[root@hu /]# cat /etc/group
test1:x:5006:
[root@hu /]# groupmod -g 5001 test1
test1:x:5001:

groupmod -n:修改组的名字

[root@hu /]# cat /etc/group
test1:x:5001:
[root@hu /]# groupmod -n te test1
[root@hu /]# cat /etc/group
te:x:5001:

3、groupdel命令:- delete a group

groupdel命令用于删除用户组

(1)使用格式

	groupdel [options] GROUP

(2)option
注意:若为某用户的附加组则删除此组会使该组对应的用户失去组

4、useradd命令:- create a new user or update default new user information

useradd命令用于创建一个新的用户或更新默认新用户信息

(1)使用格式

A、useradd [options] LOGIN
B、useradd -D
C、useradd -D [options]

(2)option

A、格式下的使用
useradd -u:指定用户的UID

[root@hu /]# useradd -u 100 tt
tt:x:100:5002::/home/tt:/bin/bash

useradd -g:指定用户的基本组ID——但此组事先必须存在

[root@hu /]# useradd -u 5008 -g 5002 teb
teb:x:5008:5002::/home/teb:/bin/bash

useradd -G:指明用户所属的附加组,多个组之间用逗号分隔

[root@hu /]# useradd -u 5005 -g 5002 -G test1 test
test1:x:5002:test

useradd -c:指明注释信息

[root@hu /]# useradd -c "sodjgwiejg" tt
tt:x:5006:5006:sodjgwiejg:/home/tt:/bin/bash

useradd -d:用指定的路径为用户的家目录——最好是指定不存家目录
补充:

通过复制/etc/shel此目录并重命名实现环境变量配置;若指定的家目录事先存在则不会为用户复制环境变量配置文件

useradd -s:指定用户默认shell,可用的所有shell列表为—/etc/shells文件中

[root@hu ~]# useradd -u 5004 -g 5002 -c "cl-13312342345" -s /bin/csh test1
test1:x:5004:5002:cl-13312342345:/home/test1:/bin/csh

useradd -r:创建系统用户
useradd -m:若用户家目录不存在则创建新的用户家目录,骨架目录中的文件会复制到主目录中。
useradd -M:不创建用户家目录即使系统设定了也不创建
useradd -f:密码过期后,账户彻底禁用之前的天数——0-表示立即禁用;-1 表示禁用此功能

B、格式下使用
useradd -D:表示创建用户时默认信息

[root@hu ~]# useradd -D
GROUP=100					==>表示与用户同名的组
HOME=/home					==>创建家目录的起始位置,在此位置下创建一个与用户名相同的目录
INACTIVE=-1				==>非活动期限为永不过期
EXPIRE=						==>
SHELL=/bin/bash			==>默认shell
SKEL=/etc/skel				==>用户环境信息复制地址
CREATE_MAIL_SPOOL=yes		==>是否创建邮筒——yes表示自动创建

C、格式下使用
useradd -D option:用于修改和更改默认值

[-b][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s <shell>]

==注意:
/etc/login.defs:创建用户时默认设置保存的文件夹
/etc/default/useradd:保存修改的结果
5、usermod命令:- modify a user account

useradd命令用于修改用户的属性

(1)使用格式

 usermod [options] LOGIN

(2)option
usermod -u:修改用户的UID

test1:x:5004:5002:cl-13312342345:/home/test1:/bin/csh				==>修改前
[root@hu ~]# usermod -u 5006 test1
[root@hu ~]# cat /etc/passwd
test1:x:5006:5002:cl-13312342345:/home/test1:/bin/csh				==>修改后

usermod -g:修改用户所属的基本组

test1:x:5006:5002:cl-13312342345:/home/test1:/bin/csh		==>修改前
[root@hu ~]# usermod -g 1000 test1
test1:x:5006:1000:cl-13312342345:/home/test1:/bin/csh		==>修改后

usermod -G:修改用户所属的附加组,若原用户有附加组则用新的组覆盖原附加组

jack:x:5003:							==>修改之前
[root@hu ~]# usermod -G jack test1
jack:x:5003:test1						==>修改后添加到了Jack组

usermod -a:与-G一同使用,用于用户追加新的附加组

jack2:x:5004:
[root@hu ~]# usermod -a -G jack2 test1
jack2:x:5004:test1

usermod -c:修改注释信息

test1:x:5006:1000:cl-13312342345:/home/test1:/bin/csh
[root@hu ~]# usermod -c "cl 13315824456" test1
test1:x:5006:1000:cl 13315824456:/home/test1:/bin/csh

usermod -d:修改用户家目录,但文件并不会转移到修改的路径下

test1:x:5006:1000:cl 13315824456:/home/test1:/bin/csh
[root@hu ~]# usermod -d /recover test1
test1:x:5006:1000:cl 13315824456:/recover:/bin/csh
ls: cannot access /recover/test1: No such file or directory
[root@hu ~]# ls /recover								==>仅仅是更改了家目录路径并没有将文件移动到此目录
cptest  io

usermod -m:只能与-d一同使用;用于将原来的家目录移到新的路径下
usermod -l:修改用户名

test1:x:5006:5003:cl 13315241379:/recover/test1:/bin/csh
[root@hu ~]# usermod -l test2 test1
test2:x:5006:5003:cl 13315241379:/recover/test1:/bin/csh

usermod -s:修改用户默认shell

test2:x:5006:5003:cl 13315241379:/recover/test1:/bin/csh
[root@hu ~]# usermod -s /bin/bash test2
test2:x:5006:5003:cl 13315241379:/recover/test1:/bin/bash

usermod -L:锁定用户密码,禁止登陆——即在原密码字符串前面添加一个“!”

[root@hu ~]# usermod -L test2
test2:!$6$qKLAjX5V$E

usermod -U:解锁用户的密码

[root@hu ~]# usermod -U test2
test2:$6$qKLAjX5V$E

6、userdel命令:- delete a user account and related files

userdel命令用于删除用户

(1)使用格式

userdel [options] LOGIN

(2)option
userdel -r:表示删除用户同时删除用户家目录

[root@hu ~]# ll /home
total 8
drwx------. 14 hu    hu   4096 Oct 24 19:53 hu
drwx------.  3 test2 jack 4096 Nov 28 22:33 test1
[root@hu ~]# userdel -r test1
[root@hu ~]# ls /home
hu

7、passwd命令:- update user’s authentication tokens

passwd命令用于修改用户的密码

(1)使用格式

passwd :表示不带参数是修改自己的密码
passwd username:有用户名表示修改指定用户的密码——默认root用户才有此权限

(2)option
passwd -l:表示用于锁定用户

[root@hu ~]# passwd -l test1
Locking password for user test1.
passwd: Success

passwd -u:表示用于解锁用户

[root@hu ~]# passwd -u test1
Unlocking password for user test1.
passwd: Success

passwd -d:清除用户密码
passwd -e:密码过期期限的日期
passwd -i:非活动期限
passwd -n:密码最短使用期限
passwd -x:密码最常使用期限
passwd -w:警告期限

[root@hu ~]# passwd -i 800
:17864:30:900:10:800::

passwd --stdin:通过标准输入或文件中的文字设置为密码

[root@hu ~]# echo "test1"|passwd --stdin test1
Changing password for user test1.
passwd: all authentication tokens updated successfully.
或者
[root@hu recover]# cat test.txt | passwd --stdin test1
Changing password for user test1.
passwd: all authentication tokens updated successfully.

8、gpasswd命令:- administer /etc/group and /etc/gshadow

gpasswd命令用于管理组、管理组密码和修改组密码

(1)使用格式

gpasswd [option] group

(2)option
gpasswd groupname:修改指定组的密码

[root@hu hu]# gpasswd jack
Changing the password for group jack
New Password: 

gpasswd -a:向组中添加用户

[root@hu hu]# gpasswd -a test1 jack2
Adding user test1 to group jack2

gpasswd -d:从组中移除用户

[root@hu hu]# gpasswd -d test1 jack2
Removing user test1 from group jack2

9、newgrp命令: - log in to a new group

newgrp命令用于切换用户所在用户组命令 登入另一个群组
说明:如果一个用户同时隶属于两个或两个以上分组,需要切换到其它用户组来执行一些操作,就用到了newgrp命令切换当前登陆所在组

(1)使用格式

newgrp [-] [group]
例:
[root@hu hu]# newgrp - jack2
欢迎进入Linux世界,尽情期待!
[root@hu ~]#

补充:

	若加 “—”则模拟用户重新登录以后再进入到临时指定的基本组,若不加则直接切换到临时指定的基本组中。

10、chage命令:- change user password expiry information

chage命令用于修改用户密码和密码到期信息

(1)使用格式

chage [options] LOGIN

(2)option

11、id命令:- print real and effective user and group IDs

id命令用于打印真实有效的用户和组id

(1)使用格式

id [OPTION]... [USER]

(2)option
id -u:仅显示有效的ID
id -g:仅显示GID
id -G:显示所有组的组ID
id -n:显示名称

[root@hu hu]# id -G -n test1			==>显示所属组的组名称
jack jack2

12、su命令:- run a command with substitute user and group ID

su命令用于切换用户

补充:

1)、有两种切换方式
		登录时切换——完全切换:会通过重新读取用户配置文件来重新初始化
		非登录式切换——半切换:不会读取目标用户配置文件进行初始化
	注意:若要完全切换工作目标用户环境下应使用登录切换
	
2)、两种登陆如何实现
		登录式:su - username
				su -l username
		非登录式:su username
	注意:管理员可以无密码切换到其他任意用户;非管理员切换至目标用户必须使用目标用户密码——否则无法切换

(1)使用格式

su - username
su username

(2)option
su - user -c ‘command’:仅利用指定用户身份运行此处指定的命令

[hu@hu ~]$ su - root -c 'cat /recover/test.txt'
Password: 
123456

13、chsh命令:- change your login shell

chsh命令用于修改当前用户或指定用户的shell(仅在root用户下)

(1)使用格式

chsh [-s shell] [-l] [-u] [-v] [username]

14、chfn命令:- change your finger information

chfn命令用于修改finger信息

(1)使用格式

chfn [-f full-name] [-o office] ,RB [ -p office-phone] [-h home-phone] -u] [-v] [username]

15、finger命令:

finger命令用于用户信息查询的工具

16、whoami命令:- print effective userid

whoami命令用于打印当前登陆的用户

(1)使用格式

whoami [OPTION]...

17、pwck命令:- verify integrity of password files

pwck命令用于验证密码的完整性——即检查用户存在哪些问题

(1)使用格式

pwck [options] [passwd [ shadow ]]

[root@hu hu]# pwck
user 'ftp': directory '/var/ftp' does not exist
user 'saslauth': directory '/run/saslauthd' does not exist
user 'avahi-autoipd': directory '/var/lib/avahi-autoipd' does not exist
user 'pulse': directory '/var/run/pulse' does not exist
user 'gnome-initial-setup': directory '/run/gnome-initial-setup/' does not exist
pwck: no changes

18、grpck命令:- verify integrity of group files

grpck命令用于验证组文件完整性

(1)使用格式

grpck [options] [group [ shadow ]]

vipw
vigr

groups

ac
grpconv
grpunconv
lastlog
logname
users
lastb

猜你喜欢

转载自blog.csdn.net/qq_29954175/article/details/84556435