Linux write permissions to folder

Ubuntu Documentation

Understanding and Using File Permissions

In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user «root» has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.

To change or edit files that are owned by root, sudo must be used — please see RootSudo for details.

If the owner read & execute bit are on, then the permissions are:

There are three types of access restrictions:

Permission

Action

chmod option

There are also three types of user restrictions:

User

ls output

Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.

Folder/Directory Permissions

Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.

Permission

Action

chmod option

(view contents, i.e. ls command)

(create or remove files from dir)

(cd into directory)

read restricts or allows viewing the directories contents, i.e. ls command

write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)

execute restricts or allows changing into the directory, i.e. cd command

» height=»16″ src=»/moin_static198/light/img/icon_cool.png» title=»Info » width=»16″/> Folders (directories) must have ‘execute’ permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus).

Permissions in Action

Using the example above we have the file «/etc/hosts» which is owned by the user root and belongs to the root group.

What are the permissions from the above /etc/hosts ls output?

Changing Permissions

The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.

Источник

Linux chmod and chown – How to Change File Permissions and Ownership in Linux

Linux is a multi user OS which means that it supports multiple users at a time.

Читайте также:  Linux list all logged in users

As many people can access the system simultaneously and some resources are shared, Linux controls access through ownership and permissions.

Linux file ownership

In Linux, there are three types of owners: user , group , and others .

Linux User

A user is the default owner and creator of the file. So this user is called owner as well.

Linux Group

A user-group is a collection of users. Users that belonging to a group will have the same Linux group permissions to access a file/ folder.

You can use groups to assign permissions in a bulk instead of assigning them individually. A user can belong to more than one group as well.

Other

Any users that are not part of the user or group classes belong to this class.

Linux File Permissions

File permissions fall in three categories: read , write , and execute .

Read permission

For regular files, read permissions allow users to open and read the file only. Users can’t modify the file.

Similarly for directories, read permissions allow the listing of directory content without any modification in the directory.

Write permission

When files have write permissions, the user can modify (edit, delete) the file and save it.

For folders, write permissions enable a user to modify its contents (create, delete, and rename the files inside it), and modify the contents of files that the user has write permissions to.

Execute permission

For files, execute permissions allows the user to run an executable script. For directories, the user can access them, and access details about files in the directory.

Below is the symbolic representation of permissions to user, group, and others.

Symbolic representation of permissions

Note that we can find permissions of files and folders using long listing ( ls -l ) on a Linux terminal.

Output of long listing

In the output above, d represents a directory and — represents a regular file.

How to Change Permissions in Linux Using the chmod Command

Now that we know the basics of ownerships and permissions, let’s see how we can modify permissions using the chmod command.

Syntax of chmod :

  • permissions can be read, write, execute or a combination of them.
  • filename is the name of the file for which the permissions need to change. This parameter can also be a list if files to change permissions in bulk.

We can change permissions using two modes:

  1. Symbolic mode: this method uses symbols like u , g , o to represent users, groups, and others. Permissions are represented as r, w, x for read write and execute, respectively. You can modify permissions using +, — and =.
  2. Absolute mode: this method represents permissions as 3-digit octal numbers ranging from 0-7.

Now, let’s see them in detail.

How to Change Permissions using Symbolic Mode

The table below summarize the user representation:

User representation Description
u user/owner
g group
o other

We can use mathematical operators to add, remove, and assign permissions. The table below shows the summary:

Operator Description
+ Adds a permission to a file or directory
Removes the permission
= Sets the permission if not present before. Also overrides the permissions if set earlier.

Example:

Suppose, I have a script and I want to make it executable for owner of the file zaira .

Current file permissions are as follows:

Let’s split the permissions like this:

To add execution rights ( x ) to owner ( u ) using symbolic mode, we can use the command below:

Output:

Now, we can see that the execution permissions have been added for owner zaira .

Additional examples for changing permissions via symbolic method:

  • Removing read and write permission for group and others : chmod go-rw .
  • Removing read permissions for others : chmod o-r .
  • Assigning write permission to group and overriding existing permission: chmod g=w .

How to Change Permissions using Absolute Mode

Absolute mode uses numbers to represent permissions and mathematical operators to modify them.

The below table shows how we can assign relevant permissions:

Permission Provide permission
read add 4
write add 2
execute add 1

Permissions can be revoked using subtraction. The below table shows how you can remove relevant permissions.

Permission Revoke permission
read subtract 4
write subtract 2
execute subtract 1

Example:

  • Set read (add 4) for user , read (add 4) and execute (add 1) for group, and only execute (add 1) for others.

chmod 451 file-name

This is how we performed the calculation:

Note that this is the same as r—r-x—x .

  • Remove execution rights from other and group .

To remove execution from other and group , subtract 1 from the execute part of last 2 octets.

  • Assign read , write and execute to user , read and execute to group and only read to others.

This would be the same as rwxr-xr— .

How to Change Ownership using the chown Command

Next, we will learn how to change the ownership of a file. You can change the ownership of a file or folder using the chown command. In some cases, changing ownership requires sudo permissions.

Syntax of chown :

How to change user ownership with chown

Let’s transfer the ownership from user zaira to user news .

chown news mymotd.sh

Command to change ownership: sudo chown news mymotd.sh

Output:

How to change user and group ownership simultaneously

We can also use chown to change user and group simultaneously.

How to change directory ownership

You can change ownership recursively for contents in a directory. The example below changes the ownership of the /opt/script folder to allow user admin .

How to change group ownership

In case we only need to change the group owner, we can use chown by preceding the group name by a colon :

Linux Permissions Guided Exercise

Up until now we have explored permissions, ownerships, and the methods to change them. Now we will reinforce our learning with a guided exercise.

Goal: To create groups and assign relevant permissions to its members. Verify access by accessing it from unauthorized users.

Task: Create a group called dev-team and add two members (John and Bob) to it. Create a folder /home/dev-team and change ownership to group dev-team . Verify that both users in the dev-team group have read and write access to the folder.

Create another group project-manager and add a user Fatima to it. Verify if the folder /home/dev-team is accessible by Fatima .

Visualization of the problem

We can visualize the problem like this:

Step 1: Switch to root user.
Switch to root user so that we have the rights to create new users and groups.

Use the sudo command with flag i .

If you have the root password, you can login using that as well.

Enter sudo -i to switch to the root user.

Enter whoami to find out if you are the root user:

If you do not have root access, use the commands with appending sudo .

Step 2: Create a group dev-team

Use the groupadd command.

Syntax: groupadd group-name

Enter groupadd dev-team to create the dev-team group

Verify: cat /etc/group | grep dev-team

Step 3: Create two new users John and Bob and add them to the dev-team group

Use command useradd .

useradd creates a new user and adds to the specified group.

Syntax: useradd -G groupname username

Where -G specifies the group.

useradd -G dev-team John

useradd -G dev-team Bob

Verify: cat /etc/group | grep dev-team

Step 4: Provide passwords for users John and Bob

Use command passwd

passwd creates a password for users.

Syntax: passwd username

Step 5: Create a directory in /home and name it dev-team

Use command mkdir

mkdir creates a directory.

Syntax: mkdir directory-name

Step 6: Change the group ownership of the folder dev-team to group dev-team

Use command chown

Syntax: chown :group-name folder

chown :dev-team /home/dev-team/

Step 7: Make sure the permissions of folder dev-team allow group members to create and delete files.

Use command chmod

Write permissions allow users and groups to create and delete files.

Syntax: chmod permissions folder

chmod g+w /home/dev-team/

Step 8: Ensure that ‘others’ don’t have any access to the files of dev-team folder.

Use command chmod

Remove read, write, execute permissions from ‘others’ if they exist.

Syntax: chmod permissions folder

chmod o-rx dev-team

Step 9: Exit the root session and switch to John

Use command exit to logout of the root user.

Use su to switch users.

Syntax: su — user

To confirm current user, use command whoami .

Verify with command whoami .

Step 10: Navigate to folder: /home/dev-team

Use command cd to switch folders.

Syntax: cd /path/to/folder

Confirm current path with pwd .

Step 11: Create an empty file in the folder: /home/dev-team

Use command touch to create an empty file.

Syntax: touch filename

Step 12: Change the group ownership of the created file to dev-team and verify.

Use command chown to change ownership.

Syntax: chown :group file-name

chown :dev-team john-file.txt

Once group ownership is modified, all members of the group can access this file.

Step 13: Exit the shell and switch to user Bob

Use command exit to exit the terminal.

Use su to switch users.

Syntax: su — user

To confirm current user, use command whoami .

Verify the current user with command whoami .

Step 14: Navigate to the path /home/dev-team

Use command cd to switch folders.

Syntax: cd /path/to/folder

Confirm current path with pwd .

Step 15: Find out Bob’s privileges to access john-file.txt

Use command ls -l for long listing.

Syntax: ls -l | grep file-name

Does group have rw- permissions?

ls -l | grep john-file.txt

Step 16: Modify the file john-file.txt while logged in as Bob

Use command echo to add some text to the file.

Syntax: echo «Some text» >>file-name

This would redirect the quoted text to end of the file.

echo «This is Bob’s comment» > john-file.txt

If all the permissions are correctly set, Bob would be allowed to edit and save this file. Otherwise you would get an error like this: Permission denied .

Verify cat john-file.txt

Step 17: Create another group project-manager and assign a member Fatima to it

Use command groupadd to add a new group.

Syntax: groupadd group-name

Create a new user with command useradd .

Use flag -G to assign a user to it.

Step 18: Navigate to folder /home/dev-team and verify if Fatima can access it

Use cd to navigate to /home/dev-team .

We get this error:

This is because, others don’t have any access to the folder dev-team .

If we recall, below are the rights of the dev-team folder.

Wrapping up

Permissions and ownerships are useful concepts for enforcing security across multi-user operating systems. I hope you were able to learn about changing permissions and ownerships in depth.

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can also read my other posts here.

Thanks to Tom Mondloch for his help with the guided exercise.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector