[Fix] PHP Server Monitor Shows “Online” for Failed Pings on Newer Linux Distros

I recently deployed PHP Server Monitor v3.5.2 on my server to keep track of my hosting status. However, right after setting it up, I noticed a very bizarre issue:

I purposely added a completely unreachable, invalid IP to test the Ping monitoring, but the system reported it as “Online” with a latency of 0ms!

I checked the system error logs, but they were completely clean—no error messages at all. A monitoring system that fails to alert you when a server goes down is virtually useless. To fix this fatal flaw, I began a deep dive to troubleshoot the issue.

🕵️‍♂️ The Diagnosis: Where did it go wrong?

Initially, I suspected the server’s PHP security settings (like disable_functions in php.ini) might be restricting the execution of system commands. If the exec() function is disabled, PHP wouldn’t be able to run the system’s ping command.

To test this theory, I wrote a simple PHP diagnostic script (test_ping.php) to check the underlying connection:

<?php
$test_ip = "192.168.255.255"; 
$cmd = "ping -c 1 -w 2 " . escapeshellarg($test_ip) . " 2>&1";
exec($cmd, $output, $return_var);

echo "Return Code: " . $return_var . "<br>";
print_r($output);
?>

The execution results were eye-opening:

  • Return Code: 1 (which means failure)
  • Actual Terminal Output: It clearly printed 2 packets transmitted, 0 received, 100% packet loss, time 1016ms.

This proved that the server environment and permissions were perfectly fine! The system successfully sent the packets and accurately recognized that the IP was unreachable (100% packet loss). The environment was not the culprit.

💡 The Root Cause: Outdated Regex vs. Modern Linux

With the environment ruled out, the root of the problem pointed directly to a bug within PHP Server Monitor v3.5.2 itself.

After digging into the official GitHub repository, I realized the software hasn’t seen a major active update since around 2021. The old codebase uses a very rigid method to determine ping status: it uses Regular Expressions (Regex) to parse the text output of the terminal. If it spots the keyword time anywhere in the output string, it blindly assumes “there’s a response time, so the host must be Online.”

In older Linux distributions, a failed ping (100% packet loss) usually wouldn’t print the time taken at the end of the output. However! In newer Linux distributions (like recent Debian versions), the ping command reliably prints out time 1016ms at the very end, even if every single packet is dropped.

Because of this simple phrase—time 1016ms—the outdated code in PHP Server Monitor gets completely confused. It ignores the glaring 100% packet loss right next to it and falsely reports the server as Online. This is also exactly why the “fix” available in the official development branch still fails on newer Linux systems.

🛠️ The Ultimate Solution: Rewriting the Core Logic

Since official maintenance has stalled, the most reliable way forward is to rewrite the validation logic ourselves. Instead of relying on the app to parse terminal text—which can constantly change depending on the Linux distribution—we should just grab the most honest metric the OS provides: the Return Code (0 means success, anything else means failure).

How to apply the fix:

  1. Access your PHP Server Monitor web directory via SSH or FTP.
  2. Locate and open this specific file: src/psm/Util/Server/Updater/StatusUpdater.php
  3. Search for the protected function updatePing function inside the file (it should be roughly past the middle).
  4. Replace the entire updatePing function block with this updated, precise version:
    protected function updatePing($max_runs, $run = 1) {
        $starttime = microtime(true);
        $ip = $this->server['ip'];
        $os_is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
        
        if ($os_is_windows) {
            // Windows Ping: -n 1 (1 packet), -w 2000 (2000ms timeout)
            exec('ping -n 1 -w 2000 ' . escapeshellarg($ip), $output, $result);
            $output_str = implode("", $output);
            // Windows ping notoriously returns 0 on unreachable hosts. Verify with "TTL="
            $status = ($result === 0 && stripos($output_str, 'TTL=') !== false) ? true : false;
        } else {
            // Linux/Unix Ping: -c 1 (1 packet), -w 2 (2s timeout), capture stderr
            exec('ping -c 1 -w 2 ' . escapeshellarg($ip) . ' 2>&1', $output, $result);
            // Core fix for Linux: Only trust the Return Code! (0 is success)
            $status = ($result === 0) ? true : false;
        }
        
        // Record the response time
        $this->rtime = (microtime(true) - $starttime);

        // If it fails and we haven't reached max retries, try again
        if(!$status && $run < $max_runs) {
            return $this->updatePing($max_runs, $run + 1);
        }
        
        return $status;
    }

Save the file and head back to your PHP Server Monitor dashboard. Test that invalid IP again, and the system will finally behave as it should, properly displaying a red “Offline” status!

📝 Conclusion

Sometimes, when running older open-source software on modern operating systems, official patches might not be enough to resolve bugs caused by OS-level changes. In situations like this, writing a simple script to test the underlying commands directly can help cut through the confusion and point you toward the most effective fix.

If you are using PHP Server Monitor and experiencing this “Fake Online” bug, give this fix a try!

Tags: PHP Server Monitor, Bug Fix, Server Monitoring, Ping, Linux

Linux ZFS 指令大全(OpenZFS 實戰版)

適用:

  • Ubuntu 22.04 / 24.04
  • Debian 12
  • Rocky Linux / AlmaLinux 9
  • OpenZFS 2.x

本文適用於現代 Linux 系統(OpenZFS 2.x)


一、安裝 ZFS(Linux)

✅ Ubuntu / Debian

sudo apt update
sudo apt install zfsutils-linux

✅ Rocky / AlmaLinux

sudo dnf install epel-release
sudo dnf install zfs

啟用模組:

sudo modprobe zfs

確認版本:

zfs --version

二、Zpool(儲存池)管理


✅ 建立 ZFS 儲存池(Mirror)

sudo zpool create -o ashift=12 mpool mirror /dev/sdb /dev/sdc

📌 說明:

  • ashift=12 → 4K 磁碟最佳化(強烈建議)
  • mirror → 類似 RAID1

✅ 建立 RAIDZ1

sudo zpool create -o ashift=12 mpool raidz /dev/sdb /dev/sdc /dev/sdd

✅ 建立 RAIDZ2

sudo zpool create -o ashift=12 mpool raidz2 /dev/sdb /dev/sdc /dev/sdd /dev/sde

✅ 查看儲存池

sudo zpool list
sudo zpool status

✅ 增加容量(新增 vdev)

⚠️ 注意:ZFS 無法擴充單一 vdev,只能新增一組 vdev

sudo zpool add mpool mirror /dev/sde /dev/sdf

✅ 加入 Hot Spare

sudo zpool add mpool spare /dev/sdg

✅ 更換故障磁碟

sudo zpool replace mpool /dev/sdb /dev/sdh

✅ Scrub(資料一致性檢查)

sudo zpool scrub mpool

查看進度:

sudo zpool status mpool

建議每月執行一次。


✅ 匯出 / 匯入 Pool

匯出(卸載):

sudo zpool export mpool

匯入:

sudo zpool import mpool

✅ 刪除 Pool(⚠️ 會刪除所有資料)

sudo zpool destroy mpool

三、ZFS 檔案系統管理


✅ 建立檔案系統

sudo zfs create mpool/data

✅ 設定掛載目錄

sudo zfs set mountpoint=/data mpool/data

✅ 建立子檔案系統

sudo zfs create mpool/data/projects

✅ 刪除檔案系統

sudo zfs destroy mpool/data/projects

✅ 查看檔案系統

sudo zfs list

四、Snapshot(快照)

ZFS 最強功能之一 ✅


✅ 建立 Snapshot

sudo zfs snapshot mpool/data@2026-05-06

✅ 查看 Snapshot

sudo zfs list -t snapshot

✅ 還原 Snapshot

sudo zfs rollback -r mpool/data@2026-05-06

✅ 刪除 Snapshot

sudo zfs destroy mpool/data@2026-05-06

✅ 建立可寫 Clone

sudo zfs clone mpool/data@2026-05-06 mpool/testclone

五、壓縮與空間管理


✅ 啟用壓縮(推薦)

sudo zfs set compression=lz4 mpool/data

✅ lz4 幾乎無效能損耗,強烈建議開啟。


✅ 關閉壓縮

sudo zfs set compression=off mpool/data

✅ 設定容量限制(Quota)

sudo zfs set quota=100G mpool/data

✅ 設定保留空間(Reservation)

sudo zfs set reservation=20G mpool/data

六、ZFS Volume(區塊設備)

類似虛擬磁碟,可給 iSCSI / VM 使用。


✅ 建立 Volume

sudo zfs create -V 10G mpool/vm_disk

✅ 查看裝置

ls -l /dev/zvol/mpool/

✅ 刪除 Volume

sudo zfs destroy mpool/vm_disk

七、Linux 最佳實務建議 ✅


✅ 使用磁碟 ID(避免重開機順序改變)

查看:

ls -l /dev/disk/by-id/

建立 pool 範例:

sudo zpool create -o ashift=12 mpool mirror \
/dev/disk/by-id/ata-Samsung_SSD_1 \
/dev/disk/by-id/ata-Samsung_SSD_2

✅ 強烈建議使用 by-id 而非 /dev/sdX


✅ 查看磁碟

lsblk

✅ 自動開機掛載(ZFS 會自動處理)

sudo systemctl enable zfs-import-cache
sudo systemctl enable zfs-mount

八、常用維護指令總整理

功能指令
查看 Poolzpool list
查看 Pool 狀態zpool status
查看 Datasetzfs list
查看 Snapshotzfs list -t snapshot
開始 Scrubzpool scrub mpool
查看 I/Ozpool iostat 2

✅ 結語

ZFS 在 Linux 上已非常成熟,適合:

  • NAS
  • VM 主機
  • Docker 儲存
  • 資料備份伺服器
  • Home Lab
  • 企業儲存環境

Rocky Linux 10 + TPM 自動解鎖完整標準流程(從裸機開始)

🔹 第一階段:BIOS 設定

進入 BIOS:

✅ 開啟:

  • TPM 2.0
  • Secure Boot(建議開啟)
  • UEFI 模式(不要 Legacy)

儲存離開。


🔹 第二階段:安裝 Rocky Linux 10(含 LUKS2)

用 Rocky Linux 10 ISO 開機。

在安裝畫面:

1️⃣ 選擇語言 → Installation Destination

2️⃣ Storage Configuration

選:

Custom

Automatic (然後勾選 Encrypt my data)

✅ 必須勾選:

Encrypt my data

3️⃣ 設定 LUKS 密碼

系統會要求設定:

👉 這個密碼非常重要
👉 這就是日後主機壞掉時的救援密碼

請妥善保存。


4️⃣ 建議分割方式(標準企業配置)

/boot        (1GB, 不加密)
EFI          (600MB)
/            (加密)

✅ root 分割區必須加密


5️⃣ 完成安裝並重開機

此時開機會:

👉 要求輸入 LUKS 密碼
(這是正常的)


🔹 第三階段:啟用 TPM 自動解鎖


登入 root。


✅ Step 1:安裝套件

dnf install -y clevis clevis-luks clevis-dracut clevis-systemd tpm2-tools

✅ Step 2:確認 TPM 存在

ls /dev/tpm*

應看到:

/dev/tpm0
/dev/tpmrm0

✅ Step 3:確認 LUKS 位置

lsblk -f

找到:

rl-root

然後確認版本:

cryptsetup luksDump /dev/mapper/rl-root | grep Version

必須是:

Version: 2

✅ Step 4:將 TPM 綁定到 LUKS

執行:

clevis luks bind -d /dev/mapper/rl-root tpm2 '{}'

會要求輸入:

👉 您安裝時的 LUKS 密碼

成功會顯示:

Successfully bound

✅ Step 5:重建 initramfs(非常重要)

dracut -f

這一步會把 clevis 打包進開機映像。



🔹 第四階段:測試


重新開機:

reboot

如果成功:

✅ 不會要求輸入密碼
✅ 自動開機



🔹 第五階段:測試備援


進 BIOS:

👉 暫時關閉 TPM

開機:

✅ 應要求輸入密碼
✅ 輸入後可進入系統

再重新開啟 TPM。



🔹 如果主機壞掉


把硬碟接到另一台 Linux:

cryptsetup luksOpen /dev/sdX rescue

輸入原本密碼即可。

TPM slot 會失效,但密碼 slot 還在 ✅


🔹最終安全架構

LUKS2
 ├─ Slot 0 → 密碼(備援)
 ├─ Slot 1 → TPM2 自動解鎖

🔹 安全等級說明

情境結果
硬碟被偷無法解鎖 ✅
主機被偷可開機 ⚠
主機壞掉可用密碼救援 ✅

🔹 進階強化(企業級)

如果您是(金融相關)

建議下一步:

  • 加入 PCR 綁定
  • 限制 Secure Boot 狀態變更即鎖死
  • 設定 TPM policy
  • 設定自動 recovery SOP

🔹您現在的狀況總結

✔ 已成功安裝
✔ 已啟用 LUKS2
✔ 結構正確
✔ 可以直接做 TPM 綁定

Rocky Linux 安裝多版本 PHP 及切換 CLI PHP 教學

本文示範如何在 Rocky Linux 9 / 10 上:

  • ✅ 安裝多個 PHP 版本(7.4 / 8.1 / 8.2)
  • ✅ 同時並存
  • ✅ 切換 CLI PHP 預設版本
  • ✅ 啟用不同版本的 PHP-FPM

一、安裝 Remi Repository

Rocky Linux 預設只提供單一 PHP 版本,因此需要使用 Remi repo 來安裝多版本 PHP。

1️⃣ 安裝 Remi 與 EPEL

dnf install -y https://rpms.remirepo.net/enterprise/remi-release-$(rpm -E %rhel).rpm
dnf install -y epel-release
dnf update -y

2️⃣ 重設系統 PHP module

dnf module reset php -y

⚠️ 注意:不要使用 dnf module enable php:remi-xx
否則會變成單一 system PHP。


二、安裝多個 PHP 版本(並存模式)

我們使用 Remi 的 平行安裝版本(remi-safe)


✅ 安裝 PHP 7.4

dnf install -y php74 php74-php-cli php74-php-fpm php74-php-mysqlnd php74-php-gd php74-php-mbstring php74-php-xml php74-php-curl php74-php-zip

安裝完成後可用:

/usr/bin/php74

✅ 安裝 PHP 8.1

dnf install -y php81 php81-php-cli php81-php-fpm php81-php-mysqlnd php81-php-gd php81-php-mbstring php81-php-xml php81-php-curl php81-php-zip

✅ 安裝 PHP 8.2

dnf install -y php82 php82-php-cli php82-php-fpm php82-php-mysqlnd php82-php-gd php82-php-mbstring php82-php-xml php82-php-curl php82-php-zip

三、查看已安裝 PHP 版本

ls /usr/bin/php*

或:

alternatives --list | grep php

四、切換 CLI PHP 版本(推薦方法)

建議使用 alternatives 系統機制 切換 CLI PHP。


1️⃣ 註冊不同版本到 alternatives

如果尚未註冊:

alternatives --install /usr/bin/php php /usr/bin/php74 74
alternatives --install /usr/bin/php php /usr/bin/php81 81
alternatives --install /usr/bin/php php /usr/bin/php82 82

2️⃣ 選擇預設 CLI PHP

alternatives --config php

系統會顯示:

1   /usr/bin/php74
2   /usr/bin/php81
3   /usr/bin/php82

輸入對應號碼即可切換。


3️⃣ 確認切換成功

php -v

應顯示所選版本。


五、臨時使用指定版本(不切換系統)

如果只想執行某個版本,不需更改預設:

php74 -v
php81 -v
php82 -v

或執行腳本:

php81 script.php

六、啟用不同版本 PHP-FPM

多版本 FPM 可以同時存在。

例如啟動 PHP 7.4 FPM:

systemctl enable php74-php-fpm
systemctl start php74-php-fpm

查看狀態:

systemctl status php74-php-fpm

同樣方法可啟動 8.1 或 8.2。


七、檢查目前 CLI PHP 位置

which php
php -v

應顯示:

/usr/bin/php

八、總結

✅ 使用 Remi repo 安裝多版本 PHP
✅ 使用 remi-safe 平行安裝
✅ 使用 alternatives 切換 CLI
✅ FPM 可同時並存
✅ 不建議使用 module enable(除非只需要單一版本)

使用 iptables hashlimit 限制 HTTP 流量與安全回滾方法

在高流量或遭受爬蟲攻擊時,Web Server(如 Apache / Nginx)可能會因為大量連線而消耗過多 PHP-FPM 或系統資源。
Linux iptableshashlimit 模組可以有效限制「每個 IP 的連線速率」,是一種輕量且高效的防護方式。

本文說明:

  • hashlimit 的用途
  • 基本設定方法
  • 測試方式
  • 如何安全取消規則(回滾)

一、什麼是 hashlimit?

hashlimit 是 iptables 的一個 match module。

它的特色是:

✅ 針對「每個來源 IP」獨立計算流量
✅ 不會把所有訪客視為同一個來源
✅ 適合防止單一 IP 短時間內大量請求

-m limit 不同,limit 是全局限制,容易誤傷正常使用者。


二、基本 HTTP 限制範例

以下規則限制:

  • 每個 IP 每秒最多 30 個請求
  • 瞬間最多 80 個 burst
  • 超過後丟棄(DROP)

bash

iptables -A INPUT -p tcp --dport 80 \
-m hashlimit \
--hashlimit 30/sec \
--hashlimit-burst 80 \
--hashlimit-mode srcip \
--hashlimit-name http_limit \
-j ACCEPT

iptables -A INPUT -p tcp --dport 80 -j DROP

參數說明

參數說明
–hashlimit 30/sec每秒最多 30 個封包
–hashlimit-burst 80瞬間可達 80 個
–hashlimit-mode srcip以來源 IP 為單位計算
–hashlimit-name此限制規則的名稱

三、如何確認規則是否生效?

使用:

bash

iptables -L INPUT -n -v

可以看到每條規則的封包計數。

如需查看行號(刪除時會用到):

bash

iptables -L INPUT -n --line-numbers

四、如何安全取消規則?

✅ 方法一(推薦):使用行號刪除

先查看規則:

bash

iptables -L INPUT -n --line-numbers

假設顯示:

basic

num  target
1    ACCEPT  tcp dpt:80 hashlimit ...
2    DROP    tcp dpt:80

刪除方式:

bash

iptables -D INPUT 1
iptables -D INPUT 1

⚠ 注意:刪除第一條後,下面規則會往上移動。


✅ 方法二:使用完整規則刪除

bash

iptables -D INPUT -p tcp --dport 80 \
-m hashlimit \
--hashlimit 30/sec \
--hashlimit-burst 80 \
--hashlimit-mode srcip \
--hashlimit-name http_limit \
-j ACCEPT

iptables -D INPUT -p tcp --dport 80 -j DROP

⚠ 必須與建立規則時完全一致。


五、如果使用 iptables-persistent

若系統有安裝:

iptables-persistent

或使用:

netfilter-persistent

刪除規則後,請記得重新儲存:

bash

iptables-save > /etc/iptables/rules.v4

否則重開機後規則會恢復。


六、安全測試建議(避免誤封)

⚠ 在 Production 環境請勿直接啟用 DROP 規則。

建議測試流程:

1️⃣ 先只加入 hashlimit ACCEPT 規則
2️⃣ 觀察流量與錯誤日誌
3️⃣ 確認正常後再加入 DROP

或先於測試機驗證。


七、適用場景

  • WordPress 遭受爬蟲過度抓取
  • WooCommerce 頁面被大量請求
  • 想降低 PHP-FPM 壓力
  • 防止單一 IP 造成資源耗盡

八、注意事項

  • 若使用 Cloudflare,來源 IP 可能會變成 CDN IP
  • iptables 屬於 L3/L4 防火牆,無法判斷 URL
  • 規則順序非常重要

結語

hashlimit 是一種:

  • 高效
  • 核心層級
  • 不依賴應用程式

的流量控制方式。

但部署前務必測試,並確保有回滾方案,以避免影響正常使用者。

Linux 防火牆完整備份與還原教學(iptables + ipset)

在使用 Fail2Ban + ipset + iptables 的伺服器環境中,
很多人只備份了 iptables,卻忽略了 ipset

結果在災難還原時發現:

  • ✅ 規則還在
  • ❌ 被封鎖的 IP 全部消失

這篇教學會完整說明:

  1. 為什麼要同時備份 iptables 與 ipset
  2. 正確備份方式
  3. 正確還原順序
  4. 建議自動化備份腳本

🔎 一、iptables 與 ipset 差在哪?

在 Fail2Ban 環境中實際架構是:

Fail2Ban
   ↓
ipset(存放被封鎖 IP 清單)
   ↓
iptables(引用 ipset)
   ↓
Linux Kernel 防火牆

✅ iptables 負責「規則」

例如:

-m set --match-set f2b-http-404-scan src -j DROP

意思是:

如果來源 IP 在這個 set 裡 → 就 DROP


✅ ipset 負責「IP 名單」

例如:

add f2b-http-404-scan 65.20.67.134

真正被封鎖的 IP 存在 ipset 裡。


🚨 二、常見錯誤

很多人只做:

iptables-save > /root/iptables.backup

但這只會備份規則,不會備份 ipset 內的 IP。

結果:

  • 重開機後
  • 或手動 restore 後

IP 清單消失。


✅ 三、正確完整備份方式

必須備份兩個部分。


✅ 1️⃣ 備份 iptables

bash

iptables-save > /root/iptables.backup

✅ 2️⃣ 備份 ipset

bash

ipset save > /root/ipset.backup

這個檔案會包含:

sql_more

create f2b-http-404-scan hash:ip
add f2b-http-404-scan 65.20.67.134
add f2b-http-404-scan 1.2.3.4

這才是真正的封鎖名單。


✅ 四、正確還原方式(非常重要)

⚠️ 還原順序不能錯。


✅ Step 1:先還原 ipset

bash

ipset restore < /root/ipset.backup

✅ Step 2:再還原 iptables

bash

iptables-restore < /root/iptables.backup

❗ 為什麼順序不能反?

因為:

iptables 規則會引用 ipset。

如果 ipset 不存在,會出現錯誤:

iptables: Set f2b-http-404-scan doesn't exist

✅ 五、建議每日自動備份

可以建立一個腳本:

nano /root/backup-firewall.sh

內容如下:

bash

#!/bin/bash

DATE=$(date +%F)

iptables-save > /root/fw-$DATE.iptables
ipset save > /root/fw-$DATE.ipset

給執行權限:

bash

chmod +x /root/backup-firewall.sh

加入 crontab:

bash

crontab -e

每天凌晨 3 點備份:

basic

0 3 * * * /root/backup-firewall.sh

✅ 六、如果發生誤封全站怎麼辦?

假設錯誤規則導致:

0.0.0.0/0 tcp dpt:80 DROP

網站全部無法連線。

只要執行:

bash

ipset restore < /root/ipset.backup
iptables-restore < /root/iptables.backup

30 秒內救回網站。


✅ 七、關於 Fail2Ban 的補充

如果你使用:

banaction = iptables-ipset-proto6-allports

Fail2Ban 重啟後會自動重建 ipset。

因此:

✅ 一般重開機不會遺失封鎖
✅ 但手動清空 firewall 會遺失

所以備份仍然是好習慣。


✅ 八、總結

項目是否需要備份
iptables✅ 必須
ipset✅ 必須
只備份 iptables❌ 不完整

🎯 最重要一句話

iptables 只是規則
ipset 才是黑名單

兩個都要備份,才算完整防火牆備份。

Linux 指令教學:如何使用 ss -K 強制中斷指定 IP 的 TCP 連線

在 Linux 伺服器管理中,有時需要立即中斷某個 IP 的現有連線,例如:

  • fail2ban 已經 Ban IP
  • iptables 規則已生效
  • 但既存 TCP 連線仍在持續

這時不必重啟服務,可以使用:

ss -K

來精準關閉指定連線。


什麼是 ss

ss(Socket Statistics)是用來查看 socket 連線狀態的工具,
功能比舊版 netstat 更完整、更快速。


ss -K 是什麼?

-K  = Kill

意思是:

強制關閉符合條件的 TCP 連線

它會透過 kernel 直接發送 TCP RST,立即中斷連線。


基本用法

中斷某個來源 IP 的所有連線

ss -K src 20.91.210.252

意思是:

關閉所有「來源為 20.91.210.252」的 TCP 連線

⚠ 在伺服器上,攻擊者是來源(source),
所以必須使用 src


查看是否有連線存在

執行前可先確認:

ss -ant | grep 20.91.210.252

如果看到:

ESTAB

代表仍有已建立連線,可以使用 ss -K 中斷。


常見錯誤

❌ 錯誤寫法:

ss -K dst 20.91.210.252

在伺服器端通常應該使用 src
否則可能找不到對應連線。


與重啟服務的差別

方法影響範圍
restart Apache所有使用者
ss -K src IP只有指定 IP

使用 ss -K 可以精準操作,而不影響正常流量。


權限需求

必須使用 root 執行:

Operation not permitted

代表權限不足。


小結

ss -K 是一個非常實用的進階網路管理指令:

  • 可立即中斷指定 IP 連線
  • 不必重啟服務
  • 不影響其他使用者
  • 適用於安全事件處理

在需要精準控制 TCP 連線時,是比重啟服務更優雅的做法。

備份/恢複 CentOS 7 系統到網絡磁碟

備份 CentOS 7 系統到網絡磁碟

  • 掛載你的網絡磁碟機
mkdir -p /mnt/backup
mount -t cifs //<SMB_SERVER_IP>/backup /mnt/backup -o username=<USERNAME>,password=<PASSWORD>
  • 使用 tar 備份系統
tar --xattrs --xattrs-include='*' --acls --selinux -cvpzf /mnt/backup/centos7_backup.tar.gz --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/tmp --exclude=/root_backup.tar.gz /

從網絡磁碟 恢複到 CentOS 7 系統

掛載你的網絡磁碟機

mkdir -p /mnt/backup
mount -t cifs //<SMB_SERVER_IP>/backup /mnt/backup -o username=<USERNAME>,password=<PASSWORD>

確認 centos7_backup.tar.gz 在 /mnt/backup

ls -lh /mnt/backup/centos7_backup.tar.gz

還原 tar.gz 備份

解壓 tar.gz 到 /

tar --xattrs --xattrs-include='*' --acls --selinux -xvpzf /mnt/backup/centos7_backup.tar.gz -C /

確保 /dev、/proc、/sys、/run 目錄存在

mkdir -p /dev /proc /sys /run

修復 fstab

獲取磁碟的 UUID

blkid

確保 /etc/fstab 使用正確的 UUID

nano /etc/fstab

修復 Bootloader(GRUB)

掛載必要的系統目錄

mount --bind /dev /dev
mount --bind /proc /proc
mount --bind /sys /sys
mount --bind /run /run

切換到 chroot 環境

chroot /

重新安裝 GRUB

grub2-install --target=i386-pc /dev/sda
grub2-mkconfig -o /boot/grub2/grub.cfg

更新 initramfs

dracut --force --regenerate-all

退出 chroot

exit

SELinux 修復

創建 .autorelabel

touch /.autorelabel

重新啟動並測試

重啟系統

reboot

確認系統是否正常運行

df -h
lsblk

Linux Find Command

ExamplesCreated by @dan_nanni on Instagram

find . -name “my.txt” find all files named “my.txt”

find .-type d-name “mydir” find all directories named “mydir”

find . -type f-name “*.jpg” find all “.jpg” files

find . -type f-size +100M find all files larger than 100MB

find . -type f-size +100M-size -500M find files with a specific size range

find . -type f-mtime-1 find all files modified in last 24 hours

find .-mtime-7-mtime +1 find files modified betn yesterday & a week ago

find . -type f-name “*.tmp” -delete find and remove all “.tmp” files

find . -type f-perm 0777 find all files with “777” permission

find . -type f-perm-u+x find all files executed by the user

find . -type f-name “*.txt” -exec cat {} \; find and cat all “*.txt” files

find . -type f-amin-60 find all files accessed within the last hour

find . -type f-user dan find all files owned by the user “dan”

find .-type f-ctime -2 find files created within last 2 days

find .-maxdepth 1 -name “my.txt” search only in current dir

find . -type f-name “*.txt” | xargs chmod 644 chmod all “.txt” to 644

find . -type f-name “*.jpg” | xargs tar -cf img.tgz archive all “.jpg” files

find . -type f-name “*.png” | xargs -I {} mv {} /tmp move all “.png” files

find . -type f-name “*.txt” | xargs grep “Hello” search for Hello in “.txt”

find .-xtypel-delete find and remove all broken symbolic links

find .-type d-empty-delete find and remove all empty directories

find .-newermt “2024-01-01”! -newermt “2024-03-15” use a time range