Linux add write permission to directory

File Permissions in Linux/Unix: How to Read/Write & Change?

Updated October 7, 2021

Linux is a clone of UNIX, the multi-user operating system which can be accessed by many users simultaneously. Linux can also be used in mainframes and servers without any modifications. But this raises security concerns as an unsolicited or malign user can corrupt, change or remove crucial data. For effective security, Linux divides authorization into 2 levels.

In this Linux file commands tutorial, you will learn-

The concept of Linux File permission and ownership is crucial in Linux. Here, we will explain Linux permissions and ownership and will discuss both of them. Let us start with the Ownership.

Click here if the video is not accessible

Ownership of Linux files

Every file and directory on your Unix/Linux system is assigned 3 types of owner, given below.

A user is the owner of the file. By default, the person who created a file becomes its owner. Hence, a user is also sometimes called an owner.

Group

A user- group can contain multiple users. All users belonging to a group will have the same Linux group permissions access to the file. Suppose you have a project where a number of people require access to a file. Instead of manually assigning permissions to each user, you could add all users to a group, and assign group permission to file such that only this group members and no one else can read or modify the files.

Other

Any other user who has access to a file. This person has neither created the file, nor he belongs to a usergroup who could own the file. Practically, it means everybody else. Hence, when you set the permission for others, it is also referred as set permissions for the world.

Now, the big question arises how does Linux distinguish between these three user types so that a user ‘A’ cannot affect a file which contains some other user ‘B’s’ vital information/data. It is like you do not want your colleague, who works on your Linux computer, to view your images. This is where Permissions set in, and they define user behavior.

Let us understand the Permission system on Linux.

Permissions

Every file and directory in your UNIX/Linux system has following 3 permissions defined for all the 3 owners discussed above.

  • Read: This permission give you the authority to open and read a file. Read permission on a directory gives you the ability to lists its content.
  • Write: The write permission gives you the authority to modify the contents of a file. The write permission on a directory gives you the authority to add, remove and rename files stored in the directory. Consider a scenario where you have to write permission on file but do not have write permission on the directory where the file is stored. You will be able to modify the file contents. But you will not be able to rename, move or remove the file from the directory.
  • Execute: In Windows, an executable program usually has an extension “.exe” and which you can easily run. In Unix/Linux, you cannot run a program unless the execute permission is set. If the execute permission is not set, you might still be able to see/modify the program code(provided read & write permissions are set), but not run it.

File Permissions in Linux/Unix

Let’s see file permissions in Linux with examples:

ls – l on terminal gives

Here, we have highlighted ‘-rw-rw-r–‘and this weird looking code is the one that tells us about the Unix permissions given to the owner, user group and the world.

Here, the first ‘‘ implies that we have selected a file.p>

Else, if it were a directory, d would have been shown.

The characters are pretty easy to remember.

r = read permission
w = write permission
x = execute permission
= no permission

Let us look at it this way.

The first part of the code is ‘rw-‘. This suggests that the owner ‘Home’ can:

Читайте также:  Получить dns по ip windows

  • Read the file
  • Write or edit the file
  • He cannot execute the file since the execute bit is set to ‘-‘.

By design, many Linux distributions like Fedora, CentOS, Ubuntu, etc. will add users to a group of the same group name as the user name. Thus, a user ‘tom’ is added to a group named ‘tom’.

The second part is ‘rw-‘. It for the user group ‘Home’ and group-members can:

  • Read the file
  • Write or edit the file

The third part is for the world which means any user. It says ‘r–‘. This means the user can only:

Changing file/directory permissions with ‘chmod’ command

Say you do not want your colleague to see your personal images. This can be achieved by changing file permissions.

We can use the ‘chmod’ command which stands for ‘change mode’. Using the command, we can set permissions (read, write, execute) on a file/directory for the owner, group and the world.

Syntax:

There are 2 ways to use the command –

  1. Absolute mode
  2. Symbolic mode

Absolute(Numeric) Mode

In this mode, file permissions are not represented as characters but a three-digit octal number.

The table below gives numbers for all for permissions types.

Number Permission Type Symbol
0 No Permission
1 Execute –x
2 Write -w-
3 Execute + Write -wx
4 Read r–
5 Read + Execute r-x
6 Read +Write rw-
7 Read + Write +Execute rwx

Let’s see the chmod permissions command in action.

In the above-given terminal window, we have changed the permissions of the file ‘sample to ‘764’.

‘764’ absolute code says the following:

  • Owner can read, write and execute
  • Usergroup can read and write
  • World can only read

This is shown as ‘-rwxrw-r–

This is how you can change user permissions in Linux on file by assigning an absolute number.

Symbolic Mode

In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you can modify permissions of a specific owner. It makes use of mathematical symbols to modify the Unix file permissions.

Adds a permission to a file or directory

Removes the permission

Sets the permission and overrides the permissions set earlier.

Источник

Give user write access to folder [duplicate]

How do i give the user ‘testuser’ write privileges on the folder: /var/www/test/public_html

1 Answer 1

The simplest way is to use chown :

This will make the user & group testuser the owner of the file. IF YOU ARE USING A DIFFERENT USERNAME (run whoami or look before the @ of your terminal promp, or be lazy and replace testuser with $USER ), use that username instead. For instance user Flora colossus may have the username groot , in which case you would run sudo chown -R groot:groot . . If in doubt use the manual pages linked below.

or to use chmod (read and use carefully):

Which will allow read-write-execute permissions for the owner, group, and any other users. The execute bit is required for directories to work, files can get by with 666 permissions (strictly speaking most files shouldnt need the execute permission, but this is least likely to break stuff and does not require find etc). chmod is much more difficult to ‘undo’ if needed that the other options.

Here are manual pages on chown and chmod (these can also be found by running man chown and man chmod .)

I should add you can give groups of users write access as well (examples here and here).

Also beware giving global write access with the chmod command if you have not as trustworthy users/scripts running on the server etc — I recommend changing the group or the user permissions instead. If using chmod please read up on this and understand what it is doing.

Источник

how set read and write permissions for a directory

I just copied a 2.8 Gb folder to my usr/home directory using Nautilus. It contains many folders and files but I have permission to read none of them. How can I globally set read & write & execute permissions for myself.

I have one user account & the root account on my computer. However I don’t want to do everything as root.

2 Answers 2

First change the ownership:

(the : after the username means in fact the user default group, so it resets the group too at the same time)

Now you do not need sudo anymore you can operate under your normal user account.

First get yourself read and write access to all content:

Which means Read and Write access for User (the user owning the files, so that is you), but only Read for Group and Other. The = means to set the right, whatever it is now, you can also use + and — to respectively add or remove the given permission.

And the result should be clear from the explanation above (I do not know why people absolutely continue to use octal encoding to do the same thing, it has no superior value, but anyway if needed, Read is 4, Write is 2 and eXecute is 1, and you have to add the values. So my last example would be 660)

There is only one remaining step. You need to put the eXecute right on each directory and subdirectory otherwise cd will not work.

For that you can do:

The find command like it says will find, starting at directory every object that is of type d, d meaning directory here, and the xargs command will apply the following ( chmod u+x ) on all of them, and based on the previous explanations, the u+x part should be straightforward.

Also, next time, if you start the copy directly under your username, the permissions should be ok from the beginning. If not it means you may have strange permissions on the top directory where you do the copy.

Источник

How can I give write-access of a folder to all users in linux?

I installed apache2 on Ubuntu just now, and noticed that the /var/www folder is protected. I can just sudo everything but I would rather just give it write access.

How can I do this?

I tried sudo chmod 7777 /var/www but it didn’t work.

8 Answers 8

To best share with multiple users who should be able to write in /var/www , it should be assigned a common group. For example the default group for web content on Ubuntu and Debian is www-data . Make sure all the users who need write access to /var/www are in this group.

Then set the correct permissions on /var/www.

Additionally, you should make the directory and all directories below it «set GID», so that all new files and directories created under /var/www are owned by the www-data group.

Find all files in /var/www and add read and write permission for owner and group:

You might have to log out and log back in to be able to make changes if you’re editing permission for your own account.

There’s a simpler way to do this, try doing this command.

Essentially, the chmod command alters permissions and the -R switch affects all users. Then it is simply giving the correct permissions to use.

You can also replicate what jtimberman suggested using access control lists. The setfacl command accepts -s to replace an existing ACL or -m to modify it; -R to make directory ACLs recursive; and -d to make the specified settings the default, which is useful if you’re anticipating forthcoming user accounts.

These just set the permissions as you would for the user, group, other, and mask using chmod:

And this could be how you’d do it for a specified user or his/her group:

And of course, the strength is that you can designate any specific user, multiple users, etc., all without having to modify your group settings. And unlike chmod, if you want some groupies to have access to one directory and other groupies to have access only to another, it’s actually possible with setfacl. Finally, to view a directory’s ACLs, run getfacl:

And you can specify -R to see the ACLs for subdirectories or -d to see the defaults.

Источник

How do I set read/write permissions my hard drives?

I’ve recently installed Ubuntu 11.10 on my laptop, but I can’t do anything with my 1.5TB external drive, and my 500GB because I don’t have write permission. Are there any specific commands I can use in the terminal to set the read/write permissions?

The external is NTFS, and the 500GB is ext4.

11 Answers 11

for your 500 GB hard-drive (ext4) filesystem, you need to give the write and execute permission on /media/username/your_drive partition:-

Brief Explanation:

sudo :- it will elevate your priviledges to execute the command.

chmod:- command to change the permissions

u :- user

g:- group

o :- other

/media/username/your_drive :- partition

For your NTFS partition please follow fossfreedom’s advice.

Hope this is helpful.

If you don’t mind the security problems you can do a recursive chmod in order to change the permissions of all the files.

Also if your files were created in another OS like windows they will have different ownership you can do the same as above to change the ownership of the files

Thats the way I solved a similar problem for my friend after migrating from windows and also after migrating from Linux Mint to Ubuntu.

To fix read/write issue ntfs, just install these packages:

when installed, in the dash, type in and run: ntfs-config enter your password when prompted, and then you can enjoy read/write support for ntfs file systems.

Don’t mark all your files as executable as some answers suggest. Use 755 for directories and 644 for files. This will set the x bit for directories in order to list their contents but not for files.

I had the same problem and solved it with nautilus as root.

if nautilus is not installed:

Before running nautilus make sure the partition or hard disk is mounted.

Run nautilus as root with

Your partition or hard disk should appear on the left.

Right click on it -> select «Properties»

In the new window that appears, select the «Permissions» tab. From here you can change the owner if you need to, as well as the permission for a certain user, root, or others.

I was having a hard time solving the problem and this solution worked for me

install physical storage device manger:

Open storage device manger:

Choose your required drives

  • Press assist
  • Uncheck open as read only
  • Check owner user of file system and write your username
  • Press ok and apply
  • Remount the drive
  • Note: if you can’t change files to binary executables, go to special files and check permit execution of files as binaries, and go to step 7

    Using Terminal (Use this when you are currently logged in Ubuntu):

    Quickly open the terminal or press Ctrl Alt T

    First you need to find out the partition’s name which you want to access, run the following command:

    Then run this command in your terminal, to access your drive in read/write mode.

    OR Run this command (if the previous didn’t work)

    I formatted my extended partition using GParted and the resulted drive had only root level access. In order to grant access to my username, I executed below command.

    I look around the forum for answers.

    I have 3 users, «user1″ ,»user2», «user3»:

    • user1 : is sudo user with most of the access
    • user2 : is also sudo user with less access
    • user3 : is just another user with no sudo access

    Im trying to give access to partitions 1 and 2 to user1, user2 and user3 . The owner of the partition is root. the partitions are mounted at

    Note : I tried to mount the partition using

    Which created the directory to mount the partitions.

    • (I dont know this worked or not)
    • Used nautilus as root.
    • if nautilus is not installed: sudo apt-get install nautilus
    • Before running nautilus make sure the partition or hard disk is mounted.

    Run nautilus as root with

    Your partition or hard disk should appear on the left.

    Right click on it -> select «Properties»

    In the new window that appears, select the «Permissions» tab.

    Kept the owner as «root» and group as «user1» with read and write access for both owner and group.

    From here you can change the owner if you need to, as well as the permission for a certain user, root, or others.

    Note: The user1 ,user2 and user3 did not get access to the partitions yet

    Step 2: Added User2 and user3 to group «user1».

    Went to the last line entered:

    Saved and Exited

    Note: Now i am able to read and write files to the partitions 1 and 2.

    Only issue is, if i have logged into user1 , then try to access partition 1 from user2 , it is not accessible.

    And access partition 1 from user2.

    I don’t know if this is the right way to do it. Just combined many responses and did. Somehow its working.

    Источник

    Читайте также:  Образ windows для флэшки
    Оцените статью
    Operator Description