Linux Basics

Introduction to Linux

Linux is an open-source Unix-like operating system kernel that forms the foundation of many operating systems (distributions). Understanding Linux basics is essential for system administration and development.

Key Concepts:

  • Kernel and shell
  • File system hierarchy
  • Command line interface
  • User management
  • Process management
  • Package management
  • System services
  • Network configuration

System Information

# System information
uname -a                  # Kernel information
cat /etc/os-release      # Distribution info
lsb_release -a          # Distribution details

# Hardware information
lscpu                   # CPU information
free -h                 # Memory usage
df -h                   # Disk usage
lsblk                   # Block devices
lspci                   # PCI devices
lsusb                   # USB devices

# System resources
top                     # Process activity
htop                    # Interactive process viewer
uptime                  # System uptime
w                       # Who is logged in

Filesystem

File Operations

# Navigation
pwd                     # Print working directory
cd /path/to/dir        # Change directory
cd ..                  # Go up one level
cd ~                   # Go to home directory

# Listing files
ls                     # List files
ls -la                 # Detailed list
ls -R                  # Recursive list
tree                   # Directory tree

# File operations
touch file.txt         # Create empty file
mkdir directory        # Create directory
cp source dest         # Copy file
mv source dest         # Move/rename file
rm file               # Remove file
rm -r directory       # Remove directory
ln -s target link     # Create symbolic link

# File viewing
cat file.txt          # View file content
less file.txt         # Page through file
head -n 10 file.txt   # View first 10 lines
tail -f file.txt      # Follow file updates
grep pattern file     # Search in file

Filesystem Hierarchy

Standard Directories:

  • /bin - Essential user commands
  • /boot - Boot loader files
  • /dev - Device files
  • /etc - System configuration
  • /home - User home directories
  • /lib - System libraries
  • /media - Removable media
  • /mnt - Mount point
  • /opt - Optional software
  • /proc - Process information
  • /root - Root user's home
  • /sbin - System binaries
  • /tmp - Temporary files
  • /usr - User programs
  • /var - Variable files

Permissions

File Permissions

# View permissions
ls -l file.txt         # Show file permissions

# Change permissions
chmod 755 file.txt     # Octal notation
chmod u+x file.txt     # Symbolic notation
chmod -R g+w dir       # Recursive change

# Change ownership
chown user file.txt    # Change owner
chown user:group file  # Change owner and group
chgrp group file.txt   # Change group only

# Special permissions
chmod u+s file         # Set SUID
chmod g+s directory    # Set SGID
chmod +t directory     # Set sticky bit

# Access control lists
getfacl file          # View ACL
setfacl -m u:user:rx file  # Modify ACL
setfacl -x u:user file     # Remove ACL

Permission Bits:

  • r (4) - Read permission
  • w (2) - Write permission
  • x (1) - Execute permission
  • - - No permission
  • d - Directory
  • l - Symbolic link

Process Management

Process Control

# Process information
ps aux               # List all processes
ps -ef | grep ssh    # Find specific process
pgrep process_name   # Get process ID
top                  # Live process viewer

# Process control
kill pid             # Terminate process
killall process_name # Kill by name
pkill pattern       # Kill by pattern
nice -n 10 command  # Run with priority
renice +5 pid       # Change priority

# Background jobs
command &           # Run in background
bg                  # Send to background
fg                  # Bring to foreground
jobs                # List background jobs
nohup command &     # Run immune to hangup

# System control
shutdown -h now     # Shutdown
reboot             # Restart
systemctl status service  # Service status
systemctl start service  # Start service
systemctl enable service # Auto-start

Resource Monitoring

# Memory usage
free -h              # Memory statistics
vmstat              # Virtual memory stats
swapon -s           # Swap usage

# Disk usage
df -h               # Disk space usage
du -sh directory    # Directory size
iotop               # I/O monitoring
iostat              # I/O statistics

# System load
uptime              # Load average
sar                 # System activity
mpstat              # CPU statistics
pidstat             # Process statistics

Networking

Network Configuration

# Network interfaces
ip addr             # Show addresses
ip link             # Link status
ifconfig           # Interface config
iwconfig           # Wireless info

# Connectivity
ping host          # Test connectivity
traceroute host    # Trace route
mtr host           # Network diagnostic
dig domain         # DNS lookup
nslookup domain    # Name resolution

# Network monitoring
netstat -tuln      # Open ports
ss -tuln           # Socket statistics
tcpdump            # Packet capture
iftop              # Bandwidth usage

# Firewall
iptables -L        # List rules
ufw status         # UFW status
firewall-cmd --list-all  # FirewallD

Network Files:

  • /etc/hosts - Host entries
  • /etc/resolv.conf - DNS config
  • /etc/network/interfaces - Network config
  • /etc/netplan/* - Netplan config
  • /etc/sysconfig/network-scripts/ - Network scripts
  • /proc/net/* - Network information