Recent Articles / Archives

netstat 檢查不尋常連線

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 連線數量。

February 9, 2010 · Linux / BSD 筆記 · No Comments Yet

Linux/FreeBSD 不能辨認新安裝程式

如果在 Linux 或 FreeBSD 透過自行編譯安裝程式,系統不會立即辨認到新的程式,即是不可以只輸入程式的檔案名稱,要鍵入程式的絕對路徑才可以執行。

這個問題是因為系統為了改善效能,會將存放程式的目錄加入快取。如果想更新快取,可以執行以下指令:

# hash -r

如果是 CSH 的話,便要輸入:
# rehash

September 7, 2009 · Linux / BSD 筆記 · No Comments Yet

FreeBSD 新增/更改用戶密碼 Shell Script

在 FreeBSD 下可以用以下非互動方法新增用戶或修改用戶密碼:

新增使用者: (使用者名稱為 new_user,密碼為 new_pass)

# echo topSecrete | pw add user vivek -h 0

更改現有使用者密碼:

# echo newPassword | pw mod user user_name -h 0

只要用以上指令,便可以編寫新增及更改用戶密碼的 shell script。

編寫只給特定用戶執行的 shell script

在編寫了一些 shell script 並打算只給予特定用戶執行,可以先取得用戶的 user id,或者直接用 whoami 指令檢查使用者名稱,例如:

if [ $(whoami) = "samtang" ]
then
   # 執行程式碼
else
   echo "You cannot run this script."
   exit 0
fi

如果在另一個情況下,不容許特定用戶執行,可以這樣寫:

if [ $(whoami) = "root" ]
then
   echo "You cannot run this script."
   exit 1
fi

Apache 防止 DDoS 攻擊

DDoS (distributed denial-of-service) 及 DoS (denial-of-service) 在網路上十分常見,而 DoS 攻擊所傳送的請求跟正常的請求一樣,分別在於每秒鐘發出大量請求到伺服器,使伺服器的負載增加,最常見的情況是伺服器暫停服務。

而 mod_evasive 則是一個預防 Apache 遭受 DDos 攻擊的模組,可以防止同一個 IP 對相同 URI 發出的大量請求,可設定的選項有:

– 限制同一個 IP 在一定秒數內請求一個頁面或檔案的次數。
– 限制同一個 IP 一秒內只可發出 50 個請求。
– 設定被禁止的 IP 封鎖時間。

以下是 mod_evasive 的安裝方法:

1. 先將原來的 httpd.conf 備份起來。

2. 到 http://www.zdziarski.com/projects/mod_evasive/ 下載 mod_evasive。

3. 在指令模式解壓及編譯 mod_evasive:

tar zxvf mod_evasive_1.10.1.tar.gz
cd mod_evasive/
apxs -cia mod_evasive20.c

以上的 apxs 會放在 Apache 的 bin 目錄內;如果 Apache 版本是 1.3 的話,指令要改為:

apxs -cia mod_evasive.c

安裝好 mod_evasive 後,便要修改 httpd.conf 內容。

4. 開啟 httpd.conf,加入以內容:

DOSHashTableSize 3097
DOSPageCount 5
DOSSiteCount 100
DOSPageInterval 2
DOSSiteInterval 2
DOSBlockingPeriod 10
DOSBlockingPeriod 600

DOSHashTableSize — 這是佔用記憶體的大小,如果伺服器比較繁忙,這個數值要設定大一點。
DOSPageCount — 同一 IP 在一個時段內可以存取同一頁面的次數,超過會被禁止。
DOSSiteCount — 同一 IP 在一個網站內可以佔用多少 Object,超過會禁止。
DOSPageInterval — DOSPageCount 內的時段設定。
DOSSiteInterval — DOSSiteCount 的時間設定,以秒為單位。
DOSBlockingPeriod — 當發現疑似攻擊後,使用者會收到 403 Forbidden,這是設定封鎖的時間,以秒為單位。

5. 最後重新啟動 Apache 即可。

March 4, 2009 · Linux / BSD 筆記 · No Comments Yet


提高 Apache 的 MaxClients 選項

如果碰到網頁伺服器突然變慢,然後網頁服務停止,在 apache 的 error log 找到以下語句:

[error] server reached MaxClients setting, consider raising the MaxClients setting

那麼就是 Apache 超過最大連線數,如果是 Apache 2.0 的話便不用重新編譯,只要開啟 httpd.conf 修改 MaxClients 選項。


StartServers 10
MinSpareServers 10
MaxSpareServers 15
MaxClients 200
MaxRequestsPerChild 10000

完成後重新啟動 Apache 即可。

February 27, 2009 · Linux / BSD 筆記 · Comments (1)

Ubuntu 安裝 terminus 及 Fixedsys 字型

平日使用電腦習慣在終端機及寫程式時使用 terminus 字型,而以 plain 閱讀電郵則喜歡 Fixedsys 字型。但 Ubuntu 安裝也這兩種字型也沒有,以下是安裝以上兩種字型的方法:

安裝 terminus
在終端機打入以下指令即可:

sudo apt-get install xfonts-terminus

安裝 Fixedsys
fixedsys 下載 fixedsys 的 ttf 檔,解壓後將 Fixedsys500c.ttf 複製到 /usr/share/fonts/truetype/,指令如下:

sudo cp /path/Fixedsys500c.ttf /usr/share/fonts/truetype/
sudo fc-cache -f -v

這樣就可以使用 terminus 及 Fixedsys 字型 了。

February 11, 2009 · Linux / BSD 筆記 · Comments (1)

Linux Memory Management

好文推介,介紹 Linux 的記憶體管理:

Memory management is the heart of operating systems; it is crucial for both programming and system administration. In the next few posts I’ll cover memory with an eye towards practical aspects, but without shying away from internals. While the concepts are generic, examples are mostly from Linux and Windows on 32-bit x86. This first post describes how programs are laid out in memory. Each process in a multi-tasking OS runs in its own memory sandbox. This sandbox is the virtual address space, which in 32-bit mode is always a 4GB block of memory addresses.

Anatomy of a Program in Memory

January 28, 2009 · Linux / BSD 筆記 · No Comments Yet

重新設定 MySQL root 密碼

如果忘記了 MySQL 的 root 密碼,可以透過以下方法重新設定:

1. 停止 MySQL server。

# /etc/init.d/mysql stop

2. 啟動 MySQL server,並加上 –skip-grant-tables 參數,便可以略過輸入密碼的過程:

# mysqld_safe –skip-grant-tables &

3. 用 root 連接到 MySQL server,輸入:

mysql -u root

4. 進入 MySQL server 後,輸入以下幾行 SQL 語句:
mysql> use mysql;
mysql> update user set password=PASSWORD("newpass") where User=’root’;
mysql> flush privileges;
mysql> quit

5. 重新啟動 MySQL 即可。

January 26, 2009 · Linux / BSD 筆記 · No Comments Yet

透過 Linux 恢復 Windows 系統管理員密碼

如果忘記了 Windows 的 administrator 密碼,可以透過 Linux 的 Live CD 進行恢復,而這裡會使用 Ubuntu 作為例子:

1. 使用 Ubuntu Live CD 開機

2. 安裝一個名為 “chntpw” 的程式

$ sudo apt-get install chntpw

3. 安裝 “chntpw” 後,便要將 Windows 的 NTFS 分割區掛載。

4. 進入 Windows 分割區下的 system32/config 目錄,然後輸入:

$ sudo chntpw SAM

5. 出現了一堆訊息後,系統會提示 reset 密碼,如果想設成空密碼,可以輸入星號 *,重新開機後便可以進入 Windows。

January 22, 2009 · Linux / BSD 筆記 · Comments (1)


Ubuntu 8.10 安裝 Java

安裝 Ubuntu 後系統預設沒有安裝 Java,以下是安裝方法:

$ sudo apt-get install sun-java6-jre sun-java6-plugin

安裝後再設定 Firefox 成支援 java

$ sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.10/jre/plugin/i386/ns7/libjavaplugin_oji.so /usr/lib/firefox/plugins/
January 21, 2009 · Linux / BSD 筆記 · No Comments Yet

只針對檔案或目錄 chmod

在 Unix Like 環境可以用 chmod 改變檔案或目錄的權限,以下方法可以只針對檔案或目錄 chmod:

1. 在當前目錄下遁迴 chmod 檔案:
find . -type f -exec chmod 0600 {} \;

2. 在當前目錄下遁迴 chmod 目錄:
find . -type d -exec chmod 0755 {} \;

January 21, 2009 · Linux / BSD 筆記 · Comments (1)

改變需要勇氣,投進 Linux 桌面懷抱

由剛開始用電腦開始已經使用 Windows,那時好像是 Windows 3.x 的版本,至今已經十幾年。雖然這段時間也有使用 Ubuntu 及 EeePC,但主要的作業系統還是 Windows,原因可能是因為沒有改變的勇氣。

一般上我的工作也是寫程式及管理主機,全都可以在 Linux 下完成,上個月買了一個新硬碟來灌 Ubuntu,將在 Windows 下面的資料複製到新硬碟後,直接將裝有 Windows 的硬碟拆除,免得自己想開 Windows。 但裝有 Windows 的硬碟也要留著,只怕在什麼時候真的要開個 Windows,手邊又沒有的話就麻煩了。

到目前的情況是進展良好,證實我的工作是全部在 Linux 下完成,只是自己不願作出改變。

January 20, 2009 · Linux / BSD 筆記 · No Comments Yet

Ubuntu 9.04 將會支援 Ext4 filesystem

新一代的 Ext 檔案系統 Ext4,而 Ubuntu 的下一個版本 9.04 也會支援。而 Ext4 在上個月發佈的 Linux kernel 2.6.28 正式被納入。Ext4 的設計提供更佳的效能及可靠度,而檔案系統可支援的容量相應增加到 1 exabyte,及縮短 fsck 所須的時間。

Linux 硬件網站 Phoronix 公佈了基於 Ext4 的效能測試報告,最突出的是 IOzone 測試對大型檔案的寫入效能,結果顯示效能比 XFS, JFS, ReiserFs 及 Ext3 都大幅領先。

January 14, 2009 · Linux / BSD 筆記 · Comments (1)

Linux 系統管理員輕鬆工作技巧

列出 10 個系統管理員的小技巧:

There are thousands of tricks you can learn from someone’s who’s an expert at the command line. The best systems administrators are set apart by their efficiency. And if an efficient systems administrator can do a task in 10 minutes that would take another mortal two hours to complete, then the efficient systems administrator should be rewarded (paid more) because the company is saving time, and time is money, right?

The trick is to prove your efficiency to management. While I won’t attempt to cover that trick in this article, I will give you 10 essential gems from the lazy admin’s bag of tricks. These tips will save you time — and even if you don’t get paid more money to be more efficient, you’ll at least have more time to play Halo.

Lazy Linux: 10 essential tricks for admins

January 13, 2009 · Linux / BSD 筆記 · No Comments Yet


  Next Page »