系統更新:
yum update && yum upgrade
CentOS 7 修改統自動產生的網卡名稱:代號(網路代號依個人習慣,不一定要修改):
vi /etc/default/grub
在 GRUB_CMDLINE_LINUX 最後加上 net.ifnames=0 biosdevname=0
產生新的 grub 設定檔:
grub2-mkconfig -o /boot/grub2/grub.cfg
把 ifcfg-enoxxx 改成 ifcfg-eth0:
mv /etc/sysconfig/network-scripts/ifcfg-enoxxxxxxxx /etc/sysconfig/network-scripts/ifcfg-eth0
修改網路卡代號:
sed -i 's/enoxxxxxxxx/eth0/g' /etc/sysconfig/network-scripts/ifcfg-eth0
重新開機:
reboot
關閉系統防火牆:
systemctl stop firewalld.service
systemctl disable firewalld.service
關閉 selinux:
vi /etc/sysconfig/selinux
修改:
SELINUX=enforcing
to
SELINUX=disabled
修改網路設定:
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.1.252
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 192.168.1.252
domain tw.company
search tw.company
vi /etc/hostname
centos7-ad.tw.company
vi /etc/hosts
192.168.1.252 centos7-ad.tw.company centos7-ad
關閉 IPv6:
vi /usr/lib/sysctl.d/00-system.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
新增 Samba 路徑參數檔到系統內:
vi /etc/profile.d/samba-path.sh
PATH=${PATH}:/usr/local/samba/bin:/usr/local/samba/sbin
修改新增 sudoers 權限:
vi /etc/sudoers
新增下列:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/samba/bin:/usr/local/samba/sbin
重新開機:
reboot
安裝所需套件:
yum install perl gcc attr libacl-devel libblkid-devel gnutls-devel readline-devel python-devel gdb pkgconfig \
krb5-workstation zlib-devel setroubleshoot-server libaio-devel setroubleshoot-plugins policycoreutils-python \
libsemanage-python perl-ExtUtils-MakeMaker perl-Parse-Yapp perl-Test-Base popt-devel libxml2-devel libattr-devel \
keyutils-libs-devel cups-devel bind-utils libxslt docbook-style-xsl openldap-devel autoconf python-crypto pam-devel ntp
下載 Samba source 檔安裝:
cd /usr/local/src
wget https://www.samba.org/samba/ftp/samba-latest.tar.gz
tar xvf samba-latest.tar.gz
./configure
compile 過程順利時,即可安裝:
make
make install
reboot
Samba DC 加入已存在的網域(確認DNS IP 指向第一台 PDC):
安裝 BIND DNS 套件:
yum install bind
備份 smb.conf 設定檔(建立或加入網域需先移除 smb.conf 設定檔):
mv /usr/local/samba/etc/smb.conf /usr/local/samba/etc/smb.conf.roig
samba-tool domain join tw.company DC -U"TW\administrator" --dns-backend=BIND9_DLZ
(相容 Windows AD DNS,如果最初設定時,選錯 DNS 支援類型,可用 samba_upgradedns --dns-backend=BIND9_DLZ 變更)
或 開始建置 Samba AD 新網域(確認DNS IP 指向本機):
Samba DNS backend 預設採用 SAMBA_INTERNAL,如要採用 BIND9_DLZ 需再安裝 bind,
(BIND9_DLZ 相容 Windows AD DNS, SAMBA_INTERNAL 功能較陽春無法動態更新DNS記錄):
yum install bind
啟動 named 服務:
systemctl enable named.service
systemctl start named.service
建置 DC 網域:
samba-tool domain provision --use-rfc2307 --use-xattrs=yes --function-level=2008_R2 --interactive
Realm: TW.COMPANY
Domain [TW]: TW
Server Role (dc, member, standalone) [dc]: dc
DNS backend (SAMBA_INTERNAL, BIND9_FLATFILE, BIND9_DLZ, NONE) [SAMBA_INTERNAL]: BIND9_DLZ
Administrator password: 夠複雜的密碼(至少要8位元含特殊符號)
Retype password: 再確認密碼
DNS forwarder IP address (write ‘none’ to disable forwarding) [192.168.1.252]: 8.8.8.8 168.95.1.1 8.8.4.4
或者:
samba-tool domain provision --realm=tw.company --domain=tw --adminpass=夠複雜的密碼 --server-role=dc --dns-backend=BIND9_DLZ
PS:密碼至需8個字元,夠複雜,在這如果設定失敗,需刪除已產生的 smb.conf 才能再重新設定。
網域成功建置訊息:
Realm [TW.COMPANY]: TW.COMPANY
Domain [TW]: TW
Server Role (dc, member, standalone) [dc]: dc
DNS backend (SAMBA_INTERNAL, BIND9_FLATFILE, BIND9_DLZ, NONE) [SAMBA_INTERNAL]: BIND9_DLZ
Administrator password:
Retype password:
Looking up IPv4 addresses
Looking up IPv6 addresses
No IPv6 address will be assigned
Setting up secrets.ldb
Setting up the registry
Setting up the privileges database
Setting up idmap db
Setting up SAM db
Setting up sam.ldb partitions and settings
Setting up sam.ldb rootDSE
Pre-loading the Samba 4 and AD schema
Adding DomainDN: DC=tw,DC=company
Adding configuration container
Setting up sam.ldb schema
Setting up sam.ldb configuration data
Setting up display specifiers
Modifying display specifiers
Adding users container
Modifying users container
Adding computers container
Modifying computers container
Setting up sam.ldb data
Setting up well known security principals
Setting up sam.ldb users and groups
Setting up self join
Adding DNS accounts
Creating CN=MicrosoftDNS,CN=System,DC=tw,DC=company
Creating DomainDnsZones and ForestDnsZones partitions
Populating DomainDnsZones and ForestDnsZones partitions
See /usr/local/samba/private/named.conf for an example configuration include file for BIND
and /usr/local/samba/private/named.txt for further documentation required for secure DNS upda
Setting up sam.ldb rootDSE marking as synchronized
Fixing provision GUIDs
A Kerberos configuration suitable for Samba 4 has been generated at /usr/local/samba/private/
Setting up fake yp server settings
Once the above files are installed, your Samba4 server will be ready to use
Server Role: active directory domain controller
Hostname: centos7-ad
NetBIOS Domain: TW
DNS Domain: tw.company
DOMAIN SID: S-1-5-21-2798485062-3178217977-1919772479
確認 AD DNS ZONE 對應到對的版本:
named -V
cat /usr/local/samba/private/named.conf
Samba 的 smb.conf 設定值:
利用 testparm 取得建議的 smb.conf 檔設定值:
[global]
netbios name = CENTOS7-AD
realm = TW.COMPANY
server services = s3fs, rpc, nbt, wrepl, ldap, cldap, kdc, drepl, winbindd, ntp_signd, kcc, dnsupdate
workgroup = TW
passdb backend = samba_dsdb
server role = active directory domain controller
rpc_server:tcpip = no
rpc_daemon:spoolssd = embedded
rpc_server:spoolss = embedded
rpc_server:winreg = embedded
rpc_server:ntsvcs = embedded
rpc_server:eventlog = embedded
rpc_server:srvsvc = embedded
rpc_server:svcctl = embedded
rpc_server:default = external
winbindd:use external pipes = true
idmap_ldb:use rfc2307 = yes
idmap config * : backend = tdb
map archive = No
map readonly = no
store dos attributes = Yes
vfs objects = dfs_samba4 acl_xattr
log file = /var/log/samba/samba.log
max log size = 100000
[netlogon]
path = /usr/local/samba/var/locks/sysvol/tw.company/scripts
read only = No
[sysvol]
path = /usr/local/samba/var/locks/sysvol
read only = No
修改 named.conf:
acl "LocalIP" { 192.168.1.0./24; 192.168.60.0/24; 192.168.70.0/24; 172.16.10.0/24; 127.0.0.1; };
options {
//listen-on port 53 { 127.0.0.1; };
listen-on port 53 { any; };
//listen-on-v6 port 53 { none; }; //disable IPv6 and modify /etc/sysconfig/named, append line: OPTIONS="-4"
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
forwarders {
8.8.8.8;
8.8.4.4;
168.95.1.1;
168.95.1.1;
};
forward first;
listen-on {
LocalIP;
};
allow-recursion { LocalIP; };
allow-update { LocalIP; };
allow-query { LocalIP; };
allow-transfer { LocalIP; };
dnssec-enable no;
dnssec-validation no;
tkey-gssapi-keytab "/usr/local/samba/private/dns.keytab";
auth-nxdomain yes;
empty-zones-enable no;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging {
channel Named_log {
file "data/named.run";
severity dynamic;
print-severity yes;
print-time yes;
};
channel Default_log {
file "data/default.log";
severity dynamic;
print-severity yes;
print-time yes;
};
channel Query_log {
file "data/query.log";
severity dynamic;
print-severity yes;
print-time yes;
};
category xfer-out { Named_log; };
category default { Default_log; };
category queries { Query_log; };
};
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
include "/usr/local/samba/private/named.conf";
修改 keytab 檔案權限讓 bind 可以用其來動態更新 zone:
chgrp named /usr/local/samba/private
chgrp named /usr/local/samba/private/dns.keytab
chmod g+r /usr/local/samba/private/dns.keytab
重啟 named 服務:
systemctl restart named.service
確認 DNS 主機 IP:
(加入已存在的網域第一順位 DNS IP 指向 PDC。建置新的 DC 網域第一順位 DNS IP 指向本機。如此 DNS 動態更新記錄才能正確同步。)
vi /etc/resolv.conf
nameserver 192.168.1.253
nameserver 192.168.1.252
#domain tw.company
search tw.company
如有修改需重啟網路服務:
systemctl restart network.service
系統時間校正:
ntpdate -B time.stdtime.gov.tw
連結 kerberos 設定檔到 /etc 下:
mv /etc/krb5.conf /etc/krb5.conf.orig
ln -sf /usr/local/samba/private/krb5.conf /etc/krb5.conf
啟動 samba:
# samba start
測試是否可看到 Samba 預設的 netlogon 和 sysvol 資料夾:
smbclient -L localhost -U%
smbclient //localhost/netlogon -UAdministrator -c 'ls'
測試 DNS 設定(下列三項測試過了,表示正常加入 DC):
host -t SRV _ldap._tcp.tw.company (tcp port 可看到 DC Server 表示正確)
host -t SRV _kerberos._udp.tw.company (udp port 可看到 DC Server 表示正確)
host -t A centos7-ad.tw.company
測試 krb5.conf 設定是否正確:
kinit administrator@TW.COMPANY (Domain 一定要大寫)
輸入 administrator 密碼
如出現密碼會過期,可修改讓 administrator 帳號的密碼不能過期:
設定 administrator 密碼不會過期:
samba-tool user setexpiry administrator --noexpiry
輸入 klist -e 可看到下列資訊表示正確認證:
Valid starting Expires Service principal
11/07/2016 09:41:47 11/07/2016 19:41:47 krbtgt/TW.COMPANY@TW.COMPANY
renew until 11/08/2016 09:41:42
到此如果一切正常,表示已經完成 Samba AD DC 環境建置。
將 centos7-ad 增加到反解區域:
增加反解析區域:
samba-tool dns zonecreate centos7-ad.tw.company 1.168.192.in-addr.arpa
將 DC 的 PTR 加入反解區域:
samba-tool dns add centos7-ad.tw.company 1.168.192.in-addr.arpa 252 PTR centos7-ad.tw.company
測試 DNS 動態更新:
samba_dnsupdate --verbose
samba_dnsupdate --verbose --all-name
無錯誤訊息可正常 update dns 記錄及從 PDC 同步 dns 記錄表示設定正確。
設定開機啟動 Samba DC Script:
vi /etc/init.d/samba4
#!/bin/bash
# Source function library.
. /etc/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
prog=samba
prog_dir=/usr/local/samba/sbin/
lockfile=/var/lock/subsys/$prog
start() {
[ "$NETWORKING" = "no" ] && exit 1
# [ -x /usr/sbin/ntpd ] || exit 5
# Start daemons.
echo -n $"Starting samba4: "
daemon $prog_dir/$prog -D
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
[ "$EUID" != "0" ] && exit 4
echo -n $"Shutting down samba4: "
killproc $prog_dir/$prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
stop
start
;;
reload)
echo "Not implemented yet."
exit 3
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 2
esac
建立啟動連結到 /etc/rc3.d/ :
ln -sf /etc/init.d/samba4 /etc/rc3.d/S80samba4 (數字越大,越晚啟動)
發現小問題(UBUNTU AD 無此問題),有可能是建立【啟動連結檔】時給的數字太大,太晚啟動所影響(目前改用 UBUNTU 系統,留待有機會再測試)。
備註以後參考用(此為 UBUNTU 系統設定方式):如果 B 服務要 A 服務先啟動時才能正常啟動
第一個數字愈大,代表開機時愈晚被啟動
第二個數字愈大,代表關機時愈晚被關閉
通常會把這二個數字設計成加起來等於 100
# update-rc.d -f A defaults 80 20
# update-rc.d -f B defaults 90 10
CentOS 7 平台在 SAMBA DC 主機重新開機啟動後,做 samba-tool --verbose --all-name 更新測試,發現 AD DNS 功能似乎尚未完全讀取 AD ZONE 的資訊,造成 AD DNS 功能無法正常運作。
解決方式:
chmod +x /etc/rc.local
vi /etc/rc.local
加入:
sleep 30 #延遲 30 秒
today=`date | awk '{print $2$3}'`
errorMsg=`grep 'Failed DNS update' /var/log/messages | awk '{print $1$2}' | grep "$today" | tail -n 1`
if [ "${today}" == "${errorMsg}" ]; then
systemctl restart named.service
fi
讓系統重新開機完成後,可以再重新啟動一次 DNS Server
重要:
不同平台( Windows、Linux ) 同步 GPO、Script 需注意同步後的資料夾、檔案權限。
資料夾、檔案權限錯誤,將造成 GPO 損壞無法使用。
目前發現,每次調整、修改GPO後用 samba-tool ntacl sysvolcheck 檢查都會出現錯誤,必須用 samba-tool ntacl sysvolreset 重設一次權限就恢復正常了。
SYSVOL GPO、Scripts 資料夾抄寫同步:
Linux --> Linux 可用 rsync:
在 PDC(主要 DC 伺服器):
安裝 xinetd:
apt-get install xinetd
新增 rsync 啟動檔:
vi /etc/xinetd.d/rsync
內容:
service rsync
{
disable = no
only_from = 192.168.1.0/24
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
建立 rsyncd.conf 設定檔:
vi /etc/rsyncd.conf
內容:
log file = /var/log/rsyncd.log
[SysVol]
path = /usr/local/samba/var/locks/sysvol/
comment = Samba Sysvol Share
uid = root
gid = root
read only = yes
auth users = sysvol-replication
secrets file = /usr/local/samba/etc/rsyncd.secret
建立 rsync 密碼檔:
vi /usr/local/samba/etc/rsyncd.secret
sysvol-replication:pa$$w0rd
chmod 600 /usr/local/samba/etc/rsyncd.secret
註 :如不想使用密碼檔方式,也可使用 SSH 金鑰建立免密碼的連線同步資料
重啟 xinetd 服務:
systemctl restart xinetd.service
在其他 Linux DC 伺服器上建立 rsync 密碼檔,以方便排程時不用每次入密碼:
確認是否有 rsync 套件,沒有則安裝: yum install rsync
vi /usr/local/samba/etc/rsync-sysvol.secret
pa$$w0rd (與 rsync Server 相同)
測試同步指令是否能正常運作:
--dry-run 不實際執行傳送,只顯示將會有的傳輸動作
-delete-after 讓檔案傳送後,再執行刪除檔案動作
--password-file=PASSWORD_FILE 從檔案讀取與遠端rsync伺服器連結的密碼
--delete 刪除傳送端已經不存在,而目的端存在的檔案
-A --acls 保留 ACL 權限
-X --xattrs 保留額外的檔案屬性
-a --archive archive mode 權限保存模式,相當於 -rlptgoD 參數
-p --perms 保留檔案權限
-q --quiet 安靜模式,幾乎沒有訊息產生。常用在以 cron 執行 rsync
-r --recursive 對子目錄以遞迴模式處理
-u --update 僅僅進行更新,也就是略過所有已經存在於目的端,且文件時間比要備份的檔案為新。(不覆蓋較新的文件)
-v --verbose 複雜的輸出訊息
-z --compress 壓縮模式,當資料在傳送到目的端進行檔案壓縮
-o --owner 保留檔案擁有者資訊(root only)
-g --group 保留檔案所屬群組資訊
-D --devices 保留設備檔案資訊(root only)
-t --times 保留檔案時間資訊
測試 rsync 同步指令參數:
rsync --dry-run -XAavrz --delete-after --password-file=/usr/local/samba/etc/rsync-sysvol.secret rsync://sysvol-replication@(主PDC_IP)/SysVol/ /usr/local/samba/var/locks/sysvol/
排程執行同步:
*/15 * * * * rsync -XAaqruz --delete-after --password-file=/usr/local/samba/etc/rsync-sysvol.secret rsync://sysvol-replication@(主PDC_IP)/SysVol/ /usr/local/samba/var/locks/sysvol/
Linux --> Windows 可用 Robocopy:
動作:
啟動程式
程式或指令碼:
C:\Windows\SysWOW64\Robocopy.exe
新增引數:
\\centos7-ad\sysvol\ D:\AD\SYSVOL\sysvol\ /mir /sec
Samba AD DC Troubleshooting 網頁: https://wiki.samba.org/index.php/Samba_AD_DC_Troubleshooting
微軟 遠端伺服器管理工具(RSAT) 連線管理 DC 伺服器:
參考資料: