What we’ll cover in this article is how to identify permissions for files & directories and how to change them, as well as changing ownerships, groups, etc. Depending on what you want to do, you’ll want to make sure you have the appropriate permissions (obviously), so let’s find out how to change them.
Let’s start by making a file we can use.
I issued the “touch” command to make a file creatively named testfile.
Touch will just create an empty file but has all the same attributes as an actual file. You can see this by using “ls –l.”
- touch test file
- mkdir workfolder
The permisions are broken into 4 sections.
chmod – adds and removes permissions
If you wanted to add or remove permissions to the user, use the command “chmod” with a “+” or “–“, along with the r (read), w (write), x (execute) attribute followed by the name of the directory or file.
- chmod +rwx “name of the file”
- chmod –rwx “name of the directory”
- chmod +x testfile – this would allow me to execute
- chmod –wx testfile – this would take out write and executable permissions
You’ll notice that this only changes the permissions for the owner of the file, in this case roman.
Changing Permissions for the Group Owners & Others
The command is similar to what we did before, but this time you add a “g” for group or “o” for users.
- chmod g+w testfile
- chmod g-wx testfile
- chmod o+w testfile
- chmod o-rwx workfolder
Lastly you can change it for everyone: “u” for users, “g” for group, & “o” for others; uog or a (for all).
- chmod ugo+rwx workfolder – will give read, write, execute to everyone
- chmod a=r workfolder – will give only read perission for everyone
chgrp – changing groups of files & directories
Another useful option is to change file permission to the group owning the file. Perhaps you create the files, but people on the db2 team can write/execute as well. We use chgrp for this purpose.
You can see above that testfile and the work folder belong to the users group.
By issuing the command – chgrp “name of the group” “name of the file” – you can change this.
- chgrp sales testfile
- chgrp sales workfolder
This give sales control of the file & then I can take away permissions for everyone else.
Note: The group must exit before you try to assign groups to files and directories.
chown – changing ownership
Another helpful command is changing ownerships of files and directories. The command is “chwon” along with “name of new owner” & “name of file.”
The files belonged to roman. To give ownership to tom, issue the command:
- chown tom testfile
- chown tom workfolder
We can also combine the group and ownership command by:
- chown -R tom:sales /home/roman/tsfiles
The above command gives tom the ownership of the directory tsfiles, and all files and subfolders. The -R stands for recursive which is why all sub folders and files belong to tom as well.
As opposed to: chown tom workfolder
This command will give ownership to tom but all sub files and directories still belong to the original owner. The -R will transfer ownership of all sub directories to the new owner.
As you can see, you have several options when it comes to permissions. You have the capability to dictate who can do what & the flexibility to limit usability among users. It may be easier to just give all permission to everyone but this may end up biting you in the end, so choose wisely.
Permission in numeric mode
The above way of changing permissions will work fine but you may also need to know how to change permissions in numeric mode. chmod is used in much the same way, but instead of r, w, or x you will use numbers instead.
What are the numbers?
0 = No Permission
1 = Execute
2 = Write
4 = Read
You basically add up the numbers depending on the level of permission you want to give.
- chmod 777 workfolder
- Will give read, write, and execute permissions for everyone.
- chmod 700 workfolder
- Will give read, write, and execute permission for the user, but nothing to everyone else.
- chmod 327 workfolder
- Will give write and execute (3) permission for the user, w (2) for the group, and read, write, and execute for other users.
- Permission numbers
- 0 = —
- 1 = –x
- 2 = -w-
- 3 = -wx
- 4 = r—
- 5 = r-x
- 6 = rw-
- 7 = rwx
Either variation of changing permissions will work, just remember how to use the numeric values.
ដោយ៖CLC4U ; reasource: trainsignal