linux笔记2(文件的特性、文件的操作)

文件

检查文件

ls –l     以长模式查看文件的详细信息

file 检查文件类型

cd cd ~ 都是直接回到家

文件类型

linux中,所有的东西都被当成文件。

文件权限前的第一个字母用来标识文件类型

-        一般文件

d:        目录文件

b:        块设备文件

c:        字符设备文件

l:        链接文件,类型windows系统中的快捷方式

p:        人工管道(管道文件)

文件权限

对于每一个文件,Linux都提供了一套文件权限系统。

文件权限系统,将操作文件的用户都分成三类。

文件的拥有者(u)

文件所属组的成员(g)

其他用户(o)

文件权限类型

(r):用户是否有权限读取文件

(w):用户是否有权限写文件

执行(x):用户是否有权限执行文件

例:[root@localhost root]# ls -l

total 32

- rw- r-- r-- 1 root root 1201 Oct 21 05:37 anaconda-ks.cfg

d rwx r-x r-x 12 luowei luowei 4096 Oct 22 11:57 httpd-2.2.4

文件类型 u g o 文件硬连接参数文件的拥有者文件的所属群组文件大小

更改文件的权限

例:

[root@localhost root]# su - luowei

[luowei@localhost luowei]$ ls

dirtest test2

[luowei@localhost luowei]$ ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-rw-rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

[luowei@localhost luowei]$ chmod u-w test2    删除拥有者对此文件的写的权限

[luowei@localhost luowei]$ ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-r--rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

[luowei@localhost luowei]$ chmod u+w test2    给拥有者添加对此文件写的的权限

[luowei@localhost luowei]$ ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-rw-rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

[luowei@localhost luowei]$ chmod g-w test2    删除此群组对此文件的写权限

[luowei@localhost luowei]$ ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-rw-r--r-- 1 luowei luowei 149 Oct 22 10:30 test2

[luowei@localhost luowei]$ chmod o+w test2    给其他人添加对此文件的写权限

[luowei@localhost luowei]$ chmod g+x test2    给此群组添加对此文件的执行权限

[luowei@localhost luowei]$ chmod u=rwx test2 给拥有者设置对此文件读写执行权限

通过数字的方式来更改

4     2     1 执行

[luowei@localhost luowei]$ chmod 644 test2 设置权限

[luowei@localhost luowei]$ ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-rw-r--r-- 1 luowei luowei 149 Oct 22 10:30 test2

只有文件的拥有者和root才可以改变文件的权限

2

[luowei@redhat luowei]$ su - root

Password:

[root@redhat root]# cd /home/luowei

[root@redhat luowei]# mkdir test

[root@redhat luowei]# chown luowei:luowei test    修改test文件夹的所属用户及用户组

[root@redhat luowei]# chmod 677 test        设置它的权限,拥有者只能读和写,不能执行

[root@redhat luowei]# su - luowei

[luowei@redhat luowei]$ ls -l

总用量 4

-rwxrwxrw- 1 luowei luowei 0 10 24 19:45 aa

drw-rwxrwx 2 luowei luowei 4096 10 24 19:54 test

[luowei@redhat luowei]$ cd test

-bash: cd: test: 权限不够

建立链接

硬链接

语法:ln 源文件新建链接名

例:

[luowei@localhost luowei]$ ln /home/luowei/test.txt tom.txt 建立硬链接文件

[luowei@localhost luowei]$ ls

dirtest test.txt tom.txt

[luowei@localhost luowei]$ ls -l

total 12

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

-rw-r--r-- 2 luowei luowei 149 Oct 22 10:30 test.txt

-rw-r--r-- 2 luowei luowei 149 Oct 22 10:30 tom.txt

[luowei@localhost luowei]$ echo "hello world" >/home/luowei/test.txt 输入内容

[luowei@localhost luowei]$ cat /home/luowei/test.txt

hello world

[luowei@localhost luowei]$ cat tom.txt

hello world

[luowei@localhost luowei]$ rm -f /home/luowei/test.txt    删除链接文件

[luowei@localhost luowei]$ cat tom.txt        打开文件

hello world

[luowei@localhost luowei]$

软链接

语法:ln –s 源文件新建链接名

例:

[luowei@localhost luowei]$ ls

dirtest tom.txt

[luowei@localhost luowei]$ touch /home/luowei/test.txt

[luowei@localhost luowei]$ echo "hello" >/home/test.txt

-bash: /home/test.txt: No such file or directory

[luowei@localhost luowei]$ echo "hello" >/home/luowei/test.txt    //另一个方式向文件中写入文件

[luowei@localhost luowei]$ ln -s /home/luowei/test.txt jack.txt

[luowei@localhost luowei]$ ls -l

total 12

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

lrwxrwxrwx 1 luowei luowei 21 Oct 22 13:55 jack.txt -> /home/luowei/test.txt

-rw-rw-r-- 1 luowei luowei 6 Oct 22 13:55 test.txt

-rw-r--r-- 1 luowei luowei 12 Oct 22 13:49 tom.txt

[luowei@localhost luowei]$ cat jack.txt

hello

[luowei@localhost luowei]$ rm -f /home/luowei/test.txt

[luowei@localhost luowei]$ cat jack.txt

cat: jack.txt: No such file or directory

[luowei@localhost luowei]$

注:硬链接不能给目录做链接,软链接可以给目录做链接。

ext2/3中文件的构成

ext2ext3文件系统中,文件以inod+block的方式存在。一旦用rm指令删除文件中的inode记录。文件无法被找回。stat 指令可以用来检查文件的blockinode状况。

所属用户

每一个文件都有一个拥有者。文件的拥有者可以改变文件的权限。

root 用户可以用chown来改变文件的拥有者。

例:

[root@localhost luowei]# useradd xiaowang

[root@localhost luowei]# chown xiaowang tom.txt

[root@localhost luowei]# ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

lrwxrwxrwx 1 luowei luowei 21 Oct 22 13:55 jack.txt -> /home/luowei/test.txt

-rw-r--r-- 1 root root 0 Oct 22 14:06 test.txt

-rw-r--r-- 1 xiaowang luowei 12 Oct 22 13:49 tom.txt

所属组

每一个文件只能属于一个指定的组,文件的拥有者与root用户,可以chgrp改变文件所属的组。

修改群组,例:

[root@localhost luowei]# chgrp xiaowang jack.txt        更改jack.txt的群组

[root@localhost luowei]# ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

lrwxrwxrwx 1 luowei xiaowang 21 Oct 22 13:55 jack.txt -> /home/luowei/test.txt

-rw-r--r-- 1 root root 0 Oct 22 14:13 test.txt

-rw-r--r-- 1 xiaowang luowei 12 Oct 22 13:49 tom.txt

[root@localhost luowei]# chown root:luowei test.txt    另一种方式更改所属群组

[root@localhost luowei]# ls -l

total 8

drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

lrwxrwxrwx 1 luowei xiaowang 21 Oct 22 13:55 jack.txt -> /home/luowei/test.txt

-rw-r--r-- 1 root luowei 0 Oct 22 14:13 test.txt

-rw-r--r-- 1 xiaowang luowei 12 Oct 22 13:49 tom.txt

操作用户及用户组

1、建用户:

adduser phpq //新建phpq用户

passwd phpq //phpq用户设置密码

2、建工作组

groupadd test //新建test工作组

3、新建用户同时增加工作组

useradd -g test phpq //新建phpq用户并增加到test工作组

注::-g 所属组 -d 家目录 -s 所用的SHELL

4、给已有的用户增加工作组

usermod -G groupname username

或者:gpasswd -a user group

5、临时关闭:

/etc/shadow文件中属于该用户的行的第二个字段(密码)前面加上*就可以了。想恢复该用户,去掉*即可。

或者使用如下命令关闭用户账号:

passwd peter –l

重新释放:

passwd peter –u

6、永久性删除用户账号

userdel peter

groupdel peter

usermod G peter peter (强制删除该用户的主目录和主目录下的所有文件和子目录)

7、从组中删除用户

编辑/etc/group 找到GROUP1那一行,删除 A

或者用命令

gpasswd -d A GROUP

8、显示用户信息

id user

cat /etc/passwd

    文件名

文件名最大为255个字符。开头为.的文件为隐藏文件。

目录的详细信息用d标识。目录用 / 分隔。

强制位与冒险位

除了读写执行权限以外,ext3文件系统还支持强制位(setuid setgid)与冒险位(sticky)的特别权限针对ugo,分别有set uidset gid,及sticky

强制位与冒险位添加在执行权限的位置上,如果该位置上原已有执行权限,则强制位与冒险位以小写字母的方式表示,否则,以大写字母表示。

强制位set uidset gidugx位置上各采用一个s,冒险位(sticky)使用一个t在可执行文件上,用户可以添加set uidset gid,默认情况下,用户执行一个指令,会以该用户的身份来运行进程,因为指令文件上的强制位,可以让用户执行的指令,以指令文件的拥有者或所属组的身份运行进程。

Umask 冒险位和强制位,网上也有说成粘着位的

所谓的强制位和冒险位都是最最前面那个0的位置来表示,当前面那个位,24权限就叫强制位,1的权限就是冒险位,2代表的是GID4代表的是uid1代表的是sticky

gid,uid,sticky的作用

GID的作用:默认情况下,用户建立的文件属于用户当前所在的组但是设置了GID以后,表示在此目录中,任何人建立的文件,都会属于目录所属的组。注意:GID只能对目录设置. 这个在文件权限和团队使用文件目录很有用处

chmod g+s 文件名 用这个命令也可以给他设置GID

UID的作用:当一个文件设置了UID,那么所有用户执行这个文件的时候,都是以这个用户的所有者的权限来执行。

Sticky:一旦目录上设置了冒险位,则表示在此目录中,只有文件的拥有者、目录的拥有者与系统管理员可以删除文件。这个也是对目录进行设置,我们给目录一个冒险位,最后一个X变成了t.

一个例子是/bin/passwd ,要读写/etc/passwd文件需要超级用户权限,但一般用户也需要随时可以改变自己的密码,所以/bin/passwd就设置了suid,当用户改自己密码的时侯就拥有了超级用户权限。

    要删除一个文件,你不一定要有这个文件的写权限,但你一定要有这个文件的上级目录的写权限。也就是说,你即使没有一个文件的写权限,但你有这个文件的上级目录的写权限,你也可以把这个文件给删除,而如果没有一个目录的写权限,也就不能在这个目录下创建文件。

如何才能使一个目录既可以让任何用户写入文件,又不让用户删除这个目录下他人的文件,sticky就是能起到这个作用。stciky一般只用在目录上,用在文件上起不到什么作用。

在一个目录上设了sticky位后,(如/home,权限为1777)所有的用户都可以在这个目录下创建文件,但只能删除自己创建的文件(root除外),这就对所有用户能写的目录下的用户文件启到了保护的作用。

默认情况下,如果一个目录上有wx权限,则任何人可以在此目录中建立与删除文件。如果目录上设置了冒险位,则表示在此目录中,只有文件的拥有者、目录的拥有者与系统管理员可以删除文件。

用户可以用chmod指令来为文件设置强制位与冒险位。

set uidchmod u+s 文件名

set gidchmod g+s 文件名

stickychmod o+t 文件名

强制位与冒险位也可以通过一个数字加和,放在读写执行的三位数字前来指定。

4(set uid)

2(set gid)

1(sticky)

设置suid / guid

命令结果含义

chmod 4755 -rwsr-xr-x suid、文件属主具有读、写和执行的权限,所有其他用户具有读和执行的权限

chmod 6711 -rws--s--x suidsgid、文件属主具有读、写和执行的权限,所有其他用户具有执行的权限

chmod 4511 -rwS--xx suid、文件属主具有读、写的权限,所有其他用户具有执行的权限

上面的表中有具有这样权限的文件:rwS --x -- x,其中S为大写。它表示相应的执行权限位并未被设置,这是一种没有什么用处的suid设置可以忽略它的存在。

-------------------------------------------------------------------------------------------------

ACL

可以对某个文件设置该文件具体的某些用户的权限,意思就是通过ACL可以对一个文件权限做扩展,可以不同的用户对某个文件有不同的权限。

现在我们把redhat用户加上一个RWX的权限:setfacl -m u:redhat:rwx file

现在我们取消redhat用户的权限:setfacl -x redhat file

现在我们也取消redhat组的权限:setfacl -x g:redhat file

用ll看,权限后面有个+就可能是设置了文件权限,所以没必要没个文件都用gefacl 去看

getfacl <文件名>
获取文件的访问控制信息

setfacl设置文件的acl
-m 修改文件的acl
-x 取消对文件的设置

setfacl –m u:用户名:权限   文件名             #加用户权限
setfacl –m g:组名:权限   文件名            #加组权限
setfacl –x 用户名 文件名                     #-用户权限
setfacl –x g:组名 文件名                    #-组权限

注意,撤消ACL操作:
对用户直接加用户名字就可以了
对组,在前面加g:组名

-----------------------------------------------------------------------------------------------------

例:

[root@redhat root]# ls -l test1.txt

-rw-r--r-- 1 root root 0 10 24 20:23 test1.txt

[root@redhat root]# id root                显示root用户的详细信息

uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

[root@redhat root]# mkdir test                创建test目录

[root@redhat root]# touch test/test.txt        在目录test下建立一个test.txt文件

[root@redhat root]# chown root:luowei test    w修改目录test的所属组

[root@redhat root]# ls -l test                这样是显示目录test的文件属性

总用量 0

-rw-r--r-- 1 root root 0 10 24 20:25 test.txt

[root@redhat root]# ls -ld test                显示目录test的目录属性,拥有者是root,所属组是luowei

drwxr-xr-x 2 root luowei 4096 10 24 20:25 test

[root@redhat root]# chmod 745 test        修改目录test的权限

[root@redhat root]# ls -ld test

drwxr--r-x 2 root luowei 4096 10 24 20:25 test

[root@redhat root]# chmod g+s test        test目录所属组上设置强制位

[root@redhat root]# ls -ld test

drwxr-Sr-x 2 root luowei 4096 10 24 20:25 test

[root@redhat root]# cd test

[root@redhat test]# ls -l

总用量 0

-rw-r--r-- 1 root root 0 10 24 20:25 test.txt

[root@redhat test]# touch test1.txt

[root@redhat test]# chown root:luowei test1.txt    修改test1.txt的所属组

[root@redhat test]# cd /home/luowei

[root@redhat luowei]# chmod 776 test            修改test权限

[root@redhat luowei]# ls -ld test

drwxrwxrw- 2 luowei luowei 4096 10 24 19:54 test

[root@redhat luowei]# chown root:luowei test

[root@redhat luowei]# ls -ld test

drwxrwxrw- 2 root luowei 4096 10 24 19:54 test

[root@redhat luowei]# chmod g+s test             test目录所属组上设置强制位

[root@redhat luowei]# ls -ld test

drwxrwsrw- 2 root luowei 4096 10 24 19:54 test

[root@redhat luowei]# cd test

[root@redhat test]# ls

[root@redhat test]# touch test.txt

[root@redhat test]# ls -l test.txt

-rw-r--r-- 1 root luowei 0 10 24 21:05 test.txt

[root@redhat test]#

[root@redhat root]# cd /home/luowei

[root@redhat luowei]# mkdir sticky_test

[root@redhat luowei]# chown luowei:luowei sticky_test/

[root@redhat luowei]# chmod 777 sticky_test/

[root@redhat luowei]# chmod o+t sticky_test/         添加冒险位

[root@redhat luowei]# cd sticky_test/

[root@redhat sticky_test]# touch test.txt

[root@redhat sticky_test]# chown root:luowei test.txt

[root@redhat sticky_test]# ls -l test.txt

-rw-r--r-- 1 root luowei 0 10 24 22:44 test.txt

[root@redhat sticky_test]# su - luowei

[luowei@redhat luowei]$ cd sticky_test/

[luowei@redhat sticky_test]$ touch test2.txt

[luowei@redhat sticky_test]$ ls -l

总用量 0

-rw-rw-r-- 1 luowei luowei 0 10 24 22:46 test2.txt

-rw-r--r-- 1 root luowei 0 10 24 22:44 test.txt

[luowei@redhat sticky_test]$ rm test.txt

rm:是否删除有写保护的一般空文件'test.txt'? y

[luowei@redhat sticky_test]$ cd ..

[luowei@redhat luowei]$ ls -l

总用量 8

-rwxrwxrw- 1 luowei luowei 0 10 24 19:45 aa

[luowei@redhat luowei]$ su - root

Password:

[root@redhat root]# groupadd test             建立一个test用户组

[root@redhat root]# useradd -g test test1     test用户组下建立一个名为test1的用户

[root@redhat root]# id test1                     显示test1的用户信息

uid=501(test1) gid=501(test) groups=501(test)

[root@redhat root]# cd /home/luowei

[root@redhat luowei]# passwd test1             修改test1的密码

Changing password for user test1.

New password:

BAD PASSWORD: it is based on a dictionary word

Retype new password:

passwd: all authentication tokens updated successfully.

[root@redhat luowei]# su - luowei

[luowei@redhat luowei]$ su - test1

Password:

[test1@redhat test1]$ cd /home/luowei

-bash: cd: /home/luowei: 权限不够

查看目录或文件是哪里的

[root@redhat root]# which ls        查看ls所在的路径及命名信息

alias ls='ls --color=tty'

/bin/ls

[root@redhat root]# which ping        查看ping所在的路径信息

/bin/ping

[root@redhat root]# ls -l /bin/ping

-rwsr-xr-x 1 root root 28628 2003-01-25 /bin/ping

说明其它用户执行ping 时,是用root的权限使用的,因为ping设置了UID,并且拥有者是root

如果删除pingrootUID后:

[luowei@redhat luowei]$ ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.803 ms

--- 127.0.0.1 ping statistics ---

7 packets transmitted, 7 received, 0% packet loss, time 6008ms

rtt min/avg/max/mdev = 0.053/0.170/0.803/0.259 ms

[luowei@redhat luowei]$ su -

Password:

[root@redhat root]# chmod u-s /bin/ping         删除ping文件的UID

[root@redhat root]# ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.856 ms

--- 127.0.0.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1006ms

rtt min/avg/max/mdev = 0.098/0.477/0.856/0.379 ms

[root@redhat root]# su - luowei

[luowei@redhat luowei]$ ping 127.0.0.1            这将没有权限ping

ping: icmp open socket: Operation not permitted

[luowei@redhat luowei]$ su -

Password:

[root@redhat root]# chmod u+s /bin/ping             添加ping文件对rootUID

[root@redhat root]# su - luowei

[luowei@redhat luowei]$ ping 127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.058 ms

注意:uid只能对文件设定,gid既可以在文件上设定也可以在目录上设定,在目录上可以设定gid与冒险位(sticky),冒险位(sticky)只能设定在目录上。

umask

如果没有掩码的情况下,创建文件的默认权限为 rw- rw- rw- ; 创建目录的默认权限是 rwx rwx rwx

而在实际的环境中,一般都会受到掩码的作用。用户也可以自主改动umask值,并在改动后建立的文件上得到体现。一般用户的默认umask值为002,系统用户的默认umask值为022

如:

[luowei@redhat luowei]$ umask

0002

[luowei@redhat luowei]$ su -

Password:

[root@redhat root]# umask

0022

[root@redhat root]#

创建后的文件或目录的权限=默认权限-umask

例:

[root@redhat a]# umask 003        rootumask值改为003

[root@redhat a]# mkdir a_dir

[root@redhat a]# ls -ld a_dir

drwxrwxr-- 2 root root 4096 10 25 00:32 a_dir

[root@redhat a]# umask 022        rootumask值改回022

[root@redhat a]# mkdir b_dir

[root@redhat a]# ls -ld b_dir

drwxr-xr-x 2 root root 4096 10 25 00:32 b_dir

[root@redhat a]#

根目录下的目录

/bin:存储常用用户指令。

/boot:存储核心模块映像等启动文件

/dev:存储设备文件

/etc:存储系统、服务的配置目录与谁的

/home:存放个人主目录

/lib:存放库文件,诸如核心模块、驱动

/lost+found:存储fsck用的孤儿文件

/mnt:系统加载文件系统时用的常用挂载点

/opt:第三方工具使用的安装目录

/proc:虚拟文件系统,包含系统讯息等资料

/root:root用户的主目录

/sbin存储系统管理用指令

/tmp:临时文件的暂存点

/usr:存放用户直接相关的文件与目录

/var:存储在系统运行中可能会更改的数据

原文地址:https://www.cnblogs.com/luowei010101/p/2230866.html