Anda di halaman 1dari 5

File permissions in Linux

Linux has inherited from UNIX the concept of ownerships and permissions for files. This is basically because it was conceived as a networked system where different people would be using a variety of programs, files, etc. Obviously, there's a need to keep things organized and secure. We don't want an ordinary user using a program that could potentially trash the whole system. There are security and privacy issues here as well. Let's face it, we don't want Bill to read Bob's love letters to the Janet who works in R & D. (because Janet is Bill's fiance) In the end, it's important to know what belongs to me, to you and to everybody. As we mentioned at the beginning of this course, the big advantage that Linux has is its multi-user concept- the fact that many different people can use the same computer or that one person can use the same computer to do different jobs. That's where the system of file permissions comes in to help out in what could be a very confusing situation. We're going to explain some basic concepts about who owns the file and who can do what with a file. We won't get into an enormous amount of detail here. We'll save that for the Linux system administration course. We will show you how to understand file permission symbols and how to modify certain files so that they're more secure.

File permission symbols


If you run the command ls -l in your home directory, you will get a list of files that may include something like this
-rw-r--r-1 bob users 1892 Jul 10 18:30 linux_course_notes.txt

This basically says, interpreting this from RIGHT to LEFT that the file, linux_course_notes.txt was created at 6:30 PM on July 10 and is 1892 bytes large. It belongs to the group users (i.e, the people who use this computer). It belongs to bob in particular and it is one (1) file. Then come the file permission symbols. Let's look at what these symbols mean:

The dashes - separate the permissions into three types The first part refers to the owner's (bob's) permissions. The dash - before the rw means that this is a normal file that contains any type of data. A directory, for example, would have a d instead of a dash. The rw that follows means that bob can read and write to (modify) his own file. That's pretty logical. If you own it, you can do what you want with it. The second part of the these symbols after the second dash, are the permissions for the group. Linux can establish different types of groups for file access. In a one home computer environment anyone who uses the computer can read this file but cannot write to (modify) it. This is a completely normal situation. You, as a user, may want to take away the rights of others to read your file. We'll cover how to do that later. After the two dashes (two here because there is no write permissions for the group) come the overall user permissions. Anyone who might have access to the computer from inside or outside (in the case of a network) can read this file. Once again, we can take away the possibility of people reading this file if we so choose.

Let's take a look at some other examples. An interesting place to look at different kinds of file permissions is the /bin directory. Here we have the commands that anybody can use on the Linux system. Let's look at the command for gzip, a file compression utility for Linux.
-rwxr-xr-x 1 root root 53468 May 1 1999 gzip

As we see here, there are some differences.


The program name, date, bytes are all standard. Even though this is obviously different information, the idea is the same as before. The changes are in the owner and group. Root owns the file and it is in the group "root". Root is actually the only member of that group. The file is an executable (program) so that's why the letter x is among the symbols. This file can be executed by everybody: the owner (root), the group (root) and all others that have access to the computer As we mentioned, the file is a program, so there is no need for anybody other than root to "write" to the file, so there is no w permissions for it for anybody but root.

If we look at a file in /sbin which are files that only root can use or execute, the permissions would look like this:
-rwxr--r-1 root root 1065 Jan 14 1999 cron

'cron' is a program on Linux systems that allows programs to be run automatically at certain times and under certain conditions. As we can see here, only root, the owner of the file, is allowed to use this program. There are no x permissions for the rest of the users. We hope you enjoyed this little walk-through of file permissions in Linux. Now that we know what we're looking for, we can talk about changing certain permissions.

chmod' explained
chmod is the program that is used to change file permissions on a Linux system. As we mentioned, others cannot modify your personal user files but you may not want other people to even read these files. You can use this command to take away the possibility of others prying into your private stuff. The syntax (parts separated by brackets) for using this command is the following: chmod [a/o/g/u] [+ or -] (plus or minus sign) [r/w/x] Let's analyze the syntax:

The symbols in the first brackets refer to the four concepts of users that Linux has. o a=all, all users o o=others, (other people) o g=group, the members of your group o u=user, (this means you) The symbol + adds permissions and the symbol - takes them away. As we say in the Linux world chmod giveth, chmod taketh away

Your actual rights to files - r=read rights, w=write rights (pardon the homophonic redundancy!) and x=executable rights

Practical examples of chmod


As we mentioned before, you may want to restrict rights to read a file. You would simply type: chmod og-r my_world_domination_plan.txt Now nobody can read your plans to take over the world except you. Best to keep those secret anyway. We also talked earlier about those files that you may have copied from a Windows partition or a floppy formatted for Windows. Even if they're not executable files, they'll show up as such (-rwxr-xr-x). You can change them to their correct permissions, getting rid of the executable status with: chmod a-x the_file Remember that if you copied them as 'root', you will have to use chmod as root to do this. You could even go one step further and change the ownership of the file to the user you desire. You may want to change root ownership to yourself (your user name). We'll go into this in the next part of the lesson.

Using chmod with number values


The permissions in Linux also can also be changed with number values. An accomplished Linux/Unix user will probably use this system more. When you're beginning with Linux, the method we described before will probably be clearer to you. Without going into tedious technical detail, we'll describe the chmod number system to you and look at two of the most common examples. As I said before, we have three sections to permissions; those of the owner of the file followed by those of the group and then the permissions of others. So if you use numbers with 'chmod', there will be three numbers following the command. One number corresponds to each group. Here's an example of a chmod command with numbers: chmod 644 grocery_list.txt If you had used 'pico' to write up a grocery list for your trip to the supermarket today, Linux would have given you 644 permission by default. That means, read and write permissions for the owner but no write permissions for the group and for others. So the number 6 means read and write permissions for you, the number 4 means read permissions for the group and the last number, 4 means read permissions for others. Now the question is: Why these numbers? Why not other numbers, like the number 8 or 12 or even 1345? Let's explain this. The three types of permissions correspond to three numbers. Read permission is given a value of 4. Think of it as the most important permission. (if you can't read a file, then what

else can you do with it?) Write permission is given a value of 2. Execute permission is given a value of 1. Here's what it would look like: User (or the owner) Group Others read-write-execute read-write-execute read-write-execute 4-2-1 4-2-1 4-2-1

It's really just a question of simple arithmetic. In our example of chmod 644 grocery_list.txt, we've added the 4 for read permission and to the 2 for write permission for the owner to get 6. We've just given the group and others read permission, so there's nothing to add in these two groups. Just give it a 4 in each case. Presto! 644.

Some practical examples with numbers


Let's take another look at my file that has my plans for dominating the world (my_world_domination_plan.txt). I had taken away the read permissions for everybody except myself using +/- letter method. (chmod og-r my_world_domination_plan.txt) Now let's do the same with the numbers. chmod 600 my_world_domination_plan.txt As you can see I have read and write permissions for me (4 read + 2 write equals 6) and 0 permissions for everybody else. The Zero value means 7 (maximum permission value) minus 4 minus 2 minus 1 equals 0.

Real world examples with 'chmod'


I'd like to talk about a real world practical example for doing this. There is a command mode program called 'fetchmail' for getting your e-mail. It's a very good program if you're using your computer as different users for various jobs. It will send mail to the different users based on e-mail addresses. The different addresses are stored in a file that you create called '.fetchmailrc' This file also includes the user name and password for retrieving mail from each address. We've got a possible security hole here, particularly if you're networked. This file shouldn't be read by everybody. When you create your .fetchmailrc file, you must then use chmod 600/chmod og-r on the file to solve the security issue. In fact, 'fetchmail' won't run if you don't. It will complain about the file's permissions. Let's look at another "real world" example. As the internet becomes more popular, websites need to be more user friendly and interactive. It's becoming more and more common to use scripts to improve the quality of a website's offerings. If you used a language like perl to write the scripts and you hosted your website with a company that runs Linux on their servers (we would hope that you would do this!), you would probably be given permission to use these scripts on your website. You would then have to give the scripts read and execute permissions so that the your visitors could use them. After you uploaded a script to your website, you would then issue this command: chmod 755 my_awesome_script

and the script would become "executable" (and readable) for the outside world. (7 - 4 read, 2 write 1 executable for you, 4 read and 1 executable for group and others). You could also use chmod og+rx my_awesome_script The script is then "converted" into a "program" so to speak. You have also converted yourself into a "web developer" with the right to hang your shingle on the door. chmod is one of those commands that are used most by system administrators. In our Linux system administration course, we'll go into this command in more detail. In this lesson we've shown you just a few practical examples to get your feet wet with this very important command.

Using 'chown'
The command chown is the chmod's cousin. It is used for changing the ownership rights of a file (hence the name 'chown' - change owner). It does not change the read, write and execution permissions however. This command, though available to every user, is probably going to be used when you're working as root. The command is uses like this: chown owner:group the_file Let's say you want to copy something from your Windows partition (if you have one). You mount the partition (as root) and to save time, you copy the file to your user directory /home/bob/. If you type ls -l the_file you'll get something like this:
-rw-r--r-1 root root 2428 Nov 17 13:18 the_file

As we now know from the previous lesson, root is the owner of the file. Therefore, root is the only one who has write permissions for the file (permission to modify its content). If you plan on working with the file as "bob", there isn't a snowball's chance in hell to modify that file until, as root, you run chown on the file. So let's do it! chown bob:bob the_file This example presupposes that your Linux version creates groups for each user. There are others that will create a generic group called users for everybody who uses the computer. On a network, groups are created according to the needs of the organization. On your single home computer, just type ls -l and see what system corresponds to you. As you can see, 'chown' is absolutely necessary if you're working as more than one user with the computer.

Anda mungkin juga menyukai