码迷,mamicode.com
首页 > 系统相关 > 详细

谢烟客---------Linux之权限

时间:2017-07-30 23:49:23      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:linux

文件权限

    为了保障权责清晰和企业经营数据安全与保密,企业需要对系统中所有的操作人员进行分工,设置各自的功能权限,就能执行相应的操作。例如,为某个操作员设定了填制凭证的权限时,那么当该操作员注册进入账务子系统后,就可以填制凭证。

    只有系统管理员才有权进行权限设定


文件对权限的定义:属主的权限、属组的权限、非属主和属组的权限

-rw-rw-r--  1 root   utmp                9984 Jan  2  2016 wtmp
-
rw- 属主的权限,用u表示
rw- 属组的权限,用g表示 
r-- 非属主和属组的权限,用o表示
root  文件的属主
utmp  文件的属组


所有用户对文件只有3类权限,rwx,其含义是:

readable,r权限,可读权限位

    文件有此权限位时,代表“对应的用户对此文件可以用一些文件查看类工具查看文件的内容”

    目录:此权限位对应的用户可用"ls"查看目录中的内容

writeable,w权限,可写权限位

    文件:此权限位对应的用户可修改文件的内容

    目录:此权限位对应的用户可在目录中删除文件,创建文件

excutable,x权限,可执行权限位

    文件:此权限位对应的用户可把此文件提请到内核运行为一个进程

    目录:可用ls -l查看此目录中的文件列表,和cd 进入此目录


这三个二进制位可用一个八进制数来表示

--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
640 rw- r-- --- 
755 rwx r-x r-x


与文件权限相关的命令

chmod,chown,chgrp命令

[root@izpo45bh60h6bsz ~]# type chmod
chmod is /usr/bin/chmod
[root@izpo45bh60h6bsz ~]# chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
  or:  chmod [OPTION]... OCTAL-MODE FILE...
  or:  chmod [OPTION]... --reference=RFILE FILE...
-R, --recursive        递归修改目录及目录下的文件的权限



MODE的含义

1)对某类用户进行权限修改

   u=[rwx‘ ‘] g=[rwx‘ ‘]  o=[rwx‘ ‘]

  uo= ug= go=

2)对某类用户的某位权限修改

  u+-,g+-,o+-

  a+- 或+-

使用示例:

1、修改文件的某类用户的权限及某位权限
[root@izpo45bh60h6bsz tmp]# mkdir -m 644 testdir #创建目录文件
[root@izpo45bh60h6bsz tmp]# touch testdir/abc  #创建文件
[root@izpo45bh60h6bsz tmp]# ls -l
total 4
drw-r--r-- 2 root root 4096 Jul 30 12:35 testdir
[root@izpo45bh60h6bsz tmp]# ls -l testdir/abc
-rw-r--r-- 1 root root 0 Jul 30 12:39 testdir/abc

[root@izpo45bh60h6bsz tmp]# chmod u= testdir
[root@izpo45bh60h6bsz tmp]# ls -l
total 4
d---r--r-- 2 root root 4096 Jul 30 12:35 testdir
[root@izpo45bh60h6bsz tmp]# ls -l testdir/abc
-rw-r--r-- 1 root root 0 Jul 30 12:39 testdir/abc

[root@izpo45bh60h6bsz tmp]# chmod -R u+rwx testdir
[root@izpo45bh60h6bsz tmp]# ls -l
total 4
drwxr--r-- 2 root root 4096 Jul 30 12:39 testdir
[root@izpo45bh60h6bsz tmp]# ls -l testdir/
total 0
-rwxr--r-- 1 root root 0 Jul 30 12:39 abc

2、仿照a文件的权限修改B文件的权限
[root@izpo45bh60h6bsz tmp]# touch testdir/b
[root@izpo45bh60h6bsz tmp]# ls -l testdir/
-rwxr--r-- 1 root root 0 Jul 30 12:39 abc
-rw-r--r-- 1 root root 0 Jul 30 12:43 b

[root@izpo45bh60h6bsz testdir]# chmod --reference=abc b
-rwxr--r-- 1 root root 0 Jul 30 12:43 b

3、以八进制位修改文件的权限
[root@izpo45bh60h6bsz tmp]# ls -l testdir/
-rwxr--r-- 1 root root 0 Jul 30 12:39 abc
[root@izpo45bh60h6bsz testdir]# chmod 600 abc
[root@izpo45bh60h6bsz testdir]# ls -l
-rw------- 1 root root 0 Jul 30 12:39 abc


chown命令

[root@izpo45bh60h6bsz ~]# type chown
chown is /usr/bin/chown
[root@izpo45bh60h6bsz ~]# chown --h
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
  or:  chown [OPTION]... --reference=RFILE FILE...
  -R, --recursive        递归修改目录及目录下的文件的属主


1)修改属主

[root@izpo45bh60h6bsz tmp]# install -d hello  #创建目录
[root@izpo45bh60h6bsz tmp]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Jul 30 12:50 hello

drwxr-xr-x 2 root root 4096 Jul 30 12:55 hello
-rw-r--r-- 1 root root 0 Jul 30 12:55 abc
[root@izpo45bh60h6bsz tmp]# chown myuser hello
drwxr-xr-x 2 myuser root 4096 Jul 30 12:55 hello
-rw-r--r-- 1 root root 0 Jul 30 12:55 abc


2)修改属组

[root@izpo45bh60h6bsz tmp]# chown -R .myuser hello
-rw-r--r-- 1 root myuser 0 Jul 30 12:55 abc


3)修改属主和属组

[root@izpo45bh60h6bsz tmp]# chown -R root.root hello
drwxr-xr-x 2 root root 4096 Jul 30 12:55 hello
-rw-r--r-- 1 root root 0 Jul 30 12:55 abc



文件和目录创建时的遮罩码:umask

默认文件必然不能有执行权限,避免文件被恶意利用

默认目录有执行权限


管理员的umask

[root@izpo45bh60h6bsz tmp]# umask
0022


普通用户的umask

[root@izpo45bh60h6bsz tmp]# su - user1
-sh-4.2$ umask
0002


umask定义的位置

/etc/profile,/etc/bashrc中均定义了umask的值 
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi



创建文件的权限,默认

1、管理员
[root@izpo45bh60h6bsz tmp]# touch testfile
[root@izpo45bh60h6bsz tmp]# ls -l
-rw-r--r-- 1 root root    0 Jul 30 13:03 testfile
2、普通用户
-sh-4.2$ touch testfile
-sh-4.2$ ls -l
-rw-rw-r-- 1 user1 user1 0 Jul 30 13:02 testfile


文件的权限: 666-UMASK

目录的权限:777-UMASK

本文出自 “Reading” 博客,谢绝转载!

谢烟客---------Linux之权限

标签:linux

原文地址:http://sonlich.blog.51cto.com/12825953/1952080

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!