Linux Notes
Linux Notes
1. What is Linux?
Linux is an open-source, Unix-like operating system kernel originally developed by Linus
Torvalds in 1991. It is the core of many operating systems known as Linux distributions (or
distros). Linux is highly customizable, secure, and widely used in servers, desktops, and
embedded systems.
Key Characteristics
a
● Open-Source: The source code is freely available, allowing users to modify and
ry
distribute it.
● Multi-user and Multitasking: Supports multiple users and processes
simultaneously.
● Modular Design: Uses a kernel with modules for hardware and software integration.
su
● Community-Driven: Developed and maintained by a global community of
contributors.
Why Linux?
ith
● Free to use and distribute.
● Highly stable and reliable for long-running systems.
● Extensive support for programming and development tools.
w
In a DevSecOps pipeline, Linux servers can host Jenkins for CI/CD, run Docker containers
a
for microservices, and use tools like Trivy for vulnerability scanning, all managed via Bash
scripts or Ansible playbooks.
ry
3. Linux Architecture
su
The Linux architecture is a layered structure that enables efficient interaction between
hardware and software. It is composed of several key components: Applications, Shell,
Kernel, and Hardware. Below is a detailed explanation of each component.
ith
3.1 Hardware
The hardware layer consists of the physical components of a computer system, such as:
w
Example: A Linux system uses device drivers to manage a network card for internet
D
connectivity.
3.2 Kernel
The kernel is the core of the Linux operating system, acting as a bridge between hardware
and software. It manages system resources and provides essential services.
Key Functions
Types of Kernel
lsmod
a
ry
3.3 Shell
The shell is a command-line interface (CLI) that allows users to interact with the kernel and
run commands or scripts. It interprets user inputs and communicates them to the kernel.
su
Types of Shells
Key Features
w
touch myfile.txt
3.4 Applications
Applications are user-level programs that run on top of the shell and kernel. They provide
functionality for end-users, such as web browsers, text editors, or DevSecOps tools.
Types of Applications
● System Utilities: Tools like ls, cat, chmod for system management.
● Graphical Applications: Browsers (e.g., Firefox), IDEs (e.g., VS Code).
● DevSecOps Tools: Docker, Kubernetes, Jenkins for development and operations.
● Custom Applications: User-developed programs in languages like Python, C++, or
Java.
Role in Linux
● Applications interact with the kernel through system calls, facilitated by system
libraries (e.g., glibc).
● They can be installed via package managers (e.g., apt, yum) or compiled from
source.
a
sudo apt install nano
ry
Interaction Between Components
4. Linux Flavours
D
Linux flavours, or distributions, are operating systems built around the Linux kernel, tailored
for specific use cases. Each distro combines the kernel with different software, package
managers, and configurations.
1. Ubuntu:
○ User-friendly, widely used for desktops and servers.
○ Package manager: APT (uses .deb packages).
○ Ideal for beginners and DevSecOps (e.g., supports Docker, Kubernetes).
2. Debian:
○ Stable and reliable, used for servers.
○ Package manager: APT.
○ Basis for Ubuntu.
3. Fedora:
○ Cutting-edge features, backed by Red Hat.
○ Package manager: DNF (uses .rpm packages).
○ Suited for developers and testing new technologies.
4. CentOS Stream / Rocky Linux:
○ Enterprise-focused, compatible with Red Hat Enterprise Linux (RHEL).
○ Package manager: YUM/DNF.
○ Common in production servers.
5. Arch Linux:
○ Minimal and highly customizable.
○ Package manager: Pacman.
a
○ Preferred by advanced users.
6. Kali Linux:
ry
○ Designed for security professionals and penetration testing.
○ Pre-installed with security tools like Metasploit, Nmap.
Choosing a Flavour
su
● For DevSecOps: Ubuntu or CentOS for stability and tool support.
● For learning: Ubuntu or Fedora for ease of use.
● For security: Kali Linux for penetration testing.
ith
5. What is Unix?
w
Unix is an operating system developed in the 1970s at Bell Labs by Ken Thompson, Dennis
Ritchie, and others. It is a multitasking, multi-user system known for its stability and
portability.
ev
a
File System NTFS, FAT32 ext4, Btrfs, XFS APFS, HFS+
Command Line PowerShell, CMD Bash, Zsh, etc. Bash, Zsh (Terminal)
ry
Security Vulnerable to Strong Secure (Unix-based,
malware (permission-based) Gatekeeper)
su
Use Case
Software
General-purpose,
gaming
Wide (e.g., MS
Servers, DevSecOps,
development
Open-source tools,
Creative work, Apple
ecosystem
Apple-specific apps,
Support Office, games) DevSecOps some Unix
ith
Hardware Runs on diverse Runs on diverse Apple hardware only
hardware hardware
w
Key Differences
● Windows: User-friendly, proprietary, widely used for desktops and gaming. Less
ev
● The root directory (/) is the top-level directory in Ubuntu’s file system.
● All other directories, such as /bin, /home, and /etc, are located under /.
● It serves as the foundation for the entire file system hierarchy.
cd /
ls
Sample Output:
bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
a
2. Binary Directory (/bin)
ry
● The /bin directory stores essential binary files (executable commands) used by all
users, including the root user.
su
● These are critical system commands available even in single-user mode.
● Examples of binaries: ls, cp, mv, cat, bash.
ls
w
Sample Output:
ev
Note: Additional user-installed binaries may reside in /usr/bin, and system administration
D
● The /home directory contains home directories for all users (except the root user,
whose home is /root).
● In Ubuntu, the default user (e.g., ubuntu) has a home directory at /home/ubuntu.
● When you create a new user, their directory is automatically created under /home
(e.g., /home/newuser).
ls
a
4. Configuration Directory (/etc)
ry
● The /etc directory stores system-wide configuration files.
● Examples:
○ /etc/passwd: Contains user account information.
su ○ /etc/apt/sources.list: Lists software sources for the apt package
manager.
To check where binary files are stored in Ubuntu, you can navigate to the /bin directory
ev
cd /
ls
D
cd bin
ls
cd /
ls
cd /bin
a
ls
ry
3. Check your home directory:
su
cd /home/ubuntu
ls
ith
4. Create a new user and verify their home directory:
ls /home
ev
Ubuntu Commands
1. System Information Commands
D
To identify the operating system and its version in Ubuntu, use the lsb_release command.
Command:
lsb_release -a
Description:
● Displays detailed information about the Ubuntu version, including release number,
codename, and description.
● LSB stands for Linux Standard Base.
Example:
lsb_release -a
Sample Output:
a
Distributor ID: Ubuntu
ry
Release: 24.04
Codename:
su noble
Note: Ubuntu releases updates in April (e.g., 24.04) and October (e.g., 24.10). LTS versions
(e.g., 22.04, 24.04) are released every two years in April and supported for five years, ideal
ith
for stability in production environments.
To display the system’s date and time, use the date command.
Command:
ev
date
D
Description:
● Shows the current date and time based on the system clock.
Example:
date
Sample Output:
a
Command:
ry
whoami
su
Description:
Example:
ith
whoami
w
Sample Output:
ubuntu
ev
Note: In Ubuntu, the default user is often ubuntu for cloud or server installations.
D
Command:
Example:
echo $HOME
Sample Output:
a
Hello, Ubuntu!
ry
/home/ubuntu
su
2.2 Change Directory (cd)
cd [directory]
w
Description:
ev
Examples:
cd /
cd /home/ubuntu/Boys
cd ..
a
5. Move up two directories:
ry
cd ../..
su
2.3 List Directory Contents (ls)
ls
w
ls -l
ev
Description:
Example:
ls
ls -l
Sample Output:
# ls
# ls -l
a
● drwxr-xr-x: Indicates Boys is a directory (d).
ry
● -rw-r--r--: Indicates file1.txt and file2.txt are files (-).
Command:
ith
touch [filename]
Description:
w
Examples:
touch file.txt
touch file{1..5}.txt
ls
Sample Output:
file1.txt file2.txt file3.txt file4.txt file5.txt
Command:
mkdir [directory_name]
a
Description:
ry
● Creates one or more directories.
● Supports brace expansion for multiple directories (e.g., day{0..10}).
● Use -p to create parent and child directories together.
su
Examples:
mkdir suryamani
ith
mkdir Boys
w
mkdir day{0..10}
ev
ls
D
Sample Output:
day0 day1 day2 day3 day4 day5 day6 day7 day8 day9 day10
mkdir -p parent/child
ls parent
Sample Output:
child
Command:
mv [source] [destination]
a
ry
Description:
Examples:
ith
1. Move a directory (suryamani) into another directory (Boys):
mkdir suryamani
mkdir Boys
w
mv suryamani Boys
ev
cd Boys
ls
D
Sample Output:
suryamani
cd Boys
ls
Sample Output:
suryamani
mv Boys/suryamani Boys/newname
ls Boys
a
Sample Output:
ry
newname
su
2.7 Delete Files and Directories (rm)
rm [file]
w
rm -r [directory]
ev
Description:
for recursive).
● Use -f (force) to avoid confirmation prompts (e.g., rm -rf).
Examples:
touch file.txt
rm file.txt
ls
2. Delete a directory and its contents:
mkdir Boys
rm -r Boys
ls
a
2.8 View Command History
ry
The history command displays previously executed commands.
Command:
historysu
Description:
ith
● Shows a numbered list of commands run in the current session.
● Useful for recalling and reusing commands.
Example:
w
history
ev
Sample Output:
1 lsb_release -a
D
2 date
3 whoami
4 touch file1.txt
5 ls -l
Tip:
● Run a specific command from history using its number:
!3
Practice Workflow
To practice the commands in Ubuntu:
a
lsb_release -a
ry
2. Create directories and move one into another:
su
mkdir suryamani Boys
mv suryamani Boys
cd Boys
ith
ls
w
cd ..
ev
touch file{1..5}.txt
mkdir day{0..10}
ls
mkdir -p parent/child
6. Delete a file and directory:
rm file1.txt
rm -r Boys
history
a
ry
1. User Creation
1.1 Create a User
su
To create a new user in Ubuntu, use the useradd command with the -m option to create a
home directory.
Command:
ith
sudo useradd -m [username]
w
Description:
Example:
D
ls /home
Sample Output:
suryamani ubuntu
Note:
● The new user’s home directory (/home/suryamani) contains default subdirectories
like Documents, Downloads, etc.
● To set a shell (e.g., Bash), add -s /bin/bash:
Command:
a
sudo passwd [username]
ry
Description:
su
● Prompts for a new password for the specified user.
● Requires sudo for administrative access.
Example:
ith
sudo passwd suryamani
w
Prompt:
Output:
D
Note:
Command:
a
cat [filename]
ry
Description:
Example:
Sample Output:
Hello, Ubuntu!
ev
The vim command is a powerful text editor for editing files in the terminal.
Command:
vim [filename]
Description:
Example:
vim file.txt
a
3. Type text (e.g., “This is a test file.”).
4. Press Esc to return to normal mode.
ry
5. Type :wq and press Enter to save and quit.
6. Verify the content:
su
cat file.txt
Sample Output:
ith
This is a test file.
w
Note:
● If you get stuck, press Esc and type :q! to exit without changes.
● Install Vim if not present:
Command:
Description:
Example:
a
echo -e "Line 1\nLine 2\nLine 3\nLine 4" > file.txt
head -n 3 file.txt
ry
Sample Output:
su
Line 1
Line 2
ith
Line 3
w
Command:
Description:
Example:
tail -n 2 file.txt
Sample Output:
Line 3
Line 4
Command:
cat /etc/passwd
a
ry
Description:
Example:
ith
cat /etc/passwd
Sample Output:
w
root:x:0:0:root:/root:/bin/bash
ev
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
suryamani:x:1001:1001::/home/suryamani:/bin/bash
D
Explanation:
● suryamani:x:1001:1001::/home/suryamani:/bin/bash:
○ Username: suryamani
○ Password: x (stored in /etc/shadow)
○ UID: 1001
○ GID: 1001
○ Home: /home/suryamani
○ Shell: /bin/bash
3.2 View All Groups
Command:
Description:
a
Example:
ry
sudo cat /etc/group
su
Sample Output:
DevSecOps:x:1002:
ith
Developer:x:1003:suryamani
4. Group Management
w
Command:
D
Description:
Examples:
a
Sample Output:
ry
DevSecOps:x:1002:
Developer:x:1003:
su
4.2 Add User to a Group
ith
To add a user to a group, use the usermod command with -aG.
Command:
w
Description:
● -aG: Adds the user to the specified group (-a ensures appending, not overwriting).
● Requires sudo.
D
Example:
Sample Output:
DevSecOps:x:1002:suryamani
Note:
groups suryamani
a
5.1 Delete a User
ry
To delete a user, use the userdel command.
Command:
su
sudo userdel -r [username]
Description:
ith
● -r: Deletes the user’s home directory and mail spool.
● Requires sudo.
Example:
w
ls /home
Sample Output:
D
ubuntu
Command:
Note:
a
Practice Workflow
ry
1. Create a user and set their password:
touch file.txt
vim file.txt
# Inside vim: press i, type "Test content", press Esc, type :wq
cat file.txt
head -n 3 file.txt
tail -n 2 file.txt
cat /etc/passwd
a
sudo userdel -r suryamani
ry
sudo groupdel Developer
su
Additional Notes
● Permission Denied: Always use sudo for administrative commands (e.g., useradd,
ith
groupadd) to avoid “permission denied” errors.
● Password Security: Use strong passwords and store them securely.
● Vim Tips:
○ Practice switching between modes (insert, normal, command).
w
Server (RSA)
1. User Management
1.1 Primary and Secondary Groups
When you create a user in Ubuntu, a primary group with the same name as the user is
automatically created. Users can also be added to secondary groups for additional
permissions.
● Primary Group:
○ Assigned to a user at creation, used as the default group for files created by
the user.
○ Stored in /etc/passwd (GID field) and /etc/group.
● Secondary Group:
○ Additional groups a user can belong to for shared access to resources.
○ Added using usermod -aG.
Example:
a
1. Create a user suryamani:
ry
sudo useradd -m suryamani
Sample Output:
w
suryamani:x:1001:1001::/home/suryamani:/bin/bash
ev
Sample Output:
suryamani:x:1001:
Sample Output:
DevSecOps:x:1002:suryamani
a
groups suryamani
ry
Sample Output:
Command:
w
su [username]
ev
Description:
user’s password.
Example:
su suryamani
Prompt:
Password:
2. Perform file operations as suryamani:
ls
touch file1.txt
ls -l
Sample Output:
a
-rw-r--r-- 1 suryamani suryamani 0 May 20 13:00 file1.txt
ry
● The file’s owner is suryamani, and the group is suryamani (primary group).
3. Exit back to the original user:
exit
su
ith
Note:
● Use sudo -i suryamani for a full login session with the user’s environment.
● Without sudo, su requires the target user’s password, which may cause “permission
denied” if not known.
w
SSH keys are used for secure authentication between systems. In Ubuntu, RSA keys are
generated in the ~/.ssh directory.
D
Command:
cd ~/.ssh
Description:
Example:
cd ~/.ssh
ls
a
2. Generate an RSA key pair:
ry
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Prompt:
su
Generating public/private rsa key pair.
ith
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
3. Press Enter for defaults (creates id_rsa private key and id_rsa.pub public key).
4. Verify:
ls
D
Sample Output:
id_rsa id_rsa.pub
a
3.1 What is a Jump Server?
ry
A jump server is an intermediary server used to securely access other servers in a private
network. In this scenario, you have two AWS EC2 instances:
● EC2-1 (Jump Server): Accessible from your local machine, with a public IP (e.g.,
su
203.0.113.1).
● EC2-2 (Private Server): In a private subnet, accessible only via EC2-1, with a private
IP (e.g., 10.0.0.2).
ith
You’ll generate an RSA key on EC2-1, transfer it to your local machine using SCP, then to
EC2-2, enabling secure communication via the jump server.
3.2 Prerequisites
w
● Security groups:
○ EC2-1: Allow SSH (port 22) from your local machine’s IP.
○ EC2-2: Allow SSH (port 22) from EC2-1’s private IP.
● SSH key for EC2-1 (e.g., ec2-key.pem) for initial access.
D
3.3 Workflow
This workflow generates an RSA key on EC2-1, transfers it to your local machine, then to
EC2-2, and sets up EC2-1 as a jump server to access EC2-2.
cd ~/.ssh
a
ry
Step 2: Transfer Public Key to Local Machine Using SCP
ssh-add ec2-key.pem
ev
mkdir -p ~/.ssh
Then, on EC2-1:
a
1. On EC2-1, ensure SSH access to EC2-2:
ry
● This copies EC2-1’s public key to EC2-2’s authorized_keys.
su
Step 5: Test Jump Server Connection
whoami
ev
Sample Output:
ubuntu
D
2. Transfer a file from your local machine to EC2-2 via EC2-1:
scp -J [email protected] localfile.txt [email protected]:/home/ubuntu/
● Set:
a
PasswordAuthentication no
ry
● Restart SSH:
su
sudo systemctl restart sshd
ith
● Use 4096-bit RSA keys for better security.
● Regularly rotate SSH keys.
● Restrict security group rules to specific IPs.
3.5 Troubleshooting
w
● Timeout: Ensure EC2 instances are running and network configurations are correct.
Practice Workflow
1. Create a user and verify groups:
su suryamani
touch file1.txt
a
ls -l
exit
ry
3. Set up RSA keys on EC2-1:
su
ssh -i ec2-key.pem [email protected]
cd ~/.ssh
ith
ssh-keygen -t rsa -b 4096 -C "jump-server-key"
Additional Notes
● Primary vs. Secondary Groups:
○ Primary group owns the user’s files by default.
○ Secondary groups provide additional access (e.g., shared directories).
● SSH Security:
○ RSA keys with 4096-bit length are secure but slightly slower than ED25519.
○ Restrict .ssh directory permissions to 700:
● Jump Server:
○ Simplifies access to private EC2 instances in a VPC.
○ Use ProxyJump in SSH config for easier access:
a
● Man Pages: Use man [command] for details (e.g., man ssh-keygen, man scp).
ry
Ubuntu File Permissions
su
1. Understanding File Permissions
1.1 Permission String
ith
When you run ls -l in Ubuntu, the output shows permissions in a 10-character string, such
as drwxrwxr-x.
w
Breakdown:
○ -: Regular file
○ l: Symbolic link
● Positions 2-4 (rwx): Permissions for the owner (user) who created the file.
● Positions 5-7 (rwx): Permissions for the group associated with the file.
D
Permissions:
Example:
ls -l
Sample Output:
● drwxrwxr-x:
○ d: mydir is a directory.
○ rwx (owner): The owner (ubuntu) can read, write, and execute.
a
○ rwx (group): The group (ubuntu) can read, write, and execute.
○ r-x (others): Others can read and execute, but not write.
● -rw-r--r--:
ry
○ -: file.txt is a regular file.
○ rw- (owner): The owner can read and write.
○ r-- (group): The group can read.
su ○ r-- (others): Others can read.
● r = 4 (read)
● w = 2 (write)
w
● x = 1 (execute)
0 0 0 0 --- ---
1 0 0 1 --x --x
2 0 1 0 -w- -w-
3 0 1 1 -wx -wx
4 1 0 0 r-- r--
5 1 0 1 r-x r-x
a
6 1 1 0 rw- rw-
ry
7 1 1 1 rwx rwx
su
For drwxrwxr-x:
ith
● Owner: rwx = 7
● Group: rwx = 7
● Others: r-x = 5
w
Use numeric values to set permissions for owner, group, and others.
Command:
Example:
ls -l file.txt
Sample Output:
a
chmod 755 file.txt
ry
ls -l file.txt
su
Sample Output:
Command:
Example:
ls -l file.txt
Sample Output:
ls -l file.txt
a
Sample Output:
ry
su
4. Changing Groups (chgrp)
The chgrp command changes the group ownership of a file or directory.
ith
Command:
Description:
● Use -R for recursive changes (e.g., for directories and their contents).
Example:
touch file.txt
ls -l file.txt
Sample Output:
ls -l file.txt
Sample Output:
a
ry
3. For a directory and its contents:
mkdir mydir
su
sudo chgrp -R DevSecOps mydir
ls -ld mydir
ith
Sample Output:
Command:
Description:
touch file.txt
ls -l file.txt
a
Sample Output:
ry
2. Change the owner to suryamani:
su
sudo chown suryamani file.txt
ls -l file.txt
ith
Sample Output:
ls -l file.txt
D
Sample Output:
Example:
Set sticky bit on a directory:
ls -ld shared_dir
a
Sample Output:
ry
drwxr-xr-t 2 ubuntu ubuntu 4096 May 20 13:00 shared_dir
su
● t indicates the sticky bit.
Practice Workflow
ith
1. Create a file and directory:
touch file.txt
mkdir mydir
w
ls -l
ev
ls -l file.txt
ls -ld mydir
ls -l file.txt
ls -l file.txt
a
5. Clean up:
ry
sudo userdel -r suryamani
rmdir mydir
ith
Additional Notes
w
Ubuntu Installation
1. Installing Nginx
1.1 Update Package Lists
Before installing any package, update the package lists to ensure you get the latest version.
Command:
Description:
Example:
a
sudo apt update
ry
Sample Output:
su
Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
Command:
Description:
Command:
sudo apt install nginx -y
Description:
Example:
a
sudo apt install nginx -y
ry
Sample Output:
Note:
Command:
curl http://localhost
Description:
Sample Output:
<!DOCTYPE html>
<html>
a
<head>
ry
<title>Welcome to nginx!</title>
...
<body>
su
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working.</p>
ith
...
</body>
w
</html>
ev
Alternative:
nginx -v
Sample Output:
Command:
Description:
a
● Shows whether Nginx is active, running, or stopped.
● Displays recent logs and process details.
ry
Example:
Active: active (running) since Tue 2025-05-20 13:00:00 UTC; 5min ago
w
Memory: 5.2M
CPU: 45ms
D
CGroup: /system.slice/nginx.service
Description:
Example:
a
systemctl status nginx
ry
2.3 Stop Nginx
su
Stop the Nginx service if needed.
Command:
ith
sudo systemctl stop nginx
Description:
w
Example:
ev
Sample Output:
Active: inactive (dead) since Tue 2025-05-20 13:05:00 UTC; 10s ago
2.4 Restart Nginx
Command:
Description:
a
2.5 Reload Nginx
ry
Reload Nginx to apply configuration changes without downtime.
Command:
su
sudo systemctl reload nginx
Description:
ith
● Reloads the configuration without interrupting active connections.
Command:
ev
Description:
Example:
Sample Output:
Synchronizing state of nginx.service with SysV service script with
/lib/systemd/systemd-sysv-install.
Command:
a
ry
Description:
Command:
Command:
Description:
● Removes the Nginx package but retains configuration files (e.g., /etc/nginx).
Example:
Command:
a
sudo apt purge nginx -y
ry
Description:
su
● Removes Nginx and its configuration files.
Description:
cat /etc/nginx/nginx.conf
Example:
Replace the default page:
a
echo "<h1>Hello, Ubuntu Nginx!</h1>" | sudo tee /var/www/html/index.html
ry
curl http://localhost
su
Sample Output:
Example:
Check recent errors:
tail -n 10 /var/log/nginx/error.log
D
5. Troubleshooting
● Port 80 Conflict:
○ If Nginx fails to start (e.g., "nginx: [emerg] bind() to 0.0.0.0:80 failed"):
● Restart Nginx:
a
systemctl status nginx
ry
journalctl -u nginx -n 50
su
● Permission Denied:
○ Ensure you use sudo for commands requiring administrative access.
Practice Workflow
ith
1. Update and install Nginx:
curl http://localhost
nginx -v
4. Remove Nginx:
Additional Notes
a
● Best Practices:
○ Always run sudo apt update before installing packages.
ry
○ Use systemctl reload instead of restart to avoid downtime when
updating configurations.
○ Regularly check logs (/var/log/nginx) for errors.
su
● Configuration:
○ Edit /etc/nginx/sites-available/default for custom site
configurations.
○ Test configuration syntax:
ith
sudo nginx -t
w
● Man Pages: Use man [command] for details (e.g., man apt, man systemctl).
1. Grep Command
D
1.1 Overview
grep searches for patterns (text or regular expressions) in files or input and outputs
matching lines.
Syntax:
1.3 Examples
a
echo -e "Hello Ubuntu\nWelcome to Linux\nUbuntu is great" > file.txt
ry
Sample Output:
su
Hello Ubuntu
Ubuntu is great
ith
Example 2: Case-Insensitive Search
Sample Output:
ev
Hello Ubuntu
Ubuntu is great
D
Sample Output:
Sample Output:
1:Hello Ubuntu
3:Ubuntu is great
a
ry
2. Awk Command
2.1 Overview
su
awk is a text-processing tool for extracting and manipulating data, especially useful for
columnar data.
Syntax:
ith
awk [options] 'pattern {action}' [file...]
w
2.3 Examples
Sample Output:
name city
suryamani Delhi
abhishek Mumbai
a
ry
Sample Output:
Sample Output:
ev
abhishek
Sample Output:
suryamani
3. Find Command
3.1 Overview
find searches for files and directories in a specified path based on criteria like name, type,
or size.
Syntax:
a
3.2 Common Options
ry
● -type: Specify file type (f for file, d for directory).
● -name: Match filename (supports wildcards).
● -exec: Execute a command on found files.
● -mtime: Find files modified N days ago.
su
3.3 Examples
Sample Output:
ev
/home/ubuntu/file.txt
/home/ubuntu/data.txt
D
Sample Output:
/home/ubuntu/test
Example 3: Find and Delete Files
Find and delete .log files older than 7 days:
Sample Output:
removed '/var/log/old.log'
a
Find files containing "error":
ry
find /var/log -type f -exec grep -l "error" {} \;
su
Sample Output:
/var/log/syslog
/var/log/nginx/error.log
ith
Example 5: Use with xargs
List all .txt files and count their lines:
w
Sample Output:
3 ./file.txt
D
2 ./data.txt
5 total
sed is a stream editor for text manipulation, often used with grep and awk.
Example: Replace "Ubuntu" with "Linux":
Sample Output:
Hello Linux
Welcome to Linux
Linux is great
a
4.2 Combining Tools
ry
Search for "error" in log files, extract timestamps, and format:
su
grep "error" /var/log/syslog | awk '{print $1, $2, $3}' | sed 's/May/05/'
Sample Output:
ith
05 20 13:00:00
w
5. Troubleshooting
● Grep: No Such File or Directory:
ev
ls file.txt
D
mkdir test
touch test/file1.txt
a
ry
2. Use grep to search:
echo $SHELL
Sample Output:
/bin/bash
a
1.2 Writing a Shell Script
ry
A shell script is a file containing a series of commands. Use the #!/bin/bash shebang at
the top to specify the interpreter. We’ll use vim to edit scripts.
su
Example: Create a simple script:
vim hello.sh
ith
In vim:
2. Type:
#!/bin/bash
ev
chmod +x hello.sh
./hello.sh
Sample Output:
Hello, Ubuntu!
Vim Basics:
a
● :q: Quit vim.
● :wq: Save and quit.
● :q!: Quit without saving (if you make a mistake).
ry
Best Practice:
su
● Always include the shebang (#!/bin/bash).
● Add comments for clarity (e.g., # This script prints a greeting).
● If stuck in vim, press Esc and type :q! to exit without saving.
ith
2. Variables and Arguments
2.1 Variables
w
Syntax:
ev
variable_name=value
Example:
D
vim greet.sh
Add:
#!/bin/bash
name="Suryamani"
echo "Hello, $name!"
Run:
chmod +x greet.sh
./greet.sh
a
Sample Output:
Hello, Suryamani!
ry
Types of Variables:
su
● Local Variables: Defined within the script (e.g., name="Suryamani").
● Environment Variables: Available system-wide (e.g., export
PATH="$PATH:/usr/local/bin").
ith
Quoting Rules:
● Use double quotes (") for strings with variables: echo "Hello, $name"
● Use single quotes (') to prevent variable expansion: echo 'Hello, $name'
w
2.2 Arguments
ev
Special Variables:
Example:
vim args.sh
Add:
#!/bin/bash
Run:
a
chmod +x args.sh
ry
Sample Output:
su
First argument: Ubuntu
3. Control Structures
3.1 If, Else, and Elif
ev
Syntax:
D
if [ condition ]; then
# code
# code
else
# code
fi
Example:
vim number.sh
Add:
#!/bin/bash
a
number=10
ry
echo "Number is greater than 5"
else
ith
echo "Number is less than 5"
fi
w
Run:
chmod +x number.sh
./number.sh
D
Sample Output:
Common Conditions:
● -eq: Equal
● -ne: Not equal
● -gt: Greater than
● -lt: Less than
● -ge: Greater than or equal
● -le: Less than or equal
3.2 Loops
For Loop
Syntax:
a
for item in list; do
# code
ry
done
su
Example:
vim loop.sh
ith
Add:
#!/bin/bash
w
for i in 1 2 3 4 5; do
ev
done
D
Run:
chmod +x loop.sh
./loop.sh
Sample Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
While Loop
a
Runs as long as a condition is true.
Syntax:
ry
while [ condition ]; do
# code
done
su
ith
Example:
vim while.sh
w
Add:
ev
#!/bin/bash
count=1
((count++))
done
Run:
chmod +x while.sh
./while.sh
Sample Output:
Count: 1
Count: 2
Count: 3
a
Count: 4
Count: 5
ry
3.3 Case Statement
su
Alternative to if-elif for multiple conditions.
Syntax:
ith
case $variable in
pattern1)
w
# code
;;
ev
pattern2)
# code
;;
D
*)
# default case
;;
esac
Example:
vim case.sh
Add:
#!/bin/bash
fruit="apple"
case $fruit in
"apple")
a
echo "This is an apple"
;;
ry
"banana")
*)
ith
echo "Unknown fruit"
;;
esac
w
ev
Run:
chmod +x case.sh
D
./case.sh
Sample Output:
This is an apple
4. Functions
4.1 Defining Functions
Syntax:
function_name() {
# code
a
Example:
vim func.sh
ry
Add: su
#!/bin/bash
greet() {
ith
echo "Hello, $1!"
greet "Suryamani"
w
greet "Abhishek"
ev
Run:
D
chmod +x func.sh
./func.sh
Sample Output:
Hello, Suryamani!
Hello, Abhishek!
4.2 Return Values
Example:
vim check.sh
Add:
a
#!/bin/bash
ry
check_number() {
if [ $1 -gt 0 ]; then
su
else
return 0
return 1
ith
fi
}
w
check_number 5
Run:
D
chmod +x check.sh
./check.sh
Sample Output:
Exit status: 0
5. Server Backup Script
5.1 Backup Script Example
This script backs up a directory, compresses it, and logs the process.
Script:
vim backup.sh
Add:
a
#!/bin/bash
ry
# Variables
su
SOURCE_DIR="/var/log"
BACKUP_DIR="/home/ubuntu/backups"
DATE=$(date +%Y%m%d_%H%M%S)
ith
BACKUP_FILE="$BACKUP_DIR/backup_$DATE.tar.gz"
LOG_FILE="/home/ubuntu/backup.log"
w
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
D
fi
# Perform backup
exit 1
fi
a
echo "$(date): Deleted backups older than 7 days" >> "$LOG_FILE"
ry
Save and exit (Esc, :wq).
Explanation:
su
● Variables: Define source, backup directory, and log file.
● Error Handling: Check if backup directory exists; log errors if tar fails.
● Compression: Use tar -czf to create a compressed .tar.gz file.
ith
● Cleanup: Delete backups older than 7 days using find.
chmod +x backup.sh
w
./backup.sh
ev
Check Logs:
cat /home/ubuntu/backup.log
D
Sample Output:
Tue May 20 14:00:01 IST 2025: Deleted backups older than 7 days
6. Scheduled Backup (Cron Job)
6.1 Setting Up a Cron Job
* * * * * /path/to/script.sh
# minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-6)
a
Example: Schedule the backup script to run daily at 2 AM.
ry
1. Edit the crontab file:
crontab -e
su
● If prompted, select vim as the editor.
2. Add the following line:
ith
0 2 * * * /home/ubuntu/backup.sh
crontab -l
D
Sample Output:
0 2 * * * /home/ubuntu/backup.sh
Common Schedules:
● Daily at midnight: 0 0 * * *
● Weekly on Sunday at 3 AM: 0 3 * * 0
● Hourly: 0 * * * *
7. Daily Tasks of a Linux Admin
7.1 Monitoring System Resources
top
df -h
free -h
a
● top: Real-time system monitoring.
● df -h: Disk usage in human-readable format.
ry
● free -h: Memory usage.
tail -n 50 /var/log/syslog
ith
sudo logrotate -f /etc/logrotate.conf
who
last
cat /home/ubuntu/backup.log
ls -lh /home/ubuntu/backups
a
Keep the system updated:
ry
sudo apt update
su
8. Debugging and Error Handling
ith
8.1 Debugging Scripts
#!/bin/bash
w
set -x
ev
number=5
Sample Output:
Debugging example
+ number=5
+ echo Number is 5
Number is 5
#!/bin/bash
if ! command; then
a
exit 1
fi
ry
su
9. Troubleshooting
● Permission Denied:
○ Ensure the script is executable:
ith
chmod +x script.sh
w
● Vim Issues:
○ If stuck, press Esc and type :q! to exit without saving.
○ Install vim if not present:
Practice Workflow
1. Create a script with vim:
vim myscript.sh
Add:
#!/bin/bash
name=$1
for i in 1 2 3; do
a
done
ry
Save and exit (Esc, :wq).
./myscript.sh Suryamani
ith
Sample Output:
vim backup.sh
crontab -e
# Add: 0 2 * * * /home/ubuntu/backup.sh
df -h
tail -n 50 /var/log/syslog
a
Q1: What is Linux, and why is it used?
ry
Answer: Linux is an open-source operating system kernel used in distributions like Ubuntu.
It’s used for its stability, security, and flexibility in servers, desktops, and embedded systems.
For example, Ubuntu is widely used for web servers due to its reliability.
su
Follow-Up: Name a few Linux distributions (e.g., Ubuntu, CentOS, Fedora).
ith
Q2: What is the difference between bash and sh?
Answer: bash (Bourne Again Shell) is an enhanced version of sh (Bourne Shell). Bash
includes additional features like command history and tab completion. In Ubuntu,
w
/bin/bash is the default shell, while /bin/sh is often a symbolic link to bash or another
shell like dash.
ev
Example:
echo $SHELL
D
Output:
/bin/bash
Answer: Use the pwd command to print the current working directory.
Example:
pwd
Output:
/home/ubuntu
a
Q4: What does the ls command do, and what are some common
options?
ry
Answer: The ls command lists files and directories. Common options include:
Example:
ith
ls -la
w
Output:
Example:
mkdir mydir
ls
Output:
mydir
a
Example:
ry
touch file.txt
ls
Output:
su
file.txt
ith
w
Example:
cat file.txt
Output:
Hello, Ubuntu!
Example:
du -h /home/ubuntu
Output:
4.0K /home/ubuntu
a
ry
Q9: What are file permissions in Linux, and how do you read them?
Answer: File permissions control access to files and directories, shown in a 10-character
su
string (e.g., -rwxr-xr-x):
Example:
w
ls -l file.txt
ev
Output:
● Owner (ubuntu) has read/write (rw-), group and others have read (r--).
Answer: Use the chmod command. Permissions can be set numerically (e.g., 755) or
symbolically (e.g., u+x).
Example:
chmod 755 file.txt
ls -l file.txt
Output:
a
Q11: How do you change the owner of a file?
ry
Example:
su
sudo chown suryamani file.txt
ls -l file.txt
ith
Output:
Answer: grep searches for a pattern in files or input and outputs matching lines.
D
Example:
Output:
Ubuntu
Q13: How do you check running processes in Linux?
Answer: Use the ps command to list processes or top for real-time monitoring.
Example:
ps aux | head -2
Output:
a
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
ry
root 1 0.0 0.1 168432 8928 ? Ss 14:00 0:01 /sbin/init
su
Q14: How do you kill a process in Linux?
kill 1234
ev
D
Answer: The /etc/passwd file stores user account information, including username, UID,
GID, home directory, and shell.
Example:
head -1 /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash
Example:
a
sudo apt install vim -y
ry
su
Q17: What does the man command do?
Answer: The man command displays the manual page for a command, providing usage and
options.
ith
Example:
man ls
w
ev
Example:
D
cat output.txt
Output:
Test
Q19: What is a symbolic link, and how do you create one?
Example:
ln -s file.txt link.txt
ls -l link.txt
a
Output:
ry
lrwxrwxrwx 1 ubuntu ubuntu 8 May 20 14:00 link.txt -> file.txt
su
Q20: How do you check the system’s uptime?
ith
Answer: Use the uptime command to see how long the system has been running.
Example:
uptime
w
ev
Output:
Answer:
● Hard Link: Points directly to the inode of a file; deleting the original file doesn’t affect
the link (ln file hardlink).
● Symbolic Link: A pointer to the file path; deleting the original file breaks the link (ln
-s file symlink).
Example:
ln file.txt hardlink.txt
ln -s file.txt symlink.txt
ls -l
Output:
a
-rw-r--r-- 2 ubuntu ubuntu 0 May 20 14:00 hardlink.txt
ry
su
Q22: How do you find files modified in the last 7 days?
Q23: What does the awk command do, and how do you use it to extract a
column?
D
Answer: awk processes text and extracts data, often used for columnar data. Use $N to
access the Nth field.
Example:
age
25
Answer: Use crontab to schedule tasks. Edit with crontab -e and add a line in the
format: minute hour day month day-of-week command.
a
Example: Run a script daily at 2 AM:
ry
crontab -e
Add:
su
0 2 * * * /home/ubuntu/backup.sh
ith
Answer: The /etc/fstab file defines how disk partitions, filesystems, and mount points
are mounted at boot.
ev
Example:
cat /etc/fstab
D
Sample Output:
Example:
tail -f /var/log/syslog
Output:
a
Q27: What is a shell script, and how do you write a simple one?
ry
Answer: A shell script is a file with commands executed by the shell. Use #!/bin/bash at
the top.
su
Example:
vim script.sh
ith
Add:
#!/bin/bash
w
Run:
chmod +x script.sh
D
./script.sh Ubuntu
Output:
Hello, Ubuntu!
Q28: How do you check network connectivity in Linux?
Example:
ping -c 4 google.com
Output:
a
ry
Q29: What does the top command do, and how is it different from htop?
Answer:su
● top: Displays real-time system processes (CPU, memory usage).
● htop: An enhanced version of top with a user-friendly interface and additional
ith
features like scrolling and process killing.
Example:
top
w
ev
Q30: How do you find and kill a process using a specific port?
Example:
Example:
export MY_VAR="test"
Output:
a
test
ry
su
Q32: How do you redirect both stdout and stderr to a file?
Answer: Use > for stdout and 2> for stderr, or combine with &>.
Example:
ith
ls nonexistent 2> error.log
cat error.log
w
Output:
ev
Answer:
● sudo: Runs a single command as another user (usually root) with your password.
● su: Switches to another user (e.g., root) and requires the target user’s password.
Example:
sudo ls /root
su root
Example:
#!/bin/bash
a
tar -czf /backup/backup_$(date +%Y%m%d).tar.gz /var/log
ry
if [ $? -eq 0 ]; then
else su
echo "Backup failed"
fi
ith
w
Answer: The /var/log directory stores system logs, like /var/log/syslog (general
ev
Example:
ls /var/log
D
Output:
Example:
uname -r
Output:
5.15.0-73-generic
a
Q37: What is logrotate, and why is it used?
ry
Answer: logrotate is a utility to rotate, compress, and manage log files to prevent them
from growing too large.
su
Example:
Example:
Output:
1.2G /home/ubuntu
500M /home/ubuntu/largefile
Example:
source ~/.bashrc
a
Answer: Check the service status and logs:
ry
1. Use systemctl status to see the status.
2. Check logs with journalctl.
su
Example:
journalctl -u nginx -n 50
ith
Quiz Questions
w
D) dir
Answer: C
Explanation: The pwd command prints the current working directory's full path.
Answer: A
Explanation: useradd -m creates a user with a home directory. adduser is also valid but
is more interactive.
Question 3: Groups
a
How do you add a user to an existing group?
A) groupadd user group
ry
B) usermod -aG group user
C) useradd -g group user
D) gpasswd user group
su
Answer: B
Explanation: usermod -aG group user appends the user to the specified group without
removing them from other groups.
ith
Question 4: SSH
w
C) ssh ec2-user@ec2-host
D) ssh -key key.pem ec2-user@ec2-host
Answer: A
Explanation: ssh -i key.pem specifies the private key for secure connection to an EC2
D
instance.
Question 5: SCP
Which command copies a local directory to an AWS EC2 instance?
A) scp -i key.pem -r /local/dir ec2-user@ec2-host:/remote/dir
B) scp -i key.pem /local/dir ec2-user@ec2-host:/remote/dir
C) ssh -i key.pem /local/dir ec2-user@ec2-host:/remote/dir
D) scp -r /local/dir ec2-user@ec2-host:/remote/dir
Answer: A
Explanation: scp -i key.pem -r recursively copies a directory to the EC2 instance
using the private key.
Question 6: Grep
What does grep "error" /var/log/syslog do?
a
A) Lists all files containing "error".
B) Searches for lines containing "error" in /var/log/syslog.
ry
C) Deletes lines with "error" in /var/log/syslog.
D) Counts all files in /var/log.
Answer: B
su
Explanation: grep searches for the specified pattern ("error") in the given file and outputs
matching lines.
ith
Question 7: Awk
What does awk '{print $1}' file.txt do?
w
Answer: B
Explanation: awk '{print $1}' extracts and prints the first field (column) of each line,
using whitespace as the delimiter.
D
Question 8: Find
What does find / -type f -name "*.log" do?
A) Finds all directories named "*.log".
B) Finds all files with the .log extension.
C) Deletes all .log files.
D) Lists all files in the current directory.
Answer: B
Explanation: find / -type f -name "*.log" searches for files (-type f) with
names matching *.log across the filesystem.
a
D) No permissions for anyone.
Answer: A
ry
Explanation: 644 sets read/write (6) for the owner and read-only (4) for group and others.
su
Question 10: Installation
Which command installs a package on a Debian-based system?
ith
A) yum install package
B) apt-get install package
C) rpm -i package
D) dnf install package
w
Answer: B
Explanation: apt-get install is used to install packages on Debian-based systems like
Ubuntu.
ev
Answer: C
Explanation: systemctl restart stops and immediately starts the specified service.
Question 12: Shell Scripting
What is the shebang line in a Bash script?
A) #!/bin/bash
B) # This is a comment
C) echo "Hello"
D) exit 0
Answer: A
Explanation: The shebang (#!/bin/bash) specifies the interpreter for the script, in this
case, Bash.
a
ry
Question 13: Variables
How do you assign a variable in a Bash script?
A) VAR="value"
su
B) VAR=value()
C) set VAR=value
D) VAR:=value
ith
Answer: A
Explanation: In Bash, variables are assigned using VAR="value" without spaces around
the equals sign.
w
C) $@
D) $#
Answer: B
Explanation: $1 refers to the first command-line argument passed to the script.
Answer: B
Explanation: The -eq operator checks if the variable equals the specified number.
a
What does for i in {1..5}; do do in a Bash script?
ry
A) Loops 5 times, setting i from 1 to 5.
B) Loops indefinitely.
C) Loops over files in the current directory.
D) Loops over command-line arguments.
su
Answer: A
Explanation: The for loop iterates over the range {1..5}, setting i to 1, 2, 3, 4, and 5.
ith
Question 17: Functions
w
Answer: A
Explanation: Functions in Bash are defined using function name() { ... } or simply
D
name() { ... }.
Answer: B
Explanation: scp securely copies files between local and remote systems, such as an EC2
instance.
a
A) Runs script.sh every minute.
B) Runs script.sh every hour.
C) Runs script.sh every day.
ry
D) Runs script.sh once at system startup.
Answer: A
su
Explanation: * * * * * specifies every minute of every hour, day, month, and weekday.
ith
Question 20: File Permissions (AWS)
Why is chmod 400 key.pem used in the AWS SCP setup?
A) To make the key file executable.
B) To set read-only permissions for the owner.
w
Answer: B
Explanation: chmod 400 sets the key file to read-only for the owner, ensuring security for
SSH/SCP operations.
D