Regístrese ahora para una mejor cotización personalizada!

How to easily change folder and file permissions on Linux

Dec, 20, 2023 Hi-network.com
Rockhopper penguins
Frizi/Getty Images

File permissions are an important aspect of the Linux operating system for security and privacy. You see, Linux is a multi-user operating system, which means more than one person can be logged on to the system at one time. 

Say, for example, you have a file calledprivate.txtin the Documents directory of your home (so ~/Documents). By default on most modern Linux distributions, only the owner can view and/or edit the file. So, if userolivialogs in, she cannot see the contents of your home directory. 

Also: Thinking about switching to Linux? 9 things you need to know

But what about if you create a directory, outside of home, that you want to allow others to use? Let's say the directory is/user/share/data, which you could create with the command:

sudo mkdir/usr/share/data

Once you've created the directory, the only way to add anything to it is by usingsudofor admin privileges -- and that includes allowing anyone on the system to be able to read and write to that directory, so they can view and edit files. Now, that's not exactly the most secure approach.

Also: Want to save your aging computer? Try these 5 Linux distributions

So, what I'm going to do is show you how to alter the file permissions in such a way that any valid user can read/write to the directory/user/share/data. Then I'll show you how to set the permissions in such a way that a file can be run like an app, which comes in handy when creating scripts.

Ready? Let's do this.

Changing file and folder permissions

What you'll need:For this feature, you'll need a running instance of Linux, which can be any distribution and either a desktop or server. You'll also need a user with sudo privileges. That's it. 

1. Set the permissions for the new directory

With themkdir command we used earlier, we've created the directory /usr/share/data. We can set this directory such that any folder or file created within it enjoys the read/write permissions with the command:

sudo chmod-R ugo+rw /usr/share/da

Let me explain the above command:

  • chmod-- This is the command used to modify the permissions.
  • -R -- Informs chmod that we're working recursively, so every file and/or folder will inherit the same permissions.
  • ugo -- Means users, groups, and others (so, everyone).
  • rw -- Means read/write permissions.

A couple of things to keep in mind. First, you don't have to give permissions to all three types. You could give permissions to just u (user), g (group), or o (other). You can also give just r (read), w (write), or x (executable) permissions. For instance, you could give all users write permissions with:

sudo chmod -R u+w /usr/share/data

Also, if you're working with a file and not a folder, simply remove the -R option from the command.

2. Verify that the command worked

Once you've issued the command, you can verify it worked by issuing another command:

touch /usr/share/data/test

The above command will create an empty file, called test, in /usr/share/data. If the command worked, congratulations, you've successfully changed the permissions of the folder.

3. Give a file executable permissions

Say you've created a script that will back up the contents of your ~/Documents directory. To run that script, it must have executable permissions, which can be done with thechmod command. If your script is calledbackup, the command to give it executable permissions would be:

chmod u+x backup

Make sure to run the above command in the directory housing the script. To run the script, you could issue the command:

./backup

4. Removing permissions

You can also remove permissions with the chmod command. Let's stick with our earlier example. You now want to remove read/write permissions for the /usr/share/data directory. To do that, you would substitute - for +, so the new command would be:

sudo chmod -R ugo-rw /usr/share/data

At this point, no one would be able to read or write data to the directory.

Also: How to choose the right Linux desktop distribution for you

Now, this isn't the best method of adding or removing permissions on Linux because you wouldn't want to give every single user on a system access to modify a directory. A better way of handling this task is using groups. I'll explain that feature in an upcoming tutorial. Until then, keep practicing with this simple method of changing permissions on files and folders in Linux.

tag-icon Etiquetas calientes: Servicios y Software Sistemas operativos

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.