《初入linux》--第六部分-用户与文件权限

一.用户权力设置

 遇到普通用户因权限不够不能执行的命令时,可以由超级用户下方权力,普通用户 sudo执行。

1.切换到root用户:su - root

2.普通用户sudo执行:

 方法:在超级用户下执行 visudo 开始编辑/etc/sudoers

 #开始的段落为注释,

 在开始不为#的空白处编写:获取特权的用户名 主机名称=(获得的身份) 要执行的命令

 例:

## Allow root to run any commands anywhere 
 98 root    ALL=(ALL)       ALL
 99 mazha localhost=(root)useradd,userdel
100 ## Allows members ofthe sys' group to run networking, software, 

 注:查询主机名使用:hostname

[root@localhost mazha]# hostname
localhost.localdomain

 注:第一次该用户使用sudo需要验证密码

 sudo使用格式:sudo useradd -n 888 mazha


二. 文件权限

 

什么是套接字?

是用户访问程序的一个接口,如:linux下 mysql程序中的mysql.sock,删除后用户将不能管理。

 

 文件记录次数:需要被删除多少次才会被删掉。

 

1.chown                                            管理文件所有人或所有组

      格式:           chown + 选项 + 所有人:所有组 + 文件或目录

                -c                               显示更改的部分的信息

          -f                                忽略错误信息

    -h                               修复符号链接

    -R                              处理指定目录以及其子目录下的所有文件

    -v                               显示详细的处理信息

    -deference              作用于符号链接的指向,而不是链接文件本身

其中,最常用的有以下几个用法:

chown username file/dir             改变文件或目录的所有人

[root@localhost ~]# ll file1
-rwxrwxrwx. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# chown mazha file1
[root@localhost ~]# ll file1
-rwxrwxrwx. 1 mazha root 0 Oct 10 22:56 file1

chown username:group file/dir 改变文件或目录的所有人和所有组

[root@localhost ~]# ll file1
-rwxrwxrwx. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# chown mazha:mazha file1
[root@localhost ~]# ll file1
-rwxrwxrwx. 1 mazha mazha 0 Oct 10 22:56 file1

chown -R username dir              改变目录内所有文件

[root@localhost dir1]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 10 23:03 file1
-rw-r--r--. 1 root root 0 Oct 10 23:03 file2
-rw-r--r--. 1 root root 0 Oct 10 23:03 file3
-rw-r--r--. 1 root root 0 Oct 10 23:03 file4
[root@localhost dir1]# cd ..
[root@localhost ~]# chown -R 777 dir1/
[root@localhost ~]# cd dir1/
[root@localhost dir1]# ll
total 0
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file1
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file2
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file3
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file4

2.chgrp                                              管理文件所有组

格式: chgrp + 选项 + 所有人:所有组 + 文件或目录

                     -c                 当发生改变时输出调试信息

                     -f                  不显示错误信息

                     -R                处理指定目录以及其子目录下的所有文件

                     -v                 运行时显示详细的处理信息

                    --dereference 作用于符号链接的指向,而不是符号链接本身

 --no-dereference 作用于符号链接本身

注:常用   chgrp 组名 file/dir   改变文件或目录的所有组

       username给数字代表uid,此时若uid对应的用户不存在,则所有人改为此uid,直到此用户被创建。

 例:

[root@localhost dir1]# ll
total 0
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file1
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file2
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file3
-rw-r--r--. 1 777 root 0 Oct 10 23:03 file4
[root@localhost dir1]# ls
file1  file2  file3  file4
[root@localhost dir1]# chgrp mazha file[1-4] 
[root@localhost dir1]# ll
total 0
-rw-r--r--. 1 777 mazha 0 Oct 10 23:03 file1
-rw-r--r--. 1 777 mazha 0 Oct 10 23:03 file2
-rw-r--r--. 1 777 mazha 0 Oct 10 23:03 file3
-rw-r--r--. 1 777 mazha 0 Oct 10 23:03 file4

3.对于 r w x 三个权限

r:对文件可以查看文件内容

 对目录表示可以查看里面的文件的信息(但是无法进入目录)

w:对文件可以修改文件内容

 对目录表示可以添加删除文件

x:可以执行文件内记录的程序动作

 对目录可以进入目录(无法看到目录中的文件)


4.chmod

 修改文件权限:

 1)字符方式:chmod [-R] <u|g|o> <+|-> <r|w|x> file/dir

 例:chmod u-x,g+w file1 file1   拥有者去掉执行权限,拥有组增加修改权限

         

[root@localhost ~]# ll file1
-rwxr-xrwx. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# chmod g-w,g-x,o-w file1
[root@localhost ~]# ll file1
-rwxr--r-x. 1 root root 0 Oct 10 22:56 file1

         chmod ugo=rwx file2          忽略之前的权限直接修改权限

[root@localhost ~]# ll file1
-rwxr--r-x. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# chmod ugo=rwx file1
[root@localhost ~]# ll file1
-rwxrwxrwx. 1 root root 0 Oct 10 22:56 file1


 2)数字方法:chmod 三位数字 file/dir

 注:三位数字为一位用十进制数字表示三位二进制数u+g+o所有权限正好可以用三位十进制数表示

 例:chmod777 file1  file1拥有所有权限

[root@localhost ~]# ll file1
-rw-r--r--. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# chmod 777 file1
[root@localhost ~]# ll file1
-rwxrwxrwx. 1 root root 0 Oct 10 22:56 file1
[root@localhost ~]# 

 系统默认权限 新建目录由777-umask值 得到,而新建文件由内核再减011(安全)

 要想改变默认权限,临时改变可用命令: umask xxx

 永久改变,需要改两个文件,分别为 /etc/bashrc  和  /etc/profile中的umask行。

 使用vim修改文件时,使用  / + 关键字 查找关键字,我们输入  /umask,找到关于umask的参数

  

 # By default, we want umask to get set. This sets it for non-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

    SHELL=/bin/bash

 

5.特殊权限:

1) suid  冒险位

 suid suid只针对二进制文件,文件内记录的程序产生的进程的发起者为文件的拥有者,此时与发起人身份无关

  注:在suid给 touch时,创建的file拥有着为root 

  具体使用方法:chmodu+s file || chmod 4777 file


2) sgid  强制位

 sgid 类似于suid ,but sgid只针对目录,在有sgid权限的目录下创建的文件的拥有者为目录的拥有者。

 方法:chmod  g+s dir || chmod 2777 dir


 3)sticky 粘制位

 sticky简称为 t权限 只针对目录,当一个目录有t权限,则others访问此目录时,即使有写权限,也不能删除里面的文件只能被所有人删除

  具体使用方法:chmod o+t dir || chmod 1777 dir

 注:对于目录的拥有者,即使加上t权限,也可以删除他人文件。

         当目录没有执行权限时,t权限显示为T,表示此时t权限无意义

 

 注:chmod 7777 dir 第一个7表示1+2+4,也就是同时拥有三个特殊权限

         目录属性中:第二位的数字表示该目录下的子目录数量,第五位数表示该目录中的目录和文件的属性大小。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值