1 Linux中权限
Linux不仅是多任务系统(multitasking systems)同时也是多用户系统(multiuser systems)。这意味着多人在同时可以使用它。为了多用户的实现,必须实现保护每个用户可以正常使用系统。因此,一个用户不能 干预属于其它用户的文件等。以下是Linux中常用的关于权限的命令:
-
id—Display user identity. -
chmod—Change a file’s mode. -
umask—Set the default file permissions. -
su— Run a shell as another user. -
sudo—Execute a command as another user.zchown—Change a file’s owner. -
chgrp—Change a file’s group ownership.zpasswd—Change a user’s password.
2 权限的表示
2.1 关于权限的说明
当我们试图打开文件时候可能会碰到没有权限的情况,例如:
[me@linuxbox ~]$ file /etc/shadow
/etc/shadow: regular file, no read permission
[me@linuxbox ~]$ less /etc/shadow
/etc/shadow: Permission denied
原因是作为一名常规用户,没有读取这个文件的权限 在Unix安全模型中,用户可以拥有(own)文件和目录。 当用户拥有一个文件时,该用户可以控制其访问权限。 拥有(own)文件访问权限的用户也可以授予权限给由一个或多个用户组成的组。 除了授予对组的访问权限之外,所有者还可以向所有人授予某些访问权限集,在Unix术语中,这称为世界(world)。 查看用户的身份(identity )可以使用id命令:
[me@linuxbox ~]$ id
uid=500(me) gid=500(me) groups=500(me)
当一个用户被创建时,用户被分配一个用户标识(user ID 或 uid),与用户名一一对应。用户也被指派了一个基本用户组(primary group ID 或者 gid)并且还有可能属于另外的组,并且不同Linux版本不一定相同。比如,Fedora :
[me@linuxbox ~]$ id
uid=500(me) gid=500(me) groups=500(me)
Ubuntu:
[me@linuxbox ~]$ id
uid=1000(me) gid=1000(me) groups=4(adm),20(dialout),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),108(lpadmin),114(admin),1000(me)
uid不相同是因为 Fedora 给予的用户标识(user ID 或者 uid)从500开始,而Ubuntu 从1000开始,并且Ubuntu属于更多的组。这与Ubuntu管理系统设备和服务特权的方式有关。
关于用户的信息存储在多个文件中,用户账户信息(User accounts)存储在/etc/passwd文件中,组(groups )定义在/etc/group文件中。另外/etc/shadow文件存储着用户的密码信息。当用户和组创建时这些文件将被修改。
另外,/etc/passwd还定义着用户(登录)名,uid,gid,账户真实名称,home 文件夹,以及登录(login)shell。
除了普通用户外,如果打开/etc/passwd以及/etc/group也会注意到超级用户(uid 0)和其它的系统用户。
读写以及执行(Reading, Writing, and Executing) 当执行ls -l时候可以看到权限信息
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-r-- 1 me me 0 2012-03-06 14:52 foo.txt
其中,第一列是与权限相关的符号:
| 1 | 2 | 3 | 4 |
|---|---|---|---|
| - | rw- | rw- | r-- |
| 文件类型(见9.1表) | 所有者权限(见9.2) | 组权限(见9.2) | 世界(world)权限(见9.2) |
Table 9-1: File Types
| Attribute | File Type |
|---|---|
| - | A regularfile. |
| d | A directory. |
| l | A symbolic link. Notice that with symbolic links, the remaining file attributes are always rwxrwxrwx and are dummy values. The real file attributes are those of the file the symbolic link points to. |
| c | A character special file. This file type refers to a device that handles data as a stream of bytes, such as a terminal or modem. |
| b | A block special file. This file type refers to a device that handles data in blocks, such as a hard drive or CD-ROM dri |
Table 9-2: Permission Attributes
| Attribute | Files | Directories |
|---|---|---|
| r | Allows a file to be opened and read. | Allows a directory’s contents to be listed if the execute attribute is also set. |
| w | Allows a file to be written to or trun-cated; however, this attribute does not allow files to be renamed or deleted. The ability to delete or rename files is determined by directory attributes. | Allows files within a directory to be created, deleted, and renamed if the execute attribute is also set. |
| x | Allows a file to be treated as a pro-gram and executed. Program files writ-ten in scripting languages must also be set as readable to be executed. | Allows a directory to be entered; e.g., cd directory. |
Table 9-3: Permission Attribute Examples
| File Attributes | Meaning |
|---|---|
| -rwx------ | A regular file that is readable, writable, and executable by the file’s owner. No one else has any acces |
| -rw------- | A regular file that is readable and writable by the file’s owner. No one else has any access |
| -rw-r--r-- | A regular file that is readable and writable by the file’s owner. Members of the file’s owner group may read the file. The file is world readable |
| -rwxr-xr-x | A regular file that is readable, writable, and executable by the file’s owner. The file may be read and executed by everybody else. |
| -rw-rw---- | A regular file that is readable and writable by the file’s owner and members of the file’s owner group only. |
| Lrwxrwxrwx | A symbolic link. All symbolic links have “dummy” permis-sions. The real permissions are kept with the actual file pointed to by the symbolic link. |
| drwxrwx--- | A directory. The owner and the members of the owner group may enter the directory and create, rename, and remove files within the directory. |
| drwxr-x--- | A directory. The owner may enter the directory and create, rename, and delete files within the directory. Members of the owner group may enter the directory but cannot create, delete, or rename files |
2.2 使用 chmod 命令改变文件模式(file’s mode)
2.2.1 使用八进制(Octal Representation)来表示权限
用chmod命令来改变文件或者文件夹的模式(权限)。值得注意的是只有文件所有者或者超级用户可以改变文件权限模式。此命令支持两种方式不同的方式来改变模式:八进制数字表示(octal number representation)以及符号形式表示(symbolic repres-entation)方法。
因为表示文件模式的位数是三位因此每一种模式符号存在状态可以用一位二进制数字(0 or 1)来表示,通过使用此命令我们可以向所有者(owner),组(group)以及世界(world)设置模式:
Table 9-4: File Modes in Binary and Octal
| Octal | Binary | File Mode |
|---|---|---|
| 0 | 000 | --- |
| 1 | 001 | --x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r-- |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
例如:
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-r-- 1 me me 0 2012-03-06 14:52 foo.txt
[me@linuxbox ~]$ chmod 600 foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw------- 1 me me 0 2012-03-06 14:52 foo.txt
将参数600传递给 chmod,我们设置所有者读写权限,并且移除组(group)以及世界(world)权限。
2.2.2 使用符号(Symbolic Representation)来表示权限
chmod 还支持用于指定文件模式的符号表示法。 符号表示法分为三个部分:更改将影响谁,将执行哪些操作以及将要设置哪些权限。 要指定受影响的域,使用字符u,g,o和a的组合,如表中所示:
Table 9-5: chmod Symbolic Notation
| Symbol | Meaning |
|---|---|
| u | Short for user but means the file or directory owner. |
| g | Group owner. |
| o | Short for others but means world. |
| a | Short for all; the combination of u, g, and o |
如果没有指定上表中一个字符默认取全部字符。指定字符后后面操作符+,-分别表示增加去除权限,=号代表取指定权限,移除其余权限,以下是一些例子:
Table 9-6: chmod Symbolic Notation Examples
| Notation | Meaning |
|---|---|
| u+x | Add execute permission for the owner. |
| u-x | Remove execute permission from the owner. |
| +x | Add execute permission for the owner, group, and world. Equivalent to a+x |
| o-rw | Remove the read and write permissions from anyone besides the owner and group owner. |
| go=rw | Set the group owner and anyone besides the owner to have read and write permission. If either the group owner or world previously had execute permissions, remove them. |
| u+x,go=rx | Add execute permission for the owner and set the permissions for the group and others to read and execute. Multiple speci-fications may be separated by commas。 |
2.3 使用 umask 命令设置默认权限
umask 可以在创建文件时,设置授予文件的默认权限。
使用八进制来表示从初始文件模式(Original file mode)删除的模式属性的掩码。
[me@linuxbox ~]$ rm -f foo.txt
[me@linuxbox ~]$ umask
0002
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-r-- 1 me me 0 2012-03-06 14:53 foo.txt
首先删除文件foo.txt,接着运行不带参数的 umask 命令,可以看到目前的值为 0002,此值是八进制表示形式。接着创建新的 foo.txt 文件来观察它的权限。
可以看到所有者(owner),组(group)都具有读写两项权限以及任何人都有读权限。世界(world)因为 umask 的值转为二进制侯在写权限位上的掩码为1的原因而没有写的权限。即:
Original file mode --- rw- rw- rw-
Mask 000 000 000 010
Result --- rw- rw- r--
可以看到,当掩码中有1的位置,那个位置的属性将会被移除。在上面例子中八进制0002对应二进制000 000 000 010(右对齐,前面补0),1的位置在从右开始第二个,与之对应的世界(world)的w属性被移除。 当设置掩码为0000时(关闭掩码),可以看到世界(world)具有了写的权限。如下所示:
[me@linuxbox ~]$ rm foo.txt
[me@linuxbox ~]$ umask 0000
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-rw- 1 me me 0 2012-03-06 14:58 foo.txt
一些特别的权限
可以注意到掩码中有四个数字,左边第一个掩码表示一些特别的权限:
-
第一位掩码由二进制来表示,
4二进制表示100。第一位比特表示设置用户标识比特(setuid bit,八进制4000),当应用于可执行文件时,它将实际用户(实际运行程序的用户)的有效用户ID设置为程序所有者(通常是root)的有效用户ID。当普通用户运行setuid root进程时,程序将以超级用户的有效特权运行。这允许程序访问普通用户通常禁止访问的文件和目录。 -
第二位比特为设置组标识比特(setgid bit八进制2000),类似于设置用户标识比特(setuid bit),改变生效组标识( effective group ID)从实际组标识( real group ID)到文件所有者组标识。如果设置组标识(setgid bit)设置在一个文件夹上,在此文件夹中新建文件将会授予此文件夹的组权限。 当公用组的成员需要访问目录中的所有文件时,无论文件所有者的主组是什么。这个特点在共享目录中非常有用。
-
第三位比特为粘滞位(Sticky bit,八进制1000)。这个是从unix早期版本遗留而来,它可以使一个可执行文件不可交换(not swappable)。在文件上Linux会忽视此比特,但是如果在文件夹上,这时会防止此文件夹中的文件不会被其他用户进行删除,重命名操作(除非用户是超级用户)。这通常用于控制对共享目录(例如/tmp)的访问。
如以下例子来设置这些特别的权限:
首先,创建文件并查看权限:
[root@izuf67yuy6secatlp4sztez exercise]# >program
[root@izuf67yuy6secatlp4sztez exercise]# mkdir dir
[root@izuf67yuy6secatlp4sztez exercise]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Mar 28 14:49 dir
-rw-r--r-- 1 root root 0 Mar 28 14:49 program
对于程序设置 setuid 权限:
chmod u+s program
接着,设置 setgid 权限给文件夹:
chmod g+s dir
最后,增加粘滞位给文件夹:
chmod +t dir
可以使用 ls -l 命令来查看权限:
[root@izuf67yuy6secatlp4sztez exercise]# ls -l
total 4
drwxr-sr-t 2 root root 4096 Mar 28 14:44 dir
-rwSr--r-- 1 root root 0 Mar 28 14:44 program
3 更改用户标识
3.1 su 命令
-
su命令允许假设为另外一个用户身份来执行命令。 -
sudo命令允许管理员设置一个名为/etc/sudoers的配置文件,并定义允许特定用户在假定身份下执行的特定命令。 使用哪个命令的选择很大程度上取决于使用的Linux发行版。
使用 su 命令格式为:su [-[l]] [user]
如果su命令后面的参数-l,生成的shell会话是指定用户的登录shell。这意味着用户环境被加载并且工作目录会变成用户的home文件夹。当后面没有指定用户的时候,默认是超级用户。另外 -l 参数可以缩写成 -。
[me@linuxbox ~]$ su -
Password:
[root@linuxbox ~]#
输入su -命令后输入超级用户的密码。成功进入后可以看到结尾的符号由$变为了#表示超级用户特权并且此时目录是在 root 的 home 目录(通常为 /root),当在超级用户下执行完命令,在命令行输入 exit 命令返回原来的shell:
[root@linuxbox ~]# exit
[me@linuxbox ~]$
也可以使用su命令执行单条命令:su -c 'command'。执行此命令后单引号中命令被传到新的shell中执行。
[me@linuxbox ~]$ su -c 'ls -l /root/*'
Password:
-rw------- 1 root root 754 2011-08-11 03:19 /root/anaconda-ks.cfg
/root/Mail:total 0
[me@linuxbox ~]$
3.2 sudo 命令
sudo 类似于 su 命令,但是有一些重要的特性。管理员可以将 sudo 配置为允许普通用户作为另一个用户(通常是超级用户)以一种非常受控的方式执行命令。
另外一点是使用sudo命令并不需要管理员密码,为了使用sudo用户需要输入自己的密码。看一个例子:sudo已经配置为允许我们运行一个名为backup script的虚构备份程序,该程序需要超级用户特权。
[me@linuxbox ~]$ sudo backup_script
Password:
System Backup Starting...
输入命令后,输入当前用户密码。一旦认证完成,命令就可以执行了。
另外一个sudo与su命令不同的是sudo不开始一个新的shell,也不会加载其他用户的环境。这意味着不必在sudo命令后面的命令上加引号。
使用-l参数可以查看sudo命令授予的特权:
[me@linuxbox ~]$ sudo -l
User me may run the following commands on this host: (ALL) ALL
Ubuntu 中 sudo 的策略
普通用户经常遇到的问题之一是如何执行某些需要超级用户特权的任务。 例如安装和更新软件,编辑系统配置文件以及访问设备等等。 在Windows中,通常是通过向用户授予管理权限来完成的。 ,但是它也允许恶意软件(恶意软件)(例如病毒)自由运行计算机。
在Unix中,由于存在以下原因,由于 Unix的多用户使得常规用户和管理员之间始终存在较大的不同。 Unix中采用的方法是仅在需要时才授予超级用户特权。 为此,通常使用su和sudo命令。直到几年前,大多数Linux发行版都为此目的而依赖su。 su不需要sudo所需的配置,并且拥有root帐户在Unix中是传统的。 这带来了一个问题。 为了安全性用户倾向非root用户身份运行程序。而为了消除了所有烦人的“拒绝权限”消息,有些用户专门以root用户的身份操作他们的系统。 但是这样将Linux系统的安全性降低到Windows系统的安全级别。
在 Ubuntu 中,其创建者采取了不同的策略。 默认情况下,Ubuntu会禁用对根帐户的登录(因为无法为该帐户设置密码),而是使用 sudo 授予超级用户特权。 通过 sudo 授予初始用户帐户对超级用户特权的完全访问权限,并且可以向后续用户帐户授予类似的权限。
4 chown 命令授予权限
chown命令用来改变文件或文件夹的所有者(owner),组所有者(group owner)。使用此命令要有超级用户权限。命令格式如下:
chown [owner][:[group]] file...
根据后面的声明 chown 可以改变所有者(owner)或者组所有者(group owner)。
Table 9-7: chown Argument Examples
| Argument | Results |
|---|---|
| bob | Changes the ownership of the file from its current owner to user bob |
| bob:users | Changes the ownership of the file from its current owner to user bob and changes the file group owner to group users. |
| :admins | Changes the group owner to the group admins. The file owner is unchanged. |
| bob: | Change the file owner from the current owner to user bob and changes the group owner to the login group of user bob |
看一个例子,有两个用户:拥有超级用户权限的janet以及普通用户tony。
用户janet希望将一个文件从她的主目录复制到用户tony的主目录。由于用户janet希望tony能够编辑该文件,所以janet将复制文件的所有权从janet更改为tony。
[janet@linuxbox ~]$ sudo cp myfile.txt ~tony
Password:
[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt
-rw-r--r-- 1 root root 8031 2012-03-20 14:30 /home/tony/myfile.txt
[janet@linuxbox ~]$ sudo chown tony: ~tony/myfile.txt
[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt
-rw-r--r-- 1 tony tony 8031 2012-03-20 14:30 /home/tony/myfile.txt
上面命令值得注意的是:在第一次使用sudo输入密码后,Janet没有再次输入密码,这是因为在设置中sudo将会在第一次认证后信任若干分钟(直到信任周期结束)。
5 chgrp 命令授予权限
在较早的Unix版本中,chown 命令只更改了文件所有权(ownership),而没有更改组所有权(ownership)。为此,使用了单独的命令 chgrp 。它的工作方式与chown大致相同,只是更加有限。