Linux Server Basics: Essential Commands, Permissions, and Log Management

Linux is the most common OS for backend developers and sysadmins. Mastering core commands greatly improves daily工作效率.

1. SSH Connection

1.1 Basic Connection

ssh user@server_ip
ssh -i ~/.ssh/id_rsa user@server_ip
ssh -p 2222 user@server_ip

1.2 SSH Config

# ~/.ssh/config
Host myserver
    HostName 123.456.789.0
    User root
    Port 22
    IdentityFile ~/.ssh/id_rsa

2. File Management

2.1 File and Directory Operations

ls -la              # List all files
pwd                 # Current path
cd /var/www         # Change directory
mkdir -p a/b/c      # Recursive create
rm -rf dir/         # Force delete
cp file1 file2      # Copy file
mv file1 dir/       # Move file
rm file.txt         # Delete file
touch newfile.txt   # Create empty file
cat file.txt        # Show all content
less file.txt       # Paginated view
head -20 file.txt   # First 20 lines
tail -f file.txt    # Real-time tail

2.2 File Permissions

chmod 755 file.sh   # rwxr-xr-x
chmod 644 config    # rw-r--r--
chown user:group file.txt
chown -R www-data:www-data /var/www/

3. Process Management

ps aux              # All processes
top                 # Real-time monitor
kill 1234           # End process
kill -9 1234        # Force end
nohup command &     # Background run
screen              # Virtual terminal
tmux                # Terminal multiplexer

4. Disk and Memory

df -h               # Disk space
du -sh /var/www/    # Directory size
free -h             # Memory usage

5. Log Viewing

# Common log locations
/var/log/nginx/access.log
/var/log/nginx/error.log
/var/log/mysql/error.log
/var/log/syslog

# Log viewing tips
tail -f /var/log/nginx/access.log
grep "ERROR" /var/log/app.log
journalctl -u nginx --since "1 hour ago"

6. Useful System Commands

curl -I https://example.com
ping -c 4 example.com
ss -tlnp
uname -a
cat /etc/os-release
lscpu
apt update && apt upgrade -y
apt install nginx

7. Shortcuts

Ctrl + C    # Interrupt
Ctrl + D    # Logout
Ctrl + R    # Search history
Ctrl + L    # Clear screen
Ctrl + A    # Line start
Ctrl + E    # Line end
!!          # Repeat last command

8. Summary

These commands cover 80%+ of daily server management needs. Use man to learn more about each command.