linux command
linux command
bash
Copy
Edit
ls -l # List files with details
cd /path # Change directory
pwd # Show current directory
mkdir dir # Create a new directory
rm -rf dir # Remove a directory and its contents
cp file1 file2 # Copy files
mv file1 file2 # Move/Rename files
🔹 File Viewing & Editing:
bash
Copy
Edit
cat file # View file content
less file # Scroll through a file
head -n 10 file # Show first 10 lines
tail -n 10 file # Show last 10 lines
nano file # Open file in nano editor
vi file # Open file in vi editor
2. Process Management & Monitoring
🔹 Check Running Processes:
bash
Copy
Edit
ps aux # Show all processes
top # Real-time process monitoring
htop # Interactive process monitoring (if installed)
pgrep process # Find a process by name
🔹 Manage Processes:
bash
Copy
Edit
kill <PID> # Kill a process by PID
kill -9 <PID> # Forcefully kill a process
pkill -9 name # Kill a process by name
nohup command & # Run a command in the background
jobs # List background jobs
fg %1 # Bring job 1 to foreground
3. System Performance Monitoring
🔹 Check CPU & Memory Usage:
bash
Copy
Edit
vmstat 2 5 # CPU & memory usage every 2 sec, 5 times
mpstat -P ALL # CPU usage per core
free -m # Check memory usage in MB
🔹 Check Disk & I/O Performance:
bash
Copy
Edit
df -h # Show disk usage
du -sh * # Show directory sizes
iostat -x 5 # Monitor I/O performance
iotop # Check real-time disk usage (requires installation)
🔹 Check Network Performance:
bash
Copy
Edit
netstat -tulnp # Show open ports & listening services
ss -tulnp # Faster alternative to netstat
iftop # Monitor real-time network traffic (if installed)
nload # Show network bandwidth usage
4. Log Management & Troubleshooting
🔹 View & Monitor Logs:
bash
Copy
Edit
cat /var/log/syslog # View system logs
cat /var/log/auth.log # View authentication logs
journalctl -f # Live log monitoring
tail -f /var/log/syslog # Live log updates
grep "error" /var/log/syslog # Find errors in logs
5. User & Group Management
🔹 User Management:
bash
Copy
Edit
useradd user1 # Create a new user
passwd user1 # Set password for user1
usermod -aG sudo user1 # Add user1 to sudo group
deluser user1 # Delete a user
🔹 Group Management:
bash
Copy
Edit
groupadd admin # Create a new group
usermod -aG admin user1 # Add user1 to group admin
groups user1 # Show groups for user1
6. Network Configuration & Troubleshooting
🔹 Check Network Interfaces:
bash
Copy
Edit
ip a # Show all network interfaces
ifconfig # Alternative to ip a (deprecated)
🔹 Test Network Connectivity:
bash
Copy
Edit
ping 8.8.8.8 # Test connectivity to Google DNS
traceroute 8.8.8.8 # Trace network path to destination
curl -I https://google.com # Test HTTP response
wget --spider https://google.com # Test website availability
🔹 Check Open Ports & Connections:
bash
Copy
Edit
netstat -tulnp # Show all listening ports and services
ss -tulnp # Faster alternative to netstat
7. Package Management
🔹 Install & Manage Packages:
bash
Copy
Edit
# Debian/Ubuntu
sudo apt update # Update package list
sudo apt install package # Install a package
sudo apt remove package # Remove a package
# RHEL/CentOS
sudo yum update # Update package list
sudo yum install package # Install a package
sudo yum remove package # Remove a package
8. Services & Daemon Management
🔹 Start, Stop & Restart Services:
bash
Copy
Edit
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart service
systemctl status nginx # Check service status
🔹 Enable/Disable Services on Boot:
bash
Copy
Edit
systemctl enable nginx # Enable service at startup
systemctl disable nginx # Disable service at startup
9. Shell Scripting & Automation
🔹 Write a Simple Script:
bash
Copy
Edit
#!/bin/bash
echo "Monitoring System Resources"
top -b -n1 | head -10
Save the file as monitor.sh, then make it executable:
bash
Copy
Edit
chmod +x monitor.sh
./monitor.sh
🔹 Schedule a Cron Job to Run Every 10 Minutes:
bash
Copy
Edit
crontab -e
Add the following line:
bash
Copy
Edit
*/10 * * * * /path/to/monitor.sh >> /var/log/cpu_usage.log
10. Security & Hardening
🔹 Check Failed Login Attempts:
bash
Copy
Edit
cat /var/log/auth.log | grep "Failed password"
🔹 Block an IP Address:
bash
Copy
Edit
# Using iptables
iptables -A INPUT -s 192.168.1.100 -j DROP
bash
Copy
Edit
sudo apt install snmpd # Install SNMP daemon (Ubuntu)
sudo yum install net-snmp # Install SNMP daemon (CentOS)
sudo systemctl restart snmpd # Restart SNMP service
🔹 Test SNMP Connectivity:
bash
Copy
Edit
snmpwalk -v2c -c public 192.168.1.1
🔹 Forward Logs to a Monitoring Server:
bash
Copy
Edit
echo "*.* @192.168.1.100:514" | sudo tee -a /etc/rsyslog.conf
sudo systemctl restart rsyslog