Samba 安裝教學
在我們使用 Windows 作為客戶機的時候,通常有文件、印表機共享的需求。作為Windows 的網路功能之一,通常可以在 Windows 用戶端之間通過 Windows Network 固有的功能實現這些要求。然而,通過 Samba 我們也可以讓一臺 <STRONG>CentOS</STRONG> 主機來兼容 Windows 網路,實現同樣的功能,進而充分發揮 <STRONG>CentOS</STRONG> 主機的可用性。<BR><BR>本篇文檔主要介紹怎樣通過 Samba 服務器的構建實現 Windows 網路中的文件共享。<BR><BR>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD bgColor=#4665a6>
<TABLE border=0>
<TBODY>
<TR>
<TD bgColor=#333333><SPAN class=table-title><FONT color=white>安裝 Samba </FONT></SPAN></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>首先,通過 yum 來在線安裝 Samba 。<BR><BR>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD bgColor=#000000><FONT color=white><SPAN class=message>[root@sample ~]#</SPAN> <SPAN class=command>yum -y install samba</SPAN> <SPAN class=intro><FONT color=red>← 安裝 Samba</FONT> </SPAN><BR><BR></FONT><SPAN class=message><FONT color=white>Setting up Install Process<BR>Setting up repositories<BR>update 100% |=========================| 951 B 00:00<BR>base 100% |=========================| 1.1 kB 00:00<BR>addons 100% |=========================| 951 B 00:00<BR>extras 100% |=========================| 1.1 kB 00:00<BR>Reading repository metadata in from local files<BR>primary.xml.gz 100% |=========================| 73 kB 00:00<BR>update : ################################################## 212/212<BR>Added 212 new packages, deleted 0 old in 4.94 seconds<BR>primary.xml.gz 100% |=========================| 569 kB 00:00<BR>base : ################################################## 1500/1500<BR>Added 1500 new packages, deleted 0 old in 28.97 seconds<BR>primary.xml.gz 100% |=========================| 157 B 00:00<BR>Added 0 new packages, deleted 0 old in 0.03 seconds<BR>primary.xml.gz 100% |=========================| 32 kB 00:00<BR>extras : ################################################## 124/124<BR>Added 124 new packages, deleted 0 old in 1.93 seconds<BR>Parsing package install arguments<BR>Resolving Dependencies<BR>--> Populating transaction set with selected packages. Please wait.<BR>---> Downloading header for samba to pack into transaction set.<BR>samba-3.0.10-1.4E.9.i386. 100% |=========================| 101 kB 00:00<BR>---> Package samba.i386 0:3.0.10-1.4E.9 set to be updated<BR>--> Running transaction check<BR>--> Processing Dependency: samba-common = 0:3.0.10 for package: samba<BR>--> Restarting Dependency Resolution with new changes.<BR>--> Populating transaction set with selected packages. Please wait.<BR>---> Downloading header for samba-common to pack into transaction set.<BR>samba-common-3.0.10-1.4E. 100% |=========================| 37 kB 00:00<BR>---> Package samba-common.i386 0:3.0.10-1.4E.9 set to be updated<BR>--> Running transaction check </FONT></SPAN>
<P class=message><FONT color=white>Dependencies Resolved</FONT></P>
<P class=message><FONT color=white>=============================================================================<BR>Package Arch Version Repository Size<BR>=============================================================================<BR>Installing:<BR>samba i386 3.0.10-1.4E.9 base 13 M<BR>Installing for dependencies:<BR>samba-common i386 3.0.10-1.4E.9 base 5.0 M</FONT></P>
<P class=message><FONT color=white>Transaction Summary<BR>=============================================================================<BR>Install 2 Package(s)<BR>Update 0 Package(s)<BR>Remove 0 Package(s)<BR>Total download size: 18 M<BR>Downloading Packages:<BR>(1/2): samba-common-3.0.1 100% |=========================| 5.0 MB 00:04<BR>(2/2): samba-3.0.10-1.4E. 100% |=========================| 13 MB 00:11<BR>warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID 443e1821<BR>Public key for samba-common-3.0.10-1.4E.9.i386.rpm is not installed<BR>Retrieving GPG key from</FONT><A href="http://mirror.centos.org/centos/RPM-GPG-KEY-centos4" target=_blank><FONT color=white>http://mirror.centos.org/centos/RPM-GPG-KEY-centos4</FONT></A><BR><FONT color=white>Importing GPG key 0x443E1821 "<STRONG>CentOS</STRONG>-4 key <
[email protected]>"<BR>Key imported successfully<BR>Running Transaction Test<BR>Finished Transaction Test<BR>Transaction Test Succeeded<BR>Running Transaction<BR>Installing: samba-common ######################### [1/2]<BR>Installing: samba ######################### [2/2]</FONT></P><FONT color=white><SPAN class=message>Installed: samba.i386 0:3.0.10-1.4E.9<BR>Dependency Installed: samba-common.i386 0:3.0.10-1.4E.9<BR>Complete!</SPAN><BR></FONT></TD></TR></TBODY></TABLE><BR>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD bgColor=#4665a6>
<TABLE border=0>
<TBODY>
<TR>
<TD bgColor=#333333><SPAN class=table-title><FONT color=white>配置 Samba </FONT></SPAN></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<P><FONT color=white>然後,通過編輯 /etc/samba/smb.conf ,根據需求配置 Samba。在這裡,本文的原則是隻將文件共享應用於內網,並讓將要被共享的目錄擁有充分的讀寫權限屬性。</FONT></P>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD bgColor=#000000><FONT color=white><SPAN class=message>[root@sample ~]#</SPAN> <SPAN class=command>vi /etc/samba/smb.conf</SPAN><SPAN class=intro><SPAN class=message> </SPAN><FONT color=red>← 編輯 Samba 的配置文件</FONT></SPAN><BR><BR></FONT><SPAN class=message><FONT color=white>#======================= Global Settings =====================================<BR>[global] </FONT><FONT color=white><SPAN class=intro>← 找到這一行(全局設置標簽),在此行下面添加如下行:<BR></SPAN><BR></FONT></SPAN><SPAN class=after><FONT color=white>dos charset= big5<SPAN class=message> <SPAN class=intro>← 將 Windows 客戶端的文字編碼設置為簡體中文 GB2312</SPAN></SPAN><BR>unix charset= big5<SPAN class=message> <SPAN class=intro>← 指定 Samba 所在的 <STRONG>CentOS</STRONG> 服務端新建文件或目錄時的編碼為 GB2312 </SPAN></SPAN><BR>display charset= big5<SPAN class=message> <SPAN class=intro>← 指定使用 SWAT(一種通過瀏覽器控制Samba的工具)時頁面的默認文字編碼 </SPAN></SPAN><BR>directory mask = 0777<SPAN class=message> <SPAN class=intro>← 指定新建目錄的屬性(以下4行)</SPAN></SPAN><BR>force directory mode = 0777<BR>directory security mask = 0777<BR>force directory security mode = 0777<BR>create mask = 0777<SPAN class=message> <SPAN class=intro>← 指定新建文件的屬性(以下4行)</SPAN></SPAN><BR>force create mode = 0777<BR>security mask = 0777<BR>force security mode = 0777</FONT></SPAN><SPAN class=message><BR><BR></SPAN><SPAN class=before><FONT color=white>workgroup = MYGROUP</FONT></SPAN><FONT color=white><SPAN class=message> <SPAN class=intro>← 找到此行,將工作組名稱改為 Windows 網絡所定義的工作組名</SPAN><BR><SPAN class=intro>↓</SPAN><BR></SPAN><SPAN class=after>workgroup = WORKGROUP</SPAN></FONT><FONT color=white><SPAN class=message> <SPAN class=intro>← 變為此狀態,這裡以 Windows XP 默認的“WORKGROUP”為例</SPAN><BR><BR></SPAN><SPAN class=before>; hosts allow = 192.168.1. 192.168.2. 127.</SPAN></FONT><FONT color=white><SPAN class=message> <SPAN class=intro>← 找到此行,去掉行首的“;”,並制定訪問限制</SPAN><BR><SPAN class=intro>↓</SPAN><BR></SPAN><SPAN class=after>hosts allow = 192.168.0. 127.</SPAN><SPAN class=message> <SPAN class=intro>← 變為此狀態,指定內網IP地址及本地,隻允許這兩種情況的訪問</SPAN></SPAN><BR><BR><SPAN class=intro>然後在配置文件的末尾填如下幾行,定義公眾共享目錄:</SPAN><BR><BR></FONT><SPAN class=after><FONT color=white>[public]<BR>comment = Public Stuff<BR>path = /home/samba<SPAN class=message> <SPAN class=intro>← 指定共享目錄位置</SPAN></SPAN><BR>public = yes<BR>writable = yes<SPAN class=message> <SPAN class=intro>← 賦予共享目錄寫入權限的屬性</SPAN></SPAN></FONT></SPAN></TD></TR></TBODY></TABLE>
<P><FONT color=white>接下來,創建將要通過 Samba 共享給 Windows 網絡的專用目錄。</FONT></P>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD bgColor=#000000><FONT color=white><SPAN class=message>[root@sample ~]# </SPAN><SPAN class=command>mkdir /home/samba</SPAN></FONT><FONT color=white><SPAN class=message> <SPAN class=intro>← 建立共享文件專用目錄</SPAN><BR><BR>[root@sample ~]# </SPAN><SPAN class=command>chown -R nobody. /home/samba</SPAN></FONT><FONT color=white><SPAN class=message> <SPAN class=intro>← 設置專用目錄歸屬為 nobody </SPAN><BR><BR>[root@sample ~]# </SPAN><SPAN class=command>chmod 777 /home/samba<SPAN class=message> <SPAN class=intro>← 將專用目錄屬性設置為 777 </SPAN></SPAN></SPAN></FONT></TD></TR></TBODY></TABLE><BR><FONT color=black>在進行到服務端的連接之前,需要預先對用於登錄 Samba 的用戶進行設置。這裡我們以 <STRONG>CentOS</STRONG> 中 Samba 標準的 Samba 用戶數據庫管理工具“smbpasswd”為例,創建用於登錄 Samba 的用戶數據。這裡需要注意的一點:用 smbpasswd 創建用戶的前提是,繫統用戶中存在該用戶 -- 在基於繫統用戶之上,纔可以創建該用戶在 Samba 用戶數據庫中的信息。<BR><BR></FONT>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD class=message bgColor=#000000 height=84><FONT color=white>[root@sample ~]# <SPAN class=command>smbpasswd -a centospub</SPAN> <SPAN class=intro>← 將繫統用戶 centospub(例)加入到 Samba 用戶數據庫</SPAN><BR><BR>New SMB password: <SPAN class=intro>← 輸入該用戶用於登錄 Samba 的密碼</SPAN><BR>Retype new SMB password: <SPAN class=intro>← 再次確認輸入該密碼</SPAN><BR>Added user centospub.</FONT></TD></TR></TBODY></TABLE><BR>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD bgColor=#4665a6>
<TABLE border=0>
<TBODY>
<TR>
<TD bgColor=#333333><SPAN class=table-title>啟動 Samba 服務</SPAN></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<P>在啟動 Samba 服務之前,首先將防火牆設置中 Samba 所用到的端口進行開放。</P>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD class=message bgColor=#000000><FONT color=white>[root@sample ~]# <SPAN class=command>vi /etc/sysconfig/iptables</SPAN> <SPAN class=intro>← 編輯 iptables 配置文件</SPAN><BR><BR>-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT <SPAN class=intro>← 找到此行,在下面添加如下行:</SPAN><BR></FONT><SPAN class=after><FONT color=white>-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT<BR>-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT<BR>-A RH-Firewall-1-INPUT -p udp -m udp --dport 137 -j ACCEPT<BR>-A RH-Firewall-1-INPUT -p udp -m udp --dport 138 -j ACCEPT</FONT></SPAN><SPAN class=intro><BR></SPAN><BR><FONT color=white>[root@sample ~]# <SPAN class=command>/etc/rc.d/init.d/iptables restart</SPAN> <SPAN class=intro>← 重新啟動 iptables ,使新的規則生效 </SPAN><BR>Flushing firewall rules: [ <SPAN class=after>OK</SPAN> ]<BR>Setting chains to policy ACCEPT: filter [ <SPAN class=after>OK</SPAN> ]<BR>Unloading iptables modules: [ <SPAN class=after>OK</SPAN> ]<BR>Applying iptables firewall rules: [ <SPAN class=after>OK</SPAN> ]</FONT><SPAN class=intro><BR></SPAN></TD></TR></TBODY></TABLE>
<P>最後,啟動 Samba 服務。</P>
<TABLE width="100%" border=2>
<TBODY>
<TR>
<TD class=message bgColor=#000000><FONT color=white>[root@sample ~]# <SPAN class=command>chkconfig smb on</SPAN> <SPAN class=intro>← 設置 Samba 自啟動</SPAN><BR><BR>[root@sample ~]# <SPAN class=command>chkconfig --list smb <SPAN class=intro>← 確認 Samba 啟動標簽,確認 2-5 為 on 的狀態</SPAN></SPAN><BR>smb 0:off 1:off <SPAN class=att1>2:on 3:on 4:on 5:on</SPAN> 6:off<BR><BR>[root@sample ~]# <SPAN class=command>/etc/rc.d/init.d/smb start</SPAN> <SPAN class=intro>← 啟動 Samba 服務</SPAN><BR>Starting SMB services: [ <SPAN class=after>OK</SPAN> ]<BR>Starting NMB services: [ <SPAN class=after>OK</SPAN> ]</FONT></TD></TR></TBODY></TABLE><BR>