Mục tiêu học tập
5 bước quản trị Server
Nắm quy trình chuẩn: Cài đặt → Cấu hình → Khởi động → Bảo mật → Giám sát. Áp dụng cho mọi loại service trên Linux.
SSH Remote Access
Cài đặt openssh-server, cấu hình /etc/ssh/sshd_config, kết nối từ xa bằng ssh, thực thi lệnh remote.
Sao chép file: scp, rsync, sftp
So sánh scp và rsync, hiểu ưu điểm rsync trong backup, sử dụng sftp cho interactive file transfer qua SSH.
Key-based Authentication
Tạo SSH key pair với ssh-keygen, sao chép public key bằng ssh-copy-id, cấu hình passwordless authentication.
System Logging
Cấu hình rsyslogd, đọc log trong /var/log, dùng journalctl để xem systemd logs, logrotate để quản lý log files.
Bảo mật Server
Cấu hình PermitRootLogin, PasswordAuthentication trong sshd_config, cập nhật phần mềm định kỳ, kiểm tra filesystem.
Lý thuyết chi tiết
5 bước quản trị Linux Server
# Tìm server packages (Fedora/RHEL)
$ sudo dnf group list --hidden | grep -i server
mail-server Mail Server
smb-server Windows File Server
web-server Basic Web Server
dns-server DNS Name Server
# Cài đặt web server group
$ sudo dnf group install web-server
# Ubuntu: cài đặt service
$ sudo apt install apache2 openssh-server
# Config files thường nằm tại /etc/
$ ls /etc/httpd/conf/
httpd.conf magic
$ ls /etc/httpd/conf.d/
mod_ssl.conf php.conf welcome.conf
SSH: Secure Shell Remote Access
SSH (Secure Shell) cung cấp kênh truyền thông mã hóa, thay thế telnet/rlogin/rsh cũ. Gói openssh-server cung cấp daemon sshd lắng nghe port 22. Cấu hình bảo mật trong /etc/ssh/sshd_config.
# Cài openssh-server
$ sudo apt install openssh-server
$ sudo systemctl start ssh && sudo systemctl enable ssh
$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Active: active (running) since ...
# Đăng nhập SSH từ xa
$ ssh [email protected]
The authenticity of host '10.140.67.23' can't be established.
RSA key fingerprint is SHA256:abc123...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.140.67.23' to known hosts.
[email protected]'s password: *********
# SSH với port khác
$ ssh -p 2222 [email protected]
# Thực thi lệnh remote
$ ssh [email protected] hostname
host01.example.com
$ ssh [email protected] "df -h"
# X11 Forwarding (chạy GUI app từ xa)
$ ssh -Y [email protected] xterm
# Các cài đặt quan trọng trong sshd_config
Port 22
PermitRootLogin prohibit-password # Chặn login root bằng password
PasswordAuthentication yes # Tắt để chỉ dùng key
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
X11Forwarding yes
MaxAuthTries 3
# Áp dụng thay đổi
$ sudo systemctl restart sshd
# Test cấu hình trước khi restart
$ sudo sshd -t
Sao chép file: scp, rsync, sftp
Ba công cụ chuyển file qua SSH: scp (đơn giản, từ A đến B), rsync (thông minh — chỉ copy phần thay đổi, bảo toàn symlinks và permissions), sftp (tương tác kiểu FTP).
# scp: copy file local -> remote
$ scp /etc/services [email protected]:/tmp
services 100%|████████████| 153 0:00
# scp: copy directory recursive
$ scp -r [email protected]:/usr/share/man/man1/ /tmp/
# rsync: backup thông minh (chỉ copy file thay đổi)
$ rsync -avl [email protected]:/usr/share/man/man1/ /tmp/man1/
receiving incremental file list
man1/awk.1.gz -> /etc/alternatives/awk.1.gz (symlink preserved!)
# Lần 2: không copy gì vì đã có
$ rsync -avl [email protected]:/usr/share/man/man1/ /tmp/man1/
sent 25 bytes received 63,502 bytes speedup is 181.72
# rsync với --delete: mirror chính xác
$ rsync -avl --delete [email protected]:/home/john/ /backup/john/
# rsync qua SSH với options:
# -a (archive): recursive, symlinks, permissions, timestamps
# -v (verbose): hiển thị file đang copy
# -l (links): preserve symlinks
# -z (compress): nén dữ liệu khi transfer
# sftp: interactive session
$ sftp [email protected]
sftp> ls
sftp> get remotefile.txt
sftp> put localfile.txt
sftp> bye
Key-based Authentication (Passwordless SSH)
Thay vì nhập password mỗi lần kết nối, SSH hỗ trợ xác thực bằng cặp public/private key. Private key ở client, public key được copy đến server. Cần thiết cho automation và cron jobs.
# Bước 1: Tạo key pair trên CLIENT
$ ssh-keygen
Generating public/private ed25519 key pair.
Enter file (/home/chris/.ssh/id_ed25519): ENTER
Enter passphrase (empty for no passphrase): ENTER
Your public key saved in /home/chris/.ssh/id_ed25519.pub
$ ls ~/.ssh/
id_ed25519 id_ed25519.pub known_hosts
# Bước 2: Copy public key đến SERVER
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
[email protected]'s password: ****** (lần cuối nhập password)
Number of key(s) added: 1
# Bước 3: Kết nối không cần password
$ ssh [email protected]
Last login: Sun Jul 27 10:12:22 2025 from 10.140.67.22
[johndoe@host01]$ ← Đã vào mà không nhập password!
# Key được lưu tại server:
$ cat ~/.ssh/authorized_keys
ssh-ed25519 AAAAC3Nz... chris@client
# Tắt password auth hoàn toàn (chỉ key)
$ sudo vim /etc/ssh/sshd_config
PasswordAuthentication no
$ sudo systemctl restart sshd
System Logging: rsyslogd và journalctl
Linux dùng hai hệ thống logging: rsyslogd (truyền thống, ghi ra /var/log/) và systemd journal (binary format, xem bằng journalctl). Cả hai thường chạy song song.
# Log files trong /var/log/
$ ls /var/log/
auth.log syslog kern.log dpkg.log apt/ journal/
$ sudo tail -f /var/log/auth.log
Jul 27 10:14:22 host01 sshd[1234]: Accepted publickey for johndoe
# journalctl: xem systemd journal
$ journalctl -n 20
-- Journal begins at Mon 2025-07-27 --
$ journalctl -f
(theo dõi log real-time, Ctrl+C để thoát)
$ journalctl -u sshd
(chỉ xem log của service sshd)
$ journalctl --since "1 hour ago"
$ journalctl --since "2025-07-27 10:00" --until "2025-07-27 11:00"
$ journalctl -p err
(chỉ xem errors)
$ journalctl -b
(log từ lần boot hiện tại)
# rsyslog config
$ cat /etc/rsyslog.conf | grep "auth"
auth,authpriv.* /var/log/auth.log
Lab thực hành
Bài lab: Thiết lập SSH server, cấu hình key-based auth, thực hành scp/rsync, xem logs với journalctl.
Cài đặt và khởi động SSH Server
$ sudo apt install openssh-server # Ubuntu
$ sudo dnf install openssh-server # Fedora/RHEL
$ sudo systemctl start sshd
$ sudo systemctl enable sshd
$ sudo systemctl status sshd
● sshd.service - OpenSSH server daemon
Active: active (running)
$ ss -tlnp | grep :22
LISTEN 0 128 0.0.0.0:22 users:(("sshd",pid=1234))
Kết nối SSH và thực thi lệnh remote
# Test local (giả lập remote)
$ ssh localhost
Are you sure you want to continue connecting? yes
$ ssh localhost "uptime && df -h /"
10:30:15 up 2 days, load average: 0.15
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 234G 50G 172G 23% /
$ exit
Tạo SSH key pair và setup passwordless auth
$ ssh-keygen -t ed25519 -C "mykey@laptop"
Generating public/private ed25519 key pair.
Enter file: ~/.ssh/id_ed25519 (ENTER)
Enter passphrase: (ENTER for no passphrase)
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3Nz... mykey@laptop
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@SERVER_IP
# Test: không cần password nữa
$ ssh user@SERVER_IP
Last login: ... ← Không hỏi password!
Sao chép file với scp và rsync
# Tạo test data
$ mkdir ~/testdata && echo "hello" > ~/testdata/file1.txt
# scp: copy thư mục
$ scp -r ~/testdata user@SERVER_IP:/tmp/
file1.txt 100% 6 0.5KB/s 00:00
# rsync: backup (giữ permissions, symlinks)
$ rsync -avz ~/testdata/ user@SERVER_IP:/backup/testdata/
sending incremental file list
file1.txt
sent 150 bytes received 47 bytes
# rsync lần 2: không copy (đã có)
$ rsync -avz ~/testdata/ user@SERVER_IP:/backup/testdata/
sent 47 bytes received 12 bytes (no files transferred)
Bảo mật sshd_config
$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
$ sudo vim /etc/ssh/sshd_config
# Sửa các dòng sau:
PermitRootLogin no
MaxAuthTries 3
PasswordAuthentication no # Chỉ sau khi có key!
$ sudo sshd -t # Test config
$ sudo systemctl reload sshd
# Thử login root (phải bị từ chối)
$ ssh root@localhost
Permission denied, please try again.
Xem logs với journalctl
$ journalctl -u sshd -n 20
Jul 27 10:14:22 host sshd[1234]: Server listening on 0.0.0.0 port 22
Jul 27 10:15:00 host sshd[1235]: Accepted publickey for johndoe
$ journalctl -p err --since "1 hour ago"
$ journalctl -f
(Real-time — thử đăng nhập SSH từ terminal khác)
$ sudo tail -f /var/log/auth.log
Jul 27 10:15:45 host sshd[1236]: Failed password for root from 10.0.0.5
Câu hỏi ôn tập
5 bước chuẩn để cài đặt và vận hành một Linux server service là gì?
1. Cài đặt: dnf/apt install package
2. Cấu hình: chỉnh /etc/ config files
3. Khởi động: systemctl start + enable
4. Bảo mật: firewall, SELinux, password policy
5. Giám sát: logs, sar, Cockpit
Lệnh nào copy file /etc/hosts đến /tmp/ trên server 192.168.1.100 với user admin?
scp /etc/hosts [email protected]:/tmp/
rsync tốt hơn scp cho backup như thế nào? Kể ít nhất 3 ưu điểm.
1. Chỉ copy phần thay đổi: rsync kiểm tra và bỏ qua file đã có, tiết kiệm bandwidth
2. Bảo toàn symlinks: scp convert symlinks thành file, rsync giữ nguyên
3. Bảo toàn permissions/timestamps: dùng -a để archive đầy đủ metadata
4. Mirror với --delete: đồng bộ hai thư mục chính xác
Quy trình setup SSH key-based authentication gồm những bước nào?
1. ssh-keygen — tạo private+public key trên client (~/.ssh/id_ed25519 và .pub)
2. ssh-copy-id user@server — copy public key đến server (~/.ssh/authorized_keys)
3. Test: ssh user@server — không hỏi password
4. (Tùy chọn) Tắt password: PasswordAuthentication no trong sshd_config
PermitRootLogin prohibit-password khác PermitRootLogin no như thế nào?
prohibit-password (mặc định): chặn login root bằng password nhưng vẫn cho phép key-based authentication cho root.no: chặn hoàn toàn mọi hình thức login root qua SSH, kể cả key-based.
Khuyến nghị: dùng no cho bảo mật tối đa, yêu cầu login bằng user thường rồi sudo.
Lệnh journalctl nào xem log lỗi của service apache2 trong 30 phút qua?
journalctl -u apache2 -p err --since "30 minutes ago"
Hoặc real-time: journalctl -u apache2 -f
Xem log file trực tiếp: sudo tail -f /var/log/apache2/error.log
Daemon processes thường chạy với user nào và tại sao không chạy với root?
Daemon thường chạy với user riêng (httpd chạy là apache, ntpd là ntp, mysql là mysql). Lý do: nếu daemon bị crack (exploit), attacker chỉ có quyền của user đó, không phải root. Điều này giới hạn damage nếu service bị compromise — một nguyên tắc "least privilege" quan trọng trong bảo mật.
Sự khác biệt giữa sftp và FTP thông thường là gì?
Dù tên giống nhau, sftp không dùng FTP protocol. sftp chạy trên SSH (port 22), mã hóa toàn bộ kết nối bao gồm username, password và dữ liệu. FTP thông thường truyền mọi thứ dạng plaintext (có thể bị sniff). sftp không cần FTP server — chỉ cần sshd đang chạy. Cú pháp: sftp user@server