且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

第三天------------day 3

更新时间:2022-10-01 21:41:38

用户组

# groupadd hr

# groupadd sale # groupadd it # groupadd net01 -g 2000 添加组net01,并指定gid 2000 # groupdel net01 删除组net01 # grep 'net01' /etc/group 查看/etc/group中组net01信息

第三天------------day 3

用户

# useradd user01

[root@station230 ~]# grep 'user01' /etc/passwd
user01:x:503:506::/home/user01:/bin/bash
[root@station230 ~]# 
[root@station230 ~]# grep 'user01' /etc/shadow
user01:!!:15638:0:99999:7:::
[root@station230 ~]# grep 'user01' /etc/group
user01:x:506:
[root@station230 ~]# id user01
uid=503(user01) gid=506(user01) groups=506(user01)
[root@station230 ~]# userdel user01	//删除用户user01,但不删除用户家目录
[root@station230 ~]# ll /home/
总计 16
drwx------ 3 alice alice 4096 10-24 16:23 alice
drwx------ 3 jack jack 4096 10-24 09:48 jack
drwx------ 3 robin robin 4096 10-25 10:04 robin
drwx------ 3  503  506 4096 10-25 14:09 user01

[root@station230 ~]# useradd user01		
[root@station230 ~]# useradd user02 -u 503	//创建用户usr02,指定uid
[root@station230 ~]# useradd user03 -d /aaa	//创建用户user03 指定家目录
[root@station230 ~]# useradd user04 -M		//创建用户user04,不创建家目录
[root@station230 ~]# useradd user05 -s /sbin/nologin 	//创建用户并指定shell
[root@station230 ~]# useradd user06 -g hr		//创建用户,指定主组
[root@station230 ~]# useradd user07 -G sale		//创建用户,指定附加组
[root@station230 ~]# useradd user08 -e 2013-04-01 //指定过期时间
[root@station230 ~]# useradd user10 -u 4000 -s /sbin/nologin

第三天------------day 3

注意:gpasswd将用户添加到组或从组中删除,只针对已存在的用户
[root@station230 ~]# gpasswd -a user07 it	//将某个用户加入到某个组
[root@station230 ~]# gpasswd -M user02,user03,user04 it //将多个用户加入到it组
[root@station230 ~]# grep 'it' /etc/group	//查看it组中的成员
it:x:505:user02,user03,user04

[root@station230 ~]# gpasswd -d user07 it //删除用户usr07从it组


第三天------------day 3
小知识:修改用户的信息
[root@station230 ~]# useradd user10
[root@station230 ~]# grep 'user10' /etc/passwd
user10:x:509:509::/home/user10:/bin/bash

# usermod -u 2000 user10			//修改用户uid
# usermod -s /sbin/nologin user10		//修改用户shell

[root@station80 ~]# useradd user1000
[root@station80 ~]# passwd user1000
[root@station80 ~]# grep 'user1000' /etc/shadow

user1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7:::

第三天------------day 3

==密码锁定,解锁== [root@station80 ~]# usermod -L user1000 [root@station80 ~]# grep 'user1000' /etc/shadow user1000:!$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7::: 登录测试 第三天------------day 3 [root@station80 ~]# usermod -U user1000 [root@station80 ~]# grep 'user1000' /etc/shadow user1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7::: 登录测试 第三天------------day 3 ==账号过期== [root@station80 ~]# date 2013年 03月 11日 星期一 15:36:19 CST [root@station80 ~]# usermod -e 2013-02-11 user1000 [root@station80 ~]# grep 'user1000' /etc/shadow user1000:$1$Hw2wCJoe$FU91eSBsBx1W0CGdIhTwh/:15775:0:99999:7::15747:

登录测试

第三天------------day 3

useradd 参考的文件
1. /etc/login.defs
2. /etc/default/useradd	# useradd -D

3. /etc/skel/* 用户的初始配置文件

====文件权限====
rw-r--r-- root root install.log

权限对象:
属主:u
属组:g
其他人: o
所有人:a

权限类型:
读:r		4
写:w		2
执行: x		1

===设置权限
1. 更改文件的属主、属组
chown:
[root@station230 ~]# chown alice.hr file1 	//改属主、属组
[root@station230 ~]# chown alice  file1 	//只改属主
[root@station230 ~]# chown   .hr file1	//只改属组
chgrp:
[root@station230 ~]# chgrp it file1		//改文件属组
[root@station230 ~]# chgrp -R it dir1		//改文件属组

第三天------------day 3

2. 更改权限
a. 使用字符
		对象		赋值符		权限类型
		u		+		r
chmod 		g		-		w
		o		=		x
		a
[root@station230 ~]# chmod u+x file1	//属主加执行
[root@station230 ~]# chmod a=rwx file1	//所有人等于读写执行
[root@station230 ~]# chmod a=- file1	//所有人没有权限
[root@station230 ~]# chmod ug=rw,o=r file1 	 //属主属组等于读写,其他人只读
[root@station230 ~]# ll file1 			 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1	 //显示的结果
b. 数字
[root@station230 ~]# chmod 644 file1
[root@station230 ~]# ll file1

-rw-r--r-- 1 alice it 17 10-25 16:45 file1

第三天------------day 3

=================================================================
读、写、执行 测试
[root@station230 ~]# mkdir /home/test
[root@station230 ~]# touch /home/test/file1
[root@station230 ~]# touch /home/test/file2
[root@station230 ~]# ll -d /home/test/
drwxr-xr-x 2 root root 4096 10-26 10:44 /home/test/
[root@station230 ~]# chmod o=- /home/test/
[root@station230 ~]# ll -d /home/test/
drwxr-x--- 2 root root 4096 10-26 10:44 /home/test/

=================================================================
[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01

[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01
[root@station230 ~]# 
[root@station230 ~]# mkdir /home/up01
[root@station230 ~]# chgrp up01 /home/up01/
[root@station230 ~]# chmod 770 /home/up01/
[root@station230 ~]# ll -d /home/up01/
drwxrwx--- 2 root up01 4096 10-26 11:41 /home/up01/
特殊权限:
suid	4
sgid	2
sticky	1

umask:文件、目录创建后的默认权限是由umask决定的
root: 文件 644		umask: 0022
    目录 755

普通用户:文件 664		umask: 0002
     目录 775

[root@station230 ~]# umask -S
u=rwx,g=rx,o=rx
[root@station230 ~]# umask 	//查看当前用户的umask权限
0022
[root@station230 ~]# umask 000	//设置umask权限
[root@station230 ~]# umask 
0000
[root@station230 ~]# touch file8	//创建file8文件
[root@station230 ~]# mkdir dir8	//创建目录
[root@station230 ~]# ll -d dir8 file8	//查看文件目录权限
drwxrwxrwx 2 root root 4096 10-26 14:10 dir8
-rw-rw-rw- 1 root root  0 10-26 14:10 file8
[root@station230 ~]# umask 222

[root@station230 ~]# umask

第三天------------day 3

ACL: UGO权限的扩展
UGO传统权限:只能一个用户,一个组和其他人

[root@station230 ~]# touch /home/test.txt
[root@station230 ~]# ll /home/test.txt	
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt

[root@station230 ~]# getfacl /home/test.txt
[root@station230 ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[root@station230 ~]# setfacl -m u:jack:- /home/test.txt  //增加用户jack权限

查看:
[root@station230 ~]# ll /home/test.txt 
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[root@station230 ~]# getfacl /home/test.txt


[root@station230 ~]# setfacl -m g:hr:r /home/test.txt
[root@station230 ~]# setfacl -x g:hr /home/test.txt	//删除组hr的权限
[root@station230 ~]# setfacl -b /home/test.txt 		//删除所有acl权限

第三天------------day 3

第三天------------day 3

ACL: 继承(默认)
[root@station230 ~]# mkdir -p /home/dir100/aaa

[root@station230 ~]# setfacl -m d:u:jack:rwx /home/dir100


[root@station230 ~]# lsattr fil100.txt file200.txt 
------------- fil100.txt
------------- file200.txt
[root@station230 ~]# chattr +a fil100.txt 
[root@station230 ~]# chattr +i file200.txt 
[root@station230 ~]# lsattr fil100.txt file200.txt 
-----a------- fil100.txt
----i-------- file200.txt
[root@station230 ~]# cat fil100.txt file200.txt 
1111
222
[root@station230 ~]# rm -rf fil100.txt file200.txt 
rm: 无法删除 “fil100.txt”: 不允许的操作
rm: 无法删除 “file200.txt”: 不允许的操作
[root@station230 ~]# echo 111 >> fil100.txt
[root@station230 ~]# cat fil100.txt
1111
111
[root@station230 ~]# echo 111 >> file200.txt
bash: file200.txt: 权限不够

[root@station230 ~]# chattr -i file200.txt 
[root@station230 ~]# lsattr file200.txt 

------------- file200.txt

第三天------------day 3














本文转自zhang25yun51CTO博客,原文链接: http://blog.51cto.com/1585654/1305163,如需转载请自行联系原作者