CentOS 6 編譯安裝samba4

         微軟輸了反壟斷案,被法院判決向samba項目貢獻代碼,samba與微軟的戰爭結束,換來了samba4 十多年來的第一個正式版,徹底兼容win2k至win8全部的客戶端。php

        主流linux發行版裏的samba4版本比較舊,debian wheezy用的也是beta2,其餘都是alpha18,最新的fedora 18 自帶了samba4正式版,可是使用的kerberos是MIT的,與samba4自帶的heimdal kerberos有些出入,鑑於目前samba4的文檔比較少,在redhat系的平臺上,仍是按照官方wiki說的作成功率比較高,也就是編譯安裝。python

 

samba4內置了kerberos、dns和ldap,外加一個ntp就能夠完成一個AD了。linux

 

環境:CentOS 6.3  x64windows

少廢話,將通過測試和總結的精華寫個腳本分享給你們,簡單說明以下:centos

一、請根據須要修改【10-13行】,注意大小寫;bash

二、主機名沒有特別要求,非fqdn便可,不帶域名的那種,好比samba;dom

三、samba4 AD 安裝一步到位(加上啓動腳本實際爲2步,ntp請另行配置)ssh

四、腳本可重複執行,除去下載,安裝編譯耗時約15分鐘curl

五、配置文件位於/etc/samba,可執行文件和庫文件都位於/usr/local/,tcp

手冊在/usr/share/samba,pid和lock都位於/var,日誌位於/var/log/samba,作了日誌輪轉

 

#!/usr/bin/env bash # name = setup-samba4-ad.sh # author = purplegrape4@gmail.com # test under centos 6.3 mininal only , # at your own risk.  set -e  #------------------------------ realm=TEST.ORG domain=TEST adminpass="1q2w3edc4RFV" DomainName=test.org  #install development packages yum groupinstall "Development tools"  -y yum install python-devel ctdb-devel docbook-style-xsl libacl-devel readline-devel \     openssl-devel cups-devel libaio-devel pam-devel libtevent-devel libcap-devel \     expect libuuid-devel libtdb-devel quota-devel openldap-devel krb5-workstation -y  rm -rf samba-4.0.3*  #wget http://www.samba.org/ftp/samba/samba-4.0.3.tar.gz #wget http://192.168.122.1/pkg/samba-4.0.3.tar.gz  [ -f samba-4.0.3.tar.gz ] || wget http://www.samba.org/ftp/samba/samba-4.0.3.tar.gz [ -d samba-4.0.3 ] || tar zxvf samba-4.0.3.tar.gz  mkdir -p /etc/samba/private  cd samba-4.0.3 ./configure \     --enable-debug \     --enable-selftest \     --disable-cups \     --disable-gnutls \     --enable-fhs \     --prefix=/usr/local \     --sysconfdir=/etc \     --localstatedir=/var \     --datarootdir=/usr/share \     --with-privatedir=/etc/samba/private #   --with-aio-support make && make install  rm -rf /etc/samba/smb.conf samba-tool domain provision \     --realm=$realm \     --domain=$domain \     --workgroup=$DomainName \     --adminpass="$adminpass" \     --server-role=dc \     --use-rfc2307  cat /etc/samba/private/krb5.conf > /etc/krb5.conf echo "[kdc]" >>/etc/krb5.conf echo "check-ticket-address = false" >>/etc/krb5.conf  #/usr/sbin/samba -D #echo "/usr/local/sbin/samba -D" >>/etc/rc.local  curl http://192.168.122.1/samba4.init.sh >/etc/init.d/samba4 chmod 755 /etc/init.d/samba4 chkconfig --add samba4 chkconfig --level 2345 samba4 on /etc/init.d/samba4 start service iptables stop  echo domain $DomainName >/etc/resolv.conf echo nameserver 127.0.0.1 >>/etc/resolv.conf  #for rsyslog cat > /etc/rsyslog.d/samba.log <<SAMBA-LOG /var/log/samba/* {     notifempty     olddir /var/log/samba/old     missingok     sharedscripts     copytruncate } SAMBA-LOG  #DNS test host -t SRV _ldap._tcp.$DomainName. host -t SRV _kerberos._udp.$DomainName. host -t A $HOSTNAME.$DomainName.  service samba4 restart  sleep 10  #samba-client test smbclient --version smbclient -L localhost -U% smbclient //localhost/netlogon -U administrator -P $adminpass -c 'ls'  #kerberos test expect -c "    set timeout 5;    spawn kinit administrator@TEST.ORG    expect {        "Password*" {send \"$adminpass\r\";}    } expect eof;"  #echo please enter your passwd of administrator #kinit administrator@TEST.ORG klist  cat <<NOTE >&2#Warnning# #Remember to open the following ports in your iptables firewall #53         tcp/udp #88         tcp/udp #389        tcp/udp #464        tcp/udp #137        udp #138        udp #139        tcp #445        tcp #636        tcp #1024       tcp #3268       tcp #3269       tcp NOTE echo "Congratuations! everything done successful" #end of the script

 

 samba4開機腳本/etc/init.d/samba4

#!/bin/sh # chkconfig: 2345 91 35 # description: Initialization script for Samba  #source function library . /etc/rc.d/init.d/functions  # Make sure the configuration file exists [ -f /etc/samba/smb.conf ] || exit 1  prog=samba pidfile=/var/run/samba/samba.pid lockfile=/var/lock/samba/samba RETVAL=0  samba_start() {     echo "Starting $prog: "     /usr/local/sbin/samba -D }  samba_stop() {     echo -n $"Stopping $prog: "     killproc -p ${pidfile} ${prog}     RETVAL=$?     echo     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} }  samba_reload() {     echo -n \$"Reloading $prog: "     killproc -p ${pidfile} ${prog} -HUP     RETVAL=$?     echo }  # See how we were called case "$1" in     'start')         samba_start         ;;     'stop')         samba_stop         ;;     'restart')         samba_stop         sleep 1         samba_start         ;;     'reload')         samba_reload         ;;     *)         echo "Usage: $0 {start|stop|restart|reload}"          ;; esac

 

使用方法

chmod 755 /etc/init.d/samba4 chkconfig --add samba4 chkconfig --level 2345 samba4 on /etc/init.d/samba4 start

 

防火牆腳本也一併送上

#!/usr/bin/env bash # Name :firewall.sh # Authhor :purplegrape4@gmail.com # Description:setup a simple host-based iptables firewall  if [ "$(id -u)" != "0" ]; then    echo "This script is designed to run as root" 1>&2    exit 1 fi  #only one net card lan=192.168.0.0/16  # Load modules modprobe ip_tables modprobe iptable_filter modprobe ipt_REJECT modprobe ip_conntrack modprobe xt_limit modprobe xt_recent modprobe xt_state  # Flush the current iptables rules iptables -F iptables -X iptables -Z  # To prevent us blocked out of the server # Set the INPUT policy to ACCEPT for the moment iptables -P INPUT ACCEPT  # Allow related,established connection iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Limit the speed of ping,1 package per second iptables -A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT  # Always trust lookback interface iptables -A INPUT -i lo -j ACCEPT  # Allow ssh but limit 10 new connections per minute # This will help to prevent too much password failure iptables -A INPUT -s $lan -p tcp --dport 22 -m recent --set --name ssh --rsource iptables -A INPUT -s $lan -p tcp --dport 22 -m recent ! --rcheck --seconds 60 --hitcount 10 --name ssh --rsource -j ACCEPT   # Open some port to local network only iptables -A INPUT -s $lan --dport 53 -j ACCEPT iptables -A INPUT -s $lan --dport 88 -j ACCEPT iptables -A INPUT -s $lan --dport 389 -j ACCEPT iptables -A INPUT -s $lan --dport 464 -j ACCEPT iptables -A INPUT -s $lan -p udp --dport 137 -j ACCEPT iptables -A INPUT -s $lan -p udp --dport 138 -j ACCEPTiptables -A INPUT -s $lan -p tcp --dport 135 -j ACCEPTiptables -A INPUT -s $lan -p tcp --dport 139 -j ACCEPT iptables -A INPUT -s $lan -p tcp --dport 445 -j ACCEPT iptables -A INPUT -s $lan -p tcp --dport 636 -j ACCEPT iptables -A INPUT -s $lan -p tcp --dport 1024 -j ACCEPT iptables -A INPUT -s $lan -p tcp --dport 3268 -j ACCEPT iptables -A INPUT -s $lan -p tcp --dport 3269 -j ACCEPT  iptables -A INPUT -s $lan -p udp --dport 123 -j ACCEPT   # Set the global polciy now iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT  # Drop some output request iptables -A OUTPUT -s 224.0.0.0/8 -j DROP iptables -A OUTPUT -d 224.0.0.0/8 -j DROP iptables -A OUTPUT -s 255.255.255.255/32 -j DROP iptables -A OUTPUT -m state --state INVALID -j DROP  # Save rules to /etc/sysconfig/iptables /etc/init.d/iptables save # Restart iptables service /etc/init.d/iptables restart # Show the final rules on the screen iptables -n -v -L

 

windows 客戶端配置(以win2003爲例)

"個人電腦" =>"屬性" =>"計算機名" =>"更改" =>勾選"隸屬於域",填上test.org ,而後輸入域管理員的用戶名和密碼便可。

linux客戶端配置(以centos6爲例)

 在終端運行命令authconfig-tui,user information選擇winbind,authentication選擇winbind,但不要取消shadows passwords

 

Samba4 AD甚至可直接使用微軟的工具進行管理,見samba官網wiki (點擊進入)

 

 

順帶提點一下,本文僅爲測試,實際環境中,會有潛在的風險,你懂的。

腳本運行結果,截圖以下 

update2013-02-27

更新samba版本爲4.0.3

修正kerberos測試中的嵌入的expect腳本,從而消除腳本中惟一交互之處,實現徹底自動。

 

update 2016-04-16

因爲badlock漏洞,redhat已經將centos 6自帶的samba4.0 升級至samba 4.2.10

相關文章
相關標籤/搜索