2024年3月29日 星期五

Linux ping report no answer

Linux ping 預設不會印出無回應的狀況,只要加上 -O 就可以了,就跟 Windows ping 差不多
有掉包的話比較清楚

# ping -O 192.168.1.8
PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.347 ms
64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.237 ms
no answer yet for icmp_seq=3
no answer yet for icmp_seq=4
64 bytes from 192.168.1.8: icmp_seq=5 ttl=64 time=0.224 ms
64 bytes from 192.168.1.8: icmp_seq=6 ttl=64 time=0.213 ms

再加 -D 參數可以在前面印出時間,可惜是 UNIX timestamp 格式,不方便直接讀取

# ping -DO 192.168.1.8
PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
[1711724576.274247] 64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.254 ms
[1711724577.288744] 64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.225 ms
[1711724578.312747] 64 bytes from 192.168.1.8: icmp_seq=3 ttl=64 time=0.220 ms

若要在前面加入時間,推薦用 ts,在 moreutils 套件內,它是個 Perl 腳本,可搭配任何程式使用

# ping -O 192.168.1.8 | ts
Mar 29 23:08:25 PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
Mar 29 23:08:25 64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.257 ms
Mar 29 23:08:26 64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.239 ms
Mar 29 23:08:27 64 bytes from 192.168.1.8: icmp_seq=3 ttl=64 time=0.240 ms








iptables hashlimit

把 Linux 充當 NAT 的情況下,可以用 iptables hashlimit module 來達到簡單的限速

iptables -A FORWARD -d 192.168.0.0/16 -m hashlimit --hashlimit-name download --hashlimit-mode dstip --hashlimit-above 2048kb/s -j DROP
iptables -A FORWARD -s 192.168.0.0/16 -m hashlimit --hashlimit-name upload --hashlimit-mode srcip --hashlimit-above 1024kb/s -j DROP

第1行,下載規則,當目的是內網時, 以 dstip 分類,限速 2048kbytes/s = 16Mbps
第2行,上傳規則,當來源是內網時, 以 srcip 分類,限速 1048kbytes/s = 8Mbps

效果: 內網每個 IP 都享有 16Mbps/8Mbps 頻寬。測速會超過一些,誤差在 10% 內,還算滿準的。

ref. man iptables-extensions

2024年3月21日 星期四

RHEL 9 install xrdp

xrdp 是 Linux 版的 RDP server (遠端桌面)
它運作時需要 Xvnc 的 libvnc.so, 所以安裝時需要連同 VNC 一起安裝(只需 minimal)

dnf -y install xrdp tigervnc-server-minimal
systemctl enable xrdp --now

在 Rocky Linux 9 的環境,有遇到登入後立即斷線的狀況,似乎是 gdm 的問題,換成 lightdm 就正常了

dnf -y install lightdm
systemctl disable gdm --now
systemctl enable lightdm --now

2024年3月14日 星期四

DNS negative cache

在查詢一個域名時,若當資料不存在,會出現 NXDOMAIN
DNS 也會 cache 這個查不到的結果,其預設的 TTL 跟 DNS 及 Zone 的 SOA TTL 有關
例如 BIND 的 max-ncache-ttl 預設是 10800 秒
當 SOA 中的 TTL 小於 DNS 的 ncache-ttl,會看 SOA 中的 TTL

某些情境,需要比較快速查到最新資料,需要縮短這個值,BIND 的 max-ncache-ttl 可以設 0
也就是 disable negative cache,遇到 NXDOMAIN 都不 cache
這樣設定的話,本來沒設定的域名,設了解析後就可以馬上查到,公用服務的 DNS 不建議這樣設定

New SSH client to old SSH server

從新版 SSH 8.x 連到舊版 6.x SSH server 若出現 Unable to negotiate with x.x.x.x port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss ...