[ Pobierz całość w formacie PDF ]
.The 4-2-1 sequencecorresponds to the three rwx sequences in the permissions flags.2 1Handling Files 325How does this work? Well, suppose you want to make one of your files private, so that noone else (except the root operator, of course) can read or write your file.When you first createthe file, you and your group can read and write the file, while others can only read it.Knowingthat 4-2-1 matches rwx, and knowing that the group and others permissions follow yourpermissions in the permissions flag, you can use chmod with the octal number 600 to changethe permissions, for example:# touch afile# ls -l afile-rw-rw-r-- 1 bball bball 0 Nov 23 12:34 afile# chmod 600 afile# ls -l afile-rw------ 1 bball bball 0 Nov 23 12:34 afileThis makes the file readable and writable only by you, because you ve enabled read (4) + write(2) for yourself and no one else.To change the file permissions back to the original accesspermissions, you would want to enable read (4) + write (2) for you (6), your group (6), andread-only permissions for all others (4), and use the octal number 664, for example:# chmod 664 afile# ls -l afile-rw-rw-r-- 1 bball bball 0 Nov 23 12:34 afileYou can also change file directory permissions, and either let other people list the contentsof your directory, or have access only to the files in a directory, and not be able to list thedirectory contents.For example, to protect a directory from prying eyes (again, fromeveryone but the root operator), you can try# mkdir temp# cd temp# touch file1 file2 file3# cd.# chmod 700 temp# ls -ld tempdrwx------ 2 bball bball 1024 Nov 23 12:51 tempIf anyone else tries to look into your directory, they will see# ls /home/bball/templs: /home/bball/temp: Permission deniedBut what if you want to allow others to read files in the directory without being able to listthe contents? To do this, you can enable execute permission of your directory, for example:# chmod 701 temp# ls -ld tempdrwx----x 2 bball bball 1024 Nov 23 12:51 temp2 1Now, no other users will be able to list the contents of your directory, but can read files thatyou tell them are within, for example:# ls -ld /home/bball/templs: /home/bball/temp: Permission denied# ls -l /home/bball/temp/file1-rw-rw-r-- 1 bball bball 0 Nov 23 12:51 /home/bball/temp/file1326 Hour 21As you can see, using the chmod command s octal notation is not that hard.What you haveto decide is to whom you want to grant access, and what kind of access you d like your filesto have.The chmod command also has a command-line form as follows:ugoa +-= rwxXstugoThis book doesn t go into all the details of this notation (you can read the chmod command smanual page for more details), but the next few examples duplicate chmod s actions using theprevious examples.You can protect a file from anyone else with# ls -l file1-rw-rw-r-- 1 bball bball 0 Nov 23 13:50 file1# chmod go-rwx file1# ls -l file1-rw------ 1 bball bball 0 Nov 23 13:50 file1As you can see, the file is now readable and writable only by you, because you have specifiedthat your group (g) and others (o) do not (-) have read, write, or execute (rwx) permission.Now, you can protect your directory from prying eyes as follows:# chmod go-rwx tempAnd, to mimic the last example, to enable others to read files in the directory, but not list thedirectory contents, you can use# chmod o+x tempYou re now familiar with file and directory permissions, and using the chmod command.Thenext section shows you how you can change ownership of files or directories using the chowncommand.Changing File Ownership with the chownCommandThechown (change ownership) command, found under the /bin directory, is used to change,either permanently or temporarily, the ownership of files or directories.If you recall theprevious discussion of the /etc/group file in this hour, you ll remember that your users canbe assigned to different groups.Using the chown command, you can assign ownership todifferent users or groups.For example, if you ve created a text file, you can share it with members of your group orothers with the chmod command.By using chown, you can tell Linux specifically what otherusers or groups can have access to your file.You can use the groups command to find out whatgroups you belong to, for example:# groupsbball users2 1Handling Files 327This shows that the user, bball, belongs to two groups: bball and users [ Pobierz całość w formacie PDF ]