bash-tutorial/tutorial/changeowner.org

147 lines
3.5 KiB
Org Mode
Raw Normal View History

2022-07-18 14:23:28 +02:00
#+options: toc:nil num:nil author:nil
* Changing permissions for files and directories
There are three types of permissions for files and directories in the system:
2023-04-08 22:20:56 +02:00
| Permission | Files | Directories |
|---------------+-----------------+-------------------|
| Read ('r') | Read the file | List contents |
| Write ('w') | Change the file | Create amd remove |
| Execute ('x') | Execute files | Access the files |
2022-07-18 14:23:28 +02:00
2023-04-08 22:20:56 +02:00
Permissions can be differente for different users:
- Owner of the files
- Group: other users in the file's group
- Others
2022-07-18 14:23:28 +02:00
** Creating a file and checking permissions
Here we create a file with the sentence "this is a fancy file". The sentence is
redirected to the name "fancyfile.org" and a new file is created!
#+begin_src bash
2022-09-04 13:15:23 +02:00
echo "this is a fancy file" > fancyfile.org
2022-07-18 14:23:28 +02:00
#+end_src
#+RESULTS:
In the ouptut of ~ls~ command, there letters showing each permission for each user: owner, group, others, respectively.
#+begin_src bash
ls -la fancyfile.org
#+end_src
#+RESULTS:
2023-04-08 22:20:56 +02:00
: -rw-r--r-- 1 user user 21 jul 18 06:06 fancyfile.org
2022-07-18 14:23:28 +02:00
** Settings permissions for the file
2023-04-08 22:20:56 +02:00
Setting permissions for the
- User who owns the file = "u"
- Other users in the group = "g"
- Other users = o
You may change the permissions for each user, using letters (r-w-x):
2022-07-18 14:23:28 +02:00
#+begin_src bash
2023-04-08 22:20:56 +02:00
chmod u=r, g=rw, o=r fancyfile.org
2022-07-18 14:23:28 +02:00
#+end_src
#+RESULTS:
Then it changed!
#+begin_src bash
ls -la fancyfile.org
#+end_src
#+RESULTS:
2023-04-08 22:20:56 +02:00
: -r--rw-r-- 1 user user 21 jul 18 06:06 fancyfile.org
2022-09-02 17:44:27 +02:00
** Changing permission for directories
Let's say I (unintentionally) changed the permissions for a directory that
contains multiple subdirectories. Let's call that directory "buds"
I can't access the files without being `root`. This is the outuput in the
command line When a list the directory using `ls -la`.
#+begin_example bash
2022-09-09 23:07:04 +02:00
drw-r--r-- 81 user user 4096 dec 26 06:21 buds
2022-09-02 17:44:27 +02:00
#+end_example
When I go to my git repository and check the change I can see this output for
every file within the directory:
#+begin_example bash
diff --git a/doc/index.rst b/doc/index.rst
old mode 100644
new mode 100755
#+end_example
2023-04-08 22:20:56 +02:00
Thus, the directory has the following permissions
2022-09-02 17:44:27 +02:00
2023-04-08 22:20:56 +02:00
- For the owner = read (4) and write (2)
- For the group = read (4)
- For others = read (4)
Using ~chmod~ 744 will change the permissions for:
2022-09-09 23:07:04 +02:00
* The owner (4 = read; 2 = write; 1 = execute)
2023-04-08 22:20:56 +02:00
* The group (4 = read) and
2022-09-09 23:07:04 +02:00
* Others (4 = read)
2023-04-08 22:20:56 +02:00
Applying the same logic to a file:
2022-09-09 23:07:04 +02:00
#+begin_src bash
chmod 744 NameOfFileHere
#+end_src
2023-04-08 22:20:56 +02:00
Use ~-R~ if you want to apply recursive ~chmod~
2022-09-09 23:07:04 +02:00
#+begin_src bash
chmod -R 755 NameOfFileHere
#+end_src
2022-09-02 17:44:27 +02:00
*** The output now is:
#+begin_example bash
2023-04-08 22:20:56 +02:00
drwxr-xr-x 81 user user 4096 dec 26 06:21 buds
2022-09-02 17:44:27 +02:00
#+end_example
2023-04-08 22:20:56 +02:00
Remember how it was before
2022-09-02 17:44:27 +02:00
#+begin_example bash
drw-r--r-- 81 user user 4096 dec 26 06:21 buds_before
#+end_example
2023-04-08 22:20:56 +02:00
** Changing owner
But I still can open the files. That is because need to change the owner.
To change the owner of files the command ~chown~ may be used
2022-09-02 17:44:27 +02:00
#+begin_example bash
2022-09-09 23:07:04 +02:00
chown NameOfownerHere buds # Try this and check the result
2022-09-02 17:44:27 +02:00
#+end_example
2022-09-09 23:07:04 +02:00
Include the owner then ~:~ and then the name of the group to change ownership for each one of those users
#+begin_example bash
chown user:usergroup buds # Then try this and check the result
#+end_example
2022-09-04 13:15:23 +02:00
ps: should use`sudo` to run this command, so be careful and make a scratch directory
to run tests.
2023-04-08 22:20:56 +02:00
** References
- Check the manual: info -> Coreutils -> File permissions. In bash: ~info coreutils~
Coreutils are standard programs for text and file manipulation