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.
whoami
- Displays the current user.Root - The superuser with full permissions.
sudo su
- Switch to the root user.cd /
- Go to the root directory.sudo useradd -m <user_name>
- Adds a new user;-m
creates a home directory for the user.sudo passwd <user_name>
- Sets a password for user login.su <user_name>
- Switch to a different user.sudo groupadd <group_name>
- Creates a new group.sudo gpasswd -a <user_name> <group_name>
- Adds a user to a group;-a
stands for adduser.cat /etc/group
- Lists all groups.cat /etc/passwd
- Shows user IDs and names.Sudo - It is a group with root user privileges.
File Permissions
File permissions in Linux determine who can read, write, or execute files.
ls -a
- Lists all files, including hidden ones.ls -l
- Shows file details, including permissions.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.
sudo chown <user_name> <file_name>
- Changes the file owner.- Example:
sudo chown amitabh demo.txt
- Example:
SSH (Secure Shell)
SSH is used for secure remote access.
SSH - Allows secure, remote login to servers.
- Example: Connecting to an EC2 server from a remote PC.
ssh-keygen
- Creates a public and private key for SSH.SSH Login Command -
ssh -i "linux.pem" ubuntu@<public_ip_address>
SCP (Secure Copy Protocol)
SCP allows secure copying of files over a network.
SCP - Uses SSH for secure file transfers.
- Example:
scp -i "../linux.pem"
ubuntu@ec2-44-204-187-84.compute-1.amazonaws.com
:/home/ubuntu/.ssh/id_ed25519 .
- Example:
Tip:
sudo apt update
downloads updated files, andsudo apt upgrade
installs those updates, actually updating the system.
Nginx Installation and Management
sudo apt install nginx
- Installs Nginx.sudo apt purge nginx
- Removes Nginx.sudo systemctl stop nginx
- Stops Nginx.sudo systemctl start nginx
- Starts Nginx.systemctl status nginx
- Shows Nginx status.
Assignment: Install and Manage Docker
sudo apt install
docker.io
- Installs Docker.systemctl status docker
- Shows Docker status.Output Image:
sudo systemctl stop docker
- Stops Docker.Output Image:
sudo systemctl start docker
- Starts Docker.Output Image:
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.
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/
Downloading a Log File:
Searching in a Log File:
- Command:
grep WARN -i Zookeeper_2k.log
- Command:
Appending Warnings to Another File:
- Command:
grep WARN -i Zookeeper_2k.log > warnings_only_zookeeper.log
- Command:
AWK
awk
is a powerful tool for handling columnar data.
Basic Pattern Matching:
- Command:
awk '/WARN/' Zookeeper_2k.log
- Command:
Extracting Specific Columns:
- Command:
awk '/WARN/ {print $1}' Zookeeper_2k.log
- Command:
Multiple Column Output:
- Command:
awk '/INFO/ {print $1$2$5}' Zookeeper_2k.log
- Command:
Row Number Output:
- Command:
awk '/WARN/ {print NR $5 $6}' Zookeeper_2k.log
- Command:
Time-Filtered Output:
- Command:
awk '$2>="19:13:00" && $2<="19:17:00" && /WARN/ {print NR,$1,$2,$5}' Zookeeper_2k.log
- Command:
Assignment: Analyze log files from Loghub.
SED
sed
(Stream Editor) allows text substitution in files.
Replacing Text Globally:
- Command:
sed -i 's/test@123/****/g' passwords.txt
- Command:
Instant Text Replacement Without File Modification:
- Command:
sed 's/test@123/********/g' passwords.txt
- Command:
Find
find
is useful for locating files in directories.
Finding Files by Extension:
- Command:
find . -name "*.log"
- Command:
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.