File permissions in Linux are crucial for security and privacy. This is because Linux is a multi-user operating system, meaning that multiple users can be logged on to the system at once.
By default, on most modern Linux distributions, only the owner can view and edit a file. For example, if user Olivia logs in, she cannot see the contents of the home directory.
So, how do you handle creating a directory outside of home that you want to allow others to use? For example, let’s say the directory is /usr/share/data. You can create this directory using the command:
sudo mkdir /usr/share/data
Once the directory is created, you can add content to it by using the sudo command for admin privileges. However, this method is not the most secure approach.
Here’s how to alter the file permissions to allow any valid user to read and write to the directory /usr/share/data.
First, with the mkdir command, use the following command to set the directory so that any file or folder created within it enjoys read and write permissions:
sudo chmod -R ugo+rw /usr/share/data
This command modifies the permissions by informing chmod to work recursively for every file and/or folder to inherit the same permissions. ugo means users, groups, and others, and rw means read and write permissions. You can also give permissions to just user, group, or other and give read, write, or executable permissions.
After issuing the command, you can verify that it worked by creating an empty file in the directory. Once a file is created, it means the command successfully changed the permissions of the folder.
Additionally, you can give a file executable permissions using the chmod command. For example, if you have a script called backup, you can give it executable permissions using the chmod command:
chmod +x backup
To remove permissions, you can use the chmod command in the reverse manner. For example, to remove read and write permissions for the /usr/share/data directory, you would use the following command:
sudo chmod -R ugo-rw /usr/share/data
Overall, while altering file and folder permissions in Linux in this manner is simple, it’s not the best method. In an upcoming tutorial, we’ll discuss a better way of handling this task using groups. Until then, keep practicing with simple methods of changing permissions on files and folders in Linux.