File Permissions in Linux (chmod)
Linux is a popular operating system that gives users access to a wide range of tools and features. One of the most important is the chmod command, which lets you manage permissions for files and directories. The chmod command is an essential Unix tool that provides precise control over access permissions for files and directories. chmod has many uses, including securing files, granting permissions to groups, making scripts executable, controlling read and write permissions, and changing permissions for multiple files at once. Learning these practical examples will help you make full use of chmod and better protect your data on Unix systems.
Using chmod
chmod can be used in several ways, depending on your preferences and level of experience. Here are some of the most important methods.
Symbolic (text) method:
The symbolic method lets you change permissions using symbols and operators. You can use symbols such as u (owner), g (group), and o (other users) to specify which user categories should have their permissions changed. If these symbols are not provided, the permissions are applied to all users. Permission types are represented by r (read), w (write), and x (execute). Examples:
Granting full permissions to the owner, group, and other users (both forms are equivalent):
chmod ugo+rwx nazwa_plikuchmod +rwx nazwa_plikuGranting execute permission (for example, allowing a script to be run) to all users:
chmod +x skrypt.shGranting read and write permissions to the owner and read-only permission to the group:
chmod u+rw,g+r nazwa_plikuRemoving write permission from the group and other users:
chmod go-w nazwa_plikuGranting read-only permission to all users:
chmod a=r nazwa_plikuNumeric method
The numeric method is based on numeric values that represent sets of permissions. Each user category (owner, group, others) is assigned specific numbers. For example, 4 means read, 2 means write, and 1 means execute. Adding the values for the required permissions gives you the complete permission set. Examples:
Granting read and write permissions to the owner and read-only permission to the group:
chmod 640 nazwa_plikuGranting full permissions to the owner, read and execute permissions to the group, and execute-only permission to other users:
chmod 751 nazwa_plikuUsing a reference file
Another useful technique is to use a reference file that already has the required permissions. You can copy the permissions from that file and apply them to another file or directory using the following command:
chmod --reference=plik_referencyjny plik_docelowyChanging permissions recursively
Sometimes you need to change permissions for many files and directories at once. In this case, you can use the -R or --recursive option, which recursively changes permissions for all files and directories inside a given directory. Example:
Granting read and write permissions to the owner, group, and other users for all files and directories inside a given directory:
chmod -R ugo+rw nazwa_kataloguWorking with symbolic links
The chmod command also lets you change permissions for files that are symbolic links. You can use the -h or --no-dereference option to change the permissions of the symbolic link itself rather than the target file. Example:
Changing permissions for the symbolic link itself rather than the file it points to:
chmod -h 777 link_symbolicznyChanging permissions for multiple files
The chmod command lets you change permissions for multiple files at once, which is extremely useful when you need to apply the same permissions to a larger number of files or directories. Examples:
Granting permissions to all files in a directory
find /var/www/my_website -type f -exec chmod 755 {} \;find /var/www/my_website -type f -exec chmod u=rwx,go=rx {} \;Granting permissions to all folders in a directory
find /var/www/my_website -type d -exec chmod 755 {} \;find /var/www/my_website -type d -exec chmod u=rwx,go=rx {} \;Comments (1)
bez googlowania tylko sudo chmod +x pamiętam ;)
Add a comment
Comments are published after moderation. Your e-mail address stays private.