limits of authority
Operating systems in the Unix tradition differ from those in the MS-DOS tradition in that they are not only multitasking systems,but also multi-users systems,as well.What exactly does this mean?It means that more than one person can be using the computer at the same time.While a typical computer will likely have only one keyboard and monitor,it can still be used by more than one user.For example,if a computer is attached to a network or the Internet,remote users can log in via (取道,通过,经由) ssh (secure shell) and operate the computer.In fact,remote users can execute graphical applications and have the graphical output appear on a remote display.The X Window System supports this as part of its basic design.
The multi-user capability(能力)of Linux is not a recent "innovation(创新)," but rather a feature that is deeply embedded into the design of the operating system.Considering the environment in which Unix was created,this maked perfect sense.(这会很有意义。)Years ago,before computers were"personal,"they were large,expensive,and centralized(集中化的).A typical university computer system,for example,consisted of a large central computer located in one building and terminals which were located throughout the campus(校园),each connected to the large central computer.The computer would support many users at the same time.In order to make this practical,a method had to be devised (想出)to protect the users from each other.After all,the actions of one user could not be allowed to crash the computer,nor could one user interfere with the files belonging to another user.
In this chapter we are going to look at this essential part of system security and introduce the following commands:
id - Display user identity
chmod-Change a files mode
umask-Set the default file permissions
su - Execute a command as another user
chown - Change a file's owner
chgrp -Change a file's group ownership
passwd - Change a user's password
Owner,groupmember and others
When we were exploring the system back in Chapter 4,we may have encountered a problem when trying to examine a file such as /etc/shadow:

The reason for this error message is that,as regular users,we do not have permission to read this file.
In the Unix security model,a user may own files and directories.When a user owns a file or directory,the user has control over (别像呆子一样的翻译成控制超过,控制)its access.Users can,in turn,belong to a group consisting of one or more users who are given access to files and directories by their owners.(用户反过来又属于一个由一个或多个用户组成的用户组,用户组成员由文件和目录的所有者授予对文件和目录的访问权限)In additon to granting access(授予权限) to a group,an owner may also grant some set of access rights to everybody,which in Unix terms is referred to as the world.To find out information about your identity,use the id command:

Let's look at the output.When user accounts are created,users are assigned a number called a user ID or uid which is then,for the sake (缘故) of (for the sake of 为了)the humans,(为了符合人类的习惯)mapped to a user name.The user is assigned a primary group ID or gid and may belong to additional groups.The above example is from a RHEL system.On other systems,such as Fedora,the output may look a little different:

As we can see,the uid and gid numbers are different.This is simply because Fedro starts its numbering of regular user accounts at 500,while Ubuntu starts at 1000.We can also see that the Ubuntu user belongs to a lot more groups.This has to do with the way Ubuntu managers privileges for system devices and services.
So where does this information come from?Like so many things in Linux,from a couple of text files.User accounts are defined in the /etc/passwd file and groups are defined in the /etc/group file.When user accounts and groups are created,these files are modified along with /etc/shadow which holds information about the user's password.For each user account,the /etc/passwd file defines the user(login)name,uid,gid,the account's real name,home directory,and login shell.If you examine the contents of /etc/passwd and /etc/group,you will notice that besides the regular user accounts,there are accounts for the superuser(uid o) and various other system users.
In the next chapter,when we cover processes,you will see that some of these other "users" are,in fact,quite busy.
While many Unix-like systems assign regular users to a common group such as "users",modern Linux practice is to create a unique,single-member group with the same name as the user.This makes certain types of permission assignment easier.
Read,write and execute
Access rights to files and directories are defined in terms of read access,write access,and execution access.If we look at the output of the ls command,we can get some clue as to how this is implemented:

The first ten characters of the listing are the file attributes.The first of these characters in the file type.Here are the file types you are most likely to see (there are other,less common types(不常见) too):
Table 10-1:File Types
| Attribute | File Type |
| - | a regular file |
| d | a directory |
| l | A symbolic link.Notice that with symbolic links,the remainning 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 drive. |
The remaining nine characters of the file attributes,called the file mode,represent the read,write,and execute permissions for the file's owner,the file's group owner,and everybody else:

When set,the r,w,and x mode attributes have the following effect on files and directories:
Table 10-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 truncated,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.(正好回应当文件有w属性时,不允许对文件进行重命名或删除,重命名是由目录属性决定的) |
| x | Allows a file to be treated as a program and executed.Program files written in scripting languages must also be set as readable to be executed. | Allows a directory to be entered,eg.,cd directory. |
Here are some examples of file attribute settings:
Table 10-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 access. |
| -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 regualr 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 group owner only. |
| lrwxrwxrwx | A symbolic link.All symbolic links have "dummy" permissions.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. |
chmod - change the mode of the file and directory
To change the mode (permissions) of a file or directory,the chmod command is used.Be aware that only the file's owner or the superuser can change the mode of a file or directory.chmod supports two distinct ways of specifying mode changes:octal(八进制) number representation,or symbolic representation.We will cover octal number representation first.
What The Heck is ls Octal?
Octal(base 8),and its cousin,hexadecimal(base 16) are number systems often used to express numbers on computers.We humans,owing to the fact that we(or at least most of us) were born with ten fingers,count using a base 10 number system.Computers,on the other hand,were born with only one finger and thus do all their counting in binary(base 2).Their number system only has two numerals,zero and one.So in binary,counting looks like this:
0,1,10,11,100,101,110,111
In octal,counting is done with the numberals zero through seven,like so:
0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,21.....
Hexadecimal counting uses the numerals zero through nine plus the letters"A" through "F":
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13....
While we can see(知道) the sense in binary(since computers only have one finger),what are octal and hexadecimal good for?The answer has to do with human convenience.Many times,small portions(部分)of data are represented on computers as bit patterns.Take for example an RGB color.On most computer display,each pixel is composed(组成) of three color components:eight bits of red,eight bits of green,and eight bits of blue.A lovely medium blue would be a twenty-four digit number:
010000110110111111001101
How would you like to read and write those kinds of numbers all day?I didn't think so.Here's where another number system would help.Each digit in a hexadecimal represents four digits in binary.In octal,each digit represents three binary digits.So our twenty-four digit medium blue could be condensed down to a six digit hexadecimal number:
Since the digits in the hexadecimal number "line up" with the bits in the binary number we can see that red component of our color is "43",the green "6F",and the blue "CD".
These days,hexadecimal notation(often spoken as "hex") is more common than octal,but as we shall soon see,octal's ability to express three bits of binary will be very useful...
With octal notation we use octal numbers to set the pattern of desired permissions.Since each digit in an octal number represents three binary digits,this maps nicely to the scheme used to store the file mode.This table shows what we mean;
| 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 |
By using three octal digits,we can set the file mode for the owner,group owner,and world;

By passing the argument "600",we were able to set the permissions of the owner to read and write while removing all permissions from the group owner and world.Though remembering the octal to binary mapping may seem inconvenient,you will usually only have to use a few common ones:7(rwx),6(rw-),5(r-x),4(r--),and o (---).
chmod also supports a symbolic notation for specifying file modes.Symbolic notation is divided into three parts:who the change will affect,which operation will be performed,and what permission will be set.To specify who is affected,a combination of the characters "u","g","o",and "a" is used as follows:
Table 10-4:chmod Symbolic Notation
| 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 comination of "u","g",and "o". |
If no character is specified,"all" will be assumed.The operaion may be a "+" indicating that a permission is to be added,a "-" indicating that a permission is to be taken away,or a "=" indicating that only the specified permissions are to be applied and that all others are to be applied and that all others are to be removed.
Permissions are specified with the "r","w","x"characters.Here are some example of symbolic notation:
Table 10-5:chmod Symbolic Notation Examples
| u+x | Add execute permission for the owner. |
| u-x | Remove execute permission from the owener. |
| +x | Add execute permission for the owner,group,and world.Equivalent to a+x |
| o-rw | Remove read and write permission 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,they are removed. |
| u+x,go=rw | Add execute permission for the owner and set the permission for the group and others to read and execute.If either the group or world previously had execute permissions,they are removed.Multipe specifications may be separated by commas.(多种设定可以用,隔开) |
Some people prefer to use octal notation,some folks(人们) really like the symbolic.Symbolic notation does offer the advantage of allowing you to set a single attribute without disturbing any of the others.
Take a look at the command man page for more details and a list of options.A word of caution regarding the "-recursive" option:it acts on both files and directories,so it's not as useful as one would hope since,we rarely want files and directories to have the same permissions.
Using GUI to set the file mode
Now that we have seen how the permission on files and directories are set,we can better understand the permission dialogs(会话) in the GUI.In both Nautilus(GNOME) and Konqueror(KDE),right-clicking a file or directory icon will expose a properties dialog.Here is an example from KDE 3-5:

Here we can see the settings for the owner,group,and world.In KDE,clicking on the "Advanced Permissions" button brings up another dialog that allows you set each of the mode attributes individually.Another victory for understanding brought to us by the command line!
umask - set the default permission
The umask command controls the default permissions given to a file when it is created.It uses octal notation to express a mask of bits to be removed from a file's mode attributes.Let's take a look:

We first removed any old copy of foo.txt fo make sure we were starting fresh.Next,we ran the umask command without an argument to see the current value.It responded with the value 0002(the value 0022 is another common default value),which is the octal representation of our mask.(这个数值是掩码的八进制表示形式)We next create a new instance of the file foo.txt and observe its permissions.
We can see that both the owner and group both get read and write permission,while everyone else only gets read permission.The reason that world does not have write permission is because of the value of the mask.Let's repeat our example,this time setting the mask ourselves:

When we set the mask to 0000 (effectively turning it off),we see that the file is now world writable.To understand how this works,we have to look at octal numbers again.If we take the mask and expand it into binary,then compare it to the attributes we can see what happens:
| Original file mode0 | ---rw-rw-rw- |
| Mask | 000 000 000 010 |
| Result | --- rw - rw- r-- |
Ignore for the moment the leading zeros(we'll get to those in a minute)and observe that where the 1 appears in our mask,an attribute was removed-in this case,the world write permission.That's what the mask does.Everywhere a 1 appears in the binary value of the mask, an attribute is unset.(掩码的二进制形式中,出现数字1的位置,相应地关掉一个文件模式属性)If we look at a mask value of 0022,we can see what it does:
| Origianal file mode | --- rw- rw- rw- |
| Mask | 000 000 010 010 |
| Result | --- rw- r-- r-- |
Again,where a 1 appears in the binary value,the corresponding (对应的) attribute is unset.Play with some values (try some sevens) to get used to how this works.When you're done,remember to clean up:

Most of the time you won't have to change the mask;the default provided by your distribution will be fine.In some high-security situations,however,you will want to control it.
Some Special Permissions
Though we usually see an octal permission mask expressed as a three digit number,it is more technically correct to express it in four digits.Why?Because,in addition to read,write,and execute permission,there are some other,less used,permission settings.
The first of these is the setuid bit (octal 4000).When applied to an executable file,it sets the effective user ID from that of real user (the user actually running the program) to that of the program's owner.Most often this is given to a few grograms owned by the superuser.When an ordinary user runs a program that is "setuid root",the program runs with the effective privileges(特权) of the superuser.This allows the program to access files and directories that an ordinary user would normally be prohibited(阻止,禁止) from accessing.Clearly,because this raises security concerns(关注),number of setuid programs must be held to an absolute minimum.
The second is the setgid bit (octal 2000) which,like the setuid bit,changes the effective group ID from the real group ID of the user to that of the file owner.If the setgid bit is set on a directory,newly created files in the directory will be given the group ownership of the directory rather the group ownership of the file's creator.This is useful in a shared directory when members of a common group need access to all the files in the directory,regardless of the file owner's primary group.
The third is called the sticky bit (octal 10000).This is a holdover from ancient Unix,where it was possible to mark an executable file as '"not swappable."(它可能把一个可执行文件标志为“不可交换的”)On files,Linux ignores the sticky bit,but if applied to a directory,it prevents users from deleting or renaming files unless the user is either the owner of the directory,the owner of the file,or the superuser.This is often used to control access to a shared directory(共享目录),such as /tmp.
Here are some examples of using chmod with symbolic notation to set these special permissions.First assigning setuid to a program:
chmod u+s program
Next,assigning setgid to a directory
chmod g+s dir
Finally,assigning the sticky bit to a directory:
chmod+dir
When viewing the output from ls,you can determine the special permissions.Here are some examples.First,a program that is setuid:
-rwsr-xr-x
A directory that has te setgid attribute
drwxrwsr-x
A directory with the sticky bit set:
drwxrwxrwt
Change identity
At various times,we may find it necessary to take on the identity of another user.Often we want to gain superuser privileges to carry out some administrative task,but it is also possible to "become" another regular user for such things as testing an account.There are three ways to take on an alternate identity(从事多重身份):
1.Log out and log back in as the alternate user.
2.Use the su command.
3.Use the sudo command.
We will skip the first technique since we know how to do it and it lacks the convenience of the other two.From within our own shell session,the su command allows you to assume the identity of another user,and either start a new shell session with that user's IDs,or to issue a single command as that user.The sudo command allows an administrator to set up a configuration file called /etc/sudoers,and define specific commands that particular users are permitted to execute under an assumed identity.The choice of which command to use is largely determined by which Linux distribution you use.Your distribution probably includes both commands,but its configuration will favor either one or the other.We'll start with su.
su - start a shell with another user and gid(以其他用户身份和组ID运行一个shell)
The su command is used to start a shell as another user.The command syntax looks like this:

If the "-l" option is included,the resulting shell session is a login shell for the specified user.This means that the user's environment is loaded and the working directory is changed to the user's home directory.This is usually what we want. If the user is not specified,the superuser is assumed.Notice that (strangely) the "-l" may be abbreviated(缩短) “-”,which is how it is most often used.To start a shell for the superuser,we would do this:
After entering the command,we are prompted for the superuser's password.If it is successfully entered,a new shell prompt appears indicating that this shell has superuser privileges(the trailing"#"rather than a "$") and the current working directory is now the home directory for the superuser (normally /root.) Once in the new shell,we can carry out commands as the superuser.When finished,type "exit" to return to the previous shell:

It is also possible to execute a single command rather than starting a new interactive command by using su this way:

Using this form, a single command line is passed to the new shell for execution.It is important to enclose the command in quotes,as we do not want expansion to occur in our shell,but rather in the new shell:

sudo - execute command with another user (以另一个用户身份执行命令)
The sudo command is like su in many ways,but has some importat additional capabilities.The administrator can configure sudo to allow an ordinary user to execute commands as a different user (usually the superuser) in a very controlled way.(管理员能够配置sudo命令,从而允许一个普通用户以不同的身份(通常是超级用户),通过一种非常可控的方式来执行命令)In particular,a user may be restricted to one or more specific commands and no others.Another important difference is that the use of sudo does not require access to the superuser's password.To authenticate using sudo,the user uses his/her own password.Let's say,for example,that sudo has been configured to allow us to rn a fictitious(假的) backup program called "backup_script", which requires superuser privileges.With sudo it would be done like this:

!!!!!!!这个实验自己没做成功
After entering the command,we are prompted for our passwd(not the superuser's) and once the authenication is complete,the specified command is carried out.One important difference between su and sudo is that sudo does not start a new shell,nor does it load another user's environment.This means that commands do not need to be quoted any differently than they would be without using sudo.Note that this behavior can be overridden by specifying various options.See the sudo man page for details.
To see what privileges are granted by sudo,use the "-l" option to list them:

!!!!!这个依旧自己电脑上搞实验没搞成,一直要输什么passwd,反正不是本地用户的passwd也不晓得是啥passwd
Unbuntu And sudo
One of the recurrent problems for regular users is how to perform certain tasks that require superuser privileges.These tasks include installing and updating software,editing system configuration files,and accessing devices.In the Windows world,this is often done by giving uers administrative privileges.This allows users to perform these tasks.However,it also enables programs executed by the user to have the same abilities.This is desirable in most cases,but it also permits malware(恶意软件) (malicious software)such as viruses to have free reign(统治,支配) of the computer.
In the Unix world,there has always been a larger division between regular users and administrators,owing to the multi-user heritage of Unix.The approach taken in Unix is to grant superuser privileges only when needed.To do this,the su and sudo commands are commonly used.
Up until a couple of years ago,most Linux distributions relied on su for this purpose.su didn't require the configuration that sudo required,and having a root account is traditional in Unix.This introduced a problem.Users were tempted to operate as root unnecessarily. In fact,some users operated their systems as the root user exclusively,(专门地)since it does away with all those annoying "permission denied" messages.This is how you reduce the security of a Linux system to that of a Windows system.Not a good idea.
When Ubuntu was introduced,its creators took a different tack(航向,换向,方法).By default,Ubuntu disables logins to the root account(by failing to set a password for the account因为不能为root用户设置密码),and instead uses sudo to grant superuser privileges.The initial user account is granted full access to superuser privileges via(取道,通过,经由) sudo and may grant similar powers to subsequent user accounts.
chown - change the owner and group owner of a file or directory
The chown command is used to change the owner and group owner of a file and directory.Superuser privileges are required to use this command.The syntax(句法,句法规则)of chown looks like this:

chown can change the file owner and/or file group owner depending on the first argument of the command.Here are some examples:
Table 10-6: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. |
Let's say that we have two users;janet,who has access to superuser privileges and tony,who does not.User janet wants to copy a file from her home directory to the home directory of user tony.Since user janet wants tony to be able to edit the file,janet changes the ownership of the copied file from janet to tony:

Here we see user janet copy the file from her directory to the home directory of user tony.Next,janet changes the ownership of the file from root(a result of using sudo) to tony.Using the trailing colon(:) in the first argument,janet also changed the group ownership of the file to the login group of tony,which happens to be group tony.
Notice that after the first use of sudo,janet was not prompted for her password?This is because sudo,in most configurations,"trusts" you for several minutes until its timer runs out.
chgrp - change file ownership
In older versions of Unix,the chown command only changed file ownership,not group ownership.For that purpose,a separate command,chgrp was used.It works much the same way as chown,except for being more limited.
Exercise permissions
Now that we have learned how this permissions thing works,it's time to show it off.We are going to demonstrate(说明) the solution to a comman problem -setting up a shared directory.Let's imagine that we have two users named "bill" and "karen." They both have music CD collections and wish to set up a shared directory,where they will each store their music files as Ogg Vorbis or MP3.User bill has access to superuser privileges via sudo.
The first thing that needs to happen is creating a group that will have both bill and karen as members.Using the graphical user management tool,bill creates a group called music and adds users bill and karen to it:

Next,bill creates the directory for the music files:

Since bill is manipulating files outside his home directory,superuser privileges are required.After the directory is created,it has the following ownerships and permissions:

As we can see,the directory is owned by root and has 755 permissions.To make this directory sharable,bill needs to change the group ownership and the group permissions to allow writing:

So what does this all mean?It means that we now have a directory,/usr/local/share/Music that is owned by root and allows read and write access to group music.Group music has members bill and karen,thus bill and karen can create files in directory /usr/local/share/Music.Other uers can list the contents of the directory but cannot create files there.
But we still have a problem.With the current permissions,files and directories created within the Music directory will have the normal permissions of the users bill and karen:

But we still have a problem.With the current permissions,files and directories created within the Music directory will have the normal permissions of the users bill and karen:

Actually there are two problems.First,the default umask on this system is 0022 which prevents group members from writing files belonging to other members of the group.This would not be a problem if the shared directory only contained files,but since this directory will store music,and music is usually organized in a hierarchy of artists and albums,members of the group will need the ability to create files and directories inside directories created by other members.We need to change the umask used by bill and karen to 0002 instead.
Second,each file and directory created by one member will be set to the primary group of the user rather than the group music.This can be fixed by setting the setgid bit on the directory:
浙公网安备 33010602011771号