Common Linux Commands Reference

Common Linux Commands Reference

May 15, 2017 · 4 min read · 707 Words · -Views -Comments

terminal

Navigating the command-line interface is essential for understanding and managing Linux systems. Here is a curated summary of commands I use most frequently in my daily work.

System Information

uname -a               # Display kernel version, OS, and CPU architecture
head -n 1 /etc/issue   # View operating system version
env                    # List all environment variables

# Check CentOS version
cat /etc/redhat-release

# CPU details
cat /proc/cpuinfo

# Memory details
cat /proc/meminfo

# Update system timezone
timedatectl set-timezone Asia/Shanghai

Process Monitoring

# Search for a specific process using ps and grep
$ ps aux | grep <process_name>

ps aux Output Columns:

  • USER: Process owner
  • PID: Process ID
  • %CPU: CPU usage percentage
  • %MEM: Memory usage percentage
  • VSZ: Virtual memory size
  • RSS: Resident Set Size (physical memory)
  • TTY: Terminal type
  • STAT: Process status:
    • D: Uninterruptible sleep (usually IO)
    • R: Running
    • S: Sleeping
    • T: Stopped or traced
    • Z: Zombie process
  • START: Process start time
  • TIME: Total CPU time used
  • COMMAND: The command that started the process

Account Management

# Change the current user's password
passwd

# Show who is logged on and what they are doing
w

File and Directory Management

Permissions and Attributes

# List file attributes, owners, and groups
ls -l

# Change file group
chgrp [-R] groupname filename

# Change file owner (and optionally the group)
chown [-R] ownername filename
chown [-R] owner:group filename

Basic File Operations

# Remove a file
rm filename

# Remove a directory and its contents recursively
rm -rf dirname

# Find and delete specific files/folders
find . -name "test" | xargs rm -rf   

# Copy a file
cp config.conf config.conf.bak

# Rename or move a file
mv old.txt new.txt

# Move all files from a subdirectory to the current directory
mv ./temp/deploy/* .

# List all files (including hidden) in long format
ls -Al

# List a specific range of files (e.g., 101st to 200th)
ls | sort -n | head -200 | tail -100

The ln command creates links between files. Use -s for symbolic (soft) links. ln -s /path/to/source /path/to/destination

Archives (zip/tar)

# Extract a zip archive
unzip archive.zip

# Create a compressed tarball
tar zcvf archive.tar.gz dirname/

# Extract a tarball
tar zxvf archive.tar.gz

Text Editing (vi/vim)

Vim has three main modes: Command, Insert, and Last Line.

  • Command Mode: The default mode. Use i to enter Insert mode, x to delete a character, and : to enter Last Line mode.
  • Insert Mode: For typing text. Press ESC to return to Command mode.
  • Last Line Mode: Type : followed by commands:
    • w: Save (write)
    • q: Quit
    • wq: Save and quit
    • q!: Quit without saving

Services and System Management

chkconfig

Used to manage services at different runlevels (CentOS 6 and earlier).

chkconfig --list           # List all system services
chkconfig mysqld on        # Enable a service at boot

service / systemctl

Used to manage system services.

service nginx start        # Start a service
systemctl status nginx     # Check service status (modern Linux)

Network and HTTP

# Download a file or view source code
curl https://example.com >> index.html

# Send a cookie with a request
curl --cookie "session=123" https://example.com

SSH

# Local port forwarding (tunneling)
ssh -p 22 -L <localPort>:localhost:<serverPort> user@remote_host

# Execute a command on a remote server (using sshpass for automated scripts)
sshpass -p 'password' ssh user@host "ls -la"

Searching and Environment

# Find the absolute path of a command
which python

# Display a specific environment variable
echo $NODE_ENV

# Set an environment variable for the current session
export NODE_ENV="production"

# Remove an environment variable
unset NODE_ENV

Note: To make an environment variable permanent, add it to /etc/profile or ~/.bashrc.

Disk and Hardware Management

df / free

# View disk space in human-readable format
df -h

# View memory usage in MB
free -m

lsof

# Check which process is using a specific port
lsof -i:8080

kill

# Forcefully terminate a process by PID
kill -9 <pid>

Troubleshooting

Passwordless SSH Login

  1. Generate keys on your local machine: ssh-keygen.
  2. Copy the content of ~/.ssh/id_rsa.pub to the target server’s ~/.ssh/authorized_keys file.
  3. Ensure the .ssh directory has 700 permissions and authorized_keys has 600.

“Host key verification failed”

If a server’s fingerprint has changed (e.g., after a reinstallation), remove the old entry from your local ~/.ssh/known_hosts file and reconnect.

Resources

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover