Linux Server Mastery: Essential Shell Commands for Beginners Print

  • 1

Expert Insights: Mastering Server Management and Shell Commands for Linux

Introduction

The seamless functioning of servers is a crucial aspect in the upkeep of web applications and services, making server management a pivotal task in Linux environments, especially in distributions such as CentOS, AlmaLinux, and Rocky Linux 8. Let's delve into the quintessential shell commands vital for expert server management in these Linux distributions.

1. Fundamental Shell Commands

  • pwd

    • Explanation: Display the current working directory.
    • Example:
      • pwd - Output: /home/username
  • cd

    • Explanation: Change the current directory.
    • Example:
      • cd /var/www/html - Navigate to the '/var/www/html' directory.
  • ls

    • Explanation: List files and directories in the current directory.
    • Example:
      • ls -l - List files with detailed information.

2. File System Management

  • touch

    • Explanation: Create a new, empty file.
    • Example:
      • touch newfile.txt - Create 'newfile.txt'.
  • mkdir

    • Explanation: Create a new directory.
    • Example:
      • mkdir new_directory - Create 'new_directory'.
  • rm

    • Explanation: Remove files or directories.
    • Example:
      • rm -r old_directory - Remove 'old_directory' and its contents.

3. Process and Job Management

  • ps

    • Explanation: Display information about the running processes.
    • Example:
      • ps -aux - Display detailed information about all processes.
  • top

    • Explanation: Display a dynamic view of the processes running on the system.
    • Example:
      • top - Display a real-time updating list of processes.
  • bg

    • Explanation: Place a job in the background.
    • Example:
      • bg %1 - Place job number 1 in the background.

4. Network and Security Management

  • ifconfig

    • Explanation: Configure network interfaces.
    • Example:
      • ifconfig - Display information about all network interfaces.
  • iptables

    • Explanation: Administration tool for packet filtering and NAT.
    • Example:
      • iptables -L - List all current rules.
  • ss

    • Explanation: Utility to investigate sockets.
    • Example:
      • ss -tulwn - Display all open ports.

5. Monitoring and Troubleshooting

  • dmesg

    • Explanation: Display kernel messages.
    • Example:
      • dmesg | less - Display kernel messages one page at a time.
  • vmstat

    • Explanation: Report virtual memory statistics.
    • Example:
      • vmstat 1 5 - Display virtual memory stats every 1 second, 5 times in a row.
  • lsof

    • Explanation: List open files and the processes that opened them.
    • Example:
      • lsof -u username - List all files opened by a specific user.

6. User and Group Management

  • useradd

    • Explanation: Command to add a new user to the system.
    • Example:
      • useradd -m username - Create a new user with a home directory.
  • userdel

    • Explanation: Command to remove a user from the system.
    • Example:
      • userdel username - Delete a user from the system.
  • groupadd

    • Explanation: Command to add a new group to the system.
    • Example:
      • groupadd groupname - Create a new group.
      • groupdel groupname - Delete the group named "groupname".

7. File Permissions and Ownership

  • chmod

    • Explanation: Command to change file permissions.
    • Example:
      • chmod 755 filename - Set permissions of "filename" to 755 (rwxr-xr-x).
  • chown

    • Explanation: Command to change file ownership.
    • Example:
      • chown user:group filename - Change the owner and group of a file.
  • chgrp

    • Explanation: Command to change group ownership of a file.
    • Example:
      • chgrp groupname filename - Change the group ownership of a file to "groupname".

8. System Information and Hardware Stats

  • uname

    • Explanation: Command to display system information.
    • Example:
      • uname -a - Display all system information, including the kernel version and system architecture.
  • lscpu

    • Explanation: Command to display CPU information.
    • Example:
      • lscpu - Display detailed CPU information.
  • lsblk

    • Explanation: Command to list block devices.
    • Example:
      • lsblk - Display all block devices in a tree-like format.

9. Package and Software Management

  • yum / dnf

    • Explanation: Commands to manage packages in Red Hat-based distributions. Yum has been replaced by DNF in recent releases but functions similarly.
    • Example:
      • yum install package_name or dnf install package_name - Install a package.
      • yum update or dnf update - Update all packages to the latest version.
  • rpm

    • Explanation: Command to manage RPM packages.
    • Example:
      • rpm -i package.rpm - Install an RPM package.
      • rpm -qa - List all installed RPM packages.

10. Disk and Storage Management

  • df

    • Explanation: Command to display disk space usage.
    • Example:
      • df -h - Display disk space usage in human-readable format.
  • du

    • Explanation: Command to display directory space usage.
    • Example:
      • du -sh /path/to/directory - Display the space usage of a directory in a human-readable format.
  • mkfs

    • Explanation: Command to create a file system on a disk or partition.
    • Example:
      • mkfs.ext4 /dev/sdb1 - Create an ext4 file system on the sdb1 partition.

11. Backup and Recovery

  • tar

    • Explanation: Command to create and extract archives, often used for backup purposes.
    • Example:
      • tar -cvf backup.tar /path/to/directory - Create a tar archive of a directory.
      • tar -xvf backup.tar - Extract the contents of a tar archive.
  • gzip Explanation: The gzip command is used to compress files using the Lempel-Ziv coding (LZ77). Examples:

    • gzip filename.txt - Compress the filename.txt file.
    • gzip -d filename.txt.gz - Decompress the filename.txt.gz file.
  • gunzip Explanation: The gunzip command is essentially the same as invoking gzip with the -d option; it is used for decompressing files compressed with gzip. Examples:

    • gunzip filename.txt.gz - Decompress the filename.txt.gz file.
  • dd

    • Explanation: Command to copy and convert files, often used for disk backups and restores.
    • Example:
      • dd if=/dev/sda of=/path/to/backup.img - Create a disk image of the sda disk.
  • rsync

    • Explanation: Command to synchronize files and directories between different locations, often used for backups.
    • Example:
      • rsync -a /source/directory /destination/directory - Synchronize the source directory with the destination directory.

12. Network Management and Troubleshooting

  • ifconfig / ip

    • Explanation: Commands to configure and display network interfaces. While ifconfig is older and deprecated, it is still used in various systems. The ip command is its modern replacement.
    • Example:
      • ifconfig eth0 192.168.1.1 netmask 255.255.255.0 - Assign IP address to a network interface using ifconfig.
      • ip addr add 192.168.1.1/24 dev eth0 - Assign IP address using ip.
  • netstat

    • Explanation: Command to display network connections, routing tables, and a number of network interfaces.
    • Example:
      • netstat -tuln - Display all active listening ports.
  • traceroute

    • Explanation: Command to trace the route taken by packets to reach a network host.
    • Example:
      • traceroute domain.com - Trace the route to “domain.com”.

13. Logs and Monitoring

  • dmesg

    • Explanation: Command to display kernel ring buffer messages, helpful for diagnosing issues and reviewing system startup messages.
    • Example:
      • dmesg | less - View kernel messages in a paginated manner.
  • tail

    • Explanation: Command to view the end of files, commonly used to monitor logs in real time.
    • Example:
      • tail -f /var/log/messages - Monitor system messages log in real time.
  • top / htop

    • Explanation: Commands to display a dynamic view of system processes. htop is an advanced version of top with additional features.
    • Example:
      • top - Display a dynamic view of system processes.
      • htop - Display an enhanced view of system processes with htop.

14. System Boot and Services Management

  • systemctl

    • Explanation: Command to control the systemd system and service manager, which is used to manage services and check system status.
    • Example:
      • systemctl start service_name - Start a service.
      • systemctl status service_name - Check the status of a service.
  • journalctl

    • Explanation: Command to query and display messages from the systemd journal.
    • Example:
      • journalctl -u service_name - Display journal entries for a specific service.
  • grub2-mkconfig

    • Explanation: Command to generate a GRUB 2 configuration file.
    • Example:
      • grub2-mkconfig -o /boot/grub2/grub.cfg - Generate a new GRUB 2 configuration file.

15. Security Management

  • iptables

    • Explanation: A command for configuring Linux kernel firewall rules, often used to secure a server by controlling the traffic allowed to and from the server.
    • Example:
      • iptables -L - List all current firewall rules.
      • iptables -A INPUT -p tcp --dport 80 -j ACCEPT - Allow incoming traffic on port 80 (HTTP).
  • chown

    • Explanation: Command used to change the owner of files and directories.
    • Example:
      • chown user:group filename - Change the owner and group of a file.
  • chmod

    • Explanation: Command to change the permissions of files and directories.
    • Example:
      • chmod 755 filename - Set read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

16. Performance Tuning

  • sysctl

    • Explanation: Command to configure kernel parameters at runtime.
    • Example:
      • sysctl -w net.ipv4.ip_forward=1 - Enable IP forwarding.
  • nice and renice

    • Explanation: Commands to run a program with modified scheduling priority and change the priority of a running process, respectively.
    • Example:
      • nice -n 10 command_name - Run a command with a nice value of 10.
      • renice +10 1234 - Change the nice value of a process with PID 1234 to +10.

17. Automation and Scripting

  • cron

    • Explanation: Command to schedule tasks to run at fixed times or intervals.
    • Example:
      • crontab -e - Open the crontab file for editing, allowing you to add or modify cron jobs.
  • at

    • Explanation: Command to schedule a command or script to run at a specific time.
    • Example:
      • at 5pm today - Schedule a command to run at 5 pm today, followed by typing the command and pressing Ctrl+D.

18. Disk Management

  • fsck

    • Explanation: Command used to check and repair inconsistencies in file systems.
    • Example:
      • fsck /dev/sda1 - Check and repair the file system on device /dev/sda1.

19. Advanced Shell Usage

  • grep

    • Explanation: Command to search for specific patterns within files.
    • Example:
      • grep "search_term" filename - Search for "search_term" in the file named "filename".
  • sed

    • Explanation: Stream editor for filtering and transforming text, allowing for advanced text manipulations.
    • Example:
      • sed 's/old/new/g' filename - Replace all instances of "old" with "new" in the file named "filename".
  • awk

    • Explanation: A programming language used for pattern scanning and processing language, suitable for a variety of text transformation tasks.
    • Example:
      • awk '{print $1}' filename - Print the first field of each line in the file named "filename".

20. Log Management and System Monitoring

  • dmesg

    • Explanation: Displays kernel ring buffer messages, helping in troubleshooting and diagnosing system issues.
    • Example:
      • dmesg | grep -i error - Filter and display error messages from the kernel ring buffer.
  • logrotate

    • Explanation: Automates the rotation, compression, removal, and mailing of log files.
    • Example:
      • To configure log rotation, you would typically edit or create a configuration file in /etc/logrotate.d/.
  • iostat

    • Explanation: Reports CPU statistics and input/output statistics for devices and partitions.
    • Example:
      • iostat -x 1 - Display extended statistics updated every second.

21. Firewall Management

  • iptables

    • Explanation: A powerful tool used for configuring Linux kernel firewall.
    • Example:
      • iptables -L - List all the current firewall rules.
  • firewalld

    • Explanation: A firewall management solution available on Red Hat derivatives for managing network communications.
    • Example:
      • firewall-cmd --list-all - List all current firewall settings.

22. Package Management

  • yum (CentOS/AlmaLinux/Rocky Linux 8)

    • Explanation: A software package manager that installs, updates, and removes packages on RPM-based Linux distributions.
    • Example:
      • yum install packageName - Install a package using YUM.
  • dnf (Rocky Linux 8)

    • Explanation: The next-generation version of YUM, used to manage RPM packages.
    • Example:
      • dnf update - Update all installed packages to the latest version using DNF.

23. Scripting and Automation

  • cron

    • Explanation: A time-based job scheduler that can be used to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.
    • Example:
      • To add a new cron job, you would typically use the crontab -e command to edit the cron table.
  • bash

    • Explanation: Bash is a command language interpreter. It is widely used to write scripts to automate tasks.
    • Example:
      • Create a simple bash script using a text editor and save it with a .sh extension, making it executable with chmod +x scriptname.sh.

24. Text Editor Commands

  • vi/vim

    • Explanation: vi is a text editor that comes pre-installed with UNIX-like systems. vim is an improved version of vi. They are both used to create or edit text files from
    • Example:
      • vi filename.txt - Open filename.txt in vi editor. If it does not exist, it will be created.
      • :w - Save changes in vi/vim.
      • :q - Quit vi/vim.
      • :wq - Save changes and quit vi/vim.
      • :q! - Quit vi/vim without saving changes.
  • nano

    • Explanation: nano is a user-friendly, command-line text editor that is often easier to use for beginners compared to vi/vim. Examples:
    • Example:
      • Ctrl+O - Write the changes to disk in nano.
      • Ctrl+X - Exit nano.
      • Ctrl+W - Search for text in nano.
      • nano filename.txt - Open filename.txt in nano editor. If it does not exist, it will be created.

25. Service Management Commands

  • service

    • Explanation:The service command is used to initialize and manage services that are scripted for SysV init.
    • Example:
      • service service_name start - Start a service.
      • service service_name stop - Stop a service.
      • service service_name restart - Restart a service.
      • service service_name status - Get the status of a service.
  • chkconfig

    • Explanation:The chkconfig command is used to list all available services and view or update their startup configurations.
    • Example:
      • chkconfig --list - List all services and their startup information.
      • chkconfig service_name on - Enable a service to start at boot.
      • chkconfig service_name off - Disable a service from starting at boot.
  • systemctl

    • Explanation:Command to control the systemd system and service manager, used to manage services and check system status.
    • Example:
      • c
      • systemctl start service_name - Start a service.
      • systemctl stop service_name - Stop a service.
      • systemctl restart service_name - Restart a service.
      • systemctl enable service_name - Enable a service to start at boot.
      • systemctl disable service_name - Disable a service from starting at boot.

Further Reading



Was this answer helpful?

« Back