顯示具有 FreeBSD Operation 標籤的文章。 顯示所有文章
顯示具有 FreeBSD Operation 標籤的文章。 顯示所有文章

星期二, 1月 01, 2013

less is more:設定 man 的 pager




FreeBSD:
在 FreeBSD 中,man 的設定預設是使用 more ,常常拉到最後面就跳回 shell 了,我們可以透過設定環境變數 PAGER 來設定 man 使用的換頁程式。
如果你是使用 csh/tcsh 在 /etc/csh.cshrc 加上:
setenv PAGER "less -is"

Linux:
Linux 的預設 PAGER 就是 less -is,但是設定 man pager 的方式有點不太一樣,以 Fedora(Red-Hat) 來說,一般可在 /etc/man.config 這個檔案中設定。
一般不不太需要改,除非你想改 less 的 prompt,以下是我的 man.config 設定:
PAGER /usr/bin/less -is -P "?f%f .?m(file %i of %m) .?ltlines %lt-%lb?L/%L. . byte %bB?s/%    s. ?e(END) :?pB%pB\%..%t"

星期一, 1月 03, 2011

FreeBSD mount 新 Partition 的方法


假設我們要新增的 slice 是 "ad2s2"。

Step 1.
開機時選單人(single user)或安全(safe mode)模式.

Step 2.
sysinstall->Configure->Fdisk->按 Z 切換容量單位->選 Slice->按 S 決定開機 Slice (確定 Flag 上有 A)->選擇要新增的 Slice 磁區->選擇 unused space->按 C (Create Slice)->按 enter  (自動容量)->按 W (Write Changes)->boot manager 選 none 或 standard MBR.


Step 3.

在 shell 下指令:
bsdlabel -w /dev/ad2s2
newfs -U /dev/ad2s2a
mount /dev/ad2s2a /newdisk

星期三, 11月 24, 2010

Install dovecot on FreeBSD

Install dovecot on FreeBSD:

davecot 是一個 secure 的 imap/pop3 server。

http://dovecot.org/

    一. 安裝
    cd /usr/ports/mail/dovcot/ && make install clean

    修改/etc/rc.conf加上:
    #dovcot
    dovecot_enable="YES"

    二. 改設定檔
    cd /usr/local/etc/
    cp dovecot-example.conf dovecot.conf
    chmod u+w dovecot.conf

    修改dovecot.conf:
    1. 設定你所想支援的protocols.
    protocols = imaps pop3 pop3s

    2. 允許使用 plaintext auth
    disable_plaintext_auth = no

    3. 設定 ssl key file
    ssl_cert_file = /usr/local/etc/cert/ooxx.pem
    ssl_key_file = /usr/local/etc/cert/xxoo.pem
    (cert file可以透過 /usr/local/share/dovecot/mkcert.sh 做出來)

    * netstat -an | more
    * netstat -an | grep 143
    * portsentry 會佔用 port 143 (imap~)

Add group(s) to user in Linux or FreeBSD

Add group(s) to user in Linux or FreeBSD

FreeBSD :
pw usermod username -G grouplist
* grouplist is a comma(逗號) separated list of group to be assign to the username.
* Exit & login again for the group permission to take effect.

Linux :
usermod -G groupname username

星期二, 7月 07, 2009

chmod notes

SYNOPSIS

chmod 可用來改變檔案屬性


DESCRIPTION

以下是可以設置的屬性:

分類:
a all
o owner
g group
a another

操作元:
= 直接指定
- 移除
+ 新增

屬性:
r read
w write
x execute
t saved-text (sticky)
s "u+s" sets SUID, "g+s" sets SGID.


EXAMPLES

chmod 0755 script.sh
chmod 755 script.sh
chmod 644 test.txt

chmod a+x *.sh
chmod a-x *.sh
chmod u=rwx,g=rx,o=rx script.sh
chmod u=rwx,go=rx script.sh
chmod u+rwx,g+rx,g-w,o+rx,o-w script.sh
chmod u+rwx,go+rx,go-w script.sh

安裝 Apache on FreeBSD 筆記


安裝 Apache on FreeBSD 筆記

# 用 ports 安裝 apache 1.3
# 也可以裝 apache22 或更新版本,目錄不同而已。
cd /usr/ports/www/apache13/
make all install clean

# 編輯 /etc/rc.conf 加入 apache_enable="YES"
echo 'apache_enable="YES"' >> /etc/rc.conf

# 啟動
/usr/local/etc/rc.d/apache.sh start


* 一般設定

# 編輯 /usr/local/etc/apache/httpd.conf

[httpd.conf]
    # 管理者帳號
    ServerAdmin you@your-host.com

    # 網站名稱
    ServerName www.your-host.com

    # 網站根目錄
    DocumentRoot "/usr/local/www/data"

    # 根目錄細部設定
    <Directory "/usr/local/www/data">

    # 常見設定:
    # Options 後面可以接的關鍵字除了 None 和 All 之外也可以是其他關鍵字的合併:
    #  None    => 什麼也沒有
    #  All     => 除了 MultiViews 之外通通都有
    #  ExecCGI   => 可以執行 CGI
    # FollowSymLinks  => 使用系統的 symbol link 存取目錄
    #  Indexes   => 沒有索引檔時顯示目錄
    # Includes  => 可以使用 Apache 的 SSI (Server-Side Include)功能
    # IncludesNOEXEC => 可以使用 SSI 但不能執行 CGI
    Options Indexes FollowSymLinks MultiViews

    # 可以使用 .htaccess
    AllowOverride All

    # allow 與 deny 順序
    Order allow,deny

    # 所有人都可以看
    Allow from all

    </Directory>
[/httpd.conf]


* 虛擬主機設定 (v1.3)

[httpd.conf]
    # 虛擬主機的 DNS
    NameVirtualHost [本機IP]

    # 設定含入檔
    include /usr/local/etc/apache/vhosts/

    # 預設伺服器, 當 client 輸入的位址查不到時會連到這

    <VirtualHost _default_:80>
        Options ExecCGI
        ServerAdmin root@[本機IP]
        DocumentRoot /usr/local/www/data
        ServerName [本機IP]
        ErrorLog logs/default-error_log
        CustomLog logs/default-access_log combined
    </VirtualHost>

    # 當 client 使用 ip 連線時會連到這

    <VirtualHost [本機IP]>
        Options ExecCGI
        ServerAdmin root@[本機IP]
        DocumentRoot /usr/local/www/data
        ServerName [本機IP]
        ErrorLog logs/default-error_log
        CustomLog logs/default-access_log combined
    </VirtualHost>
[/httpd.conf]

# 這種設法是把每個虛擬主機設定存一個檔案,要新增或刪除主機時在含入目錄中增刪檔案即可。

[虛擬主機設定檔範例: alpha]
    alias /alpha /usr/local/www/vhosts/alpha
    <VirtualHost *:80>
        ServerAdmin [you]@[domain]
        DocumentRoot /usr/local/www/vhosts/alpha
        ServerName alpha.[domain]
        ErrorLog /var/log/alpha.[domain]-error_log
        CustomLog /var/log/alpha.[domain]-access_log combined
        <Directory "/usr/local/www/vhosts/alpha">
            Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
        </Directory>
    </VirtualHost>
[/虛擬主機設定檔範例: alpha]


* 虛擬主機設定 (v2.2)

# 與 1.3 設定類似,相關設定會放在 /usr/local/etc/apache22/extra/httpd-vhosts.conf
# 在 /usr/local/etc/apache22/httpd.conf 中把上面檔案引入,並修改為自己想要的如下:

[httpd.conf]
Include /usr/local/etc/apache22/extra/httpd-vhosts.conf
[/httpd.conf]


* 錯誤記錄存放處:

cat /var/log/httpd-error.log


* apache 防盜連設法

# 參考官網範例: http://httpd.apache.org/docs/2.0/env.html#examples

[httpd.conf]
    #允許存取的主機位置
    SetEnvIf Referer "^http://www.your-host.com/" local_referal
    SetEnvIf Referer "^http://192.168.1.100/" local_referal

    #允許用戶不送 referer (從"我的最愛"/"書籤"開啟時)
    #SetEnvIf Referer "^$" local_referal

    #要保護的目錄
    <Directory /usr/local/www/data/mrtg/>
        Order Deny,Allow
        Deny from all
        Allow from env=local_referal
    </Directory>
[/httpd.conf]

星期一, 7月 06, 2009

xargs 指令使用筆記

xargs 指令使用筆記

* xargs - build and execute command lines from standard input
* xargs 可以把從 stdio 傳來的字串當成某個指令的參數。

把 /tmp 下的 core dump 檔案都砍掉
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

顯示與砍掉所有 .svn 的目錄
find . -name .svn | xargs ls -ld
find . -name .svn | xargs rm -rf

顯示系統內所有 user id 並排序
cut -d: -f1 < /etc/passwd | sort | xargs echo

tr 指令筆記 - translate or delete characters

tr - translate or delete characters

tr 指令可以用來過濾字元,一般會用 pipe 丟資料給 tr 處理。
如 tr -d "\r\n" 刪掉 0x0d 與 0x0a。

FreeBSD 7.0 的 History Key Bind

FreeBSD 7.0 的 key bind 改過了,改成舊式的方法如下:
# fix bindkeys to old style:
bindkey "^[[A" history-search-backward # up
bindkey "^[[B" history-search-forward # down
bindkey -k up history-search-backward
bindkey -k down history-search-forward

星期日, 7月 13, 2008

denyhosts notes

denyhosts notes
OS: FreeBSD 6.3 Release pl2

denyhosts 是一個用來分析 log messages 的 script,主要用來對付 ssh 的暴力破解。
當 ssh 驗證失敗次數過多時,會將 ip 加入 tcp wrapper 的拒絕連線清單 (hosts.allow)。

詳細資訊請參考:
http://denyhosts.sourceforge.net/faq.html
http://www.freebsdsoftware.org/security/denyhosts.html

安裝:
cd /usr/ports/security/denyhosts
make all install clean


把 denyhosts_enable="YES" 加到 /etc/rc.conf 裡如下:
echo 'denyhosts_enable="YES"' >> /etc/rc.conf

把下面兩行加到 /etc/hosts.allow 裡
sshd : /etc/hosts.deniedssh : deny
sshd : ALL : allow


為了讓 denyhosts 能注意到多次嘗試登入的記錄,理論上 syslogd 應該加上 -c 的參數。
在 /etc/rc.conf 裡找到 syslogd_flags 加上 -c 參數如下:
syslogd_flags="-c"

然後編輯 /usr/local/etc/denyhosts.conf
vi /usr/local/etc/denyhosts.conf
要先設定要 block 的 service
BLOCK_SERVICE = sshd

然後開一個空檔給 denyhosts 使用
touch /etc/hosts.deniedssh

執行
/usr/local/etc/rc.d/denyhosts start

星期四, 3月 27, 2008

CSUP 筆記


SYNOPSIS

CSup (http://www.mu.org/~mux/csup.html) 是由 mux@FREEBSD.org 用 C 重寫來取代
CVSup 的一個專案,效能比 CVSup 更佳,在 FreeBSD 6.2 Release 之後的版本都附帶
CSup。


DESCRIPTION

# 如果是 6.2 Release 之前的版本要自行安裝
whereis csup
cd /usr/ports/net/csup
make all install clean

# 修改 /etc/make.conf
vi /etc/make.conf

SUPHOST= cvsup.tw.freebsd.org
SUP_UPDATE= yes
SUP= /usr/bin/csup
SUPFLAGS= -g -L 2

# ports file 修改方式,參考 cvsup 筆記
#SUPFILE= /usr/share/examples/cvsup/stable-supfile
#SUPFILE= /usr/stable-supfile
SUPFILE= /usr/standard-supfile
#PORTSSUPFILE= /usr/share/examples/cvsup/ports-supfile
PORTSSUPFILE= /usr/ports-supfile

# 更新 ports tree
cd /usr/ports
make update
make fetchindex
# 也可用傳統 cvsup 的做法:csup -g -L2 /usr/ports-supfile
# 其實 man csup 裡是不會有 -g 這個指令的,不過用了也不會怎樣,
# 可能是為了相容 cvsup 而存在的。
# 注意:如果太老舊的 ports tree 請用 make fetchindex 來更新 index
# ports index 跟 ports tree 是不一樣的

# 更新 source
cd /usr/src
make update


EXAMPLES

[make.conf]
# the csup settings
# added by Ken [2008-07-09]
SUPHOST=cvsup.tw.freebsd.org
SUP_UPDATE=yes
SUP=/usr/bin/csup
SUPFLAGS=-g -L 2
#SUPFILE=/usr/share/examples/cvsup/stable-supfile
#SUPFILE=/usr/stable-supfile
SUPFILE=/usr/standard-supfile
#PORTSSUPFILE=/usr/share/examples/cvsup/ports-supfile
PORTSSUPFILE=/usr/ports-supfile

# mirror sites for ports(distfiles) in taiwan
# refer to http://www.freebsd.org/doc/en/books/handbook/mirrors-ftp.html
# added by Ken [2008-07-09]
MASTER_SITE_BACKUP?=    \
    ftp://ftp.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp2.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp3.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp4.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp5.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp7.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp8.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp9.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp10.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp11.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp12.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp13.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp14.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/\
    ftp://ftp15.tw.freebsd.org/pub/FreeBSD/distfiles/${DIST_SUBDIR}/
MASTER_SITE_OVERRIDE?=  ${MASTER_SITE_BACKUP}
[/make.conf]


SEE ALSO

http://www.mu.org/~mux/csup.html
http://www.george.idv.tw/index.php/FreeBSD/CsupInstall
http://new-acos.blogspot.com/2007/03/csupport-tree.html

星期二, 2月 19, 2008

VIM Notes

* What is Vi

    * Vi 是一個文字編輯器 (plain text editor)
    * Vi = "Vi"sual Editor
    * Vi 是 POSIX 定義的標準編輯器,Emacs 則不是。
    * Vim = "Vi"sual editor i"m"proved,是 vi 的加強版
    * Vi 加強版有 Vim, Nvi, Elvis, Vile 等
    * Nvi 最接近 Vi,BSD 內建的 Vi 即 Nvi
    * Gvim = GUI version of Vim 是 Vim for Windows 的 Porting


* Why Vim

    * Vim 是最多人用的 Vi 加強版,功能也最多。
    * Vim 是完全免費的,是 FreeWare 也是 CharityWare(慈善軟體)。
    * 支援各式主流平台,如 Unix, BSD, Linux, DOS/Windows, Mac 等等。
    * 中文編碼支援完善。
    * 自定功能強大,也有許多現成的 Scripts 可以用。


* Where to get

    下載:
    ------------
    # 可以到以下這幾個站點 Download 各平台的 Porting。
    http://www.vim.org/download.php
    http://vim.sourceforge.net/download.php
    ftp://ftp.vim.org/pub/vim/


* Color Scheme

    設定配色:
    ------------
    :colorscheme darkblue
    # 配色檔在 /usr/share/vim/vim71/colors 下面。

    測試配色:
    ------------
    :so $VIMRUNTIME/syntax/hitest.vim


* 基本操作

    輸入模式:
    ------------
    a       在游標的前一字元進入輸入模式 (append)
    i       在游標的後一字元進入輸入模式 (insert)
    o       在游標的下一行插入新行並進入輸入模式
    O       在游標的上一行插入新行並進入輸入模式

    移動:
    ------------
    h       上
    j       下
    k       上
    l       右

    移動速記圖示:
    ------------
    +-------+
    |   k   |
    | h   l |
    |  j    |
    +-------+

    字與行移動:
    ------------
    0       移到行首
    ^       移到該行第一個非白字的的字元
    $       移到行尾
    w       移到後一字首 (word)
    e       移到後一字尾 (end of word)
    b       移到前一字首 (before word)
    Note: 移到行首行尾的 ^ 和 $ 用 Regular Expression.

    頁與檔移動:
    ------------
    Ctrl-f  向下一頁  (forward)
    Ctrl-b  向上移一頁  (back)
    1G, gg  移到檔首 (go)
    [n]G    移到第 n 行 (number go)
    G       移到檔尾
    M       移到營幕中間 (middle)
    H       移到營幕開頭 (head)

    刪除:
    ------------
    x           刪除該字元
    X           刪除該字元前一字
    D, d$       刪至行尾
    dd          刪除整行
    [n]D, d[n]d 刪除 n 行

    複製:
    ------------
    y{字元}     複製 (yank)
    yy          複製整行
    [n]Y, y[n]y 複製 n 行
    p           貼上 (put)
    :reg        查看複製暫存區 (register)

    其他:
    ------------
    u       回上一動 (undo)
    J       合併下一行 (join)
    gJ      合併下一行但不加空白

    檔案:
    ------------
    :q      結束 (quit)
    :q!     強制結束不儲存
    :x      有修改時才儲存並結束
    :w      儲存 (write)
    :wq     儲存並結束
    ZZ      等於 :q!
    ZQ      等於 :x

    C語言程式:
    ----------------
    [[      尋找上一個 function
    ]]      尋找下一個 function
    %       尋找對應的 bracer

    視窗:
    ------------
    :split [file]   分割視窗(有加檔名時開啟檔案為新視窗 )
    :new            開新檔視窗
    :close          關畢現行視窗
    :only           只留下現行視窗
    :vsplit [file]  分割為垂直視窗
    :vnew           開新檔垂直視窗
    :qall           結束全部
    :wall           寫入全部
    :wqall          寫入並結束全部
    :qall!          結束全部不詢問
    Note: vim [-o|-O] one.txt two.txt three.txt (-o 水平, -O 垂直)

    CTRL-w o        同 :only
    CTRL-w c        同 :close
    CTRL-w s        同 :split
    CTRL-w n        同 :new
    Ctrl-w w        循環切換視窗

    CTRL-W h        切換到左邊那個視窗
    CTRL-W j        切換到下面那個視窗
    CTRL-W k        切換到上面那個視窗
    CTRL-W l        切換到右邊那個視窗

    CTRL-W t        切換到第一個視窗
    CTRL-W b        切換到最後個視窗

    CTRL-W H        把現行視窗設到左邊
    CTRL-W J        把現行視窗設到下面
    Ctrl-w K        把現行視窗設到上面
    CTRL-W L        把現行視窗設到右邊

    Ctrl-w +        增大視窗(一行)
    Ctrl-w -        縮小視窗

    分頁(tab browsing):
    ------------
    :tabedit [file]
    :tab split [file]
    :tab new
    :tabo[nly]
    :tabc[lose]
    :tabn[ext]
    :tabp[revious]
    :tabfir[st]
    :tabl[ast]
    :tabr[rewind]
    :tabs
    ctrl + pageup   上一分頁
    ctrl + pagedown 下一分頁

    書籤:
    ------------
    m[a-z][A-Z]     設定書籤 (mark)
    '[a-z][A-Z]     跳到書籤

    搜尋:
    ------------
    /[regular expression pattern]   往下尋找
    ?[regular expression pattern]   往上尋找(與 / 相反)
    n       跳至下一個符合
    N       跳至上一個符合
    :set ignorecase     搜尋忽略大小寫 
    :set ic             同上
    :set noignorecase   搜尋不忽略大小寫
    :set noic           同上

    搜尋取代:
    ------------
    %s/[RE pattern]/[to replace string]/
     * 可以加 modifier 如 g 表示替換全部 (global)


* Visual Mode

    操作方式:
    ------------
    1. 按 v, V 或 Ctrl-v 進入 Visual Mode。
    2. 按 hjkl 或方向鍵標記。
    3. 按操作鍵如 y, d, v 等等...

    基本指令:
    ------------
    v       字元選取
    V       行選取
    Ctrl-v  區塊選取

    gv      最後一次選取
       結束 Visual Mode
    Ctrl-C  結束 Visual Mode

    o       到區塊開頭
    O       到區塊結尾

    ~       切換大小寫
    d, x    刪除
    r       修改 (填滿為某字)
    c       修改
    y       複製
    >       左平移(4)
    <       右平移(4)


* 排版相關:

    整行平移
    --------
    >>      整行向右移一個 shiftwidth
    <<      整行向左移一個 shiftwidth

    設定平移單位
    ------------
    * shiftwidth 簡寫為 sw,如 :set sw?
    :set shiftwidth?    得知目前的設定值
    :set shiftwidth=4   設定 shiftwidth = 4 個字元

    文字對齊
    --------
    :ce(nter) 整行以 textwidth 為基準置中
    :ri(ght) 整行以 textwidth 為基準靠右
    :le(ft)     整行以 textwidth 為基準靠左

    設定行寬
    --------
    * textwidth 簡寫為 tw,試試 :set tw?
    :set textwidth=80   設定 textwidth

    排版指令
    --------
    * gq 是只有 vim 才提供的指令
    gq          重排 visual mode 標記起來的部份
    gqq         本行重排
    gqap     整段重排(或 gqip)
    gqG         全文重排(游標所在處的段落開始重排至檔尾)

    排版指令變化形
    --------------
    gq3q   重排三行
    gq6ap  重排六個段落
    gq9j  重排九行

    中文相容
    --------
    李果正前輩(http://info.sayya.org/~edt1023/)
    有提到一個與中文相容的 macro:
    http://info.sayya.org/~edt1023/vim/format.vim


* 設定檔

    Unix:
    ~/.vimrc
    --------

    Windows:
    [GVIM安裝目錄]\_vimrc
    ---------------------

    "加強 vim 但某部份會與 vi 不相容
    set nocompatible

    "自動縮排
    "set cindent
    "set autoindent
    "set smartindent

    "顯示 bracer 配對
    set showmatch
    set foldmethod=marker

    "檔案編碼
    set fileencodings=utf-8,big5
    set fileencoding=big5

    "終端機編碼
    if has("gui_running")
      set termencoding=utf-8
    else
      set termencoding=big5
    endif

    "換行字元
    set fileformats=unix,dos
    set fileformat=unix

    "行號
    set number
    set numberwidth=3

    "尺標
    set ruler

    "備份
    set history=100
    "set backup
    "set backupdir=~/.bak
    "set backupdir=D:\bak

    "tab 定位鍵設定,每次跳四格,
    set softtabstop=4
    set shiftwidth=4
    set tabstop=4

    "空白取代tab碼
    set expandtab

    "開啟語法高亮
    syntax on
    "搜尋關鍵字高亮
    set hlsearch
    "搜尋不分大小寫
    set ignorecase

    "配色方案
    " blue darkblue default delek desert elflord evening koehler morning
    " murphy pablo peachpuff ron shine slate torte zellner
    colorscheme darkblue

    "狀態列
    set statusline=%F%m%r%h%w\ [%{&ff}]\ [%Y]\ LEN[%L]\ [%3p%%]\ LOC[%l,%v]%=ASC[%03b]\ HEX[%02B]
    set laststatus=2

    "顯示游標行
    set cursorline
    highlight CursorLine guibg=#000033 ctermbg=black

    "色彩設定
    highlight Comment guifg=cyan ctermfg=cyan
    highlight Search term=reverse guibg=darkmagenta guifg=white ctermbg=darkmagenta ctermfg=white
    highlight Normal guibg=black guifg=gray ctermbg=black ctermfg=gray
    highlight Visual gui=reverse guifg=blue guibg=white
    highlight VisualNOS gui=reverse,underline guifg=blue guibg=white

    ""_gvimrc (以下設定為 Windows 版本專用)
    ""------------
    "啟動後最大化視窗
    set lines=999999
    set columns=999999

    "字型 font=Dina, size=10 (Dina 是自己抓的字型檔)
    "set guifont=Dina:h10

    "字型 font=Courier New, size=11
    "set guifont=Courier_New:h11 "版本 7.1 以後的空白改成 underline
    "set guifont=Courier New:h11 "for Version 7.1

    "字型 font=Consolas, size=12
    set guifont=Consolas:h12

    "tab browsing 設定
    set tabpagemax=15
    "set showtabline=2

    "修改 key-mapping 讓 vim7 的 tab browsing 功能更好用
    "map  -> normal mode
    "imap -> insert mode
    "vmap -> visual mode
    "lmap -> line mode

    "VIM7 預設是  
    "仿 UltraEdit 可設成  
    "用終端機跑 VIM 可以設成  
    "Ctrl-DOWN goes to next tab
    map <c-down> :tabnext
    imap <c-down> :tabnext
    vmap <c-down> :tabnext 

    "Ctrl-UP goes to previous tab
    imap <c-up> :tabprev
    map <c-up> :tabprev
    vmap <c-up> :tabprev

    "Ctrl-t makes a new tab
    " Ctrl-t 原本是用來跳回標簽的
    "vmap <c-t> :tabnew
    "imap <c-t> :tabnew
    "map <c-t> :tabnew 

    "Ctrl-x closes the tab
    map <c-x> :tabclose
    imap <c-x> :tabclose
    vmap <c-x> :tabclose

* 小技巧

    tab 很好用,但是每次要手工打開 tab 很麻煩,使用下面的命令就可以在當前 vim 裡開啟新的 tab。
    "C:\Program Files\Vim\vim71\gvim.exe" -p --remote-tab-silent "%1" "%*"

    Total Commander 的 external editor 設定:
    C:\Program Files\Vim\vim71\gvim.exe -p --remote-tab-silent
    
    如果一次開啟多個檔案如 vim a.c b.c 時,會開成 buffer,可用 :tab ball 轉成 tabs。

    搜尋忽略大小寫的方法:
    :set ignorecase 或 :set ic
    不忽略大小寫:
    :set noignorecase 或 :set noic

    在 terminal 中輸入 ^M 的方法:
    如把 DOS 的 CR(0x0d) 移除的指令 :%s/^M//g
    這個 ^M 按 ctrl+v ctrl+m
    其實也可以用 :%s/\r//g 

星期一, 9月 10, 2007

編碼轉換工具 iconv, convmv, locale... Notes

編碼轉換工具 iconv, convmv, locale... Notes

* Overview

    * locale .. 顯示與編碼有關的環境變數設置
    * iconv ... 轉換檔案內容的編碼
    * convmv .. 轉換檔案名稱的編碼


* locale -- 顯示與編碼有關的環境變數設置

    # 範例:顯示可以設置的編碼
    shell# locale
    shell# locale -m
    shell# locale -a | grep zh_TW


* iconv -- 轉換檔案內容的編碼

    # 範例:將 gbk 轉成 big5
    shell# iconv -f cp936 -t cp950 [filename]

    # 常用參數
    -l      顯示支援的編碼
    -c      碰到無法轉換的字會自動跳過,不顯示錯誤訊息
    -s      安靜模式 (silent)


* convmv -- 轉換檔案名稱的編碼

    * convmv 是使用 perl 寫成
    * 為了避免失誤,在未加上 --notest 參數時是測試模式,不會真正轉換檔名
    * 常見微軟中文編碼為 cp950(big5), cp936(gbk)
    * 轉檔功能還是不如 convertz 好用
    * 可以轉換 %xx%xx 之類的 unicode 編碼 (convertz 無法轉)

    # 範例:將 gbk 轉成 big5
    shell# convmv -f cp936 -t cp950 [filename] // gb to big5
    shell# convmv -f cp936 -t cp950 --notest [filename]
    shell# convmv -unescape [filename] // 轉換 %xx%xx 類型的 unicode 編碼

    # 基本參數
    -f [fliename]   (from) 來源編碼
    -t [filename]   (to) 目的編碼
    -r              (recursively) 轉換包含所有子目錄的檔案
    -i              (interactiv) 互動式操作,會逐檔詢問

    # 常用參數
    --list          列出所有支援的編碼
    --unescape     可以移除掉像 %20 的 % Hex Sequences
    --notest       直接重新命名
    --nosmart      強制轉換,不偵測檔名本身是否已經是目的編碼
    --exec [cmd]    另外執行 [cmd] 指令
    --replace       如果目的檔名已經存在,將會覆寫目的檔

Unix 指令 find notes

語法:

    find [搜尋路徑] -name "[檔名]" [選項參數]


常用選項:

    ----------------------------    ----------------------------
    -name [filename]                找出符合 [filename] 的檔案
    -exec [command] {}\;            執行 [command]
    -type [d] or [f]                找目錄或是檔案
    -print                          每個檔案以 \n 為間隔, 預設
    -print0                         連續輸出檔案 (符合 xargs -0)


範例:

1. 在 /usr 找 mysql_install_db* :

    find /usr -name "mysql_install_db*" -print

2. 設定目前所有的子目錄權限 0777 而所有檔案的權限 0664:

    find ./ -type d -exec chmod 777 {}\;
    find ./ -type f -exec chmod 664 {}\;

3. 在 /home 裡找出所有 *.cmd 並刪除

    find /home -name "*.cmd" -exec rm {}\;
    find /home -name "*.cmd" | xargs rm

4. 下面這兩種方法都可以找出目錄下 .c 檔中有 'modprobe' 的行:

    find . -name "*.c" | xargs grep modprobe
    grep -r --include="*.c" modprobe .


Tips:

    * -exec 必須由一個 ; 結束,而通常 shell 都會對 ; 進行處理,所以用 \; 防止這種情況。
    * {} 可能需要寫做 '{}',也是為了避免被 shell 過濾。
    * find 的 -exec 指令,在檔案過多時會出現參數太多的 error,最好用 xargs。
    * -name 選項支援 shell 的 wildcard 如 * ? 等,相對來說, shell 保留字如 [ 與 ] 等也需要用 \ 來跳脫。

星期六, 7月 07, 2007

Bash 筆記

Bash Notes

* Bash & Tcsh 差異對照表:


比較項目
Bash
Tcsh
設定環境變數
export var=value
setenv var value
設定變數
var=value
set var=value
代名 alias
alias var=value
alias var value
提示符變數名
PS1
prompt
共用設定檔
/etc/profile
/etc/csh.cshrc
個人設定檔
~/.profile
~/.cshrc
一般用戶提示符
$
%
顯示何種 shell
echo $SHELL
echo $shell
優點
shell script模組化
history 機制較佳


* "Bash" stands for "Bourne Again Shell"

# Bash 在許多 Linux Distribution 被當成預設的 Shell,
# 設定與指令與 tcsh/csh 有點出入,如環境變數。


* Bash 設定檔:

# Bash 在 Login 後如為預設 Shell 時,依照下列順序讀取設定檔:
1. /etc/profile
1a. /etc/bashrc (Red-Hat Linux Only)
2. ~/.bash_profile
2a. ~/.bashrc (Red-Hat Linux Only)
3. ~/.bash_login
4. ~/.profile

# Bash 在 Logout 後會讀取:
~/.bash_logout

# Bash 不是登入時的 Shell (由其他 Shell 啟動)會讀取:
~/.bashrc


* 設定較佳的 ll 指令

# 各 user 自訂
echo "alias ll='ls -alF'" >> ~/.bash_profile
echo "alias ll='ls -alF'" >> ~/.bashrc

# 所有使用 bash 當預設 shell 者
echo "alias ll='ls -alF'" >> /etc/profile
echo "alias ll='ls -alF'" >> /etc/bashrc

# 記得用 source /etc/profile 來生效


* 設定較佳的 prompt

# 我只有把預設的 \W 改成小寫 \w 來顯示完整路徑名。
# 彩色可參考:http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37

# 單色
echo 'PS1="[\u@\h \w]\\$ "' >> /etc/bashrc

# 彩色
echo ' PS1="[\[\033[36m\]\u\[\033[1;34m\]@\h \[\033[m\]\w]\[\033[35m\]\\$\[\033[m\] "' >> /etc/bashrc

# 如果用 xterm 相容終端機就顯示彩色 Prompt
# 在 /etc/bashrc 或 /etc/profile 中加上
    if [ "$TERM" = "xterm" ]; then
      PS1="[\[\033[36m\]\u\[\033[32m\]@\h \[\033[m\]\w]\[\033[35m\]\\$\[\033[m\] "
    elif [ "$TERM" = "linux" ]; then
      PS1="[\[\033[36m\]\u\[\033[32m\]@\h \[\033[m\]\w]\[\033[35m\]\\$\[\033[m\] "
    else
      PS1="[\u@\h \w]\\$ "
    fi

source /etc/bashrc

* 關掉 beep

# 修改 /etc/inputrc 加入或把此行註解移除:
set bell-style none


* 讓按上下方向鍵的行為與 tcsh 相同

csh/tcsh 最讓人稱道的就是 history 的功能,這也是 bash 不如 csh 的地方,當然
現在的 bash 在 history 機制上也偷學了不少 csh 的功能並加強了,有用過 csh 的
人都知道 csh 有一個很好用的機制,就是可以用上下鍵來尋找最近輸入過以某幾個字
開頭的指令,如我們之前有用過 vi /etc/profile 與 vi /etc/bashrc,想叫出來時
只要按 vi 然後按上下鍵就可以快速的找到。
最近改用 bash 之後,最不習慣的就是沒有這樣功能,查了半天 bash manual 之後終
於發現 bash 其實有提供這個功能,只是預設並沒有配置任何按鍵給這兩個功能,(謎
之音:怕被人說抄襲嗎?)
我們可以自己在 /etc/inputrc 裡讓上下鍵對應到 history-search-backward 與
history-search-forward 這兩個指令來讓上下鍵功能跟 csh 一樣好用:

編輯 /etc/inputrc 加上下面三行:
# up key & down key = history search
"\e[A": history-search-backward
"\e[B": history-search-forward

星期一, 5月 14, 2007

FreeBSD via Microsoft Virtual PC 2007 timecounter error Solution

FreeBSD 6.2 安裝在 MS Virtual PC 上如果會看到如下錯誤訊息:

kernel: calcru: negative runtime of -484144 usec for pid 26954 (vim)

Solution:

#Step 1. check timecounter hardware: (could be: kern.timecounter.hardware: ACPI -safe)
sysctl -a | grep timecount

#Step 2. disable ACPI to use TSC
vi /boot/device.hints
hint.acpi.0.disabled="1"

星期四, 2月 15, 2007

How-to Log Cisco Router/Switch with FreeBSD

How-to Log Cisco Router/Switch with FreeBSD

一般來說,安裝完 FreeBSD 之後系統就會有 syslog daemon,不過預設的那個 syslog 功能蠻陽春的,所以我們改用 syslog-ng 來替換原來的 syslog。

Step.1:
在 FreeBSD 上用 ports 安裝 syslog-ng:
cd /usr/ports/sysutils/syslog-ng
make install clean
修改 rc.conf 置換預設的 syslogd:

syslogd_enable="NO"
syslog_ng_enable="YES"
Step.2:
設定 syslog-ng:
cd /usr/local/etc/syslog-ng
cp syslog-ng.conf.sample syslog-ng.conf
vi syslog-ng.conf
基本上前面那堆設定都不用去改,在最後面加上如下設定:
(這個範例是兩台機器,一台叫 core 1 是在 192.168.1.1,另一台叫 core 2 在 192.168.1.2,這個設定是在真正運作中的設定取出兩台當範例的,完整的設定實際上是記錄十幾台機器)
### Cisco Routers, Switches ###

# core 1
destination core1.tpe { file("/var/log/cisco/core1.log"); };
filter f_core1.tpe { host("^192.168.1.1$"); };
log { source(src); filter(f_local7); filter(f_core1.tpe); destination(core1.tpe); };

# core 2
destination core2.tpe { file("/var/log/cisco/core2.log"); };
filter f_core2.tpe { host("^192.168.1.2$"); };
log { source(src); filter(f_local7); filter(f_core2.tpe); destination(core2.tpe); };
然後 touch 空檔案來做記錄,如此例中請用:
touch /var/log/cisco/core1.log
touch /var/log/cisco/core2.log
Step.3:
然後到 Cisco 的 Router 與 Switch 裡設定:
假設 log server 主機在 192.168.2.1 裡,你還必須要有 lv15 的權限:
conf t
logging 192.168.2.1
logging on
end
exit
這樣就可以了。

如果要知道更多詳細的設定請參考:
http://www.cisco.com/univercd/cc/td/doc/product/software/ios11/dbook/dintro.htm
http://www.gentoo.tw/got-doc/syslog-ng.xml
http://www.campin.net/syslog-ng/faq.html

星期三, 1月 03, 2007

CSH/TCSH @ FreeBSD 簡易設置

vi /etc/csh.cshrc
# 設置中文 big5 編碼環境
setenv LANG zh_TW.Big5
setenv LC_CTYPE zh_TW.Big5
set dspmbyte="big5"

# 彩色 ls
alias ls ls -G
setenv LSCOLORS "ExGxCxDxCxgedaFaFaBadx"
alias ll ls -alF

# 較佳的提示符號
set prompt = "%B[%n@%m]%/\%%b"

# 檔案名稱自動完成
set filec
# 不產生嗶聲
set nobeep
# 按 tab 時自動列示檔案 (同 ctrl + d)
set autolist
# 指令自動校正
set correct = all
# 直接輸入目錄名稱切換目錄
set implicitcd
Note:
prompt 在 su 為 root 時會失效,這是因為 /root 的 .cshrc 會判斷身份,當 root 時提示號為 #,所以會將 prompt 重置,這時候只要將 /root 下的 .cshrc 裡的 prompt 修改為 "%B[%n@%m]%/#%b" 即可。

星期二, 12月 12, 2006

Sentry Tools 防止不特定人士掃瞄伺服器

Sentry Tools 有兩個工具分別為 Portsentry 與 Logcheck,Portsentry 可用來防止未經授權的 port 掃瞄,Logcheck 則是用來分析系統記錄以用來判斷是否有人意圖入侵,更多的資訊請參考 Portsentry 官網:http://sourceforge.net/projects/sentrytools/

Step 1. 依照慣例使用懶人安裝法來安裝 Portsentry:
cd /usr/ports/security/portsentry
make all install clean
Step 2. 複製設定檔:
cd /usr/local/etc
cp portsentry.conf.default portsentry.conf
cp portsentry.ignore.default portsentry.ignore
chmod 600 portsentry.conf portsentry.ignore
Step 3. 修改 portsentry.conf 內容:
# 不要存取 DNS 增加效率,把預設的 1 改為 0。
RESOLVE_HOST = "0"

# 預設就是 1,有些人會很雞婆的改成 2 送個 message,建議不用這麼無聊。
BLOCK_UDP="1"
BLOCK_TCP="1"

# 找到 FreeBSD 的 KILL_ROUTE 組擋相關設定,把這行的 remark 拿掉。
# KILL_ROUTE 只能有一個,另一個是重設路由的(會讓對方網路斷線)。
KILL_ROUTE="/sbin/ipfw add 1 deny all from $TARGET$:255.255.255.255 to any"

# 敏感度預設為 0,如果誤判很嚴重再調成 1 或 2。
SCAN_TRIGGER="0"
Step 4. 修改 /etc/rc.conf 啟動 ipfw (防火牆):
#firewall
firewall_enable="YES"
firewall_type="open"
firewall_logging="YES"
Step 5. 修改 /etc/rc.firewall:

在 OPEN 那個 section 把:
${fwcmd} add 65000 pass all from any to any
改成:
${fwcmd} add 65534 pass all from any to any
NOTE: FreeBSD 6.1 Release 的防火牆有一個 bug,就是設為 open 時會 deny all,因為 allow all 那個編號預設為 65000 有問題,把那個編號改掉就可以了。

FreeBSD 上的 TACACS+ Server for Cisco NAS

術語解釋:
TACACS --- Terminal Access Controller Access Control System 終端存取控制器存取控制系統。
AAA ---
Authentication、Authorization、Accounting 認證、授權、記帳。
NAS --- Network Access Server,會向 TACACS+ 要求認證(AAA)需求的『客戶端』伺服器,如 CISCO Router、Switch、Server 等等。
daemon -- 提供 TACACS+ AAA 服務的行程。
passwd(5) files -- Unix 密碼格式檔案,請參照 Unix 手冊中的文件第5段。
AV pairs -- 在 NAS 和 tacacs+ daemon 之間傳遞格式如
"attribute=value" 的一組文字,是 Tacacs+ 傳輸協定的一部份。

因為要求 Tacacs+ AAA 的機器本身很有可能也是伺服器,為了避免混淆,所以我們不用 Server 來稱呼提供 Tacacs+ 服務的機器,而是用 Daemon(Server) 與 NAS(Client) 來表示 Server/Client。

TACACS 介紹:
目前一般人提及的 TACACS 認證協定至少有三種版本。第一種就是傳統的 TACACS,最先是在 Cisco 的設備上使用多年的。第二種叫做 XTACACS 則是在 1990 發展出來,是 TACACS 的擴展版本。第三種則是 TACACS+,也是目前通用的版本,但是這個版本和舊版本的 TACACS 是完全不相容的。

tac_plus:
tac_plus 是一個 freeware,可以在大多數的類 Unix 的環境上找到,tac_plus 可以使用內建的資料庫、s/key 或 passwd(5) 格式的檔案,除此之外也可以透過 LDAP 或 RADIUS 由外部取得認證。

Step.1
在 FreeBSD 上,我們可以直接使用 ports 來安裝:
cd /usr/ports/net/tac_plus4
make install clean
Step.2
將 /usr/local/etc/tac_plus.conf.example 複製一份來修改:
cd /usr/local/etc
cp tac_plus.conf.example tac_plus.conf
vi tac_plus.conf
Step.3
tac_plus.conf 的內容:
#自定義用來跟 router 溝通的 key
key = "I think therefor I am"

# log 檔路徑
accounting file = /var/log/tac.log

user = irene {
login = des H9rhDCGdkjVG2
service = exec {
priv-lvl = 15
}
}

user = daisy {
login = cleartext "lollipop%@)"
service = exec {
priv-lvl = 1
}
}

user = $enab15$ {
login = des $1$tBwWeG3L$viCdJZu4gLw7zQBPOysGu0
}
NOTE: 密碼可以使用明文(plain text)或是 des 編碼,des 編碼可以用 tac_pwd -e 來產生。

Step.4
到 Cisco Router/Switch 上設定:
測試過的型號: GSR 12012、GSR 12008、7204 VXR、C2950。
IOS 版本:Version 12.0(21)S7, EARLY DEPLOYMENT RELEASE SOFTWARE (fc2)
tacacs-server host 192.168.1.100
tacacs-server key I think therefor I am
aaa new-model
aaa authentication login default tacacs+ local
aaa authorization exec default tacacs+ local
aaa accounting exec default start-stop tacacs+
aaa accounting commands 15 default stop-only tacacs+
aaa accounting network default start-stop tacacs+
aaa accounting connection default start-stop tacacs+
aaa accounting system default start-stop tacacs+
其他資訊請參考 Cisco 官網: http://www.cisco.com/warp/public/480/tacplus.shtml