Let's learn VIM and also User Management...
Tags: beginners, linux, devops
Published: 2022-03-16 13:27:08

There are different text editors like emacs, nano, vim, etc. But vim text editor has gained popularity between developers and sysadmins.

The main takeaway is we learn to insert text in vim and more specifically learn about quitting the vim editor🤣.

---

### Working with VIM Editor

**How do we edit files in CLI?**

We have a built-in text editor in Linux like nano, vi/vim(Improved version of vi), etc.

Use cases to use the text editor in **CLI** :

* small modifications can be faster, especially when you are currently working in CLI.
    
* faster to create & edit at the same time.
    
* supports multiple formats.
    
* when working on a remote server it speeds up the workflow.
    

### Important VIM commands

`vim [filename]` =&gt; opens the file with vim.

**vim** has two modes **:**

***<mark>Command Mode</mark>***

* this is the default mode and you cannot edit the text.
    
* whatever you type is interpreted as a command.
    
* Navigate, Search, Delete, Undo, etc. can be done in this mode.
    

![vim-config-1024x462.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436275605/OBxFkJ-RA.png)

***<mark>Insert Mode</mark>***

* allows you to enter text.
    
* to **switch** from ***command*** mode to ***insert*** mode, press '**i**'.
    
* and from insert to command mode, press '**Esc**'.
    

![vim-insert-mode.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436267186/W_W5xCDe6.png)

**NOTE:** All these below commands should be run in ***command*** mode.

* `:wq` =&gt; writes and saves the file to **disk** and **quits** the vim editor.
    
* `:q!` =&gt; quits the vim editor without saving the changes.
    
* `dd` =&gt; delete an entire line.
    
* `d[number]` =&gt; delete a bunch of lines.
    
    * For example, `d10` -&gt; deletes the next 10 lines.
        
* `u` =&gt; undo the changes.
    
* `A` =&gt; jumps to the end of the line and switches to insert mode.
    
* `0` =&gt; jumps to the start of the line(but does not switch to insert mode).
    
* `$` =&gt; jumps to the end of the line(without switching to insert mode).
    
* `[number]G` =&gt; go to the line `[number]`
    
    * For example, `12G` -&gt; go to line 12.
        
* `/[pattern/word]` =&gt; search for that pattern/word.
    
    * For example, `/nginx` -&gt; searches for the pattern/word *'nginx'*
        
* `Type 'n'` =&gt; jumps to the next match.
    
* `Type 'N'` =&gt; jumps to the previous match/ in opposite direction.
    
* `:%s/[old]/[new]` =&gt; replace the **old word** with the **new word** throughout the file.
    

![vim-cheat-sheet-en.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436077394/1j06eFsRQ.png)

---

### Linux Accounts and Groups

There are ***three*** user categories :

i. superuser**(sudo)** account

* this is the root user with *unrestricted* permissions.
    
* used for administrative tasks*(No need to log in as a Root user or execute any commands as root****(sudo command)****)*.
    

ii. user account

* a regular user, we create to login.
    
    * For example, username=dipanshu then it is found under `/home/dipanshu.`
        

iii. service account

* relevant for Linux server distros as each service will get its own user.
    
    * For example, a MySQL user will start a MySQL application(Best practice for security).
        

**NOTE:**

* Don't run services with a root user
    
* Always keep **one root user** per computer.
    

**Can we have multiple regular users & service users and why multiple standard users?**

Many companies use windows for their employees. Usually, employees can log in to their accounts on every computer.

**How does this work?**

* Windows is able to **centrally manage users**. Admins add users to the system and all computers are connected to this system.
    
* When someone tries to **log in**, OS checks it with the system*(Only have access to their* ***home*** *folder)***\[completely isolated system\]**(No access to system files or **other user's home** folder).
    
* But Linux **doesn't** have this *centrally managed system*. This is the reason why Windows is preferred in companies or universities.
    

### **Multiple Users on Linux**

User accounts are managed on that specific hardware*(cannot access from any other hardware)*

***Multiple Users on a Server***

For Linux having a multi-user is important for servers. Usually, teams administer a server.

***Why not just use a shared user? & Why having a user for each team member is important?***

They need a **non-root** user. Permissions can be assigned per team member.

**Traceability** =&gt; who did what on the system?

**Admin** creates a user with permissions.

### Groups and Permissions

**How to manage Permissions?**

User Level =&gt; give permission to the user directly.

Group Level =&gt; group users into Linux groups and give permissions to the group.

This is the way to go is if you manage multiple users***(Best Practice)***.

### User management in practice

**Access Control Files :**

`/etc/passwd` =&gt; stores user account information and everyone can read it but only the **root** user can change the file.

![etc-passwd.jfif](https://cdn.hashnode.com/res/hashnode/image/upload/v1647435999198/bPMAfvRBM.jfif)

`/etc/shadow` =&gt; contains information about the system user's passwords.

`/etc/group` =&gt; contains a list of groups and the members belonging to each group.

**Managing users :**

* Do not edit these access control files with a text editor. Instead, use the dedicated commands.
    

---

### Commands for user/group creation and management

**NOTE: *root*** user privileges are required**.**

`adduser [username]` =&gt; creates a new user and automatically creates a home directory with skeletal configuration.

**Where does the primary group come from?**

Whenever we create a user using `useradd` , by default, it creates the same group named the user and sets that as the primary group ID of the user.

`passwd [username]` =&gt; changes the password of a user.

`su - [username]` =&gt; login as a `username`*( su - short for substitute or switch user)*.

`su -` =&gt; login as root.

`groupadd [groupname]` =&gt; creates a new group and by default, the system assigns the next available **GID** from the range of group IDs specified in the login.defs file.

**Different User and Group Commands**

`adduser` `addgroup`

* Interactive and more user-friendly*(easier to use).* It chooses conformant **UID** and **GUID** values for you.
    
* Creates a home directory with skeletal config automatically or asks for input in interactive mode.
    

`useradd` `groupadd`

* You need to provide the information. Low-level utilities.
    

---

**Which one to use?**

`adduser` `addgroup` =&gt; when executing it manually.

`useradd` `groupadd` =&gt; when executed in an automated way.

Same goes for `deluser`*(deletes user),* `delgroup`*(deletes groups)* and `userdel` `groupdel`.

---

### Modifying user account

`usermod [options] [username]` =&gt; modify a user account.

for changing group =&gt; `usermod -g [groupname] [username]`

In addition to one primary group, the user can have multiple secondary groups and as a result, the user will get all the permissions that the groups have to which it belongs*(which means we can add one user to multiple groups)*.

`sudo usermod -G [groupsname(separated with commas)] [username]`

* add a user to multiple groups and the option will overwrite the whole secondary group's list. So, it always sets a new list of groups for the user.
    
* If you need to add a user to a new secondary group in addition to the existing ones it already belongs to we use `sudo usermod -aG [groupname] [username]`
    

`groups [username]` -&gt; displays the groups to which the user is added.

**More efficient way**

`useradd [options] [username]` =&gt; creates a new user.

the low-level command compared to `adduser`  
`-G` =&gt; create a user with multiple secondary groups  
`-d` =&gt; custom home directory  
`sudo gpasswd -d [username] [groupname]` =&gt; removes the user from the specified group.

---

### File Permissions and Ownership

User permissions are related to reading, writing and executing files in Linux.

`ls -l` =&gt; print files in a long listing format.

![ls -l.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436015300/bzkJuCi7s.gif)

**Ownership =&gt;** It means who owns the file/directory.

There are two levels of a file/directory.

* Which user owns the file?
    
    * The owner is the user who created the file.
        
* Which group owns the file?
    
    * The owning group is the primary group of that user.
        

***(should be executed with root privileges)***

`chown [username]:[groupname] [filename]` =&gt; changes the ownership*(in this command you can also change user by not adding* `[groupname]`*)*

`sudo chgrp [groupname] [filename]` =&gt; changes the group

### **File Permissions**

**Modifying Permissions**

**\[Symbolic values method\]**

`sudo chmod [flags] [filename]` =&gt; Changes the permission for that file and for all users.

**Flags** **:**

1. `+r` -&gt; add read permission
    
2. `+w` -&gt; add write permission
    
3. `+x` -&gt; add execute permission
    
4. `-r` -&gt; takes away read permission
    
5. `-w` -&gt; takes away writing permission
    
6. `-x` -&gt; takes away execute permission
    

**Adjusting permissions :**

* Owner(u), Group(g), Others(o) & All(a)
    
    * For example, `sudo chmod g-w config.yaml` =&gt; removes the write permission for the group.
        

**Alternate ways to give permissions :**

`sudo chmod u/g/o/a=rwx [filename]` =&gt; gives multiple permission to mentioned ones.

Type out the whole `rwx` block or `r--`**/**`rw-` etc.

**\[Numeric values method\]**

`sudo chmod [numeric value] [filename]`

![777.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647435959435/CVk-6hdzb.png)

---

### Bonus Content :)

### Input, Output and Pipes in Linux

![image-55.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436450000/Gsr6iyMhE.png)

* Every program has input and output. The output from one program can become the input of another command.
    
* The syntax for this is the "pipe" command `|`. It pipes the output of the previous command as an input to the next command.
    

For example, `cat /var/log/syslog` *(outputs log in non-user friendly way)* `| less` *(helps to see the output page-by-page)***(to jump to the next page press 'spacebar' and to jump to the previous page press 'b')**

To filter commands for specific keywords we use `grep [keyword]` =&gt;

* grep stands for Globally Search for Regular expression and print-out. And it searches for a particular pattern of characters & displays all lines which contain that pattern.
    
* To search for a phrase we have to enclose in (" ") double-quotes.
    

### **Redirects in Linux**

It is used to save the output of another command into a file.

**Redirection**

* `>` character is the redirect operator.
    
* Takes the output from the previous command and sends it to a file that you mention. For example, `history | grep sudo > sudo-commands.txt`
    

In order to add new lines in the existing file by using redirect can be done using

`>>` =&gt; append text to the end of the file.

### Standard Input and Standard Output

![bl10-01-commands-without-pipes.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647436439970/qA7GUL9c9.png)

Every program has 3 built-in streams

* **STDIN(0)** =&gt; Standard Input
    
* **STDOUT(1)** =&gt; Standard Output
    
* **STDERR(2)** =&gt; Standard Error
    

We pipe or redirect the standard output from one command to the standard output of another command. We cannot execute many commands in one line but can be done by separating them with `;`(semi-colon).

For example, `clear; sleep 2; echo "Hello, welcome back"`