Linux Advanced Concepts

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:

Numbersrwx
0000
1001
2010
3011
4100
5101
6110
7111

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

  1. 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.


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 - Installs Docker.

  2. systemctl status docker - Shows Docker status.

    • Output Image:

  3. sudo systemctl stop docker - Stops Docker.

    • Output Image:

  4. sudo systemctl start docker - Starts Docker.

    • Output Image:

  5. sudo apt purge docker.io - Uninstalls Docker.

    • Output Image:


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:

  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.


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:


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.