特殊权限set_uid
例:
[root@bogon ~]# ls -l /usr/bin/passwd
-rw
s
r-xr-x. 1 root root 27832 6月 10 2014
/usr/bin/passwd
[root@bogon ~]#
s=set_uid权限 临时所有者身份
当普通用户执行带有s权限的文件时,会在执行时临时拥有文件所有者的权限(目录本身带x权限即可浏览,不需要加s权限)
例:给ls命令赋予s权限,让普通用户用ls命令查看/root目录
[root@localhost ~]# ls -l /usr/bin/ls
-
rwx
r-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls
[root@localhost ~]# chmod u+s /usr/bin/ls
[root@localhost ~]# ls -l /usr/bin/ls
-
rws
r-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls
root@localhost ~]#
su - elon
切换至普通用户elon
-bash-4.2$
ls /root
anaconda-ks.cfg
已经可以查看了
-bash-4.2$
加s权限方法 chmod u+s filename(
当文件没有x权限时,s权限显示为大S)
减s权限方法 chmod u-s filename
[root@localhost ~]#
chmod u+s
1.txt
[root@localhost ~]# ls -l 1.txt
-
rwS
r--r--. 1 root root 44 3月 29 09:36 1.txt
[root@localhost ~]#
chmod a+x
1.txt
[root@localhost ~]# ls -l 1.txt
-
rws
r-xr-x. 1 root root 44 3月 29 09:36 1.txt
[root@localhost ~]#
chmod u-s
1.txt
[root@localhost ~]# ls -l
总用量 8
-
rwx
r-xr-x. 1 root root 44 3月 29 09:36 1.txt
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
[root@localhost ~]#
特殊权限 set_gid
s=set_gid权限 临时所属组
当set_gid被用于文件时,非文件所属组的用户,访问此文件时,临时拥有文件所属组身份
当set_gid被用于目录时,任何用户在目录下创建的文件或子目录都会继承此目录的所属组身份
目录同时还会继承set_gid权限
例:
[root@localhost /]# ls -l /home/
总用量 0
d
rwx
rwx
r-x. 2 elon elon 32 3月 30 01:07 elon
[root@localhost /]# ls -l /home/elon
总用量 0
-rw-
rw-
r--. 1
elon elon
0 3月 30 01:04 1.txt
-rw
-rw-
r--. 1
elon elon
0 3月 30 01:07 2.txt
[root@localhost /]#
chmod g+s /home/elon
[root@localhost /]# ls -l /home
总用量 0
d
rwx
rws
r-x. 2 elon elon 32 3月 30 01:07 elon
[root@localhost /]# cd /home/elon/
[root@localhost elon]#
touch 3.txt
[root@localhost elon]# ls -l /home/elon
总用量 0
-rw-rw-r--. 1 elon elon 0 3月 30 01:04 1.txt
-rw-rw-r--. 1 elon elon 0 3月 30 01:07 2.txt
-rw-r--r--. 1
root elon
0 3月 30 01:38 3.txt
[root@localhost elon]#
mkdir aa/
[root@localhost elon]# ls -l
总用量 0
-rw-rw-r--. 1 elon elon 0 3月 30 01:43 1.txt
-rw-rw-r--. 1 elon elon 0 3月 30 01:07 2.txt
-rw-r--r--. 1 root elon 0 3月 30 01:38 3.txt
d
rwx
r-s
r-x. 2
root elon
6 3月 30 01:54 aa
[root@localhost elon]#
特殊权限stick_bit
t=stick_bit
[root@localhost elon]# ls -ld /tmp
d
rwxrwxrw
t
. 11 root root 4096 3月 29 22:58 /tmp
防删除位,除root用户外,文件所有者对文件有所有权
若用户将目录的权限设为777,则其它用户可以对其目录下的文件和子目录进行读写执行操作,即使文件本身未对其它用户赋权。
软链接文件
软链接文件(link)是源文件或目录的链接文件,相当于windows系统下的快捷方式。
ln -s 命令 是用来给源文件或源目录做软链接的命令
用法 ln -s /file path/filename(source addr) /file path/filename (destination addr)
例:
绝对软链接 (源文件或目录是以根 / 开头)
[root@localhost elon]# which ls
alias ls='ls --color=auto'
/bin/ls
[root@localhost elon]#
ln -s /bin/ls /tmp/ls2
[root@localhost elon]# ls -l /tmp
总用量 4
-rw-rw-r--. 1 andy andy 0 3月 30 03:33 1.txt
l
rwxrwxrwx. 1 root root 7 3月 30 05:48
ls2 -> /bin/ls
左端
l
代表文件属性,软链接文件用
l
表示
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vgauthd.service-e3qGDH
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vmtoolsd.service-QSnZYa
drwxrwxr-x. 2 andy andy 6 3月 30 03:37 usa
-rw-------. 1 root root 0 3月 28 07:19 yum.log
-rw-------. 1 root root 236 3月 28 11:24 yum_save_tx.2018-03-28.11-24.8U0ICh.yumtx
[root@localhost elon]#
用法 ln -s 给源目录做软链接
例:
[root@localhost elon]#
ln -s /home/elon /tmp/elon
[root@localhost elon]# ls -l /tmp
总用量 4
-rw-rw-r--. 1 andy andy 0 3月 30 03:33 1.txt
l
rwxrwxrwx. 1 root root 10 3月 30 05:53
elon -> /home/elon
l
rwxrwxrwx. 1 root root 7 3月 30 05:48
ls2 -> /bin/ls
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vgauthd.service-e3qGDH
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vmtoolsd.service-QSnZYa
drwxrwxr-x. 2 andy andy 6 3月 30 03:37 usa
-rw-------. 1 root root 0 3月 28 07:19 yum.log
-rw-------. 1 root root 236 3月 28 11:24 yum_save_tx.2018-03-28.11-24.8U0ICh.yumtx
[root@localhost elon]#
相对软链接(源文件或目录不是以绝对路径根 / 开始)弊端:若源文件/目录或软链接文件/目录被移动,则软链接失效
例:
[root@localhost tmp]# cd /tmp
[root@localhost tmp]# ls -l
总用量 4
-rw-rw-r--. 1 andy andy 0 3月 30 03:33 1.txt
lrwxrwxrwx. 1 root root 10 3月 30 05:53 elon -> /home/elon
l
rwxrwxrwx. 1 root root 7 3月 30 05:48
ls2 -> /bin/ls
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vgauthd.service-e3qGDH
drwx------. 3 root root 17 3月 29 20:58 systemd-private-5ff136219105418aa92f2f0073d472b5-vmtoolsd.service-QSnZYa
drwxrwxr-x. 2 andy andy 6 3月 30 03:37 usa
-rw-------. 1 root root 0 3月 28 07:19 yum.log
-rw-------. 1 root root 236 3月 28 11:24 yum_save_tx.2018-03-28.11-24.8U0ICh.yumtx
[root@localhost tmp]#
ln -s ls2 ls3
[root@localhost tmp]# ls -l
总用量 4
-rw-rw-r--. 1 andy andy 0 3月 30 03:33 1.txt
lrwxrwxrwx. 1 root root 10 3月 30 05:53 elon -> /home/elon
lrwxrwxrwx. 1 root root 7 3月 30 05:48 ls2 -> /bin/ls
l
rwxrwxrwx. 1 root root 3 3月 30 06:09
ls3 -> ls2
实用例子:(慎用!!)
将较小磁盘分区里较大的动态文件,软链接到较大的磁盘分区里
cp /boot/login.log /login.log
ln -s /login.log /boot/login.log
软链接不可删源文件或目录
硬链接文件 (目录不能做硬链接)
ln sourcename destinationname硬链接命令
例:
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.1
[root@localhost ~]#
ln
anaconda-ks.cfg anaconda-ks.cfg
.2
[root@localhost ~]# ls -l
总用量 12
-rw-------.
2
root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-------. 1 root root 1587 3月 30 07:15 anaconda-ks.cfg.1
-rw-------.
2
root root 1587 3月 28 07:23 anaconda-ks.cfg.2
[root@localhost ~]# ls -i
33574979
anaconda-ks.cfg 33575014 anaconda-ks.cfg.1
33574979
anaconda-ks.cfg.2
[root@localhost ~]#
跨分区不能做硬链接
例如
[root@localhost ~]#
ls /boot
config-3.10.0-693.el7.x86_64 initrd-plymouth.img
efi symvers-3.10.0-693.el7.x86_64.gz
grub System.map-3.10.0-693.el7.x86_64
grub2 vmlinuz-0-rescue-c709bd3b0870476e871a09457bd8799b
initramfs-0-rescue-c709bd3b0870476e871a09457bd8799b.img vmlinuz-3.10.0-693.el7.x86_64
initramfs-3.10.0-693.el7.x86_64.img
[root@localhost ~]#
ln /boot/initrd-plymouth.img /tmp/initrd.1
ln: 无法创建硬链接
"/tmp/initrd.1" => "/boot/initrd-plymouth.img":
无效的跨设备连接
[root@localhost ~]#
硬链接可以删源文件,但必须保留一个,全删会丢失数据