Wednesday, November 5, 2008

How to set folder permission

Linux (and almost all other Unixish systems) have three user classes as follows:
  • User (u): The owner of file
  • Group (g): Other user who are in group (to access files)
  • Other (o): Everyone else

You can setup following mode on each files. In a Linux and UNIX set of permissions is called as mode:

  • Read (r)
  • Write (w)
  • Execute (x)

However, above three modes or permission have different meaning for file and directory:

Linux Read mode permissions

  • Read access on a file allows you to view file
  • Read access on a directory allows you to view directory contents with ls command

Write mode permissions

  • Write access on a file allows you to write to file
  • Write access on a directory allows you to remove or add new files

Execute mode permissions

  • Execute access on a file allows to run program or script
  • Execute access on a directory allows you access file in the directory

Octal numbers and permissions

You can use octal number to represent mode/permission:

  • r: 4
  • w: 2
  • x: 1

For example, for file owner you can use octal mode as follows. Read, write and execute (full) permission on a file in octal is
0+r+w+x = 0+4+2+1 = 7

Only Read and write permission on a file in octal is
0+r+w+x = 0+4+2+0 = 6

Only read and execute permission on a file in octal is
0+r+w+x = 0+4+0+1 = 5

Use above method to calculate permission for group and others. Let us say you wish to give full permission to owner, read & execute permission to group, and read only permission to others, then you need to calculate permission as follows:
User = r+w+x = 0+4+2+1 = 7
Group= r+w+x = 0+4+2+0 = 6
Others = r+w+x = 0+0+0+1 = 1

Effective permission is 761.

chmod command

To setup file permission you need to use chmod command:
chmod {mode} {file-name}

To setup file permission 761 you need to use chmod command as follows:
# chmod 0761 file
To setup a file readable by anyone and writable by the owner only:
# chmod 644 file
To setup a file readable/executable by everyone and writable by the owner only:
# chmod 755 file
You can change permissions for all files and directories within a directory by using the -R option on the chmod command. For example, to setup others read and execute access to all files and directories (and files and directories within directories), you need to type command as follows (i.e. change the modes of the file hierarchies rooted in the files instead of just the files themselves):
# chmod -R 755 directory-name/

However, Linux (and UNIX) stores and process user database in different format.

The root user

  • The root user is the super user.
  • The root user can control entire Linux system including files, process, applications etc. The root user has full access to system.
  • You should not use root for day-to-day tasks as root has full system access.
  • Never ever, give root password to anyone.

For more information see What defines a user account?

User database

  • User database is stored in /etc/passwd file
  • User passwords and password expiry information stored in /etc/shadow file

Group database

  • User group information is stored in /etc/group file

UNIX/Linux User rights

There are two types of user rights (traditional):

  • File level user rights or permissions granted using chmod and chown command
  • Administrative (root) level rights granted using sudo.

If you type the following command:

$ ls -l

You’ll see something like the following:

-rw-r--r-- 1 indiver webusers 14814 2006-07-26 13:25 working-nixcraft.txt 

Look at 3rd, 4th and last columns.

  • vivek is username or the owner of the file (userid)
  • webusers is group name, so all users in webusers can access file working-nixcraft.txt
  • working-nixcraft.txt is the file name.

As the root user you can change or setup user file related rights/permission using chmod and chown command.

Task: change file owner and group

Consider following example:

$ ls -l foo.txt

Output:

-rw-r--r-- 1 vivek webgroups 8 2006-08-08 17:57 foo.txt

Change files ownership to tony user:

# chown tony foo.txt
# ls -l foo.txt

Output:

-rw-r--r-- 1 tony webgroups 8 2006-08-08 17:57 foo.txt

Change foo.txt group to ftpusers:

# chown tony:ftpusers foo.txt
# ls -l foo.txt

Output:

-rw-r--r-- 1 tony ftpuseers 8 2006-08-08 17:57 foo.txt

You can also operate on files and directories recursively using -R option. For example setup /var/www/html ownership to user lighttpd including subdirectories:

# chown -R lighttpd /var/www/html

Task: change files access rights/permissions

You need to use chmod command. Please refer the old article - how Linux file permissions work.

Task: Grant administrative rights to a normal user

You need to use sudo tool. It allows a permitted user to execute a command as the superuser or another user, as specified in the /etc/sudoers configuration file.

A note for Ubuntu Linux users

You can use chmod and chown command to setup user rights. Make sure you prefix all commands with word (command) sudo:

$ sudo chown tony:ftpusers foo.txt

Q. How do I use chmod and chown command under Linux?

A. Use chown command to change file owner and group information. Use chmod command to change file access permissions such as read, write etc.

chown command

chown command changes the user and/or group ownership of each given file.

For example following command will setup user and group ownership to root user only for /backup directory:
# chown root:root /backup
Set user user ownership to root user and allow any member of ftp group to access file.txt (provided that they have sufficient read/write rights).
# chown root:ftp /home/data/file.txt

HOPE THIS TIPS WILL HELP TO KNOW THE THE BASIC COMMAND OF LINUX ADMIN

No comments:

Post a Comment