DEV Community

Cover image for Manage Permissions In Linux ๐Ÿ”
Prashant Pal
Prashant Pal

Posted on

Manage Permissions In Linux ๐Ÿ”

This week, I explored๐Ÿ”ญ two essential aspects of working with Linux: managing permissions ๐Ÿ” and getting comfortable with the Vim editor โœ๏ธ. Both are crucial for anyone working in a Linux environment. Letโ€™s break down what Iโ€™ve learned ๐Ÿ˜‡.

VI & VIM Editor

VIM Editor
VIM is a built in editor in Linux. Well we have VS Code and editor to edit or create file, why we use VIM.

  1. For small modifications๐Ÿ”ง it is faster especially when working in CLI.
  2. Faster to create and edit at the same timeโฐ.
  3. It Support Multiples formats
  4. While working on remote servers it is very useful.

To open file with VIM write this command in CLI

vim <file_name> 
Enter fullscreen mode Exit fullscreen mode

VIM have two modes๐Ÿ“• Command Mode and Insert Mode:
Command Mode :- This is the default mode. In this you can't edit the text. But you can navigate, search, delete, undo etc.

Insert Mode :- This mode allows you to enter text, edit text etc.
To switch to insert mode press i key.

Insert Mode

After adding some text you need to save and exit the editor, for that simply press esc key. Then type :-

:wq!
Enter fullscreen mode Exit fullscreen mode

Save and Exit
This command writes the file to disk and quit vim editor.
To quit vim without saving the changes then type :-

:q!
Enter fullscreen mode Exit fullscreen mode

In a file to remove whole line you can use command dd this delete entire line. To delete๐Ÿ—‘๏ธ multiple line then use type d20 here 20 means number of lines, you can just count the lines from where you want to delete. For Undo type u. To jump to a specific line use command 16G here 16 is line number. To search use /pattern.

Note:- When I say command then it should be written in command mode only. Commands don't work in Insert Mode.

NANO Editor

Nano
Nano is more user friendly than vim. To make file or to open file use command :-

nano filename
Enter fullscreen mode Exit fullscreen mode

This opens nano editor. You can see various commands at the end of the editor use these to navigate through it. I recommend to use all commands and try out every one.

Managing Permissions ๐Ÿ”’

In a linux system there are 3 user๐Ÿ‘ค categories:-

  1. Superuser Account๐Ÿฆน๐Ÿปโ€โ™€๏ธ:- In this root user have unrestricted permissions. For administrative tasks you need to login as root user.
  2. Regular User Account๐Ÿ™‹๐Ÿปโ€โ™‚๏ธ:- A user we create to login into the system. For eg. /home/pal.
  3. Service user๐Ÿšน:- It is relevant for Linux Server Distributions.

How To Manage permissions? There are two levels first User Level and second is Group Level. In User Level we give permissions to user directly. In Group Level we group users into linux groups and then give permission to that group.

All users information is stored๐Ÿช in /etc/passwd. This stores user account information. Everyone can read it, but only root user can change the file. After running๐Ÿƒ๐Ÿป this command you will get information of the user,

pal:x:1000:1000:pal,,,:/home/pal:/bin/bash
Enter fullscreen mode Exit fullscreen mode

The above line represent this:- USERNAME:PASSWORD:UID:GID:GECOS:HOMEDIR:SHELL

To create new user, use command

sudo adduser<username>
Enter fullscreen mode Exit fullscreen mode

It automatically chooses UID and GID values and creates a home directory for new user. After running this command enter new password and other details.

To change the password of user

sudo passwd<username>
Enter fullscreen mode Exit fullscreen mode

To login to this username use su-.
To add group use this command:-

sudo groupadd<groupname>
Enter fullscreen mode Exit fullscreen mode

By default, the system assigns the next available GID ๐Ÿ”ข.

You noticed that we can write the command adduser as useradd, so what is the difference between adduser and useradd, and similarly groupadd and addgroup? ๐Ÿค”

adduser & addgroup โžก๏ธ These are more interactive ๐Ÿ’ฌ, more user-friendly ๐Ÿ˜Š. It is easier to use and automatically chooses UID and GID values itself. Use these commands when you are executing manually ๐Ÿ–ฑ๏ธ.

useradd & groupadd โžก๏ธ In these commands, you need to provide the information yourself ๐Ÿ› ๏ธ. These commands are used when executing in an automated way ๐Ÿค–.

To change a user's group to another group, use:

sudo usermod[Options]<username>
Enter fullscreen mode Exit fullscreen mode

There are various tags use linux man page to access all tags related to usermod command or you can use google to search it.

To delete group use

sudo delgroup <groupname>
Enter fullscreen mode Exit fullscreen mode

Ownership And Permissions ๐Ÿ‘ค๐Ÿ”‘

In simple words ownership means who owns the file or who have access to that file. Owner is the user, who created the file, and owning group is the primary group of that use. To change the ownership use command

sudo chown <username>:<groupname> <filename>
Enter fullscreen mode Exit fullscreen mode

To understand it better lets use this command ls -l. This shows long view of the directory.

Terminal

I'll take drwxrwxr-x 2 pal pal as a example your terminal should have this type of oputput.

  1. d :- This is the file type. There are are different file types. "-" is for regular file, "d" is for directory, "c" is for character device file, "l" symbolic link, etc.

  2. rwx :-First group of rwx is for owner. This indicates owner have access to this file, "r" stands for read, "w" stands for write, "x" stands for execute, "-" stands no permission.

  3. rwx :- Second group of rwx is for group owner.

  4. r-x :- This is for all other users, who are not the file owner or don't belong to the group owner.

Note - If "-" is used in place of rwx it means anyone is missing, don't have permission. For example r-x in this w is missing and instead of w, "-" is used this means it don't have permission for write.

To remove permission use, here - stands for removing the permission and g stands for group.

sudo chmod g-w <filename>
Enter fullscreen mode Exit fullscreen mode

To add permission use, here + stands for adding the permission. For user use u instead of g.

sudo chmod g+x <filename>
Enter fullscreen mode Exit fullscreen mode

Note ๐Ÿ“: You can use the absolute numeric method to give permission. You can search it on Google ๐Ÿ”. For each number, there is a permission assigned, for example, 7 is for rwx.

Resources ๐Ÿ“š

  1. If you want to access the Linux terminal online, use this website: https://bellard.org/jslinux/ ๐ŸŒ
  2. Use ChatGPT ๐Ÿ˜‰ to explore more. ๐Ÿค–

Wrapping Up And Getting Ready for Another Learning Adventure ๐ŸŒŸ

Last week, I took a deep dive into managing permissions in Linux ๐Ÿ”, mastering the Vim editor โœ๏ธ, and learning about access controls ๐Ÿ›ก๏ธ. Each of these topics has given me a strong foundation in how Linux handles file security and text editing. I know this will be helpful when I set up my remote VM for my DevOps projects ๐Ÿ’ป.

In this week, Iโ€™m excited to explore shell scripting ๐Ÿš and networking in Linux ๐ŸŒ! Shell scripting will open up new possibilities for automating tasks ๐Ÿ”„ and making my workflow more efficient, while understanding Linux networking will give me the knowledge to configure and troubleshoot connections in various environments ๐Ÿ› ๏ธ.

Stay tuned for more Linux and open-source adventures next week! ๐Ÿš€

You can check my blog on hashnode too.

Top comments (0)