[root@hf-01 ~]# which passwd
/usr/bin/passwd
[root@hf-01 ~]# ls -l /usr/bin/passwd 会发现passwd文件含有特殊的s权限
-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd
[root@hf-01 ~]# ls /etc/shadow
/etc/shadow 改用户密码的文件
[root@hf-01 ~]# ls -l !$ 会发现更改密码的文件权限为000
ls -l /etc/shadow
----------. 1 root root 665 10月 26 08:04 /etc/shadow
[root@hf-01 ~]# ls -l /usr/bin/ls 这里想给ls命令加一个
-rwxr-xr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
然后复制SSH渠道,打开另一个终端2hf(1)下
[root@hf-01 ~]# su - hanfeng 切换到普通用户hanfeng下去
[hanfeng@hf-01 ~]$ whoami 来查看当前用户
hanfeng
[hanfeng@hf-01 ~]$ ls /root/ 这时访问/root/目录,会提示没权限
ls: 无法打开目录/root: 权限不够
这时回到之前的终端下1hf(0)下
[root@hf-01 ~]# chmod u+s /usr/bin/ls 给ls命令增加+s权限
[root@hf-01 ~]# ls -l /usr/bin/ls 这时查看ls命令,会发现x变化为了s权限(小写的s包括了x权限,大写的S表示没有x权限)
-rwsr-xr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
在切换到另一个终端2hf(1)下
[hanfeng@hf-01 ~]$ ls /root/ 这时会看到有访问权限了,就是因为set_uid权限赋给ls。(当在其他用户下使用ls命令时,在执行过程中会以root身份来运行)
11.txt 123 234 2.txt 33.txt anaconda-ks.cfg.1 ha.txt hf
[hanfeng@hf-01 ~]$ ls -ld /root/
dr-xr-x---. 5 root root 4096 10月 26 09:41 /root/
回到终端1hf(0)下
[root@hf-01 ~]# chmod u-s /usr/bin/ls 去除set_uid赋予给ls的权限
[root@hf-01 ~]# chmod u+s /usr/bin/ls 命令ls被赋予了s权限
[root@hf-01 ~]# chmod u-s /usr/bin/ls 收回s权限
[root@hf-01 ~]# chmod u=rws /usr/bin/ls 这也是赋予ls权限的方法,但是它缺少可执行的权限(小写的s包括了x权限,大写的S表示没有x权限)
[root@hf-01 ~]# !ls 会发现这里显示的是大写S
ls -l /usr/bin/ls
-rwSr-xr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
[root@hf-01 ~]# chmod u+x /usr/bin/ls 在这里再给它加上+x可执行的权限即可
[root@hf-01 ~]# !ls 会发现这里显示的是小写s
ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
[root@hf-01 ~]#
- set_gid作用在文件上时,和set_uid效果一致,会使普通用户临时拥有所属组的身份
[root@hf-01 ~]# chmod u=rwx /usr/bin/ls 将ls命令权限恢复成默认初始权限
[root@hf-01 ~]# !ls
ls -l /usr/bin/ls
-rwxr-xr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
[root@hf-01 ~]# chmod g+s /usr/bin/ls 设置set_gid权限
[root@hf-01 ~]# !ls
ls -l /usr/bin/ls
-rwxr-sr-x. 1 root root 117616 6月 10 2014 /usr/bin/ls
在切换到另一个终端2hf(1)下
[hanfeng@hf-01 ~]$ ls /root/ 将set_gid权限赋给ls,当在其他用户下使用ls命令时,(因为ls的所属组是root,所以就会以root身份来执行)
11.txt 123 234 2.txt 33.txt anaconda-ks.cfg.1 ha.txt hf
[hanfeng@hf-01 ~]$ ls -ld /root/
dr-xr-x---. 5 root root 4096 10月 26 09:41 /root/
[root@hf-01 ~]# ls -l
总用量 8
-rw-r--r--. 1 root root 0 10月 26 08:39 11.txt
drwxr-xr-x. 2 root root 6 10月 26 08:39 123
drwxrwxr-x. 2 root root 6 10月 26 08:57 234
-rwxrwxrwx. 1 root root 924 10月 25 06:49 2.txt
-rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt
-rw-------. 1 root root 973 8月 21 05:05 anaconda-ks.cfg.1
-rw-rw-r--. 1 root root 0 10月 26 09:41 ha.txt
dr-x--xr-x. 2 root root 18 10月 26 06:56 hf
[root@hf-01 ~]# chmod g+s 234 更改234目录的set_gid权限
[root@hf-01 ~]# ls -ld 234
drwxrwsr-x. 2 root root 6 10月 26 08:57 234
[root@hf-01 ~]# chown :hanfeng 234 再来更改234目录所属组的权限
[root@hf-01 ~]# ls -ld 234
drwxrwsr-x. 2 root hanfeng 6 10月 26 08:57 234
[root@hf-01 ~]# touch 234/gurui 在目录234下新建文件gurui
[root@hf-01 ~]# ls -l 234/ 查看所有者和所有组
总用量 0
-rw-r--r--. 1 root hanfeng 0 10月 27 07:46 gurui
[root@hf-01 ~]# mkdir 234/am 在目录234下新建目录am
[root@hf-01 ~]# ls -l 234/
总用量 0
drwxr-sr-x. 2 root hanfeng 6 10月 27 07:47 am
-rw-r--r--. 1 root hanfeng 0 10月 27 07:46 gurui
[root@hf-01 ~]# chmod g-s 234 去除set_gid临时权限
[root@hf-01 ~]# touch 234/gurui111 在234目录下新建gurui111文件
[root@hf-01 ~]# !ls 发现在root用户下创建的文件,所有组默认都是root
ls -l 234/
总用量 0
drwxr-sr-x. 2 root hanfeng 6 10月 27 07:47 am
-rw-r--r--. 1 root hanfeng 0 10月 27 07:46 gurui
-rw-r--r--. 1 root root 0 10月 27 07:48 gurui111
[root@hf-01 ~]# mkdir 234/am1 在目录234下新建目录am1
[root@hf-01 ~]# ls -l 234/ 发现在root用户下创建的目录,所有组默认都是root
总用量 0
drwxr-sr-x. 2 root hanfeng 6 10月 27 07:47 am
drwxr-xr-x. 2 root root 6 10月 27 07:49 am1
-rw-r--r--. 1 root hanfeng 0 10月 27 07:46 gurui
-rw-r--r--. 1 root root 0 10月 27 07:48 gurui111
系统中的/tmp/目录是拥有stick_bit权限的
[root@hf-01 ~]# ls -ld /tmp/
drwxrwxrwt. 9 root root 4096 10月 27 05:14 /tmp/
这drwxrwxrwt.里面的t(t里面包含x权限),就是stick_bit权限,
stick_bit(t):又叫做防删除位。只对目录有效,对文件无效。
相当于stick-bit设置others权限位。
在终端2hf(1)下
[hanfeng@hf-01 ~]$ whoami 查看所属主
hanfeng
[hanfeng@hf-01 ~]$ cd /tmp/ 切换到/tmp/目录下
[hanfeng@hf-01 tmp]$ ls
aminglinux amning mysql.sock yum.log
[hanfeng@hf-01 tmp]$ touch yunwei 新建文件yunwei
[hanfeng@hf-01 tmp]$ ls -l 会看到文件yunwei的所属主和所属组都是hanfeng
总用量 0
drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 aminglinux
drwxr-xr-x. 3 root root 14 10月 25 06:29 amning
srwxrwxrwx. 1 mysql mysql 0 10月 27 02:47 mysql.sock
-rw-r--r--. 1 user1 root 0 10月 26 07:48 yum.log
-rw-rw-r--. 1 hanfeng hanfeng 0 10月 27 08:40 yunwei
[hanfeng@hf-01 tmp]$ vi yunwei 并可以编辑文件
[hanfeng@hf-01 tmp]$ chmod 777 yunwei 将yunwei文件权限修改为777
[hanfeng@hf-01 tmp]$ ls -l 会看到yunwei的权限变化为-rwxrwxrwx.
总用量 4
drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 aminglinux
drwxr-xr-x. 3 root root 14 10月 25 06:29 amning
srwxrwxrwx. 1 mysql mysql 0 10月 27 02:47 mysql.sock
-rw-r--r--. 1 user1 root 0 10月 26 07:48 yum.log
-rwxrwxrwx. 1 hanfeng hanfeng 26 10月 27 08:41 yunwei
这时在切换到1hf(0)下
[root@hf-01 tmp]# whoami 查看所属主
root
[root@hf-01 tmp]# su - user1 切换到user1用户下
[user1@hf-01 ~]$ cd /tmp/ 切换到目录/tmp/下
[user1@hf-01 tmp]$ ls 会发现可以查看到文件
aminglinux amning mysql.sock yum.log yunwei
[user1@hf-01 tmp]$ vi yunwei 也可以进行编辑
[user1@hf-01 tmp]$ rm -f yunwei 不可以删除文件yunwei的
rm: 无法删除"yunwei": 不允许的操作
所以说,这个stick_bit权限叫防删除位,只有有权限的用户(比如root)才可以删除,而其他的用户是根本无法删除掉的
在终端1hf(0)下
[user1@hf-01 tmp]$ mkdir user1 新建目录user1
[user1@hf-01 tmp]$ chmod 777 user1 并把权限修改为777
[user1@hf-01 tmp]$ ls -l
总用量 4
drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 aminglinux
drwxr-xr-x. 3 root root 14 10月 25 06:29 amning
srwxrwxrwx. 1 mysql mysql 0 10月 27 02:47 mysql.sock
drwxrwxrwx. 2 user1 user1 6 10月 27 08:57 user1 777权限,意味着任何用户都可以去写,可读,可执行
-rw-r--r--. 1 user1 root 0 10月 26 07:48 yum.log
-rwxrwxrwx. 1 hanfeng hanfeng 45 10月 27 08:43 yunwei
切换到终端2hf(1)下
[hanfeng@hf-01 tmp]$ cd user1 切换到目录user1下面
[hanfeng@hf-01 user1]$ touch 1.txt 发现可以创建文件
[hanfeng@hf-01 user1]$ mkdir 234 可以创建目录
[hanfeng@hf-01 user1]$ ls -l
总用量 0
-rw-rw-r--. 1 hanfeng hanfeng 0 10月 27 09:03 1.txt
drwxrwxr-x. 2 hanfeng hanfeng 6 10月 27 09:03 234
在切换到终端1hf(0)下
[user1@hf-01 tmp]$ cd user1
[user1@hf-01 user1]$ ls
1.txt 234
[user1@hf-01 user1]$ ls -l
总用量 0
-rw-rw-r--. 1 hanfeng hanfeng 0 10月 27 09:03 1.txt
drwxrwxr-x. 2 hanfeng hanfeng 6 10月 27 09:03 234
[user1@hf-01 user1]$ rm -f 1.txt
[user1@hf-01 user1]$ ls
234
[user1@hf-01 user1]$ rm -r 234
rm:是否删除有写保护的目录 "234"?y
[user1@hf-01 user1]$ ls
[user1@hf-01 user1]$ pwd
/tmp/user1
[user1@hf-01 user1]$ ls -ld .
drwxrwxrwx. 2 user1 user1 6 10月 27 09:07
上述例子,这是因为这个目录,删除的这个文件所在的目录有没有写权限,而不是看删除的文件本身的权限,user1目录下有1.txt文件,要想删除1.txt,看的不是1.txt文件的权限,而是看1.txt所在目录的权限,它所在的目录是user1,user1就是777,777是任何用户可编辑的,所以就可删除,而一旦我们加上了stick_bit权限
```
[root@hf-01 ~]# ls -l /bin 这个/bin,就是一个软链接文件,真正是在/usr/bin/下
lrwxrwxrwx. 1 root root 7 8月 21 05:03 /bin -> usr/bin
[root@hf-01 ~]# /bin/ls
11.txt 123 234 2.txt 33.txt anaconda-ks.cfg.1 ha.txt hf
[root@hf-01 ~]# /usr/bin/ls 会发现和/bin/ls是一个软链接文件
11.txt 123 234 2.txt 33.txt anaconda-ks.cfg.1 ha.txt hf
```
软链接其实就是在一个文件本身存了另一个文件目录的路径。链接文件的大小适合路径是有关系的,路径越长,那这个软链接文件越大
软链接就像是windows中的快捷方式,可以很大程度的节省磁盘空间
比如要找一个文件,必须要在/tmp/123/12/1.txt,而本身这个1.txt文件放到了/root/123/这个目录下
而这个程序必须要在/tmp/12/目录下找到1.txt文件
方法一:可以拷贝一份到该目录下,但若是文件更改后,就会继续要拷贝,所以很麻烦。
方法二:给/root/123/1.txt做一个软链接
[root@hf-01 ~]# ls
11.txt 123 234 2.txt 33.txt anaconda-ks.cfg.1 ha.txt hf
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# ls
aminglinux amning mysql.sock user1 yum.log yunwei
[root@hf-01 tmp]# ln -s /tmp/yum.log /root/123/yum.log
[root@hf-01 tmp]# ls -l /root/123/ 这样就形成了一个软链接文件
总用量 0
lrwxrwxrwx. 1 root root 12 10月 30 05:44 yum.log -> /tmp/yum.log
[root@hf-01 tmp]# ln -s /tmp/amning/ /root/amning111
[root@hf-01 tmp]# ls -l !$
ls -l /root/amning111
lrwxrwxrwx. 1 root root 12 10月 30 05:51 /root/amning111 -> /tmp/amning/
[root@hf-01 tmp]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 10月 30 06:09 aa.log
srwxrwxrwx. 1 mysql mysql 0 10月 30 04:49 mysql.sock
drwxrwxrwx. 2 user1 user1 6 10月 27 09:07 user1
[root@hf-01 tmp]# ln -s aa.log ss.log
[root@hf-01 tmp]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 10月 30 06:09 aa.log
srwxrwxrwx. 1 mysql mysql 0 10月 30 04:49 mysql.sock
lrwxrwxrwx. 1 root root 6 10月 30 06:13 ss.log -> aa.log
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# ls -l
总用量 4
drwxr-xr-x. 4 user1 hanfeng 53 10月 30 06:01 aminglinux
lrwxrwxrwx. 1 root root 6 10月 30 06:13 ss.log -> aa.log
[root@hf-01 tmp]# mv ss.log aminglinux/
[root@hf-01 tmp]# cd aminglinux
[root@hf-01 aminglinux]# ls -l
总用量 0
drwxr-xr-x. 4 user1 hanfeng 31 10月 25 06:55 aming2
lrwxrwxrwx. 1 root root 6 10月 30 06:13 ss.log -> aa.log
这里会发现aa.log在飘红闪烁,这是因为这个文件不存在
[root@hf-01 ~]# df -h 查看磁盘分区情况
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 2.4G 16G 14% /
devtmpfs 489M 0 489M 0% /dev
tmpfs 494M 0 494M 0% /dev/shm
tmpfs 494M 6.7M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 197M 75M 123M 38% /boot
[root@localhost ~]# ln 1.txt 1_heard.txt
[root@localhost ~]# ln -s 1.txt 1_sorft.txt
[root@localhost ~]# ls -l
总用量 32
drwxr-xr-x. 3 root root 45 10月 26 16:11 111
-rw-r--r--. 1 root root 0 10月 26 15:54 111.12.txt
-rw-r--r--. 2 root root 391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root 5 10月 30 15:06 1_sorft.txt -> 1.txt
[root@localhost ~]# ls -i
33584735 111 33575033 1_sorft.txt 33575034 2.txt 33574987 anaconda-ks.cfg
33584737 111.12.txt 33575035 1.txt 33584734 3.txt 33575006 anaconda-ks.cfg.1
33575035 1_heard.txt 33584733 1.txt~ 33584736 3.txt~ 33575036 windows.txt
[root@localhost ~]# rm 1.txt
rm:是否删除普通文件 "1.txt"?y
[root@localhost ~]# ls -l
总用量 28
drwxr-xr-x. 3 root root 45 10月 26 16:11 111
-rw-r--r--. 1 root root 0 10月 26 15:54 111.12.txt
-rw-r--r--. 1 root root 391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root 5 10月 30 15:06 1_sorft.txt -> 1.txt
[root@localhost ~]# ls -i 1_heard.txt
33575035 1_heard.txt
目录不能做硬链接
不能跨文件系统(跨分区),因为不同的文件系统有不同的inode,不同的table目录体系。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/0
[root@localhost ~]# ls -i /boot/
71 config-3.10.0-514.el7.x86_64
67 grub
131136 grub2
76 initramfs-0-rescue-513f8b3950084e768a99df3a6cd3d9e6.img
75 initramfs-3.10.0-514.el7.x86_64.img
189445 initramfs-3.10.0-514.el7.x86_64kdump.img
74 initrd-plymouth.img
72 symvers-3.10.0-514.el7.x86_64.gz
70 System.map-3.10.0-514.el7.x86_64
77 vmlinuz-0-rescue-513f8b3950084e768a99df3a6cd3d9e6
73 vmlinuz-3.10.0-514.el7.x86_64
[root@localhost ~]# ln /boot/config-3.10.0-514.el7.x86_64 /tmp/conifg.1
ln: 无法创建硬链接"/tmp/conifg.1" => "/boot/config-3.10.0-514.el7.x86_64": 无效的跨设备连接