Important Linux Commands Handbook

Important Linux Commands Handbook

There are various linux commands which are used in daily operations, I have tried to combine most of them below.

  1. Display OS name, kernel version and hostname
    uname -a

  2. Display system uptime, load average and users logged in
    uptime

  3. Kernel details
    cat /proc/version

  4. Listing all the files

    ls

  5. cd - Change Directory

    cd /path/to/directory

  6. Print Working Directory

    pwd

  7. Copy
    cp source_file destination

  8. Move/Rename
    mv old_file new_location

  9. Remove/Delete
    rm file

  10. Make Directory

    mkdir new_directory

  11. Global Regular Expression Print
    grep pattern file.txt

  12. Change File Permissions
    chmod +x file.sh

  13. Change Ownership
    chown user:group file

  14. Process Status
    ps aux

  15. Terminate a Process
    kill PID

  16. Disk Free
    df -h

  17. Disk Usage
    du -h

  18. Tape Archive
    tar -czvf archive.tar.gz directory/

  19. Network Interface Configuration
    ifconfig

  20. Test Network Connectivity

    ping google.com

  21. Test if remote server is reachable over a specific port

    telnet host port

  22. Trace Route to a Host

    traceroute google.com

  23. Network Statistics

    netstat -a

  24. Socket Statistics

    ss -a

  25. IP Packet Filter and NAT
    iptables -L

  26. Check process running on a port
    lsof -i :80

  27. Query DNS for Information
    nslookup google.com

The resolv.conf file is a configuration file used by Linux systems to configure the DNS (Domain Name System) resolver. It contains information about the DNS servers and search domains that the system should use to resolve domain names to IP addresses.

cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

Firewall Commands

There are various firewall commands which can be used to check zones, open ports and close ports either temporary or permanently.

Ubutnu

On Ubuntu, the default firewall management tool is usually ufw (Uncomplicated Firewall).

1. Enable UFW
sudo ufw enable

2. Allow a Specific Port
sudo ufw allow 80/tcp

3. Allow a Specific Application (Service)
sudo ufw allow ssh

4. Deny a Specific Port
sudo ufw disable

Redhat

firewall-cmd is a command-line utility for managing firewalld, the default firewall management tool on many Linux distributions, such as CentOS, Fedora, and RHEL.

1. List all zones
firewall-cmd --get-zones
2. Show the default zone
firewall-cmd --get-default-zone
3. List all services
firewall-cmd --get-services
4. List allowed services in a zone
firewall-cmd --zone=public --list-services
5. Add a service to a zone
firewall-cmd --zone=public --add-service=http
6. Add a port to a zone
firewall-cmd --zone=public --add-port=80/tcp
7. Reload firewall rules
firewall-cmd --reload
8. Permanent changes
firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --reload
9. Open a range of ports
firewall-cmd --zone=public --add-port=3000-3100/tcp

Crontab

The crontab command in Unix-like operating systems is used to create, edit, display, and manage the cron jobs for a user. Cron jobs are scheduled tasks that run automatically at specified intervals.

Here are some common crontab commands and examples

crontab - Schedule Jobs
crontab -e

crontab - View Crontab
crontab -l

Cron Syntax

* * * * * command_to_be_executed

* represents minute, hour, day of the month, month, and day of the week.

30 15  * /path/to/script.sh
Example: Run a script every day at 3:30 PM.

systemctl - Enable/Start/Stop Services

systemctl enable cron       # Enable the cron service
systemctl start cron        # Start the cron service
systemctl stop cron         # Stop the cron service