Last active
August 25, 2025 03:31
-
-
Save duongthanhthai/0a8b9b8fbfe158d5f668bbb6957e20bf to your computer and use it in GitHub Desktop.
Check ping block and open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# enable-ping.sh - Gỡ chặn ICMP echo-request (ping) trên Linux | |
# Hỗ trợ: sysctl, firewalld, UFW, iptables, nftables (best effort) | |
# Chạy với quyền root | |
set -euo pipefail | |
log() { echo -e "[*] $*"; } | |
ok() { echo -e "[✔] $*"; } | |
warn() { echo -e "[!] $*"; } | |
err() { echo -e "[✘] $*" >&2; } | |
require_root() { | |
if [[ "$(id -u)" -ne 0 ]]; then | |
err "Vui lòng chạy với quyền root (sudo)." | |
exit 1 | |
fi | |
} | |
backup_file() { | |
local f="$1" | |
if [[ -f "$f" ]]; then | |
cp -a "$f" "${f}.bak.$(date +%s)" | |
log "Đã backup $f -> ${f}.bak.$(date +%s)" | |
fi | |
} | |
# --- 1) SYSCTL --- | |
fix_sysctl() { | |
local cur | |
if [[ -f /proc/sys/net/ipv4/icmp_echo_ignore_all ]]; then | |
cur=$(cat /proc/sys/net/ipv4/icmp_echo_ignore_all || echo 0) | |
if [[ "$cur" == "1" ]]; then | |
log "sysctl đang chặn ping (icmp_echo_ignore_all=1) → mở lại." | |
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all || true | |
# Ghi bền vững | |
local sysctl_conf="/etc/sysctl.d/99-icmp.conf" | |
backup_file "$sysctl_conf" | |
echo "net.ipv4.icmp_echo_ignore_all=0" > "$sysctl_conf" | |
sysctl -p "$sysctl_conf" >/dev/null || true | |
ok "Đã bật ping trong sysctl." | |
else | |
log "sysctl không chặn ping (icmp_echo_ignore_all=$cur)." | |
fi | |
fi | |
} | |
# --- 2) FIREWALLD --- | |
fix_firewalld() { | |
if command -v firewall-cmd >/dev/null 2>&1; then | |
if systemctl is-active --quiet firewalld; then | |
log "Phát hiện firewalld đang chạy." | |
local blocks | |
blocks=$(firewall-cmd --list-icmp-blocks || true) | |
if echo "$blocks" | grep -qw "echo-request"; then | |
log "firewalld đang block icmp echo-request → gỡ." | |
firewall-cmd --permanent --remove-icmp-block=echo-request >/dev/null | |
firewall-cmd --reload >/dev/null | |
ok "Đã gỡ block ping trong firewalld." | |
else | |
log "firewalld không block icmp echo-request." | |
fi | |
fi | |
fi | |
} | |
# --- 3) UFW (Ubuntu/Debian) --- | |
# Lưu ý: UFW mặc định KHÔNG chặn ICMP. Nếu bị chặn, thường là bị thêm rule trong /etc/ufw/before.rules | |
fix_ufw() { | |
if command -v ufw >/dev/null 2>&1; then | |
log "Phát hiện UFW. Kiểm tra /etc/ufw/before.rules…" | |
local f="/etc/ufw/before.rules" | |
if [[ -f "$f" ]]; then | |
# Tìm các dòng chặn echo-request (DROP/REJECT) | |
if grep -Eiq 'icmp.*echo-request.*(DROP|REJECT)' "$f"; then | |
log "Phát hiện dòng chặn ping trong $f → sửa thành ACCEPT." | |
backup_file "$f" | |
# Chuyển DROP/REJECT thành ACCEPT chỉ với dòng có echo-request | |
sed -ri '/icmp/s/(--icmp-type[[:space:]]+echo-request[^#\n]*)(DROP|REJECT)/\1ACCEPT/I' "$f" | |
# Nếu từng bị comment trick, thử bỏ comment ACCEPT mẫu | |
# Reload UFW | |
ufw reload || true | |
ok "Đã sửa UFW để cho phép ping." | |
else | |
log "Không thấy rule chặn ping trong UFW before.rules." | |
fi | |
fi | |
fi | |
} | |
# --- 4) IPTABLES (legacy) --- | |
fix_iptables() { | |
if command -v iptables >/dev/null 2>&1; then | |
log "Kiểm tra iptables rules chặn ICMP…" | |
# Liệt kê các rule chặn echo-request trong INPUT chain | |
local rules | |
rules=$(iptables -S INPUT | grep -Ei '\-p icmp' | grep -Ei '(echo\-request|icmp\-type 8)' | grep -Ei '(DROP|REJECT)' || true) | |
if [[ -n "$rules" ]]; then | |
backup_file "/etc/iptables/rules.v4" || true | |
while read -r r; do | |
[[ -z "$r" ]] && continue | |
# Chuyển -S thành -D để delete (giữ nguyên thứ tự tham số) | |
# Ví dụ: -A INPUT -p icmp --icmp-type echo-request -j DROP | |
local del="${r/-A /-D }" | |
log "Xóa rule: iptables $del" | |
iptables $del || true | |
done <<< "$rules" | |
# Lưu lại nếu có chỗ lưu | |
if [[ -d /etc/iptables ]]; then | |
iptables-save > /etc/iptables/rules.v4 || true | |
ok "Đã xóa rule chặn ping trong iptables và lưu /etc/iptables/rules.v4." | |
else | |
ok "Đã xóa rule chặn ping trong iptables." | |
fi | |
else | |
log "Không thấy rule iptables chặn ICMP echo-request." | |
fi | |
fi | |
} | |
# --- 5) NFTABLES (best effort, khi KHÔNG dùng firewalld) --- | |
# Chiến lược: nếu có chain inet filter input → chèn rule ACCEPT echo-request lên đầu. | |
fix_nftables() { | |
if command -v nft >/dev/null 2>&1; then | |
# Nếu firewalld đang dùng backend nft, đã xử lý qua firewalld ở trên. | |
if command -v firewall-cmd >/dev/null 2>&1 && systemctl is-active --quiet firewalld; then | |
log "nftables đang dưới quyền quản lý của firewalld → đã xử lý qua firewalld." | |
return | |
fi | |
log "Kiểm tra nftables…" | |
if nft list chain inet filter input >/dev/null 2>&1; then | |
# Check xem đã có rule accept echo-request chưa | |
if nft list chain inet filter input | grep -Eiq 'icmp.*echo-request.*accept'; then | |
log "nftables đã có rule accept echo-request." | |
else | |
log "Chèn rule accept echo-request vào inet/filter/input (vị trí đầu)." | |
nft insert rule inet filter input icmp type echo-request accept || warn "Không chèn được rule nftables (cần kiểm tra thủ công)." | |
ok "Đã cố gắng cho phép ping trong nftables." | |
fi | |
else | |
warn "Không tìm thấy chain 'inet filter input'. Bỏ qua nftables (cần kiểm tra thủ công)." | |
fi | |
fi | |
} | |
# --- 6) Test lại nội bộ (optional) --- | |
self_test() { | |
log "Tự kiểm tra nhanh:" | |
if command -v firewall-cmd >/dev/null 2>&1 && systemctl is-active --quiet firewalld; then | |
firewall-cmd --list-icmp-blocks || true | |
fi | |
if command -v ufw >/dev/null 2>&1; then | |
ufw status verbose || true | |
fi | |
if command -v iptables >/dev/null 2>&1; then | |
iptables -S INPUT | sed -n '1,120p' || true | |
fi | |
if command -v nft >/dev/null 2>&1; then | |
nft list ruleset | sed -n '1,120p' || true | |
fi | |
if [[ -f /proc/sys/net/ipv4/icmp_echo_ignore_all ]]; then | |
echo "icmp_echo_ignore_all=$(cat /proc/sys/net/ipv4/icmp_echo_ignore_all)" | |
fi | |
ok "Hoàn tất. Hãy thử ping từ máy của bạn." | |
} | |
main() { | |
require_root | |
log "Bắt đầu gỡ chặn ping…" | |
fix_sysctl | |
fix_firewalld | |
fix_ufw | |
fix_iptables | |
fix_nftables | |
self_test | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use wget