# Linux Advanced Concepts

# Lecture-07 : Linux Advanced of DevOps batch-8

In this blog, we’ll dive into advanced Linux concepts, covering user management, file permissions, SSH, SCP, software installation, and practical usage of commands like `grep`, `awk`, `sed`, and `find`. This knowledge is essential for managing Linux systems effectively and is valuable for DevOps and systems administration.

---

## User Management

User management is fundamental in Linux, as it helps control system access.

1. `whoami` - Displays the current user.
    
2. **Root** - The superuser with full permissions.
    
3. `sudo su` - Switch to the root user.
    
4. `cd /` - Go to the root directory.
    
5. `sudo useradd -m <user_name>` - Adds a new user; `-m` creates a home directory for the user.
    
6. `sudo passwd <user_name>` - Sets a password for user login.
    
7. `su <user_name>` - Switch to a different user.
    
8. `sudo groupadd <group_name>` - Creates a new group.
    
9. `sudo gpasswd -a <user_name> <group_name>` - Adds a user to a group; `-a` stands for adduser.
    
10. `cat /etc/group` - Lists all groups.
    
11. `cat /etc/passwd` - Shows user IDs and names.
    
12. **Sudo - It is** a group with root user privileges.
    

---

## File Permissions

File permissions in Linux determine who can read, write, or execute files.

1. `ls -a` - Lists all files, including hidden ones.
    
2. `ls -l` - Shows file details, including permissions.
    
3. **File Permissions Format** - The format `-rw-rw-r--` indicates read (`r`), write (`w`), and execute (`x`) permissions for user, group, and others, respectively. The starting `-` means it’s a file; `d` denotes a directory.
    

Example:

* `-rw-rw-r--` - File where the user and group can read and write, others can only read.
    
* `drwxrwxr-x` - Directory with read, write, and execute permissions for user and group, and read and execute for others.
    

**Permission Values**:

| Numbers | r | w | x |
| --- | --- | --- | --- |
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 1 | 0 | 0 |
| 5 | 1 | 0 | 1 |
| 6 | 1 | 1 | 0 |
| 7 | 1 | 1 | 1 |

For example, `chmod 400 <file_name>` gives read-only permission to the current user and Group and other user does not have any permission.

4. `sudo chown <user_name> <file_name>` - Changes the file owner.
    
    * Example: `sudo chown amitabh demo.txt`
        

---

## SSH (Secure Shell)

SSH is used for secure remote access.

1. **SSH** - Allows secure, remote login to servers.
    
    * Example: Connecting to an EC2 server from a remote PC.
        
2. `ssh-keygen` - Creates a public and private key for SSH.
    
3. **SSH Login Command** - `ssh -i "linux.pem" ubuntu@<public_ip_address>`
    

---

## SCP (Secure Copy Protocol)

SCP allows secure copying of files over a network.

1. **SCP** - Uses SSH for secure file transfers.
    
    * Example: `scp -i "../linux.pem"` [`ubuntu@ec2-44-204-187-84.compute-1.amazonaws.com`](mailto:ubuntu@ec2-44-204-187-84.compute-1.amazonaws.com)`:/home/ubuntu/.ssh/id_ed25519 .`
        

---

> **Tip:** `sudo apt update` downloads updated files, and `sudo apt upgrade` installs those updates, actually updating the system.

---

## Nginx Installation and Management

1. `sudo apt install nginx` - Installs Nginx.
    
2. `sudo apt purge nginx` - Removes Nginx.
    
3. `sudo systemctl stop nginx` - Stops Nginx.
    
4. `sudo systemctl start nginx` - Starts Nginx.
    
5. `systemctl status nginx` - Shows Nginx status.
    

---

## Assignment: Install and Manage Docker

1. `sudo apt install` [`docker.io`](http://docker.io) - Installs Docker.
    
2. `systemctl status docker` - Shows Docker status.
    
    * **Output Image:**
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730472580998/89fa4076-8b1f-4054-b965-babb46646e33.png align="center")
        
3. `sudo systemctl stop docker` - Stops Docker.
    
    * **Output Image:**
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730472594754/0ad85e13-8628-4799-acfd-8d560c38d8d3.png align="center")
        
4. `sudo systemctl start docker` - Starts Docker.
    
    * **Output Image:**
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730472606099/ffc43e76-a257-4d61-a11b-9fdd7a2c2a91.png align="center")
        
5. `sudo apt purge` [`docker.io`](http://docker.io) - Uninstalls Docker.
    
    * **Output Image:**
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730472617709/3eb23686-8654-499f-9c43-7ef67d6d52f1.png align="center")
        

---

### Assignment: Read HTTPS Status Codes

Explore HTTPS status codes as part of this assignment.

---

## Grep

`grep` is used for searching patterns in files.

1. `grep <keyword> -r <path>` - Searches for a pattern recursively.
    
    * Example: `grep junoon -r /home/ubuntu/`
        
    * **Case-Insensitive**: Use `-i` to ignore case, e.g., `grep junoon -ir /home/ubuntu/`
        
2. **Downloading a Log File**:
    
    * **Command**: `wget` [`https://github.com/logpai/loghub/blob/master/Zookeeper/Zookeeper_2k.log`](https://github.com/logpai/loghub/blob/master/Zookeeper/Zookeeper_2k.log)
        
3. **Searching in a Log File**:
    
    * **Command**: `grep WARN -i Zookeeper_2k.log`
        
4. **Appending Warnings to Another File**:
    
    * **Command**: `grep WARN -i Zookeeper_2k.log > warnings_only_zookeeper.log`
        

---

## AWK

`awk` is a powerful tool for handling columnar data.

1. **Basic Pattern Matching**:
    
    * **Command**: `awk '/WARN/' Zookeeper_2k.log`
        
2. **Extracting Specific Columns**:
    
    * **Command**: `awk '/WARN/ {print $1}' Zookeeper_2k.log`
        
3. **Multiple Column Output**:
    
    * **Command**: `awk '/INFO/ {print $1$2$5}' Zookeeper_2k.log`
        
4. **Row Number Output**:
    
    * **Command**: `awk '/WARN/ {print NR $5 $6}' Zookeeper_2k.log`
        
5. **Time-Filtered Output**:
    
    * **Command**: `awk '$2>="19:13:00" && $2<="19:17:00" && /WARN/ {print NR,$1,$2,$5}' Zookeeper_2k.log`
        

---

> **Assignment**: Analyze log files from [Loghub](https://github.com/logpai/loghub).

---

## SED

`sed` (Stream Editor) allows text substitution in files.

1. **Replacing Text Globally**:
    
    * **Command**: `sed -i 's/test@123/****/g' passwords.txt`
        
2. **Instant Text Replacement Without File Modification**:
    
    * **Command**: `sed 's/test@123/********/g' passwords.txt`
        

---

## Find

`find` is useful for locating files in directories.

1. **Finding Files by Extension**:
    
    * **Command**: `find . -name "*.log"`
        
2. **Finding Specific Patterns in a Directory**:
    
    * **Command**: `find ubuntu/ -name "*.txt" | grep -ir Junoon`
        
    * **Output Image:**
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730472644195/63aa7a7c-58c4-4a23-a8e1-76a8a547600c.png align="center")
        

---

This completes the overview of advanced Linux commands and tools, covering critical aspects of user management, file permissions, SSH, SCP, and software installation and management. Practicing these commands will help you master Linux for DevOps and other technical roles.
