Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1498084
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: LINUX

2012-12-13 21:32:04

了解文件系统:
超块: 文件系统的相关信息,比如inode,block使用的情况
inode: 存储文件的元数据(权限,时间,属性,指向的block)
block: 存储文件的实际数据
block size: 当前文件系统的块大小
查看文件系统的信息:
dumpe2fs 文件系统
tune2fs -l 文件系统
修改文件系统的信息:
# tune2fs -L /boot /dev/sda2
tune2fs 1.41.12 (17-May-2010)
# tune2fs -o ^acl /dev/sda2  修改默认挂载的选项
# tune2fs -o acl /dev/sda2
硬链接:
ln 源文件 目标文件
ln -s 源文件(路径) 目标文件
 
文件权限:
======================================================================
用户
1. UGO
2. 特殊权限 set uid/set gid
3. ACL
4. umask
5. 文件属性
===管理用户:
和用户相关的文件:
/etc/passwd 用户的基本信息
  root:x:0:0:root:/root:/bin/bash
  用户名:密码占位符:UID:GID:描述:HOME:shell
/etc/shadow 用户的密码
  root:$6$lyBtlZ77HD/0:15682:0:99999:7:::
         密码策略
/etc/group 组的相关信息
  jack:x:500:

建立用户和组:
[root@server ~]# groupadd hr
[root@server ~]# groupadd sale
[root@server ~]# groupadd testgroup
[root@server ~]# groupdel testgroup
[root@server ~]# useradd alice
[root@server ~]# passwd alice
[root@server ~]# id alice   //查看用户ID信息
uid=501(alice) gid=503(alice) 组=503(alice)
[root@server ~]# useradd user01 -u 2000  //指定用户的UID
[root@server ~]# useradd user02 -g hr  //指定用户的主组
[root@server ~]# useradd user03 -G hr  //指定用户的附加组
[root@server ~]# useradd user04 -M  //不创建HOME
[root@server ~]# useradd user05 -d /user05 //指定HOME
[root@server ~]# useradd user06 -s /sbin/nologin //指定的shell

将用户加入或从组中删除:
[root@server ~]# gpasswd -a user05 hr  //将用户添加到组
[root@server ~]# gpasswd -d user05 hr
[root@server ~]# gpasswd -M hr01,hr02,hr03 hr //将多个成员加入组

useradd参考的文件:/etc/default/useradd
   /etc/login.defs
   /etc/skel/*
usermod修改用户:
[root@server ~]# usermod -u 3000 user03
[root@server ~]# usermod -s /bin/bash user06
其它命令:
[root@server ~]# grep hr01 /etc/shadow
hr01:$6$Ve5aJJ15$642OCRT9dAfvZILSd3JLVscu3dH53XCsg1RfIf6x12Pf:15682:0:99999:7:::
[root@server ~]#
[root@server ~]# usermod -e "2013-01-08" hr01 修改密码过期时间
[root@server ~]# grep hr01 /etc/shadow
hr01:$6$Ve5aJJ15$642OCRT9dAfvZILSd3JLVscu3dH53XCsg1RfIf6x12PfxxBTCFIoBx0Rot:15682:0:99999:7::15713:
[root@server ~]#
[root@server ~]# usermod -L hr01  锁定用户
[root@server ~]# grep hr01 /etc/shadow
hr01:!$6$Ve5aJJ15$642OCRT9dAfvZILSd3JLVscu3dH53XCsg1RfIf6x12Pf:15682:0:99999:7::15713:
[root@server ~]# usermod -U hr01  解锁用户
[root@server ~]# grep hr01 /etc/shadow
hr01:$6$Ve5aJJ15$642OCRT9dAfvZILSd3JLVscu3dH53XCsg1RfIf6x12Pf:15682:0:99999:7::15713:
 
===Linux文件权限
-rw-------. 1 root root  1407 12月  8 17:42 anaconda-ks.cfg
第一种:UGO权限方案:
权限对象: 属主(U)
   属组(G)
  其他人(O)
权限类型: r 读
  w 写
  x 执行
============================================================
权限  对文件影响  对目录的影响
r  可以读取文件内容  可以列出目录的内容(文件名)
w  可以更改文件内容  可以在目录中创建、删除任何文件
x  可以作为命令执行  可以访问目录的内容
============================================================
设置权限:
chgrp,chown,chmod,chcon  -R
更改文件的属主或属组:
[root@server /]# chown alice.hr file200  更改属主和属组
[root@server /]# chown alice    file200  更改属主
[root@server /]# chown      .hr file200  更改属组
[root@server /]# chgrp sale file200  属改属组
字符方式:
 权限的对象 权限赋值符 权限类型 
    u     +    r
chmod    g       -    w  filename
    o     =    x
    a
chmod u+x file1
chmod o-r file1
chmod ug=rw,o=- file200
chmod a=rwx file200
chmod +x file200

数字方式:
r  4
w  2
x  1
chmod 777 file400
chmod 760 file400
 
ACL: UGO权限的扩展
前提:需要文件系统挂载选项的支持(acl)
getfacl 查看文件的ACL权限
setfacl 设置文件的ACL权限
=========================================================
[root@server ~]# cp /etc/fstab /
[root@server ~]# cp /etc/fstab /mnt/disk5
[root@server ~]# setfacl -m u:alice:rw /fstab
[root@server ~]# setfacl -m u:alice:rw /mnt/disk5/fstab
setfacl: /mnt/disk5/fstab: 不支持的操作
[root@server ~]# vim /etc/fstab
UUID="a51925b3-399b-4cc8-951f-213a3823bf66"     /mnt/disk5      ext4 defaults,acl 0 0
[root@server ~]# mount -o remount /mnt/disk5/
[root@server ~]# setfacl -m u:alice:rw /mnt/disk5/fstab
让文件系统支持ACL功能:
方法一:在挂载时使用acl选项
UUID="a51925b3-399b-4cc8-951f-213a3823bf66"     /mnt/disk5      ext4 defaults,acl 0 0
方法二:修改文件系统的默认挂载选项
[root@server ~]# tune2fs -l /dev/sda2 |grep "Default"
Default mount options:    user_xattr acl
Default directory hash:   half_md4
[root@server ~]# tune2fs -o acl /dev/sda5
tune2fs 1.41.12 (17-May-2010)
默认挂载选项:使用mount命令是看不到

LAB:
[root@server ~]# cp /etc/hosts /var/tmp/
[root@server ~]# cd /var/tmp/
[root@server tmp]# ll hosts
-rw-r--r--. 1 root root 158 12月  8 22:26 hosts
[root@server tmp]# getfacl hosts
# file: hosts
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@server tmp]# setfacl -m u:alice:rw hosts
[root@server tmp]# setfacl -m u:jack:r hosts
[root@server tmp]# setfacl -m g:hr:rwx hosts
[root@server tmp]# ll hosts
-rw-rwxr--+ 1 root root 158 12月  8 22:26 hosts
[root@server tmp]# setfacl -x u:jack hosts 删除用户的acl权限
[root@server tmp]# setfacl -b hosts  删除所有的acl权限
ACL两个注意事项:
mask
default  针对将来
 
umaks权限
影响新建文件和目录的默认权限
=====================================================================
[root@server tmp]# umask
0022
[root@server tmp]# umask -S
u=rwx,g=rx,o=rx
[root@server tmp]# umask 000
 
特殊权限
=====================================================================
set uid:  针对文件
一个文件(二进制文件,而且必须有执行权限)一旦被设置了uid权限,
任何人在执行该文件时,都会以文件属主身份支执行
LAB1:
[root@server ~]# chmod u+s /bin/ls
[root@server ~]# ll /bin/ls
-rwsr-xr-x. 1 root root 117024 4月  17 2012 /bin/ls
~]$ ls /root/
acl.pdf          day3.txt~           模板  下载
ACL.pdf          install.log         视频  音乐
anaconda-ks.cfg  install.log.syslog  图片  桌面
day3.txt         公共的              文档
LAB2:
[alice@server ~]$ passwd
更改用户 alice 的密码 。
为 alice 更改 STRESS 密码。
(当前)UNIX 密码:
[root@server ~]# ps aux |grep passwd 查看进程
root      5307  0.0  0.0 165760  1892 pts/3    S+   23:38   0:00 passwd

set gid: 针对目录(也可以针对文件)
作用:一旦在一个目录上设置gid,在该目录下建立的文件自动继承目录的(属组)
[root@server ~]# useradd tom -G hr
[root@server ~]# useradd robin -G hr
[root@server ~]# mkdir /home/hr
[root@server ~]# chgrp hr /home/hr
[root@server ~]# chmod 770 /home/hr
[root@server ~]# ll -d /home/hr
drwxrwx---. 2 root hr 4096 12月  8 23:40 /home/hr
[tom@server ~]$ id
uid=3001(tom) gid=3001(tom) 组=3001(tom),501(hr) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[tom@server ~]$ cd /home/hr
[tom@server hr]$ touch tom.txt
[tom@server hr]$
[tom@server hr]$ ll
总用量 0
-rw-rw-r--. 1 tom tom 0 12月  8 23:41 tom.txt

[root@server ~]# chmod g+s /home/hr
[root@server ~]# ll -d /home/hr
drwxrws---. 2 root hr 4096 12月  8 23:41 /home/hr

[tom@server hr]$ touch tom2.txt
[tom@server hr]$ ll
总用量 0
-rw-rw-r--. 1 tom hr  0 12月  8 23:43 tom2.txt
-rw-rw-r--. 1 tom tom 0 12月  8 23:41 tom.txt

粘滞位sticky
作用:一旦目录被设置了sticky位,只有文件属主能删除自己的文件
=====================================================================
[root@server tmp]# su - robin
[robin@server ~]$ cd /home/hr
[robin@server hr]$ ls
tom2.txt  tom.txt
[robin@server hr]$ ll
总用量 0
-rw-rw-r--. 1 tom hr  0 12月  8 23:43 tom2.txt
-rw-rw-r--. 1 tom tom 0 12月  8 23:41 tom.txt
[robin@server hr]$
[robin@server hr]$ ll -d /home/hr
drwxrws---. 2 root hr 4096 12月  8 23:43 /home/hr
[robin@server hr]$ rm -rf *
[root@server ~]# chmod o+t /home/hr  设置sticky位
[root@server ~]# ll -d /home/hr
drwxrws--T. 2 root hr 4096 12月  8 23:46 /home/hr
[tom@server hr]$ id
uid=3001(tom) gid=3001(tom) 组=3001(tom),501(hr) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[tom@server hr]$
[tom@server hr]$ touch file2
[tom@server hr]$ ll
总用量 0
-rw-rw-r--. 1 tom hr 0 12月  8 23:48 file1
-rw-rw-r--. 1 tom hr 0 12月  8 23:48 file2
[robin@server hr]$ id
uid=3002(robin) gid=3002(robin) 组=3002(robin),501(hr) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[robin@server hr]$ ll
总用量 0
-rw-rw-r--. 1 tom hr 0 12月  8 23:48 file1
-rw-rw-r--. 1 tom hr 0 12月  8 23:48 file2
[robin@server hr]$ rm -rf *
rm: 无法删除"file1": 不允许的操作
rm: 无法删除"file2": 不允许的操作

特殊权限设置方法:
字符:
chmod u+s file  (二进制而且可执行)
chmod g+s dir
chmod o+t dir
数字:
set uid   4
set gid   2
sticky   1
[root@server ~]# chmod 7777 /home/hr
[root@server ~]# ll -d /home/hr
drwsrwsrwt. 2 root hr 4096 12月  8 23:48 /home/hr
[root@server ~]# chmod 3777 /home/hr
[root@server ~]# ll -d /home/hr
drwxrwsrwt. 2 root hr 4096 12月  8 23:48 /home/hr

文件属性
===================================================================
[root@server ~]# touch file200 file300
[root@server ~]# lsattr file200 file300
-------------e- file200
-------------e- file300
[root@server ~]#
[root@server ~]# chattr +a file200
[root@server ~]# chattr +i file300
[root@server ~]# lsattr file200 file300
-----a-------e- file200  允许追加内容
----i--------e- file300  禁止任何修改
[root@server ~]# echo 111 >> file200  往文件最后追加内容
[root@server ~]# echo 222 >> file200
[root@server ~]# cat file200
111
222
[root@server ~]# rm -rf file200
rm: 无法删除"file200": 不允许的操作
[root@server ~]# echo 222 >> file300
bash: file300: 权限不够
[root@server ~]# rm -rf file300
rm: 无法删除"file300": 不允许的操作
[root@server ~]# chattr -a file200
[root@server ~]# chattr -i file300
[root@server ~]# lsattr file200 file300
-------------e- file200
-------------e- file300
 
 
阅读(1416) | 评论(0) | 转发(0) |
0

上一篇:day3

下一篇:day4

给主人留下些什么吧!~~