2010年11月19日 星期五

雲端主機(二)--再加一台linux為node2



h2準備:
1.新增user: hadoop,並設定金鑰登入
# useradd hadoop
# su - hadoop
$ ssh-keygen
//然後將自己的public_key丟給server並放到authorized_keys中
//取回server上hadoop的key放到authorized_keys中
//完成後互相ssh看是否正常

2.修改/etc/hosts    //讓兩邊一致,用ping測試
140.137.215.238 a238
192.168.188.10 h2

3.安裝sun-jdk
//下載tar檔
# sh jdk-6u22-linux-i586-rpm.bin
................

4.修改a238上的XML檔案:
$ vim core-site.xml







$ vim mapred-site.xml






5直接將a238上的hadoop 拷貝過來
# scp -r a238:~/hadoop-0.21.0 .
//記得改過server上的XML後再拷貝

//將a238與h2都啟動
$ cd hadoop-0.21.0
$bin/start-all.sh

測試:
用browser看http://a238:50070
應該會看到兩個node

架設雲端主機--hadoop(單機測試)


相關檔案:
jdk-6u22-linux-i586-rpm.bin    //hadoop需要昇陽版本的jdk
hadoop-0.21.0.tar.gz     

環境準備:
1.檢查java環境   //安裝時不要選取java...光碟中的是openjava
# java -version
or
# rpm -qa |grep java/jdk

//如果真的裝了兩三套以上的java..可用以下指令切換實際運作的是哪一套
# alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_22/bin/java 1
//指令格式:   alternatives --install <連結> <名稱> <路徑> <優先順序>
# alternatives --config java     //這邊的java就是上面的名稱
有 2 程式提供 'java'。

  選擇        指令
-----------------------------------------------
   1           /usr/java/jdk1.6.0_22/bin/java
*+ 2           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java

請輸入以保留目前的選擇[+],或輸入選擇號碼:1
//+表示目前設定的是哪一套java
//輸入數字選擇想要的版本

2.建立帳號hadoop並設定不須密碼登入
# useradd hadoop
# su - hadoop
$ ssh-keygen
$ cd .ssh
$ cat id_rsa.pub > authorized_keys    //將公鑰內容放到authorized_keys中
//這個範例因為是第一次做可用">",安全應為">>"
$ chmod 600 authorized_keys    //要修改權限才可以用

3.將hadoop下載到hadoop家目錄並解壓縮
# su - hadoop     //用user: hadoop來執行即可
$ wget wget‭ ‬http‭://‬ftp.tcc.edu.tw/pub/Apache‭//‬hadoop/core/hadoop-0.21.0‭/‬hadoop-0.21.0‭.‬tar.gz
$ tar zxvf hadoop-0.21.0‭.‬tar.gz

4.hadoop環境設定:
$ cd hadoop-0.21.0
$ vim conf/hadoop-env.sh
export JAVA_HOME=/usr/java/jdk1.6.0_22/
//將這行的註解取消,並根據自己的java版本給予正確目錄

$ tree -L 1    //檢視hadoop-0.21.0目錄內容
.
|-- LICENSE.txt
|-- NOTICE.txt
|-- README.txt
|-- bin           //放置執行檔的目錄,會根據conf的設定
|-- c++
|-- common
|-- conf            //放置設定檔的目錄
|-- hadoop-common-0.21.0.jar
|-- hadoop-common-test-0.21.0.jar
|-- hadoop-hdfs-0.21.0-sources.jar
|-- hadoop-hdfs-0.21.0.jar
|-- hadoop-hdfs-ant-0.21.0.jar
|-- hadoop-hdfs-test-0.21.0-sources.jar
|-- hadoop-hdfs-test-0.21.0.jar
|-- hadoop-mapred-0.21.0-sources.jar
|-- hadoop-mapred-0.21.0.jar
|-- hadoop-mapred-examples-0.21.0.jar
|-- hadoop-mapred-test-0.21.0.jar
|-- hadoop-mapred-tools-0.21.0.jar         //以上為包裝好的java服務程式
|-- hdfs                //hadoop節點間共用的檔案系統目錄
|-- lib
|-- mapred
`-- webapps

$bin/hadoop     //如果可以看到下面訊息代表安裝正常
Usage: hadoop [--config confdir] COMMAND
       where COMMAND is one of:
  fs                   run a generic filesystem user client
  version              print the version
  jar             run a jar file
  distcp copy file or directories recursively
  archive -archiveName NAME -p * create a hadoop archive
  classpath            prints the class path needed to get the
                       Hadoop jar and the required libraries
  daemonlog            get/set the log level for each daemon
 or
  CLASSNAME            run the class named CLASSNAME

Most commands print help when invoked w/o parameters.

//實作計算pi值
$ bin/hadoop jar hadoop-mapred-examples-0.21.0.jar pi 100 100000     //計算pi的程式放在前面的.jar中
...............
Job Finished in 11.183 seconds
Estimated value of Pi is 3.14158440000000000000
//計算結果
5.格式化hdfs
$ export HADOOP_HOME=/home/hadoop/hadoop-0.21.0    //可寫入.bashrc讓登入時自動export
$ bin/hdfs namenode -format    
//在執行訊息中可以看到將檔案做在/tmp中
//10/11/19 15:05:54 INFO common.Storage: Storage directory /tmp/hadoop-hadoop/dfs/name has been successfully formatted.
$ ls /tmp/hadoop-hadoop      //有東西就好,請勿變動他

6.設定XML conf檔
$vim conf/core-site.xml

//由於部落格沒辦法使用<故改為圖片











$ vim conf/hdfs-site.xml












$ vim conf/mapred-site.xml











啟動hadoop:
$ bin/start-all.sh     //如果ssh金鑰登入有問題這邊會出現要打密碼

//可再拿pi那個範例來練習玩看看

檢視:
用browser:
1.http://lcaolhost:50070    //可看到namenode資訊
2.http://localhost:50030     //可看到job tracker資訊
3.http://localhost:50090     //可看到SecondNameNode資訊


可參考教學網站: 
1.http://www.inside.com.tw/11/04/cloud-diy1
2.建置到三朵雲

Transparent Proxy Server

Transparent Proxy 即不須user設定但會自動使用proxy上網(user端無感) 



環境設定:
1.Server端:
服務:
NAT
DHCP
Proxy

網路設定:
eth0:140.137.215.238/24  GW:140.137.215.254    //對外ip
eth1:192.168.188.254/24    //內部GW

2.Client:
網路設定DHCP

Server設定:
1.NAT:
# sysctl -w net.ipv4.ip_forward = 1
# sysctl -p
# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.188.0/24 -j MASQUERADE
//表示由192.168.188.0/24來目的地eth0的封包偽裝成eth0的ip封包
//記得寫入rc.local,或建立script設定成開機執行

2.DHCP:
# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf   //拷貝範例檔
# vim /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

subnet 192.168.188.0 netmask 255.255.255.0 {

# --- default gateway
        option routers                  192.168.188.254;
        option subnet-mask              255.255.255.0;

        option nis-domain               "domain.org";
        option domain-name              "domain.org";
        option domain-name-servers      168.95.1.1;

        option time-offset              -18000; # Eastern Standard Time

        range dynamic-bootp 192.168.188.100 192.168.188.200;
        default-lease-time 21600;
        max-lease-time 43200;

        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}
//詳細設定介紹請參考鳥哥或我的另一篇文章

#/etc/init.d/dhcpd restart

3.squid
# vim /etc/squid/squid.conf
http_port 3128 transparent  //v.2.6 後的版本加這段即可
acl our_networks src 192.168.188.0/24
http_access allow our_networks
//在正確的位置放上這兩行

# /etc/init.d/squid restart

4.再加一條NAT規則,將使用者的80port導向squid server
# iptables -t nat -A PREROUTING -i eth1 -s ! 192.168.188.254 -p tcp --dport 80 -j REDIRECT --to-port 3128
//將192.168.188.0/24網段過來的網頁連線導向到3128port
//-s ! ip 表示除此ip以外,本例表示排除本機eth1的ip

測試:
用Client端dhcp並且不設定proxy上網,在server端/var/log/squid/store.log應該會增加很多資料

2010年11月18日 星期四

Linux期末考

請安裝一個CentOS(vm),並符合以下需求:
  1. 檔案系統採LVM方式,VG名稱改為mainvg,並具有以下LV配置:
  LV名稱   掛載點   大小
  lv_root  /   10GB
  lv_home  /home  2GB
  lv_var    /var  5GB
 
  2. 網路卡採Bridged橋接方式,IP位址為固定(140.137.215.2xx)
  3. 主機名稱為a2xx.snpy.org(以學號方式命名xx)
  4. 安裝軟體需要勾選Server後,再加入以下自訂軟體:
    開發函式庫,MySQL伺服器,管理工具,系統工具,叢集與叢集儲存
  5. 建立一使用者tom,群組為wheel,密碼為ump392a,並加入無密碼可以sudo。
  6. 設定開機自動啟動服務:httpd, mysqld, named, nfs, saslauthd, smb, ypbind

安裝設定略過不談.....
4.如果有忘記勾選安裝的套件:
# yum grouplist      //找到要裝的套件群組名稱
#yum groupinstall "群組名稱" "群組2" ....    //建議使用英文

5.建立使用者:
# useradd -g wheel tom
# passwd tom
#visudo    //底下這行註解取消
%wheel        ALL=(ALL)       NOPASSWD: ALL

6.設定開機啟動服務
# chkconfig 服務名稱 --list     //檢查設定狀態
# chkconfig 服務名稱 on/off    //設定開機起停,--level 3,5    在rc3 與 5的起停設定
設定E-mail伺服器,並符合以下需求:
  1. 禁止使用一般SMTP送信(由來源IP或網域),改採SMTP帳號與密碼驗證方式。
  2. 能夠寄送與接收以下網域的信件:
    a2xx.snpy.org
    mail.a2xx.snpy.org
  3. 使用POP3協定供使用者收信。
  挑戰題:每個帳號信箱未讀的信件最多只能使用5MB的空間。


1.安裝postfix與sasl
# yum install postfix cyrus-sasl cyrus-sasl-lib cyrus-sasl-md5 cyrus-sasl-plain

2.確認sendmail狀態:
#/etc/init.d/sendmail status   //若有啟動記得停止,並設定開機不啟動
#netstat -naplt    //檢查25port有無被占用

3.設定postfix
# vim /etc/postfix/main.cf

myhostname = a238.snpy.org
mydomain = a238.snpy.org
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, mail.$myhostname
smtpd_sasl_auth_enable = yes      //啟動sasl相關模組功能
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous

4.設定DNS:
# cd /var/named/chroot/
# cp /usr/share/doc/bind-9.3.6/sample/* . -rf
# /usr/sbin/dns-keygen
XXXXXXXXXXXXXXXXXXXXXXXXXX      //將產生的值拷貝到應放的位置
#vim etc/nmaed.conf
view "localhost_resolver"
{
        include "/etc/named.rfc1912.zones";      //localhiost的zone檔案放置區,將a238...放到檔案裏面
};
view "internal"
{
        zone "a238.snpy.org" {
                type master;
                file "named.a238";
        };
};
key ddns_key
{
        algorithm hmac-md5;
        secret "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
};
view    "external"
{
        zone "a238.snpy.org" {
                type master;
                file "named.a238";
        };
};

# cp var/named/localhost.zone var/named/named.a238       //都在chroot情況下做
# vim var/named/named.a238
$TTL    86400
@               IN SOA  @       root (
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum

                IN NS           @
                IN A            140.137.215.238
mail            IN A            140.137.215.238
@               IN MX 10        @
mail            IN MX 10        mail

# /etc/init.d/named restart
# dig mail.a238.snpy.org MX @localhost
# dig a238.snpy.org MX @localhost
//上面兩個都要正常解析出MX紀錄

#/etc/init.d/postfix resatrt    //在outlook上設定好應該就可以收發信了

5.最多5mb---利用quota來做,下面samba也要就一起處理
#vim /etc/fstab
/dev/mainvg/lv_root     /                       ext3    defaults,usrquota       1 1
/dev/mainvg/lv_home     /home                   ext3    defaults        1 2
/dev/mainvg/lv_var      /var                    ext3    defaults,usrquota        1 2

#mount -a    //根據fstab重新掛載
#mount       //檢查一下
/dev/mapper/mainvg-lv_root on / type ext3 (rw,usrquota)
/dev/mapper/mainvg-lv_var on /var type ext3 (rw,usrquota)
#quotacheck -avgu         //產生quota檔案
#quotaon -avug       //啟動quota
#edquota tom
Disk quotas for user tom (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/mainvg-lv_root      10240       9000      10240          2        0        0
  /dev/mapper/mainvg-lv_var          4       4096       5120          1        0        0

#edquota -p tom -u 使用者    //以tom的設定為範本設定其他人

**找到postfix上設定信件大小的設定**
# 信箱大小限制,單位 bytes,0 為不限。
mailbox_size_limit = 0
# 單封信件大小限制,單位 bytes
message_size_limit = 5120000
設定Samba伺服器,提供Windows以網路芳鄰存取檔案,並符合以下需求:
  1. Samba主機資源分享的名稱為SAMBA3(設定值: server string)
  2. 使用者使用帳號/密碼才能登入Samba主機
  3. 登入成功後可看到兩個目錄,一個是使用者名稱(代表家目錄,可讀寫),另一個是分享區,名稱為public_share(可讀寫)
  4. 每個人在public_share分享區中存放的檔案不得超過10MB。


1.編輯設定檔
# vim /etc/samba/smb.conf
server string = SAMBA3
security = user

[public_share]
        comment = public_share
        path = /opt/public_share
        public = yes
        writable = yes
        browseable = yes

2.設定密碼:
#smbpasswd -a tom     //新增iom的samba密碼

3.建立目錄:
#mdir /opt/public_share
#chmod 777 /opt/public_share    //根據題意給予權限

4.測試:
# testparm    //測試有無打錯字
#/etc/init.d/smb   restart

//可用windows連網芳看是否正常
請建置NIS伺服器,並符合以下需求:
  1. 設定前請先建立使用者eddie,UID為1100,群組為rd,GID為3000。
  2. NIS網域名稱為a2xx。
  3. 以NFS提供/home/eddie/目錄供140.137.215.0/24的電腦掛載,並設定為可讀寫,並確認在其他NIS用戶端以eddie成功登入後可進入掛載的家目錄。
  4. NIS的帳號資料需包含使用者tom與eddie。

1.安裝server套件
# yum install ypbind ypserv yp-tools

2.增加eddie
# groupadd -g 3000 rd
#useradd -u 1100 -g rd eddie     //記得設定passwd

3.設定nis server
# nisdomainname a238
# vim /etc/sysconfig/network    //加入
NISDOMAIN=a238

#vim /etc/hosts     //檢查a238可否解析,應該有下面一行
140.137.215.238         a238.snpy.org a238

#vim /etc/ypserv.conf      //將最後一行註解與空白取消
*                        : *       : *                : none

#/etc/init.d/ypserv restart      //啟動服務
# /etc/init.d/yppasswdd restart       //啟動密碼服務

# rpcinfo -p     //確認是否正常啟動
    100004    2   udp    716  ypserv
    100004    1   udp    716  ypserv
    100004    2   tcp    719  ypserv
    100004    1   tcp    719  ypserv
    100009    1   udp    728  yppasswdd

# /usr/lib/yp/ypinit -m    //建立nis map檔(資料庫格式),將server帳號資料放到/var/yp/marcus 下
                                 //做完後帳號有修改要重做一次,yppasswd可以改密碼
At this point, we have to construct a list of the hosts which will run NIS
servers.  a238.snpy.org is in the list of NIS server hosts.  Please continue to add
the names for the other hosts, one per line.  When you are done with the
list, type a <control D>.
        next host to add:  a238.snpy.org
        next host to add:  a238
        next host to add:          //用ctrl+D來離開
The current list of NIS servers looks like this:

a238.snpy.org
master.marcus

Is this correct?  [y/n: y]  y
We need a few minutes to build the databases...
Building /var/yp/marcus/ypservers...
Running /var/yp/Makefile...
gmake[1]: Entering directory `/var/yp/marcus'
Updating passwd.byname...
Updating passwd.byuid...
Updating group.byname...
Updating group.bygid...
Updating hosts.byname...
Updating hosts.byaddr...
Updating rpc.byname...
Updating rpc.bynumber...
Updating services.byname...
Updating services.byservicename...
Updating netid.byname...
Updating protocols.bynumber...
Updating protocols.byname...
Updating mail.aliases...
gmake[1]: Leaving directory `/var/yp/marcus'

a238.snpy.org has been set up as a NIS master server.

Now you can run ypinit -s a238.snpy.org on all slave server.
# ypcat -h localhost passwd.byname    //可檢查是否正常運作,會show出帳號資料

4.nfs
# vim /etc/exports
/home/eddie     140.137.215.0/24(rw,sync)
#exportfs   //也可以重開服務

啟用sudo功能

在RHEL與CentOS上啟用sudo

1.檢查是否有wheel群組
# cat /etc/group  |grep wgeel

2.開啟wheel使用sudo權限
# visudo
# %wheel        ALL=(ALL)       ALL

# %wheel        ALL=(ALL)       NOPASSWD: ALL

//以上兩行選擇需要的情況解除註解
//第一行為需要輸入密碼

3.將使用者加入wheel群組
# usermod -G wheel marcus    //要加入的帳號

4.如果要像ubuntu一樣不能用root登入,請修改shadow檔
# vim /etc/shadow    //將root的密碼改成 !!
root:!!:14928:0:99999:7:::

2010年11月17日 星期三

Postfix郵件伺服器

Postfix與Sendmail一樣都是Linux上很著名的電子郵件伺服器,但好設定很多
##安裝前記得將sendmail停用並設定開機不啟動



相關套件:
postfix.i386

相關目錄:
/etc/postfix
/etc/postfix/main.cf   //設定檔

設定:
# vim /etc/postfix/main.cf  //修改為
myhostname = a238.snpy.org
mydomain = a238.snpy.org
inet_interfaces = all       //傾聽所有,以下四個不能同時啟用
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost
mydestination = $myhostname, localhost.$mydomain, localhost
//如果同時管好幾個網域,ex:a238-a240.snpy.org
//加在mydestination以逗號分隔

#/etc/init.d/postfix restart    

Relay相關設定:
# vim /etc/postfix/main.cf
#mynetworks_style = class
#mynetworks_style = subnet
#mynetworks_style = host
//設定那些情況會Relay,預設為subnet
//subnet:表示以網卡的subnet為準
//class表示已ip class為準
//host表示為localhost

#mynetworks = 168.100.189.0/28, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table
//設定後會忽略mynetworks_style...
//可以詳細規定接受relay範圍


測試:
# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 a238.snpy.org ESMTP Postfix

# telnet 140.137.215.238 25
Trying 140.137.215.238...
Connected to 140.137.215.238 (140.137.215.238).
Escape character is '^]'.
220 a238.snpy.org ESMTP Postfix

再來可用web mail寄信給tom,再用mutt收信看看
或可以到/var/log/maillog檢查有沒收到外部信件

2010年11月16日 星期二

MRTG實作

小弟一直以來都想學習一下MRTG安裝,可惜資質駑鈍,看資料也看不懂,終於有人肯教我,趕快記錄起來以免忘記



相關套件:
net-snmp           
net-snmp-utils
//兩個套件都要裝,受監控設備也裝

mrtg
httpd  --用來輸出結果

相關檔案與目錄:
/etc/snmp
/etc/httpd/conf.d/mrtg.conf    //httpd中與mrtg相關設定檔
/usr/bin/mrtg
/usr/bin/cfgmaker

設定:
1.受測設備:
#vim /etc/snmp/snmpd.conf      //修改與加入下兩行
#com2sec notConfigUser  default       public
com2sec notConfigUser  default      student       //student為自訂的community string

view    systemview    included   .1.3.6.1.2.1.2

#/etc/init.d/snmpd restart
#chkconfig snmpd on
#snmpwalk -c studemt -v 1 localhost system    //檢測snmp是否可以取得資訊

2.監控主機設定:
# vim /etc/httpd/conf.d/mrtg.conf
Alias /mrtg /opt/mrtg        //預定將mrtg資料放在/opt/mrtg,所以建立別名

       //設定成只有238,188網段可以連線
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Allow from 192.168.238. 192.168.188.



    AddDefaultCharset big5     //mrtg不能產生UTF-8網頁,故設定big5
    AllowOverride All         //啟動htaccess檔案設定機制

# vim /opt/mrtg/.htaccess       //注意弄成隱藏檔
AuthUserFile /opt/mrtg/.htpasswd
AuthName "MRTG Web"
AuthType Basic
require valid-user

# htpasswd -c /opt/mrtg/.htpasswd marcus    //產生marcus使用者帳號密碼
// #  htpasswd /opt/mrtg/.htpasswd marcus    修改密碼

# cfgmaker student@fw.188.tw > /etc/mrtg/mymrtg.cfg    //產生mrtg設定檔
//指令格式:  cfgmaker 自定義通訊碼@受監控ip > 設定檔放置位置
//可以做很多台...但第二台後要用>>才不會覆蓋第一台資料
//注意:正常結束應該沒有訊息

# vim /etc/mrtg/mymrtg.cfg   //加入
WorkDir: /opt/mrtg
Language: big5
Options[_]: growright, bits

# env LANG=C mrtg /etc/mrtg/mymrtg.cfg    //產生報表網頁與統計圖
//第一次因為沒有資料會警告...要連做三次

# vim /etc/cron.d/mrtg     //mrtg安裝好後會產生這個檔案,把內容改一下就好
*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mymrtg.cfg    //五分鐘更新一次

檢視:
用browser連到http://IP/mrtg 應該可以看到mrtg圖表

2010年11月15日 星期一

OpenLDAP實作




相關套件:
openldap.i386 : OpenLDAP 的設定檔、函式庫、以及說明文件。
openldap-clients.i386 : OpenLDAP 的用戶端程式。                 //client套件
openldap-devel.i386 : OpenLDAP 的開發函式庫與標頭檔。
openldap-servers.i386 : OpenLDAP 伺服器程式與相關檔案。     //server套件
openldap-servers-sql.i386 : OpenLDAP 伺服器的 SQL 支援模組。

相關目錄與檔案:
/etc/openldap
/var/lib/ldap
/usr/share/openldap/    //其中migration目錄有轉換工具

LDIF格式:     //LDAP儲存資料用的格式
部門:研發部                  //一筆紀錄(entry)
      層級:第一級            //這個紀錄有兩個屬性(attribute)
      辦公室位置:B棟6樓

人員:陳小明
      部門:研發部
      電話:12345678#777
      職稱:專員


設定:
# cd /etc/openldap
# vim slapd.conf     //修改下面幾行
#suffix         "dc=my-domain,dc=com"
#rootdn         "cn=Manager,dc=my-domain,dc=com"
suffix          "dc=test,dc=com"
rootdn          "cn=Manager,dc=test,dc=com"
rootpw         1qaz2wsx      //管理者密碼以明碼表示
*可用編碼過的方式
# slappasswd
New password:
Re-enter new password:
{SSHA}NHz9J41eL9VKuuiHtisp7dEbwj/HqEJO

# vim slapd.conf
rootpw {SSHA}NHz9J41eL9VKuuiHtisp7dEbwj/HqEJO

# ls /var/lib/ldap/              //檢查有無DB設定檔
openldap-severs-update.log
# cp DB_CONFIG.example /var/lib/ldap/DB_CONFIG   //cp sample檔案過去
# chown ldap:ldap /var/lib/ldap/ -Rf      //更換擁有者
# /etc/init.d/ldap restart           //啟動應會正常,但還沒有任何資料
# ldapadd -x -W -D 'cn=Manager,dc=test,dc=com'    //server online新增方式
Enter LDAP Password:                  //輸入剛設定的密碼
dn: dc=test,dc=com
objectclass: dcObject
objectclass: organization
o: TEST Corp.
dc: test

adding new entry "dc=test,dc=com"

dn: cn=Manager,dc=test,dc=com
objectclass: organizationalRole
cn: Manager

adding new entry "cn=Manager,dc=test,dc=com"

"Ctrl+D結束"
//藍色字體表示固定類別名稱,勿變動

轉換本機帳號/群組:
1.準備測試用帳號:
# groupadd -g 3800 hr
# useradd -g 3800 backy

2.轉換
# /usr/share/openldap/migration/migrate_all_online.sh     //online轉換資料,內容在170左右
Enter the X.500 naming context you wish to import into: [dc=padl,dc=com] dc=test,dc=com
Enter the hostname of your LDAP server [ldap]: localhost
Enter the manager DN: [cn=manager,dc=test,dc=com]:
Enter the credentials to bind with:                    //管理者密碼
Do you wish to generate a DUAConfigProfile [yes|no]? no
.....
//會有錯誤訊息,這個範例可以不理

測試:
1.slapcat
#  slapcat
dn: dc=test,dc=com
objectClass: dcObject
objectClass: organization
o: TEST Corp.
dc: test
structuralObjectClass: organization
entryUUID: 4b8951be-84f3-102f-8d68-ab49309a10a3
creatorsName: cn=Manager,dc=test,dc=com
createTimestamp: 20101115110025Z
entryCSN: 20101115110025Z#000000#00#000000
modifiersName: cn=Manager,dc=test,dc=com
modifyTimestamp: 20101115110025Z

dn: cn=Manager,dc=test,dc=com
objectClass: organizationalRole
cn: Manager
structuralObjectClass: organizationalRole
entryUUID: 4d4589ae-84f4-102f-8d6b-ab49309a10a3
creatorsName: cn=Manager,dc=test,dc=com
createTimestamp: 20101115110738Z
entryCSN: 20101115110738Z#000000#00#000000
modifiersName: cn=Manager,dc=test,dc=com
modifyTimestamp: 20101115110738Z

2.用另一台主機加入domain     //該主機應沒有backy帳號
#setup
正確輸入後,使用backy登入測試
//可登入後應沒有家目錄,需配合NFS做掛載家目錄 

2010年11月12日 星期五

LVM實作



環境準備:
1.新增一個硬碟:(利用VMware測試,使用100M)
*不重開機掃描硬碟方式:
#tail /vat/log/message    //確定沒抓到
#echo "- - -" > /sys/class/scsi-host/host0/scan    //重新掃描scsi硬碟
#tail /var/log/message    //應該就可看到

2.我的裝置為/dev/sdb
#fdisk /dev/sdb     //建立分割區,並設定id為"8e"

LVM相關指令:

PVVGLV
掃描pvscanvgscanlvscan
檢視pvdisplayvgdisplaylvdisplay
新增pvcreatevgcreatelvcreate
移除pvremovevgremovelvremove
放大nonevgextendlvextend
縮小nonevgreducelvreduce

設定:
1.新增一個PV
# pvcreate /dev/sdb1         
  Physical volume "/dev/sdb1" successfully created
# pvscan     //掃描PV
  PV /dev/sdb1                      lvm2 [101.98 MB]
  Total: 1 [101.98 MB] / in use: 0 [0   ] / in no VG: 1 [101.98 MB]
//  PV:/dev/sdb1  格式:lvm2
//PV大小[101.98MB] / 配置給VG的大小   /  沒有配置給VG剩餘大小

2.新增VG     //至少一個PV
# vgcreate mainVG /dev/sdb1
  Volume group "mainVG" successfully created
//指令格式:vgcreate   VGname   pv

# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "mainVG" using metadata type lvm2
# pvscan
  PV /dev/sdb1   VG mainVG   lvm2 [100.00 MB / 100.00 MB free]
  Total: 1 [100.00 MB] / in use: 1 [100.00 MB] / in no VG: 0 [0   ]
//可以看到pv已經in use
*可以用display看到更詳細的內容

3.建立LV:     //建立完成後會產生目錄/dev/mainVG,LV會放在裡面
# ls /dev/mainVG            //應該找不到
# lvcreate  -L 50M -n LV01 mainVG
  Rounding up size to full physical extent 52.00 MB
  Logical volume "LV01" created
//指令格式:lvcreate -L size -n lvname VGname
# lvscan
  ACTIVE            '/dev/mainVG/LV01' [52.00 MB] inherit

4.mount LV
#mkfs.ext3 /dev/mainVG/LV01     //格式化LV
#mkdir /dir1
#mount /dev/mainVG/LV01 /dir1
//利用dd快速建立大檔,待會要練習放大縮小
#dd if=/dev/zero of=/dir1/test bs=1M count=10  //size:1M 做10次即為10M檔案

縮放LVM   ##容易造成data lost,請先備份在操作
1.放大 --先放大LV再放大檔案系統
# lvextend -L +30M /dev/mainVG/LV01   
  Rounding up size to full physical extent 32.00 MB
  Extending logical volume LV01 to 84.00 MB
  Logical volume LV01 successfully resized
//指令格式:lvextend -L 總大小/增加的大小:80M/+30M LVpath

//雖然成功但實際上還未變大,可用df檢查
# df |grep -B1 dir1
/dev/mapper/mainVG-LV01
                         51559     15237     33660  32% /dir1

# resize2fs /dev/mainVG/LV01    //放大可不用umount
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mainVG/LV01 is mounted on /dir1; on-line resizing required
Performing an on-line resize of /dev/mainVG/LV01 to 86016 (1k) blocks.
The filesystem on /dev/mainVG/LV01 is now 86016 blocks long.

[root@a238 dir1]# df |grep -B1 dir1
/dev/mapper/mainVG-LV01
                         83363     15651     63619  20% /dir1
2.縮小 --先縮小檔案系統再縮小LV

增加PV:
1.再增加一個disk:/dev/sdc,同上處理分割區
2.加入VG
# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created
# vgextend mainVG /dev/sdc1
  Volume group "mainVG" successfully extended
# pvscan          //可以看出第二顆pv已正確加入mainVG
  PV /dev/sdb1   VG mainVG   lvm2 [100.00 MB / 16.00 MB free]
  PV /dev/sdc1   VG mainVG   lvm2 [100.00 MB / 100.00 MB free]
  Total: 2 [200.00 MB] / in use: 2 [200.00 MB] / in no VG: 0 [0   ]
# vgdisplay
  --- Volume group ---
  VG Name               mainVG
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               200.00 MB
  PE Size               4.00 MB
  Total PE              50
  Alloc PE / Size       21 / 84.00 MB
  Free  PE / Size       29 / 116.00 MB
  VG UUID               XK0Mvy-5b4U-pcBB-061Z-0dUJ-SBFR-Ro2yBf

2010年11月11日 星期四

監控web,mail,ftp的工具 -- awstats

相關套件:
awstats-6.95-1.noarch.rpm     //google "awstats"

相關目錄與檔案:
/usr/local/awstats
/etc/awstats
/usr

安裝:
#rpm -ivh awstats-6.95-1.noarch.rpm
...
----- AWStats 6.95 - Laurent Destailleur -----
AWStats files have been installed in /usr/local/awstats

If first install, follow instructions in documentation
(/usr/local/awstats/docs/index.html) to setup AWStats in 3 steps:
Step 1 : Install and Setup with awstats_configure.pl (or manually)
Step 2 : Build/Update Statistics with awstats.pl
Step 3 : Read Statistics

#rpm -ql awstats | less    //查出安裝那些目錄與檔案
//可得知awstats_configure.pl放在/usr/local/awststs/tools下
#cd /usr/local/awststs/tools
# ./awstats_configure.pl
----- AWStats awstats_configure 1.0 (build 1.8) (c) Laurent Destailleur -----
......

-----> Running OS detected: Linux, BSD or Unix

-----> Check for web server install

Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
>/etc/httpd/conf/httpd.conf       //httpd設定檔放置位置
-----> Check and complete web server config file '/etc/httpd/conf/httpd.conf'
  Add 'Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"'
  Add 'Alias /awstatscss "/usr/local/awstats/wwwroot/css/"'
  Add 'Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"'
  Add 'ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"'
  Add '' directive
  AWStats directives added to Apache config file.

-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y        //yes讓他幫忙修改httpd.conf
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> a238.snpy.org            //網站名稱

-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats              
Directory path to store config file(s) (Enter for default):
>             //存放awstats設定檔的位置,enter讓他用default就可以了

-----> Create config file '/etc/awstats/awstats.a238.snpy.org.conf'
Error: Failed to open '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' for read.
//error是因為這個版本將awstats.model.conf放在/usr/etc/awstats/下,
//將檔案copy到程式要求的位置再重新執行./awstats_configure.pl 即可

#vim /etc/awstats/awstats.a238.snpy.org.conf   //修改成
#LogFile="/var/log/httpd/mylog.log"
LogFile="/var/log/httpd/access_log"      //要分析的httpd log檔正確名稱

#mkdir /vat/lib/awstats
#cd /usr/local/awstats/wwwroot/cgi-bin/
#perl awstats.pl -update -config=a238.snpy.org   //前面設定的網站名稱

監控mail:
#cd /usr/local/awstats/wwwroot/cgi-bin
#cp awstats.model.conf awstats.mail.conf
#vim awstats.mail.conf    //修改以下內容
LogFile="/usr/local/awstats/tools/maillogconvert.pl standard < /var/log/maillog|"
LogType=M     //Log種類
LogFormat="%time2 %email %email_r %host %host_r %method %url %code %bytesd"    //log_format
SiteDomain="a238.snpy.org"      //mail 域名
...
LevelForBrowsersDetection=0         # 0 disables Browsers detection.
                                    # 2 reduces AWStats speed by 2%
                                    # allphones reduces AWStats speed by 5%
LevelForOSDetection=0               # 0 disables OS detection.
                                    # 2 reduces AWStats speed by 3%
LevelForRefererAnalyze=0            # 0 disables Origin detection.
                                    # 2 reduces AWStats speed by 14%
LevelForRobotsDetection=0           # 0 disables Robots detection.
                                    # 2 reduces AWStats speed by 2.5%
LevelForSearchEnginesDetection=0    # 0 disables Search engines detection.
                                    # 2 reduces AWStats speed by 9%
LevelForKeywordsDetection=0         # 0 disables Keyphrases/Keywords detection.
                                    # 2 reduces AWStats speed by 1%
LevelForFileTypesDetection=0        # 0 disables File types detection.
                                    # 2 reduces AWStats speed by 1%
LevelForWormsDetection=0            # 0 disables Worms detection.
                                    # 2 reduces AWStats speed by 15%
...
ShowMonthStats=HB             //以下為顯示的畫面格式
ShowDaysOfMonthStats=HB
ShowDaysOfWeekStats=HB
ShowHoursStats=HB
ShowDomainsStats=0
ShowHostsStats=HBL
ShowAuthenticatedUsers=0
ShowRobotsStats=0
ShowWormsStats=0
ShowEMailSenders=HBML
ShowEMailReceivers=HBML
ShowSessionsStats=0
ShowPagesStats=0
ShowFileTypesStats=0
ShowFileSizesStats=0
ShowOSStats=0
ShowBrowsersStats=0
ShowScreenSizeStats=0
ShowOriginStats=0
ShowKeyphrasesStats=0
ShowKeywordsStats=0
ShowMiscStats=0
ShowHTTPErrorsStats=0
ShowSMTPErrorsStats=1

#/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mail

監控FTP:
#vim /etc/vsftpd/vsftpd.conf     //修改log輸出格式
xferlog_std_format=YES

#cd /usr/local/awstats/wwwroot/cgi-bin
#cp awstats.model.conf awstats.ftp.conf
#vim awstats.ftp.conf
LogFile="/var/log/xferlog"
LogType=F
LogFormat="%time3 %other %host %bytesd %url %other %other %method %other %logname %other %code %other %other"
LogSeparator="\s"
SiteDomain="ftp.a238.snpy.org"
.....
LevelForBrowsersDetection=0         # 0 disables Browsers detection.
                                    # 2 reduces AWStats speed by 2%
                                    # allphones reduces AWStats speed by 5%
LevelForOSDetection=0               # 0 disables OS detection.
                                    # 2 reduces AWStats speed by 3%
LevelForRefererAnalyze=0            # 0 disables Origin detection.
                                    # 2 reduces AWStats speed by 14%
LevelForRobotsDetection=0           # 0 disables Robots detection.
                                    # 2 reduces AWStats speed by 2.5%
LevelForSearchEnginesDetection=0    # 0 disables Search engines detection.
                                    # 2 reduces AWStats speed by 9%
LevelForKeywordsDetection=0         # 0 disables Keyphrases/Keywords detection.
                                    # 2 reduces AWStats speed by 1%
LevelForFileTypesDetection=0        # 0 disables File types detection.
                                    # 2 reduces AWStats speed by 1%
LevelForWormsDetection=0            # 0 disables Worms detection.
                                    # 2 reduces AWStats speed by 15%
....
ShowMenu=1
ShowSummary=UVHB
ShowMonthStats=UVHB
ShowDaysOfMonthStats=HB
ShowDaysOfWeekStats=HB
ShowHoursStats=HB
ShowDomainsStats=HB
ShowHostsStats=PBL
ShowAuthenticatedUsers=HBL
ShowRobotsStats=0
ShowWormsStats=0
ShowEMailSenders=0
ShowEMailReceivers=0
ShowSessionsStats=1
ShowPagesStats=PBEX
ShowFileTypesStats=HB
ShowFileSizesStats=0
ShowOSStats=1
ShowBrowsersStats=0
ShowScreenSizeStats=0
ShowOriginStats=0
ShowKeyphrasesStats=0
ShowKeywordsStats=0
ShowMiscStats=a
ShowHTTPErrorsStats=0
ShowSMTPErrorsStats=0
...
ShowLinksOnUrl=0

#/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=ftp

測試:
用Browser連接 http://localhost/awstat/awstats.pl?config=a238.snpy.org        //web流量監控
用Browser連接 http://localhost/awstat/awstats.pl?config=mail         //mail監控
用Browser連接 http://localhost/awstat/awstats.pl?config=ftp         //ftp監控

排程更新內容:  
#crontab -e
0 0 * * * /usr/local/awstats/tools/awstats_updateall.pl now > /dev/null   
#設定整點更新,訊息丟棄
*由於awstats是解析log檔內容,故更新時間必須比logrotate要短

Proxy server --squid



相關套件:
squid

相關檔案與目錄:
/etc/squid/squid.conf
/var/spool/squid     //網頁暫存區

Squid預設值:
http-port    3128    //使用的port
//cache-dir  ufs   /vat/spool/squid   最大空間    第一層目錄數  第二層目錄數
cache-dir   ufs    /var/spool/squid    100        16              256
cache-mem    8MB     //預設快取
cache_log     /var/log/squid/....
access_log     /var/log/squid/....
//存取規則
http_access  allow    acl名稱
http_access deny    acl名稱

設定:
#vim /etc/squid/squid.conf    //增加一個acl名稱
acl   our_networks src  140.137.215.0/24
http_access allow  our_networks

#/etc/init.d/squid restart

測試:
1.先觀察/var/spool/squid
# du --max-depth=1 /var/spool/squid/
1028    /var/spool/squid/08
1028    /var/spool/squid/00
1028    /var/spool/squid/0E
1028    /var/spool/squid/07
1028    /var/spool/squid/09
....
16460   /var/spool/squid/

2.用browser設定proxy指向server後,隨便連一個網頁

3.在觀察/var/spool/squid是否有變化
# du --max-depth=1 /var/spool/squid/
1028    /var/spool/squid/08
1480    /var/spool/squid/00
1028    /var/spool/squid/0E
1028    /var/spool/squid/07
1028    /var/spool/squid/09
....
16912   /var/spool/squid/

可再用另一個瀏覽器讀取同一網頁看是否有變快

檢視記錄檔:
1./var/log/squid/cache.log
//squid啟動時的log

2./var/log/squid/access.log
 //紀錄server存取網頁紀錄

3./var/log/squid/store.log
//紀錄網頁資料存放位置與client端讀取網頁log
進階設定:
1.設定不可連線的網頁:
#vim /etc/squid/badsites.squid     //檔名自取,內容為網址
www.ibm.com          //阻擋連結ibm網址
.ubuntu-tw.org        //阻擋連結domain:ubuntu-tw.org

#vim /etc/squid/squid.conf   //加入
acl badsites dstdomain "/etc/squid/badsites.squid"

http_access deny badsites

*定義區需在http_access前,而http_access需放在該區前面,以免有符合其他條件而未處理

利用joomla快速架站

1.相關套件:
httpd or apache
php*           //記得裝sql模組
mysql*
Joomla_1.5.22-Stable-Full_Package.zip   //到www.joomla.org下載zip檔
zh-TW.administrator.1.5.17v1.zip    //管理者中文介面
zh-TW.site.1.5.17v1.zip       //網站中文介面

2.解壓縮:
先建立目錄
#mkdir joomla
#unzip Joomla_1.5.22-Stable-Full_Package.zip -d joomla   //指定解壓目錄
#mkdir joomla/language/zh-TW                 //先把語系相關目錄準備好
#mkdir joomla/administrator/language/zh-TW
#unzip zh-TW.administrator.1.5.17v1.zip -d mkdir joomla/administrator/language/zh-TW
#unzip zh-TW.site.1.5.17v1.zip -d joomla/language/zh-TW
#mv joomla /opt/           //將目錄搬到要放置的位置

3.建立資料庫:
#mysql -u root -p
>create database joolma;
>grant all on joomla.* to admin@localhost identified by 'XXXXXXX' ;   //設置管理帳號

4.設定apache:           
#vim /etc/httpd/conf/httpd.conf    //利用alias指向
alias  /joomla  "/opt/joomla"

or
#vim /etc/httpd/conf/httpd.conf     //利用虛擬主機方式,適合做首頁方式

    DocumentRoot /opt/joomla
    ServerName www.a238.snpy.org

or
直接將檔案放在/var/www/html中

#/etc/inir.d/httpd restart

5.開始安裝:
用Browser連接 http://localhost/joomla    //基本上就照圖片順序處理

下圖為檢查機制,應該要都為"是",出現"否"請根據問題處理

下圖為sql主機資訊,本範例用MySQL並位於localhost

注意設定帳號admin的密碼,記得安裝預設資料



看到這個頁面時代表安裝已完成,故要移除安裝檔案
#cd /opt/joomla    //進入joomla目錄
#rm -rf installation         //移除安裝目錄

基本安裝完成,接下來就可以根據需求修改內容

2010年11月8日 星期一

NIS server



1.相關套件:
yp-tools-2.9-1.el5       //server,client都須裝
ypbind-1.19-12.el5        //server,client都須裝
ypserv.i386 0:2.19-5.el5        //server須裝

2.相關檔案與目錄:
/etc/ypserv.conf
/etc/hosts
/etc/sysconfig/network
/var/yp/
/etc/nsswitch.conf         //client
/etc/yp.conf              //client

3.環境設定:
Server:     //由於設定nis不能使用ip,崓稱解析須正常
設定Nisdomainame:
# nisdomainname marcus
#vim /etc/sysconfig/network     //為了開機生效,加入
NISDOMAIN=marcus

#vim /etc/hosts      //加入
127.0.0.1     ......................    master.marcus     //讓本機名稱解析正常
140.137.215.238     master.marcus
192.168.213.112     client.marcus       //client端資料
Client:      //設定正確的nis主機名稱與IP
#vim /etc/hosts    //加入名稱解析
127.0.0.1   .......................   client.marcus
192.168.213.100     master.marcus

4.NIS設定:
#vim /etc/ypserv.conf      //將最後一行註解與空白取消
*                        : *       : *                : none

#/etc/init.d/ypserv restart      //啟動服務
# /etc/init.d/yppasswdd restart       //啟動密碼服務

# rpcinfo -p     //確認是否正常啟動
    100004    2   udp    716  ypserv
    100004    1   udp    716  ypserv
    100004    2   tcp    719  ypserv
    100004    1   tcp    719  ypserv
    100009    1   udp    728  yppasswdd

# /usr/lib/yp/ypinit -m    //建立nis map檔(資料庫格式),將server帳號資料放到/var/yp/marcus 下
                                 //做完後帳號有修改要重做一次,yppasswd可以改密碼
At this point, we have to construct a list of the hosts which will run NIS
servers.  a238.snpy.org is in the list of NIS server hosts.  Please continue to add
the names for the other hosts, one per line.  When you are done with the
list, type a <control D>.
        next host to add:  a238.snpy.org
        next host to add:  master.marcus      //輸入網域名稱
        next host to add:          //可在加另一個網域,用ctrl+D來離開
The current list of NIS servers looks like this:

a238.snpy.org
master.marcus

Is this correct?  [y/n: y]  y
We need a few minutes to build the databases...
Building /var/yp/marcus/ypservers...
Running /var/yp/Makefile...
gmake[1]: Entering directory `/var/yp/marcus'
Updating passwd.byname...
Updating passwd.byuid...
Updating group.byname...
Updating group.bygid...
Updating hosts.byname...
Updating hosts.byaddr...
Updating rpc.byname...
Updating rpc.bynumber...
Updating services.byname...
Updating services.byservicename...
Updating netid.byname...
Updating protocols.bynumber...
Updating protocols.byname...
Updating mail.aliases...
gmake[1]: Leaving directory `/var/yp/marcus'

a238.snpy.org has been set up as a NIS master server.

Now you can run ypinit -s a238.snpy.org on all slave server.

# ypcat -h localhost passwd.byname    //可檢查是否正常運作,會show出帳號資料

*注意防火牆iptables規則
5測試:
在client 端用ypcat測試:
# ypcat -h master.marcus passwd.byname   //看有無server端帳號資料
#setup  --> 認證設定  -->  勾選"使用NIS"      //如果使用DHCP取得IP,
網域名稱:marcus                                           //須注意DHCP srv在dhcpd.conf中有沒設定正確nisdomain
伺服器:192.168.213.100
#cat /etc/yp.conf
domain a238 server 192.168.213.100

#cat /etc/nsswitch.conf
passwd:     files nis
shadow:     files nis
group:      files nis

hosts:      files nis dns
...

*如果有問題可能開機後不能登入,就只能用single user mode來修正
*重開機後,nisdomainname設定可能會失效,可在/etc/rc.local加上
/bin/nisdomainname marcus

DHCP



1.相關套件:
dhcp.i386

2.相關目錄與檔案:
/etc/dhcpd.conf
/var/lib/dhcpd/              //放租約記錄檔
/usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample

3.設定:
安裝好dhcp套件後,設定檔/etc/dhcpd.conf沒有內容
要拷貝/usr/share/doc/dhcp-*/dhcpd.conf.sample來改      //dhcp-*表示安裝的版號
#cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
#vim /etc/dhcpd.conf      //修改為
ddns-update-style interim;
ignore client-updates;

subnet 192.168.213.0 netmask 255.255.255.0 {      //網段名稱

# --- default gateway
        option routers                  192.168.213.2;        // default gateway
        option subnet-mask              255.255.255.0;

        option nis-domain               "marcus";       //如果DHCP+NIS則須給正確值,否則client重開機會被覆蓋掉
        option domain-name              "marcus.com";
        option domain-name-servers      168.95.1.1;     //DNS server IP

        option time-offset              -18000; # Eastern Standard Time     //目前時區與格林威治的差值

        range dynamic-bootp 192.168.213.111 192.168.213.112;        //發放IP區段,練習用只發111,112
        default-lease-time 21600;          //租約時間
        max-lease-time 43200;           //最大租用時間

        # we want the nameserver to appear at a fixed address   //用來設定固定IP
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}



4.啟動與測試:
Server端:
#/etc/init.d/dhcpd restart

到client端測試:
Linux:
#dhclient eth0
#tail /var/log/message |grep dhclient

Windows:
將網卡設為自動取得IP,在修復讓他去抓

2010年11月5日 星期五

基本iptables規則

研究了一下iptables,寫了一個基本的iptables規則,並寫成script
可在開機時執行

#!/bin/bash

modprobe ip_tables
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp

#清空
iptables -F -t filter
iptables -t nat -F
iptables -X
iptables -t nat -X
iptables -F

#NAT setting
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.188.5 -j SNAT --to 140.137.215.188
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.188.0/24 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.238.0/24 -j MASQUERADE
iptables -t nat -A PREROUTING -i eth0 -d 140.137.215.188 -j DNAT --to 192.168.188.5

#Default policy setting
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

#允許lo介面
iptables -A INPUT -i lo -j ACCEPT

#ssh
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p udp --dport 22 -j ACCEPT

#www
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.7    #將連到80port轉給另一台主機

#smtp,pop3,icmp...
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp --dport 993 -j ACCEPT
iptables -A INPUT -p tcp --dport 995 -j ACCEPT
#iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j DNAT --to 192.168.1.6    #將連到25port轉給另一台主機

#DNS
iptables -A INPUT -p udp --dport 53 -j ACCEPT

#samba
iptables -A INPUT -p tcp --dport 137 -j ACCEPT
iptables -A INPUT -p tcp --dport 138 -j ACCEPT
iptables -A INPUT -p tcp --dport 139 -j ACCEPT
iptables -A INPUT -p tcp --dport 445 -j ACCEPT

#rsync --一班rsync走ssh:22,故不須開通873
#iptables -A INPUT -p tcp ! -i eth0 --dport 873 -j ACCEPT
#iptables -A INPUT -p udp ! -i eth0 --dport 873 -j ACCEPT

#icmp
iptables -A INPUT -p icmp -j ACCEPT

#自訂一個鍊other
iptables -N other

#規則
iptables -A other -m state --state ESTABLISHED,RELATED -j ACCEPT  #允許建立連線與回應的封包通過

#將INPUT,OUTUT,FORWARD表格其餘未設定封包皆由other鍊規則處理
iptables -A INPUT -j other
iptables -A OUTPUT -j other
iptables -A FORWARD -j other

2010年11月4日 星期四

Samba 網路芳鄰



1.相關套件:
samba-client-3.0.33-3.28.el5
samba-common-3.0.33-3.28.el5
samba-3.0.33-3.28.el5

2.設定檔: 
#vim  /etc/samba/smb.conf
[global]        //全域設定,這邊的設定是關於整個samba server
       workgroup = MYGROUP
       server string = Samba Server Version %v
       security = user      //預設為user
                  //可設定share,user,server,domain,ads等
       passdb backend = tdbsam
//到下一個[home]前都是[global]相關設定區塊
[home]   //登入後會看到家目錄
 
        comment = Home Directories
        browseable = no 
        writable = yes

[tmp]      //設定分享的目錄
        comment = tmp file    //註解區內容
        path = /tmp     //要分享的目錄
        writable = no   //唯讀,同 read only = yes
        public = yes    //公開讓別人看到
        hosts allow = 140.137.215.89    //這兩行表示拒絕所有連線,
        hosts deny = 0.0.0.0/0                 //僅允許89那台主機使用這個目錄
        valid users = max    //僅user: max可以讀取,群組用@group

#testparm    //用來測試smb.conf有無格式或文字上的錯誤
#/etc/init.d/smb restart

3.測試:
用windows測試:(ip:140.137.215.89)
在執行打\\ip\目錄即可,但如果要測試很多人的帳號密碼
由於windows會記得登入資訊一段時間,可輸入
c:\>net use * /del /y     //清除資訊

用linux測試:(ip:140.137.215.238)
# smbclient -L //140.137.215.238 -U max
Password:
Domain=[A238] OS=[Unix] Server=[Samba 3.0.33-3.28.el5]

        Sharename       Type      Comment
        ---------       ----      -------
        www             Disk      www file
        IPC$            IPC       IPC Service (Samba Server Version 3.0.33-3.28.el5)
        max             Disk      Home Directories
Domain=[A238] OS=[Unix] Server=[Samba 3.0.33-3.28.el5]

        Server               Comment
        ---------            -------

        Workgroup            Master
        ---------            -------
        MYGROUP

# smbclient  //140.137.215.238/www -U max
Password:
Domain=[A238] OS=[Unix] Server=[Samba 3.0.33-3.28.el5]
tree connect failed: NT_STATUS_ACCESS_DENIED
//因為有設定僅ip:140.137.215.89可以連線

4.情境實作:
建立一個目錄/opt/boss,只提供帳號zoe使用,資源名稱為storage
ans:
#mkdir /opt/boss
#chown zoe. /opt/boss
#chmod 700  /opt/boss
#vim /etc/samba/smb.conf    //加入
[storage]
        comment = boss storage
        path = /opt/boss
        writable = yes
        public = no
        valid users = zoe    //僅允許zoe使用
        browseable = no    //不會list
#/etc/init.d/smb restart

建立一資源名稱為webproject,供webuser組的所有帳號存取,目錄為/opt/project
ans:
# mkdir /opt/project
# chown :webuser /opt/project
# chmod 770 /opt/project
# vim /etc/samba/smb.conf
[webproject]
        comment = webproject storage
        path = /opt/project
        writable = yes
        public = no
        valid user = @webuser    //表群組
        browseable = no