Blog / Linux/ Essential Linux System Operation Commands

Essential Linux System Operation Commands

This article compiles commonly used Linux system operation commands, covering system information, resource monitoring, disk partitioning, network configuration, process management, user services, and more. It serves as a practical quick reference for system administrators and developers.

Hardware and System Information

Commands to view system hardware configuration and basic information.

  • uname -a: View kernel, OS, and CPU info.
  • head -n 1 /etc/issue or cat /etc/os-release: View OS distribution version.
  • cat /proc/cpuinfo: View detailed CPU information.
  • hostname: View the computer's hostname.
  • lspci -tv: List all PCI devices in a tree view.
  • lsusb -tv: List all USB devices in a tree view.
  • lsmod: List currently loaded kernel modules.
  • env: View current environment variables.
  • dmesg: View kernel ring buffer messages from system boot, often used for hardware diagnostics.

Resource Monitoring

Monitor system resource usage (memory, disk, load).

  • free -h: View memory and swap usage in a human-readable format (GB/MB).
  • df -h: View disk partition usage.
  • du -sh <directory>: View total size of a specified directory.
  • grep MemTotal /proc/meminfo: View total physical memory.
  • uptime: View system uptime, logged-in users, and load averages.
  • cat /proc/loadavg: View system load averages for the past 1, 5, and 15 minutes.

Disk and Partition Management

Manage disk partitions, filesystem mounting, and disk parameters.

  • mount | column -t: View mounted filesystems in a table format.
  • fdisk -l or lsblk: View all disk partition information.
  • swapon -s: View all active swap partitions.
  • hdparm -i /dev/sda: View parameters for a specified disk (modern systems typically use /dev/sdX).

Network Configuration and Diagnostics

Configure network interfaces, view connection status, and firewall rules.

  • ip addr or ifconfig (requires net-tools): View network interface properties.
  • iptables -L -n: View firewall rules (-n shows addresses/ports numerically).
  • ss -tunlp or netstat -lntp: View all listening ports and associated processes.
  • ss -antp or netstat -antp: View all established network connections.
  • netstat -s: View network protocol statistics.
  • route -n or ip route: View the kernel routing table.

Process Management

View and control running processes.

  • ps aux or ps -ef: View detailed information for all processes.
  • top or htop: Real-time dynamic view of process status and system resource usage.

User and Permission Management

Manage system users, groups, and permissions.

  • w or who: View currently logged-in active users.
  • id <username>: View a user's UID, GID, and group memberships.
  • last: View user login history.
  • cut -d: -f1 /etc/passwd: List all system usernames.
  • cut -d: -f1 /etc/group: List all system group names.
  • crontab -l: View the current user's scheduled tasks.
  • useradd <username>: Create a new user.
  • passwd <username>: Set or change a user's password.
  • userdel -r <username>: Delete a user and their home directory.

Service Management

Manage system service startup, stopping, and status. On modern Linux distributions (e.g., RHEL/CentOS 7+, Ubuntu 16.04+), use the systemctl command.

  • systemctl list-unit-files --type=service (Systemd) or chkconfig --list (SysVinit): List all system services and their startup status.
  • systemctl start <service>: Start a service.
  • systemctl stop <service>: Stop a service.
  • systemctl restart <service>: Restart a service.
  • systemctl status <service>: Check service status.

Package Management

Query installed software packages. Different distributions use different package managers.

  • RHEL/CentOS/Fedora: rpm -qa or dnf list installed (DNF) or yum list installed (YUM).
  • Debian/Ubuntu: dpkg -l or apt list --installed.

Directory Space Analysis

Use the du command to analyze directory disk usage.

du -sh /path/to/directory

Common options:

  • -s: Show only the total for the specified directory, not subdirectory details.
  • -h: Display sizes in human-readable format (K, M, G) using 1024 as the base.
  • -a: Show disk usage for the directory and all files within.
  • --max-depth=N: Limit the display depth to N levels.

Common Issue Troubleshooting

SSH Terminal Chinese Character Garbling

If Chinese characters appear garbled after an SSH connection, it's often due to a mismatch between the terminal and server character sets. One solution is to modify the server's locale settings. Note that modern Linux distributions generally recommend UTF-8 encoding.

For systems using Systemd, set the locale with:

localectl set-locale LANG=zh_CN.UTF-8

For older systems, edit /etc/locale.conf (Systemd) or /etc/sysconfig/i18n (SysVinit) to ensure LANG is set to zh_CN.UTF-8 or en_US.UTF-8, then log out and back in or reboot.

Configuring Firewall to Open Ports

If a firewall is enabled and you need to open specific ports (e.g., port 80 for HTTP or 22 for SSH), use the appropriate tool for your system:

  • firewalld (default on RHEL/CentOS 7+):
    firewall-cmd --permanent --add-port=80/tcp
    firewall-cmd --permanent --add-port=22/tcp
    firewall-cmd --reload
  • UFW (default on Ubuntu):
    ufw allow 80/tcp
    ufw allow 22/tcp
    ufw reload
  • iptables (direct configuration): You can edit /etc/sysconfig/iptables (if it exists) to add rules, but using the above tools or the iptables command directly is recommended.

Note: Directly editing iptables rule files may fail due to system or configuration differences; using the distribution's management tools is more reliable.

Post a Comment

Your email will not be published. Required fields are marked with *.