VPS HC
Trang Chủ / Blog / Tối ưu VPS Ubuntu cơ bản: Swap, Timezone, Sysctl và hơn thế

Tối ưu VPS Ubuntu cơ bản: Swap, Timezone, Sysctl và hơn thế

Tác giả
Nguyễn Chiến
Ngày đăng
16/06/2026
Lượt xem
52
Tối ưu VPS Ubuntu cơ bản: Swap, Timezone, Sysctl và hơn thế

Sau khi cài VPS xong, có một số cấu hình cơ bản giúp VPS chạy ổn định hơn, tiết kiệm tài nguyên và tự động cập nhật bảo mật. Bài này hướng dẫn từng bước.

 Cập nhật hệ thống trước tiên

apt update && apt upgrade -y
apt install -y curl wget nano htop net-tools

 Thiết lập Timezone đúng

# Xem timezone hiện tại
timedatectl

# Đặt timezone Việt Nam
timedatectl set-timezone Asia/Ho_Chi_Minh

# Kiểm tra lại
date

 Tạo Swap (Bộ nhớ ảo) — Quan trọng với VPS RAM thấp

Swap giúp hệ thống không bị crash khi RAM đầy, dùng disk làm RAM tạm thời.

# Kiểm tra xem đã có swap chưa
free -h
swapon --show

# Tạo swap file 2GB
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# Kiểm tra đã kích hoạt
free -h

# Để swap tự kích hoạt khi reboot
echo "/swapfile none swap sw 0 0" | tee -a /etc/fstab

Điều chỉnh swappiness (0-100, càng thấp càng ít dùng swap):

# Xem swappiness hiện tại (mặc định 60)
cat /proc/sys/vm/swappiness

# Đặt về 10 (khuyến nghị cho server)
sysctl vm.swappiness=10
echo "vm.swappiness=10" | tee -a /etc/sysctl.conf

 Bật Unattended Upgrades (Tự cập nhật bảo mật)

apt install -y unattended-upgrades

# Cấu hình
dpkg-reconfigure -plow unattended-upgrades
# Chọn "Yes" để bật

# Kiểm tra
cat /etc/apt/apt.conf.d/20auto-upgrades

 Tắt dịch vụ không cần thiết

# Xem các service đang chạy
systemctl list-units --type=service --state=running

# Tắt snapd nếu không dùng (tiết kiệm ~100MB RAM)
systemctl stop snapd
systemctl disable snapd
apt purge snapd -y

 Tối ưu Sysctl (Kernel Parameters)

nano /etc/sysctl.conf

Thêm vào cuối file:

# Tăng giới hạn file descriptor
fs.file-max = 65536

# Tối ưu network
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15

# Giảm dùng swap
vm.swappiness = 10
vm.dirty_ratio = 60
vm.dirty_background_ratio = 2
# Áp dụng ngay
sysctl -p

 Tăng giới hạn file open (ulimit)

nano /etc/security/limits.conf

Thêm vào cuối:

* soft nofile 65536
* hard nofile 65536
root soft nofile 65536
root hard nofile 65536

 Dọn dẹp và tối ưu disk

# Xoá cache apt
apt clean
apt autoremove -y

# Xoá log journal cũ hơn 7 ngày
journalctl --vacuum-time=7d

# Xem thư mục chiếm nhiều dung lượng nhất
du -sh /var/log/* | sort -rh | head -20

 Script tối ưu tổng hợp

#!/bin/bash
# Chạy: bash optimize.sh

apt update && apt upgrade -y
timedatectl set-timezone Asia/Ho_Chi_Minh

# Tạo swap 2GB nếu chưa có
if ! swapon --show | grep -q /swapfile; then
    fallocate -l 2G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    echo "/swapfile none swap sw 0 0" | tee -a /etc/fstab
fi

sysctl vm.swappiness=10
apt clean && apt autoremove -y
journalctl --vacuum-time=7d

echo "Done! VPS optimized."
Có thể bạn cần xem thêm
Quay lại Blog
Bài viết đã được kiểm duyệt bởi VPS HC Team Cập nhật lần cuối: 16/06/2026

Bài Viết Liên Quan

Cần VPS tốc độ cao, giá hợp lý?

Khởi tạo trong vài phút · Hỗ trợ 24/7 · Uptime 99.99%

Dùng Thử Miễn Phí