Fundamentals of Linux System Administration (Essential for Java Developers)

For Java developers, mastering basic Linux system administration skills is essential. Whether it is deploying applications, troubleshooting issues, or performance tuning, interaction with the Linux system is necessary. This article will introduce the fundamental knowledge and practical skills of Linux system administration that Java developers must master.

1. Basic Concepts of Linux

1.1 Linux System Architecture

The Linux system mainly consists of the following components:

  1. 1. Kernel: The core of the system, responsible for managing hardware resources, process scheduling, memory management, etc.
  2. 2. Shell: The command interpreter, the interface for user interaction with the system.
  3. 3. File System: The hierarchical structure for organizing and managing files.
  4. 4. Applications: Various software running on the system.

1.2 File System Structure

Linux adopts a tree-like file system structure, with the main directories and their purposes as follows:

/                    # Root directory
├── bin              # Basic command binaries
├── boot             # Boot loader files
├── dev              # Device files
├── etc              # System configuration files
├── home             # User home directories
├── lib              # System library files
├── opt              # Optional applications
├── proc             # Virtual file system providing process information
├── root             # Root user home directory
├── run              # Runtime variable data
├── srv              # Service data
├── tmp              # Temporary files
├── usr              # User programs and files
└── var              # Variable data (logs, caches, etc.)

2. Common Linux Commands

2.1 File and Directory Operations

# View current directory
pwd

# List directory contents
ls -la          # Show detailed information, including hidden files
ls -lh          # Display file sizes in human-readable format

# Change directory
cd /home        # Change to specified directory
cd ..           # Go back to the parent directory
cd ~            # Change to user home directory

# Create directories and files
mkdir mydir     # Create directory
touch myfile    # Create empty file

# Copy, move, delete
cp file1 file2        # Copy file
cp -r dir1 dir2       # Copy directory
mv file1 file2        # Move or rename file
rm file               # Delete file
rm -r dir             # Delete directory
rm -rf dir            # Force delete directory (use with caution)

2.2 File Viewing and Editing

# View file content
cat filename          # Display entire file content
less filename         # View file page by page
head -n 10 filename   # View the first 10 lines of a file
tail -n 10 filename   # View the last 10 lines of a file
tail -f filename      # Real-time view of the end of a file (commonly used for logs)

# File search
grep "pattern" filename     # Search for a pattern in a file
grep -r "pattern" /path     # Recursively search a directory
find /path -name "*.log"    # Find specific files

2.3 System Information Viewing

# Basic system information
uname -a              # Display system information
lsb_release -a        # Display distribution information
uptime                # Show system uptime and load

# Hardware information
lscpu                 # Display CPU information
free -h               # Show memory usage
 df -h                 # Show disk space usage
du -sh /path          # Show directory size

# Network information
ifconfig              # Display network interface information (old version)
ip addr               # Display network interface information (new version)
netstat -tuln         # Show network connection status
ss -tuln              # Faster network connection viewing tool

2.4 Process Management

# View processes
ps aux                # Display all processes
ps -ef                # Display process tree
top                   # Real-time display of process information
htop                  # More user-friendly process viewer (requires installation)

# Process control
kill PID              # Terminate specified process
kill -9 PID           # Force terminate process
killall process_name  # Terminate all processes with the same name

# Background running
nohup command &       # Run command in the background and ignore hangup signal
jobs                  # View background tasks
fg %1                 # Bring background task to the foreground
bg %1                 # Put suspended task to run in the background

3. Setting Up the Java Environment

3.1 JDK Installation

Install OpenJDK on Ubuntu/Debian systems:

# Install OpenJDK 11
sudo apt update
sudo apt install openjdk-11-jdk

# Verify installation
java -version
javac -version

Install OpenJDK on CentOS/RHEL systems:

# Install OpenJDK 11
sudo yum install java-11-openjdk-devel
# Or use dnf (newer versions)
sudo dnf install java-11-openjdk-devel

# Verify installation
java -version
javac -version

3.2 Environment Variable Configuration

Configure the JAVA_HOME environment variable:

# View Java installation path
sudo update-alternatives --config java

# Edit environment variable file
sudo vim /etc/environment

# Add the following content
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
PATH="$PATH:$JAVA_HOME/bin"

# Make configuration effective
source /etc/environment

# Verify configuration
echo $JAVA_HOME
java -version

3.3 System-Level Optimization Settings

# Adjust file handle limit
sudo vim /etc/security/limits.conf

# Add the following content
* soft nofile 65536
* hard nofile 65536

# Adjust kernel parameters
sudo vim /etc/sysctl.conf

# Add the following content
fs.file-max = 655360
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# Make configuration effective
sudo sysctl -p

4. Service Management

4.1 systemd Service Management

Modern Linux distributions use systemd as the system and service manager:

# Start, stop, restart services
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl restart service_name

# View service status
sudo systemctl status service_name

# Set to start on boot
sudo systemctl enable service_name
sudo systemctl disable service_name

# View all services
systemctl list-units --type=service

4.2 Create Custom Services

Create a systemd service file for a Java application:

sudo vim /etc/systemd/system/myapp.service

Service configuration file content:

[Unit]
Description=My Java Application
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/java -jar /opt/myapp/myapp.jar
Restart=always
RestartSec=10

# Environment variables
Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=SPRING_PROFILES_ACTIVE=prod

# Log configuration
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myapp

[Install]
WantedBy=multi-user.target

Enable and start the service:

# Reload systemd configuration
sudo systemctl daemon-reload

# Enable service
sudo systemctl enable myapp.service

# Start service
sudo systemctl start myapp.service

# View service status
sudo systemctl status myapp.service

5. Log Viewing and Analysis

5.1 System Logs

# View system logs
sudo journalctl                # View all logs
sudo journalctl -u myapp      # View specific service logs
sudo journalctl -f            # Real-time view of logs
sudo journalctl --since today # View today's logs

# Traditional log file locations
/var/log/syslog               # System logs (Debian/Ubuntu)
/var/log/messages             # System logs (CentOS/RHEL)

5.2 Application Log Management

# View application logs
tail -f /var/log/myapp/app.log

# Use logrotate to manage log rotation
sudo vim /etc/logrotate.d/myapp

logrotate configuration example:

/var/log/myapp/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 644 myuser mygroup
    postrotate
        systemctl reload myapp > /dev/null 2>/dev/null || true
    endscript
}

6. Basics of Networking and Security

6.1 Firewall Configuration

Using ufw (Ubuntu firewall):

# Enable firewall
sudo ufw enable

# View status
sudo ufw status

# Allow specific ports
sudo ufw allow 22      # SSH
sudo ufw allow 80      # HTTP
sudo ufw allow 443     # HTTPS
sudo ufw allow 8080    # Default port for Tomcat

# Delete rules
sudo ufw delete allow 8080

Using firewalld (CentOS/RHEL):

# Start firewall
sudo systemctl start firewalld
sudo systemctl enable firewalld

# View status
sudo firewall-cmd --state

# Open ports
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

6.2 User and Permission Management

# Create user
sudo adduser myuser

# Add user to sudo group
sudo usermod -aG sudo myuser

# Change file permissions
chmod 755 myfile      # Set permissions
chown myuser:mygroup myfile  # Change owner

# View user information
id myuser             # View user ID and group information
groups myuser         # View groups the user belongs to

7. Performance Monitoring

7.1 System Monitoring Commands

# Real-time monitoring of system resources
top                   # Process and system resource monitoring
htop                  # More user-friendly alternative to top
iostat                # I/O statistics
vmstat                # Virtual memory statistics
sar                   # System activity report

7.2 Java Application Monitoring

# View Java processes
jps                   # Display Java processes
ps aux | grep java    # Use ps to view Java processes

# JVM monitoring
jstat -gc PID         # View GC statistics
jstack PID            # Generate thread dump
jmap -heap PID        # View heap memory usage

8. Common Troubleshooting

8.1 Port Occupation Issues

# View port occupation status
netstat -tulnp | grep :8080
ss -tulnp | grep :8080

# Find process by port
lsof -i :8080

8.2 Disk Space Issues

# View disk usage
df -h

# Find large files
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null

# Clean system cache
sudo apt autoremove   # Ubuntu/Debian
sudo yum autoremove   # CentOS/RHEL

9. Recommended Reading Books

For learning Linux system administration, here are some recommended books categorized by different levels and professional directions:

Beginner Level Books

  1. 1. The Linux Command Line: A Complete Introduction
  • • Author: William Shotts
  • • Features: Comprehensive introduction to the Linux command line, suitable for beginners.
  • • Content covers: Basic Linux concepts, file systems, common commands, system administration, etc.
  • 2. Linux Command Line and Shell Scripting Bible
    • • Authors: Richard Blum, Christine Bresnahan
    • • Features: Systematic introduction to Linux command line and shell scripting.
    • • Suitable for: Readers who want to master command line operations.

    Intermediate Level Books

    1. 3. UNIX/Linux System Administration Handbook
    • • Authors: Evi Nemeth, Garth Snyder, Trent R. Hein, Ben Whaley, Dan Mackin
    • • Features: Known as the “bible” of Linux system administration, authoritative and comprehensive content.
    • • Content covers: System administration, network configuration, security, performance tuning, etc.
  • 4. Linux System Administration
    • • Authors: Tobin, Toi
    • • Features: In-depth discussion of various aspects of Linux system administration.
    • • Suitable for: System administrators with some foundation.

    Professional Level Books

    1. 5. Linux Kernel Design and Implementation
    • • Author: Robert Love
    • • Features: In-depth discussion of how the Linux kernel works.
    • • Suitable for: Developers who want to understand Linux kernel principles.
  • 6. Linux Performance Tuning
    • • Author: Brendan Gregg
    • • Features: Focuses on performance analysis and optimization of Linux systems.
    • • Content covers: Performance monitoring tools, tuning methods, case studies, etc.

    Networking and Security Direction

    1. 7. Linux Firewalls
    • • Author: Steve Suehring
    • • Features: Specially discusses Linux network security and firewall configuration.
    • • Suitable for: Readers concerned about system security.

    Shell Scripting Programming

    1. 8. Advanced Bash Scripting Guide
    • • Author: Mendel Cooper
    • • Features: Free online book, comprehensive introduction to Bash scripting.
    • • Suitable for: Readers who want to master shell scripting.

    Container and Cloud Computing Related

    1. 9. Docker: Up & Running
    • • Authors: Karl Matthias, Sean P. Kane
    • • Features: Detailed introduction to Docker container technology.
    • • Suitable for: Readers learning about containerized deployment.
  • 10. The Kubernetes Book
    • • Author: Nigel Poulton
    • • Features: Comprehensive introduction to Kubernetes container orchestration technology.
    • • Suitable for: Readers learning about K8s.

    10. Learning Suggestions

    1. 1. Step by Step: Start with beginner books and gradually deepen your learning.
    2. 2. Theory and Practice Combined: While learning theory, practice hands-on as much as possible.
    3. 3. Stay Updated: Linux technology develops rapidly, pay attention to the version and update time of books.
    4. 4. Refer to Multiple Resources: In addition to books, refer to official documentation and online resources.
    5. 5. Join the Community: Participate in Linux community discussions and exchange experiences with other developers.

    For Java developers, it is recommended to focus on:

    • • Basic Linux commands and system administration
    • • Performance monitoring and tuning
    • • Network configuration and security
    • • Shell scripting
    • • Container technology (Docker/K8s)

    These skills will help Java developers better deploy, operate, and optimize application performance.

    11. Conclusion

    Mastering Linux system administration skills is crucial for Java developers. This article covers the following key content:

    1. 1. Basic Commands: File operations, system information viewing, process management, etc.
    2. 2. Environment Setup: JDK installation and environment variable configuration.
    3. 3. Service Management: systemd service configuration and management.
    4. 4. Log Management: Log viewing and rotation configuration.
    5. 5. Security Configuration: Firewall and user permission management.
    6. 6. Performance Monitoring: System and application performance monitoring.
    7. 7. Troubleshooting: Diagnosis and solutions for common issues.
    8. 8. Learning Resources: Recommended books and learning suggestions.

    It is suggested that Java developers continuously practice these skills in their daily work to gradually improve their Linux system administration capabilities. By mastering these fundamental knowledge, they can better deploy, monitor, and maintain Java applications, enhancing development and operational efficiency.

    Leave a Comment