在編寫了一些 shell script 並打算只給予特定用戶執行,可以先取得用戶的 user id,或者直接用 whoami 指令檢查使用者名稱,例如:
then
# 執行程式碼
else
echo "You cannot run this script."
exit 0
fi
如果在另一個情況下,不容許特定用戶執行,可以這樣寫:
then
echo "You cannot run this script."
exit 1
fi
關於 PHP, Linux, Open Source 及個人生活記載的網誌。
在編寫了一些 shell script 並打算只給予特定用戶執行,可以先取得用戶的 user id,或者直接用 whoami 指令檢查使用者名稱,例如:
如果在另一個情況下,不容許特定用戶執行,可以這樣寫:
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:
以上的 apxs 會放在 Apache 的 bin 目錄內;如果 Apache 版本是 1.3 的話,指令要改為:
安裝好 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 即可。
如果碰到網頁伺服器突然變慢,然後網頁服務停止,在 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 即可。
平日使用電腦習慣在終端機及寫程式時使用 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 字型 了。
好文推介,介紹 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.
如果忘記了 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 即可。
如果忘記了 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。
安裝 Ubuntu 後系統預設沒有安裝 Java,以下是安裝方法:
安裝後再設定 Firefox 成支援 java
在 Unix Like 環境可以用 chmod 改變檔案或目錄的權限,以下方法可以只針對檔案或目錄 chmod:
1. 在當前目錄下遁迴 chmod 檔案:
find . -type f -exec chmod 0600 {} \;
2. 在當前目錄下遁迴 chmod 目錄:
find . -type d -exec chmod 0755 {} \;
由剛開始用電腦開始已經使用 Windows,那時好像是 Windows 3.x 的版本,至今已經十幾年。雖然這段時間也有使用 Ubuntu 及 EeePC,但主要的作業系統還是 Windows,原因可能是因為沒有改變的勇氣。
一般上我的工作也是寫程式及管理主機,全都可以在 Linux 下完成,上個月買了一個新硬碟來灌 Ubuntu,將在 Windows 下面的資料複製到新硬碟後,直接將裝有 Windows 的硬碟拆除,免得自己想開 Windows。 但裝有 Windows 的硬碟也要留著,只怕在什麼時候真的要開個 Windows,手邊又沒有的話就麻煩了。
到目前的情況是進展良好,證實我的工作是全部在 Linux 下完成,只是自己不願作出改變。
新一代的 Ext 檔案系統 Ext4,而 Ubuntu 的下一個版本 9.04 也會支援。而 Ext4 在上個月發佈的 Linux kernel 2.6.28 正式被納入。Ext4 的設計提供更佳的效能及可靠度,而檔案系統可支援的容量相應增加到 1 exabyte,及縮短 fsck 所須的時間。
Linux 硬件網站 Phoronix 公佈了基於 Ext4 的效能測試報告,最突出的是 IOzone 測試對大型檔案的寫入效能,結果顯示效能比 XFS, JFS, ReiserFs 及 Ext3 都大幅領先。
列出 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.
在 Redhat 上面使用編譯的方式安裝 Apache 後,將 bin/apachectl 複製到 /etc/rc.d/init.d/ 目錄,並想用 chkconfig 將 Apache 設定成自動啟動,但出現了 "service httpd does not support chkconfig"。
解決方法是開解 /etc/rc.d/init.d/httpd (或 /etc/init.d/httpd),在第二行加入以下兩句:
# chkconfig: 2345 10 90
# description: Activates/Deactivates Apache Web Server
這樣就會將 httpd 強制支援 chkconfig。
今日將數台 FreeBSD 主機重新編譯核心,修改完 config 後,make buildkernel 成功沒有問題,但當執行 make installkernel 時便出現以下錯誤:
ERROR: Required audit group is missing, see /usr/src/UPDATING
看一看 /usr/src/UPDATING 沒有什麼作用,還是要靠 Google。解決方法是加入一個 audit 的 group,指令為:
以下是幾條有用的指令,檢查 IP 連線數量及封鎖 IP
封鎖 IP
解除封鎖 IP
列出最多連接數的 5 個 IP 及其連接數,其中 ":80" 是連接埠號,可改成想要檢查的埠號。
以上 3 個指令可以列出總連線數目。