LINUX
## **Index of Linux Notes**
- **1. Introduction to Linux**
- **2. Linux File System Hierarchy**
- Standard Directory Structure
- Important Directories
- **3. Basic Commands**
- File Operations
- Directory Operations
- System Information Commands
- **4. User and Group Management**
- Adding/Removing Users
- Managing Groups
- Understanding File Permissions
- **5. Package Management**
- Debian/Ubuntu Package Management
- Red Hat/CentOS Package Management
- **6. Process Management**
- Viewing Processes
- Managing Processes
- **7. Shell and Scripting**
- Shell Types
- Basic Shell Scripting
- **8. Disk Management**
- Disk Partitioning
- Filesystem Creation and Management
- **9. System Boot Process**
- Bootloaders
- Run Levels
- Systemd
- **10. System Services and Daemons**
- Managing Services with systemd
- Service Management Commands
- **11. Networking Basics**
- Network Configuration
- Network Troubleshooting Commands
- **12. Security Measures**
- User and Group Security
- Firewall Management
- **13. Networking Configuration**
- Network Interface Configuration
- Network Troubleshooting Commands
- **14. Advanced File System Management**
- Advanced File System Features
- Creating and Managing File Systems
- **15. System Logging and Monitoring**
- System Logs
- Log Monitoring Tools
- Log Rotation
- **16. Advanced Process Management**
- Process Prioritization
- Process Monitoring Tools
- **17. Automation with Shell Scripting**
- Shell Scripting Best Practices
- Advanced Scripting Concepts
- **18. System Performance Tuning**
- Kernel Parameters
- Performance Monitoring Tools
- **19. Security Practices**
- User and Group Security
- SELinux Modes
- **20. Backup Strategies**
- Backup Types
- Backup Tools
- **21. Virtualization and Cloud Computing**
- KVM Virtualization
- Cloud Solutions
- **22. Resource Management**
- Control Groups (cgroups)
- Resource Limits
- **23. Additional Resources**
- Online Documentation and Forums
- YouTube Channels and Podcasts
- **24. Study Tips**
--------------------------------------------------------------------------------------------------------------------------------------
### **1. Linux Basics**
#### **Introduction to Linux**
- **Linux Kernel**: The core part of the operating system that manages system resources and
communication between hardware and software.
- **Distributions**: Different versions of Linux (e.g., Ubuntu, Fedora, CentOS, Debian). Each
distribution comes with its package management and utilities.
- **Shell**: A command-line interpreter that allows users to interact with the OS (e.g., `bash`, `zsh`,
`sh`).
- **File System Hierarchy**: The directory structure starts with `/` (root directory), with important
subdirectories such as `/home`, `/bin`, `/etc`, `/var`, and `/tmp`.
#### **Basic Commands**
- **Navigating Directories**:
- `ls`: List files and directories.
- `cd`: Change directories.
- `pwd`: Show current directory.
- **File Manipulation**:
- `touch`: Create a new file.
- `cp`: Copy files.
- `mv`: Move or rename files.
- `rm`: Delete files or directories.
- **Viewing File Contents**:
- `cat`, `less`, `more`, `head`, `tail`: View contents of files.
- **File Permissions**:
- `chmod`: Change file permissions.
- `chown`: Change file ownership.
- **File Permissions Structure**: `r` (read), `w` (write), `x` (execute).
- **Permission levels**: `u` (user), `g` (group), `o` (others).
#### **Package Management**
- **Debian-based (e.g., Ubuntu)**:
- `apt-get update`: Updates package lists.
- `apt-get upgrade`: Upgrades installed packages.
- `apt-get install`: Installs a package.
- **Red Hat-based (e.g., CentOS, Fedora)**:
- `yum install`: Installs a package.
- `dnf install`: New package manager for Fedora.
- **Arch-based**:
- `pacman -Syu`: Update and install packages.
---
### **2. User and Group Management**
- **Creating Users and Groups**:
- `useradd`: Create a new user.
- `groupadd`: Create a new group.
- `usermod`: Modify an existing user.
- `passwd`: Set/change the password for a user.
- **Managing User Permissions**:
- **sudo**: Command used by regular users to execute commands as root or another user.
- `/etc/passwd`: File that stores user account information.
- `/etc/group`: File that stores group information.
- `/etc/shadow`: File that stores user passwords (encrypted).
---
### **3. File System and Storage Management**
#### **File Systems**:
- **Common File Systems**: `ext4`, `XFS`, `Btrfs`, `ZFS`.
- **Mounting File Systems**:
- `mount`: Mounts a file system.
- `umount`: Unmounts a file system.
- **Disk Partitioning**:
- `fdisk`: Partition a hard drive.
- `lsblk`: List block devices.
- `df -h`: Shows disk usage.
#### **Logical Volume Management (LVM)**:
- **LVM Components**:
- **Physical Volumes (PV)**: Physical storage devices.
- **Volume Groups (VG)**: Group of physical volumes.
- **Logical Volumes (LV)**: Virtual partitions from the volume group.
- **Commands**:
- `pvcreate`, `vgcreate`, `lvcreate`: Create physical volumes, volume groups, and logical volumes.
- `lvextend`: Extend logical volume size.
---
### **4. Process Management**
- **Viewing Processes**:
- `ps`: Display running processes.
- `top`, `htop`: Interactive process viewers.
- **Killing Processes**:
- `kill`: Send a signal to a process (e.g., `kill -9` to forcefully terminate).
- `killall`: Kill all processes by name.
- `pkill`: Kill processes matching a pattern.
- **Prioritizing Processes**:
- `nice`: Start a process with a specified priority.
- `renice`: Change the priority of a running process.
---
### **5. Networking**
#### **Basic Networking Commands**:
- `ifconfig`, `ip`: Configure network interfaces.
- `ping`: Check network connectivity.
- `netstat`, `ss`: View network connections.
- `traceroute`: Trace the path to a network host.
- `scp`, `rsync`: Securely copy files between systems.
- **Configuring Network Interfaces**:
- `/etc/network/interfaces`: Network configuration file (Debian-based).
- `nmcli`: NetworkManager command-line tool.
- `systemctl restart network`: Restart the network service.
---
### **6. System Monitoring and Performance**
#### **Monitoring Tools**:
- **CPU and Memory**:
- `vmstat`: Reports system performance.
- `free -h`: Shows memory usage.
- `uptime`: Shows system load average.
- **Disk I/O**:
- `iostat`: Monitor CPU and I/O statistics.
- `iotop`: Monitor disk I/O usage by processes.
- **Network**:
- `iftop`: Network bandwidth monitoring.
- `nload`: Network traffic and bandwidth usage.
---
### **7. Security**
#### **File Permissions and Ownership**:
- **Understanding Permissions**: `rwx` (Read, Write, Execute) for `user`, `group`, `others`.
- **Changing Permissions**: `chmod 755 filename` (gives read/write/execute to user and
read/execute to group/others).
- **Changing Ownership**: `chown user:group filename`.
#### **Firewall Configuration**:
- **iptables**: Command-line firewall utility.
- Basic syntax: `iptables -A INPUT -p tcp --dport 80 -j ACCEPT` (allow incoming HTTP traffic).
- **firewalld**: Frontend for `iptables` on RedHat-based systems.
- Commands: `firewall-cmd --add-service=http`, `firewall-cmd --reload`.
#### **SSH Configuration**:
- **Configuring SSH**:
- `sshd_config`: The main SSH configuration file (typically found in `/etc/ssh/sshd_config`).
- **Disable root login**: Set `PermitRootLogin no` for added security.
- **SSH Key Authentication**: Generate keys using `ssh-keygen`, and copy to remote machine using
`ssh-copy-id`.
---
### **8. Automation and Scripting**
#### **Shell Scripting**:
- **Writing Shell Scripts**:
- Basic structure: `#!/bin/bash`.
- Variables: `VAR="value"`, and use with `$VAR`.
- Conditional Statements: `if`, `elif`, `else`.
- Loops: `for`, `while`.
- Functions: `function_name() { commands }`.
- **Scheduling Jobs**:
- **Cron Jobs**: Automate recurring tasks.
- Edit cron jobs with `crontab -e`.
- Example cron syntax: `0 3 * * * /path/to/[Link]` (runs the backup script every day at 3 AM).
- **at Command**: Schedule one-time tasks.
- Usage: `at 12:00` (schedules a task at 12:00).
---
### **9. Boot Process**
#### **Linux Boot Process**:
1. **BIOS/UEFI**: Initializes hardware.
2. **Bootloader (GRUB)**: Loads the Linux kernel.
3. **Kernel Initialization**: Kernel manages hardware and mounts the root file system.
4. **Init/Systemd**: Initializes system services (systemd has replaced init in most modern distros).
#### **GRUB2 Configuration**:
- **File Location**: `/boot/grub/[Link]`.
- **Commands**:
- `grub-mkconfig`: Rebuild GRUB configuration file.
- `grub-install`: Install GRUB bootloader on a disk.
---
### **10. Virtualization and Containers**
#### **Virtualization**:
- **KVM**: Kernel-based Virtual Machine.
- Commands: `virt-manager` (GUI tool), `virsh` (CLI tool).
- Configure virtual machines and manage virtualization resources.
#### **Containers**:
- **Docker**: A popular containerization platform.
- Commands:
- `docker run`: Run a container.
- `docker ps`: List running containers.
- `docker build`: Build a Docker image.
- `docker-compose`: Define and run multi-container Docker applications.
- **LXC (Linux Containers)**: Lightweight virtualization.
- Commands: `lxc-create`, `lxc-start`, `lxc-stop`.
---
### **11. Backup and Recovery**
- **Backup Tools**:
- `rsync`: For incremental backups.
- `tar`: Archive and compress files.
- **Automating Backups**: Combine cron jobs with backup scripts.
- **Data Recovery**:
- `testdisk`, `photorec`: Tools for recovering lost partitions and files.
---
### **12. Advanced Topics**
#### **Kernel Tuning with `sysctl`**:
- `sysctl` allows you to modify kernel parameters at runtime.
- Example: `sysctl -w net.ipv4.ip_forward=1
---
### **13. Networking Configuration**
#### **Network Interface Configuration**
- **Network Configuration Files**:
- **Debian/Ubuntu**: `/etc/network/interfaces`
```plaintext
auto eth0
iface eth0 inet dhcp
```
- **Red Hat/CentOS**: `/etc/sysconfig/network-scripts/ifcfg-eth0`
```plaintext
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
```
- **Static IP Configuration**:
- Example for Debian-based:
```plaintext
auto eth0
iface eth0 inet static
address [Link]
netmask [Link]
gateway [Link]
```
#### **Network Troubleshooting Commands**
- `ping [host]`: Check connectivity to another host.
- `traceroute [host]`: Trace the route to a network host.
- `nslookup [domain]`: Query DNS information.
- `dig [domain]`: Another DNS query tool providing more details than `nslookup`.
- `curl [url]`: Retrieve or send data to/from a server using various protocols.
---
### **14. Advanced File System Management**
#### **Advanced File System Features**
- **Disk Quotas**: Control how much disk space users or groups can use.
- Set up with `edquota`, `quotaon`, `quotaoff`.
- **File System Types**: Understand the differences:
- `ext4`: Most common for general use.
- `XFS`: High-performance for large files.
- `Btrfs`: Supports snapshots, pooling, and checksumming.
#### **Creating and Managing File Systems**
- **Creating a File System**:
- `mkfs.ext4 /dev/sdX1`: Format a partition with ext4.
- **Checking File System**:
- `fsck`: Check and repair a Linux file system.
- **Mount Options**:
- `rw`: Read/write, `ro`: Read-only, `noexec`: Do not allow execution of binaries.
---
### **15. System Logging and Monitoring**
#### **System Logs**
- **Log Files**:
- `/var/log/syslog`: General system log.
- `/var/log/[Link]`: Authentication logs.
- `/var/log/[Link]`: Kernel logs.
- **Log Monitoring Tools**:
- `tail -f [logfile]`: Continuously monitor a log file.
- `journalctl`: View logs managed by `systemd`.
#### **Log Rotation**
- Managed by `logrotate` to prevent log files from consuming too much space.
- Configuration files typically found in `/etc/[Link]` and `/etc/logrotate.d/`.
---
### **16. Advanced Process Management**
#### **Process Prioritization**
- **Nice Values**:
- `nice`: Start a process with a modified priority.
- `renice`: Change the priority of an existing process.
- **Nice Value Range**: -20 (highest priority) to 19 (lowest priority).
#### **Process Monitoring Tools**
- **ps**: View current processes.
- `ps aux`: Show all processes with detailed information.
- **htop**: An interactive process viewer with user-friendly interface.
- **strace**: Trace system calls and signals for debugging.
- `strace -p [PID]`: Attach to a running process.
---
### **17. Automation with Shell Scripting**
#### **Shell Scripting Best Practices**
- **Shebang Line**: Always start scripts with `#!/bin/bash` or the appropriate interpreter.
- **Comments**: Use `#` for comments to enhance readability.
- **Error Handling**:
```bash
if [ $? -ne 0 ]; then
echo "Error occurred"
exit 1
fi
```
- **Using Functions**:
```bash
my_function() {
echo "This is a function"
my_function # Call the function
```
#### **Advanced Scripting Concepts**
- **Arrays**:
```bash
my_array=(one two three)
echo ${my_array[1]} # Outputs "two"
```
- **Using `getopts` for Options Parsing**:
```bash
while getopts ":f:" opt; do
case $opt in
f) file="$OPTARG";;
\?) echo "Invalid option";;
esac
done
```
---
### **18. System Performance Tuning**
#### **Kernel Parameters**
- **Adjusting Kernel Parameters**: Modify using `sysctl`.
```bash
sysctl -w [Link]=10 # Adjust swappiness
```
- **Persistent Changes**: Add to `/etc/[Link]` for changes to persist across reboots.
#### **Performance Monitoring Tools**
- **sar**: Collect, report, or save system activity information.
- **iostat**: Report CPU and I/O statistics.
- **vmstat**: Report virtual memory statistics.
---
### **19. Security Practices**
#### **User and Group Security**
- **Password Policies**: Enforce strong passwords using `/etc/[Link]` and PAM (Pluggable
Authentication Modules).
- **SSH Security Best Practices**:
- Use SSH keys instead of passwords.
- Change the default SSH port (22) for added security.
- Use `Fail2ban` to block IPs after failed login attempts.
#### **SELinux Modes**
- **Modes**:
- **Enforcing**: SELinux policy is enforced.
- **Permissive**: SELinux policy is not enforced, but logged.
- **Disabled**: SELinux is turned off.
- **Managing SELinux**:
- Use `setenforce [0|1]` to switch between permissive and enforcing modes.
---
### **20. Backup Strategies**
#### **Backup Types**
- **Full Backup**: A complete backup of all data.
- **Incremental Backup**: Backing up only the data changed since the last backup.
- **Differential Backup**: Backing up only the data changed since the last full backup.
#### **Backup Tools**
- **rsync**: Synchronizes files and directories between two locations.
- Example: `rsync -avz /source/ /destination/`
- **Bacula**: Network-based backup solution for managing backup, recovery, and verification of
data.
- **Amanda**: Open-source backup solution for enterprise backup.
---
### **21. Virtualization and Cloud Computing**
#### **KVM Virtualization**
- **KVM Installation**:
```bash
apt-get install qemu-kvm libvirt-daemon-system libvirt-clients
```
- **Managing VMs**:
- Use `virt-manager` for a graphical interface.
- Use `virsh` for command-line management.
#### **Cloud Solutions**
- **Docker**: Containerization for running applications in isolated environments.
- **Kubernetes**: Orchestrates deployment, scaling, and management of containerized applications.
---
### **22. Resource Management**
#### **Control Groups (cgroups)**
- **Purpose**: Limit and isolate resource usage (CPU, memory, disk I/O) for processes.
- **Commands**: Use `cgcreate`, `cgset`, and `cgexec` to manage cgroups.
#### **Resource Limits**
- **ulimit**: Set user limits on system resources.
- Example: `ulimit -n 1024` to limit open file descriptors.
---
### **23. Additional Resources**
#### **Online Documentation and Forums**
- **ArchWiki**: Comprehensive resource for Linux users.
- **LinuxQuestions**: Community-driven Q&A forum.
- **Stack Overflow**: Great for troubleshooting specific issues.
#### **YouTube Channels and Podcasts**
- **Linux Academy**: Educational content on various Linux topics.
- **Learn Linux TV**: Tutorials and guides on using Linux.
- **Podcast: Linux Action News**: Weekly updates on Linux news and developments.
---
### **Study Tips**
1. **Practice**: Set up a virtual machine or use cloud services (like AWS or DigitalOcean) to practice
Linux commands and configurations.
2. **Hands-On Projects**: Create small projects like a personal web server or file server.
3. **Documentation**: Frequently refer to the official documentation and man pages (use `man
[command]`).
Here’s an additional set of study material for Linux that covers new topics, complete with code
snippets and images.
---
### **1. Linux Shell Environment**
#### **Environment Variables**
- **View Environment Variables**:
```bash
printenv # Print all environment variables
echo $HOME # Display the home directory
```
- **Set Environment Variables**:
```bash
export MY_VAR="Hello World" # Set a new environment variable
```
- **Remove Environment Variables**:
```bash
unset MY_VAR # Remove the environment variable
```
**Image**: 
---
### **2. Networking in Linux**
#### **Configuring Network Interfaces**
- **Using `ip` Command**:
```bash
ip addr show # Display all network interfaces
ip link set eth0 up # Enable a network interface
```
- **Assigning a Static IP**:
```bash
sudo ip addr add [Link]/24 dev eth0 # Set a static IP
```
**Image**: 
---
### **3. Disk Management**
#### **Disk Usage Analysis**
- **Check Disk Usage**:
```bash
df -h # Display disk space usage in human-readable format
du -sh /path/to/directory # Check size of a specific directory
```
- **Find Large Files**:
```bash
find / -type f -size +100M # Find files larger than 100MB
```
**Image**: 
---
### **4. User Management**
#### **Managing User Accounts**
- **Add a New User**:
```bash
sudo adduser newuser # Create a new user
```
- **Delete a User**:
```bash
sudo deluser newuser # Delete a user
```
- **Modify User Properties**:
```bash
sudo usermod -aG sudo newuser # Add user to the sudo group
```
**Image**: 
---
### **5. File Compression and Archiving**
#### **Using Tar and Gzip**
- **Creating a Tar Archive**:
```bash
tar -cvf [Link] /path/to/directory # Create a tar file
```
- **Extracting a Tar Archive**:
```bash
tar -xvf [Link] # Extract tar file
```
- **Using Gzip for Compression**:
```bash
gzip filename # Compress a file
gunzip [Link] # Decompress a file
```
**Image**: 
---
### **6. System Monitoring and Performance**
#### **Monitoring System Performance**
- **Using `top` Command**:
```bash
top # Display active processes
```
- **Using `htop` Command** (if installed):
```bash
htop # Interactive process viewer
```
**Image**: 
---
### **7. Log Management**
#### **Managing System Logs**
- **Viewing Logs**:
```bash
tail -f /var/log/syslog # Continuously view the syslog file
```
- **Using `journalctl`**:
```bash
journalctl -xe # View detailed systemd logs
```
**Image**: 
---
### **8. Shell Scripting Basics**
#### **Creating a Simple Shell Script**
- **Sample Shell Script**:
```bash
#!/bin/bash
echo "Hello, World!" # Print a message
```
- **Make Script Executable**:
```bash
chmod +x [Link] # Make the script executable
./[Link] # Run the script
```
**Image**: 
---
### **9. Process Scheduling**
#### **Using `cron` Jobs**
- **Editing the Crontab**:
```bash
crontab -e # Edit crontab for current user
```
- **Sample Cron Job**:
```plaintext
* * * * * /path/to/[Link] # Run script every minute
```
**Image**: 
---
### **10. Security in Linux**
#### **Using `iptables` for Firewall Configuration**
- **List Current Rules**:
```bash
sudo iptables -L # List all iptables rules
```
- **Adding a Rule**:
```bash
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
```
**Image**: ![Iptables]([Link]
---