Linux set permissions to folder and subfolders

How To Set Permission For Folders and Subfolders in Linux

During daily Linux administration we generally work generally with file and directory permissions. In this tutorial we will look different examples about how to change and revert Linux file and directory permissions. There is different ways to set folder and sub folder permission in Linux but here we will describe most convenient way.

Read Only Owner

Say we have folder /opt/lamp and we want to change all sub folders permission to change 600. Using chmod with recursive options is the simplest one.

Read By Group But Not Edited

We may want to read by the owner group but not changed. We will set binary permission 660 for this.

Read By Others But Can Not Edit

Another useful permission is given other users read permission but denying from editing the file. We will use 664 binary permission.

Change Directory Permission with Find

If we only want to change permission of directory we need to specify the type as directory .

Change File Permission with Find

We can specify the type as file and change only files permissions. In this example we will change files located at /opt/lamp to 660 .

Change File Permission According Extension with Find

We can specify the file extension we want to change the permission to the find command. In this example we will look directory /opt/lamp and change permission of .c extension files to the 660 .

How To Set Permission For Folders and Subfolders in Linux Infografic

Источник

How to Manage File and Folder Permissions in Linux

For many users of Linux, getting used to file permissions and ownership can be a bit of a challenge. It is commonly assumed, to get into this level of usage, the command line is a must. Although there is always far more power and flexibility to be had, running seemingly complicated command isn’t alwaysa necessity. With the help of some of the most user-friendly desktop interfaces available, you can get away with little to no command line usage. Even with file permission and ownership.

That’s right, much to the surprise of many a new user, managing files and folders can be done from within the file managers. But before we get to the GUI, it’s always best to have a solid understanding of what it’s doing. So, we’ll start with the command line first.

Command line: File permissions

The commands for modifying file permissions and ownership are:

chmod – change permissions

chown – change ownership.

Neither command is difficult to use. It is important, however, that you understand the only user that can actually modify the permissions or ownership of a file is either the current owner or the root user. So, if you are user Bethany, you cannot make changes to files and folders owned by Jacob without the help of root (or sudo). For example:

A new folder was created on a data partition called /DATA/SHARE. Both users Bethany and Jacob need read and write access to this folder. There are a number of ways this can be done (one of which would be to join the users to a special group – we’ll go over managing groups in another post). If Bethany and Jacob are the only users on the system (and you know your network is safe – very important), you can change the permissions of the folder to give them access. One way to do this would be to issue the command:

Читайте также:  Гаджеты валюта для windows 10

The breakdown of the above command looks like:

sudo – this is used to gain admin rights for the command on any system that makes use of sudo (otherwise you’d have to ‘su’ to root and run the above command without ‘sudo’)

chmod – the command to modify permissions

-R – this modifies the permission of the parent folder and the child objects within

ugo+rw – this gives User, Group, and Other read and write access.

As you can probably surmise, this command opens wide the SHARE folder such that anyone on the system can have access to that folder. As I mentioned earlier, a more secure method would be to use groups. But we’re just using this for the purpose of demonstration.

The breakdown of permissions looks like this:

The ‘other’ entry is the dangerous one, as it effectively gives everyone permission for the folder/file. The permissions you can give to a file or folder are:

Using the -R switch is important. If you have a number of sub-folders and files within the SHARE directory, and you want the permissions to apply from the parent object (the containing folder) to the child objects (the sub-folders and files), you must use the -R (recursive) switch so the same permissions are applied all the way to the deepest folder, contained within the parent.

Command line: File ownership

Changing the ownership of a file or folder is equally as simple. Say Jacob moved a folder for Bethany into the SHARE directory – but Jacob still has ownership. This can be changed with a simple command:

Let’s break this down.

sudo – admin rights must be used since we are dealing with a folder that belongs to another user

chown – the command for changing ownership

-R – the recursive switch to make sure all child objects get the same ownership changes

bethany – the new owner of the folder

/DATA/SHARE – the directory to be modified

Should Bethany send the folder back to Jacob, the ownership would need to again be changed (again, this will be simplified with the use of groups).

GUI: File permissions

I’m going to demonstrate changing file permissions using the Nautilus file manager on an Ubuntu 13.10 system.

Let’s say you need to allow everyone to gain read/write permissions to the folder TEST. To do this, within the Nautilus file manager, follow these steps:

  1. Open Nautilus
  2. Navigate to the target file or folder
  3. Right click the file or folder
  4. Select Properties
  5. Click on the Permissions tab
  6. Click on the Access files in the Others section
  7. Select “Create and delete files”
  8. Click Change Permissions for Enclosed Files
  9. In the resulting window, Select Read and Write under Files and Create and delete files under Folders (Figure A)
  10. Click Change
  11. Click Close.

The trick comes when you need to change the permissions of a folder which does not belong to you. It can be done, but Nautilus must be started with admin access. To do this, follow these steps:

Open up a terminal window

Issue the command sudo -i

Issue the command nautilus

The sudo -i command gives you persistent access to sudo, until you enter the exit command to remove that access. Once Nautilus is open, you can change the permissions of the folder or file as described above – even if you are not the owner of the folder or file.

NOTE: If you’re using a distribution that doesn’t use sudo, alter the above instructions to:

Читайте также:  Что такое windows office 2020

Open up a terminal window

Issue the command su

Type your root password and hit Enter

Issue the command nautilus.

After you’ve completed the task, close the Nautilus window and then the terminal window.

GUI: Change ownership

Changing the ownership of a file or folder will most often require the use of admin rights. So for this, you’ll need to start Nautilus in the method described above.

For changing ownership of a folder or file through Nautilus, do the following:

In the Nautilus window (opened with admin rights), locate the folder or file in question

Right click the folder (or file)

Click on the Permissions tab

Select the new owner from the Owner drop-down (below)

That’s all there is to it. At this point you shouldn’t have any problems changing permissions or ownership for a file or folder with either the command line or the GUI. The use of groups will empower you to alter permission and ownership with more power and security – we’ll cover that soon. Until then, enjoy modifying your files and folders!

Источник

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.

Источник

How to View File and Folder Permissions in Ubuntu Linux Command Line

To View Permissions

To list permission of each file in a directory, in this example /var :

Читайте также:  Windows 10 как отключить экран заставки

To list file permissions for filename.txt :

To list directory permissions for /var :

To list permissions files in a folder including hidden files.

To list file permissions in a folder with human readable file size:

Changing Permissions

Single File or Folder

To change permissions of file to 644 for example:

To change permissions of directory to 755 for example:

All Files

To change all files recursively to 644 .

All Folders

To change all directories recursively to 755 .

Changing Owner/Group

Change Owner/Group of a File

Change Owner/Group of All Files/Directories

To change the owner and group of all files and directories recursively within a directory.

To change the owner and group of a directory, and all the files and directories within it recursively.

Permission Groups

Each file and directory has three user based permission groups:

  • owner – The Owner permissions apply only the owner of the file or directory, they will not impact the actions of other users.
  • group – The Group permissions apply only to the group that has been assigned to the file or directory, they will not effect the actions of other users.
  • all users – The All Users permissions apply to all other users on the system, this is the permission group that you want to watch the most.

Permission Types

Each file or directory has three basic permission types:

  • read – The Read permission refers to a user’s capability to read the contents of the file.
  • write – The Write permissions refer to a user’s capability to write or modify a file or directory.
  • execute – The Execute permission affects a user’s capability to execute a file or view the contents of a directory.

Numeric Meanings

Numeric Readable Explanation
0 No access.
1 –x Execute access.
2 -w- Write access.
3 -wx Write and execute access.
4 r– Read access.
5 r-x Read and execute access.
6 rw- Read and write access.
7 rwx Read, write and execute access.

Common File Permissions

Setting Numerical Owner Group Others
-rw——- 600 Read/Write
-rw-r—– 640 Read/Write Read
-rw-r–r– 644 Read/Write Read Read
-rw-rw-r– 664 Read/Write Read/Write Read
-rwx—— 700 Read/Write/Execute
-rwxr-xr-x 755 Read/Write/Execute Read/Execute Read/Execute
-rwx–x–x 711 Read/Write/Execute Execute Execute
-rw-rw-rw- 666 Read/Write Read/Write Read/Write
-rwxrwxrwx 777 Read/Write/Execute Read/Write/Execute Read/Write/Execute

Common Directory Permissions

Setting Numerical Owner Group Others
drwx—— (700) Read/Write/Execute
drwxr—– (750) Read/Write/Execute Read
drwxr-xr-x (755) Read/Write/Execute Read/Execute Read/Execute
drwxrwx— (770) Read/Write/Execute Read/Write/Execute
drwxrwxr-x (775) Read/Write/Execute Read/Write/Execute Read/Execute

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 200% using AI 🤖. Read my Ezoic review to find out how.

4 replies

Leave a reply

Cant I just grant root to a user and limit there ability to do anythi9ng administrative?

bassicly I want my second user to be able to access anything and everything but not be able to make system changes on need to remember my password.

Hello,
I am on Ubuntu 20.04, gnome, newcomer from 30 years using Windows.
I setup a backup of my own folders using rsync, Cron scheduler.

I can see the folders and files that were created on the external SD card using terminal command line.
But, gnome file manager shows me an empty folder only. Also tried PCManFM, same thing, can’t see the resulting backup.

Maybe you can tell me what to do…
Cheers
Alex

Hi.
I’m really struggling here.
All my files have 777 permissions. I cannot figure out how to view/open the files.

Everytime I try to open them in windows it will say there is no program to view.
In Ubuntu terminal I can’t seem to view it in any way.
Any ideas?
Thank you
Dave

Who is the owner and group of the files and parent directory? You can find out with:

ls -la /path/to/directory

You may have to change the ownership of the files and directory to your own username and group.

sudo chown -R username:groupname /path/to/directory*

Источник

Оцените статью