在 Unix 或 Linux 下很多特殊字元也是指令的一部份,例如空格 (” “)、問號 (”?”) 及橫線 (”-”) 等。所以如果檔案名稱包含了一些特殊字元,在操作上便會出現錯誤,例如:
# touch -foo.txt
touch: invalid option — ‘o’
Try `touch –help’ for more information.
# touch –foo.txt
touch: unrecognized option ‘–foo.txt’
Try `touch –help’ for more information.
上面輸入了兩行指令,分別用 touch 建立檔案 “-foo.txt” 及 “–foo.txt”,但同樣發生錯誤。要解決這個問題,只要在指令後面加上兩個橫線 “–” 即可,例如:
# touch — –foo.txt
# touch — -foo.txt
而這個方法同樣可以在其他檔案操作指令使用,例如 cp, mv, rm, rmdir。
以下是 cp 及 mv 的用法:
cp - - ‘-foo.txt’ /path/to/dest
mv - - ‘-foo.txt’ /path/to/dest
GZIP 是一種壓縮技術,是在伺服器上將頁面壓縮,傳到用戶的瀏覽器再解壓。在 PHP 實要 GZIP 壓縮可以這樣做:
PHP:
-
<?php
-
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],
'gzip')){
-
-
}else{
-
-
}
-
?>
sudo 提供了簡單的方法追蹤執行紀錄,其中一項就是當透過 sudo 執行 指令時發送電郵到指定電郵地址,方法如下:
1. 開啟 sudo 的設定檔:
visudo
2. 加入下面兩行:
mailto "admin@localhost"
mail_always on
mailto 後面是設定發送電郵通知的地址。
mail_always 預設是 off,開啟後會在每次執行 sudo 後發出電郵通知。
如果在 Linux 下要限制每個 ip 的連線數,可以透過 iptables 實現。詳細指令語法如下:
/sbin/iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
限制每個 ip 只可以有 3 個 ssh 連線 (預設 ssh 使用 port 22)。
/sbin/iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset
只接受每個 ip 20 個 http 連線 (httpd.conf 裡面的 MaxClients 預設是 60)。
要留意的是,這個設定可能會把 proxy servers 阻隔,因為每個 proxy servers 可能會建立大量的連線。
Skip proxy server IP 1.2.3.4 from this kind of limitations:
/sbin/iptables -A INPUT -p tcp --syn --dport 80 -d ! 1.2.3.4 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset
這句的作用跟上面語法一樣,只是把已知的 proxy server (1.2.3.4) 給開通,避免阻隔 proxy servers 的連線。
In this example, limit the parallel http requests to 20 per class C sized network (24 bit netmask)
/sbin/iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT --reject-with tcp-reset
這個是限制同一個 class C 網絡同時建立 20 個連線。
如果想把在指定時間內建立過多連線的 ip 阻隔,這便要編輯 iptables 的 shell script。
以下例子會阻隔在 100 秒內建立多於 10 個 http 連線的 ip
#!/bin/bash
IPT=/sbin/iptables
# Max connection in seconds
SECONDS=100
# Max connections per IP
BLOCKCOUNT=10
# ....
# ..
# default action can be DROP or REJECT
DACTION="DROP"
$IPT -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
$IPT -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds ${SECONDS} --hitcount ${BLOCKCOUNT} -j ${DACTION}
# ....
# ..
要儲存 iptables 的修改可以查看 iptables-save 的 man page,在 redhat 下是用以下指令:
service iptables save
在安裝 ProFTPd 後,會發覺登入時非常援慢,但只要登入成功後,速度就會變得很暢順。
造成這個問題的原因是 ProFTPd 預設在登入時會做「反查」的動作,解決方法是將 ProFTPd 設定為不做「反查」。方法如下:
1. 開啟 proftpd.conf,一般上檔案位置會是 /etc/proftpd.conf 或 /usr/local/etc/proftpd.conf
2. 加入以下兩行:
UseReverseDNS off
IdentLookups off
3. 重新啟動 ProFTPd。
The Free Technology Academy (FTA) 釋出一本免費電子書 "The GNU/Linux operating system",內容主要圍繞系統管理,例如如何安裝及設定多種不同的伺服器及優化系統等。
官方網站介紹:
The GNU/Linux systems have reached an important level of maturity, allowing to integrate them in almost any kind of work environment, from a desktop PC to the sever facilities of a big company.
In the module called "The GNU/Linux operating system", the main contents are related with system administration. This book is the main documentation for the module.
書本資料:
* Authors: Remo Suppi Boldrito, Josep Jorba Esteve
* Coordinator: Josep Jorba Esteve
* Licenses: GNU Free Documentation License, Creative Commons Attribute ShareAlike License
* Information: 545 Pages; 18.8 Mb
* Download PDF version
Practical PHP Programming 是 PHP 免費線上電子書,比較適合對 PHP 有基礎認識的朋友,以下為電子書目錄:
1. Preface
2. Introducing PHP
3. Simple variables and operators
4. Functions
5. Arrays
6. Objects
7. HTML Forms
8. Files
9. Databases
10. Cookies and Sessions
11. Multimedia
12. XML & XSLT
13. Output Buffering
14. Java and COM
15. Networks
16. Miscellaneous topics
17. Security concerns
18. Performance
19. Writing PHP
20. Writing extensions
21. Alternative PHP uses
22. Practical PHP
23. Bringing it to a close
24. Answers to Exercises
25. The future of PHP
26. Glossary
netstat 是一個十分好用的網絡管理工具,而其中一個用途是查看不尋常連線,例如當一個 IP 發送大量連線到伺服器,那麼伺服器很大機會是否遭受 DoS 或 DDoS 攻擊。
以下是一些實用的 netstat 語法,可以檢查主機的連線數量:
netstat -na
顯示主機上所有已建立的連線。
netstat -an | grep :80 | sort
顯示所有 port 80 的連線,並把結果排序。
netstat -n -p|grep SYN_REC | wc -l
列出主機上有多少個 SYNC_REC,一般上這個數字應該相當低。
netstat -n -p | grep SYN_REC | sort -u
同樣是列出 SYNC_REC,但不只列出數字,而是將每個 SYNC_REC 的連線列出。
netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'
列出發送 SYNC_REC 的所有 ip 地址。
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
計算每一個 ip 在主機上建立的連線數量。
netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
列出從 TCP 或 UDP 連線到主機的 ip 的數量。
netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
列出每個 ip 建立的 ESTABLISHED 連線數量。
netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1
列出每個 ip 建立的 port 80 連線數量。