Samba được dùng để cho phép các users chia sẻ và sử dụng files, thư mục và máy in giữa Linux và Windows.
Samba server:
Operating system : CentOS 6.5 Hostname : sambaserver.lab IP Address : 192.168.1.16/24
Samba client:
Operating system : Windows 7 Professional Hostname : client IP Address : 192.168.1.1/24
Cài đặt Samba trên Server
[root@sambaserver ~]# yum install samba* -y
Cấu hình thư mục chia sẻ đẩy đủ quyền truy cập
Tạo thư mục /samba/share/ với full permission
[root@sambaserver ~]# mkdir -p /samba/share [root@sambaserver ~]# chmod 777 /samba/share
Sửa và thêm các dòng sau vào file cấu hình samba
[root@sambaserver ~]# vi /etc/samba/smb.conf #dòng 58 - Thêm những dòng sau ở dòng 58 unix charset = UTF-8 dos charset = CP932 #dòng 75 - đổi thành WORKGROUP workgroup = WORKGROUP #dòng 81 - Bỏ comment và chỉnh sửa lại IP range hosts allow = 127. 192.168.8 #dòng 102 - đổi thành không cần xác thực security = share # Thêm những dòng sau ở cuối file [myshare] path = /samba/share writable = yes browsable = yes guest ok = yes guest only = yes create mode = 0777 directory mode = 0777
:wq # lưu lại
Start Samba Server
[root@sambaserver ~]# service smb start Starting SMB services: [ OK ] [root@sambaserver ~]# service nmb start Starting NMB services: [ OK ] [root@sambaserver ~]# chkconfig smb on [root@sambaserver ~]# chkconfig nmb on
Test the Samba server
Chúng ta có thể kiểm tra cấu hình Samba server bằng lệnh testparm
[root@sambaserver ~]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[homes]" Processing section "[printers]" Processing section "[myshare]" WARNING: The security=share option is deprecated Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions [global] dos charset = CP932 server string = Samba Server Version %v security = SHARE log file = /var/log/samba/log.%m max log size = 50 idmap config * : backend = tdb hosts allow = 127., 192.168.8 cups options = raw [homes] comment = Home Directories read only = No browseable = No [printers] comment = All Printers path = /var/spool/samba printable = Yes print ok = Yes browseable = No [myshare] path = /samba/share read only = No create mask = 0777 directory mask = 0777 guest only = Yes guest ok = Yes
Thêm Rules trong iptables để cho các users bên ngoài sử dụng dịch vụ Samba
[root@sambaserver ~]# vi /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter # NetBIOS Name Service -A INPUT -s 192.168.8.0/24 -m state --state NEW -m udp -p udp --dport 137 -j ACCEPT # NetBIOS Datagram Service -A INPUT -s 192.168.8.0/24 -m state --state NEW -m udp -p udp --dport 138 -j ACCEPT # NetBIOS Session Service -A INPUT -s 192.168.8.0/24 -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT # Microsoft Directory Service -A INPUT -s 192.168.8.0/24 -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT # Samba Web Administration -A INPUT -s 192.168.8.0/24 -m state --state NEW -m tcp -p tcp --dport 901 -j ACCEPT :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
:wq #lưu lại
Restart iptables to save the changes
[root@sambaserver ~]# service iptables restart iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ]
Disable SELINUX
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
Cấu hình Server hoàn tất. Khởi động lại Server.
Bên Client
Click Start -> Run. Nhập vào địa chỉ IP của Samba Server như bên dưới.
Tạo folder và files thử xem 🙂
Kiểm tra xem các files và folder vừa tạo có trên Samba Server hay ko? 😀
[root@sambaserver ~]# ls -l /samba/share/ total 4 -rwxrw-rw- 1 nobody nobody 0 Aug 1 14:09 hello.txt drwxrwxrwx 2 nobody nobody 4096 Aug 1 14:09 TEST
Cấu hình 1 thư mục chia sẻ nhưng yêu cầu xác thực trên Samba Server
Tạo group samba và add các users vào
[root@sambaserver ~]# groupadd samba [root@sambaserver ~]# useradd -g samba sb1 [root@sambaserver ~]# passwd sb1 Changing password for user sb1. New password: BAD PASSWORD: it is WAY too short BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.
Tạo thư mục chia sẻ mới và phân quyền cho nó
[root@sambaserver ~]# mkdir /samba/share-auth [root@sambaserver ~]# chmod 755 /samba/share-auth/ [root@sambaserver ~]# chown sb1:samba /samba/share-auth/
Thêm các dòng sau ở dưới cùng của file cấu hình samba
[root@sambaserver ~]# vi /etc/samba/smb.conf [secure] path = /samba/share-auth/ writable = yes browsable = yes guest ok =no valid users = @samba
:wq #lưu lại
Kiểm tra lỗi file cấu hình bằng lệnh testparm
[root@sambaserver ~]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[homes]" Processing section "[printers]" Processing section "[myshare]" Processing section "[secure]" WARNING: The security=share option is deprecated Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions [global] dos charset = CP932 server string = Samba Server Version %v security = SHARE log file = /var/log/samba/log.%m max log size = 50 idmap config * : backend = tdb hosts allow = 127., 192.168.8. cups options = raw [homes] comment = Home Directories read only = No browseable = No [printers] comment = All Printers path = /var/spool/samba printable = Yes print ok = Yes browseable = No [myshare] path = /samba/share read only = No create mask = 0777 directory mask = 0777 guest only = Yes guest ok = Yes [secure] path = /samba/share-auth/ valid users = @samba read only = No
Restart samba server
[root@sambaserver ~]# service smb restart Shutting down SMB services: [ OK ] Starting SMB services: [ OK ] [root@sambaserver ~]# service nmb restart Shutting down NMB services: [ OK ] Starting NMB services: [ OK ]
Bây giờ qua bên Client và truy cập thư mục share-auth
Lưu ý rằng chỉ cho phép users nằm trong Domain .lab mới vô được nhé!
Cấu hình Samba Server với GUI mode (chế độ giao diện)
cài đặt gói samba-swat
[root@sambaserver ~]# yum install xined samba-swat -y
Configure SWAT
[root@sambaserver ~]# vi /etc/xinetd.d/swat
# default: off
# description: SWAT is the Samba Web Admin Tool. Use swat \
# to configure your Samba server. To use SWAT, \
# connect to port 901 with your favorite web browser.
service swat
{
port = 901
socket_type = stream
wait = no
only_from = 127.0.0.1 192.168.8.0/24
user = root
server = /usr/sbin/swat
log_on_failure += USERID
disable = no
}
:wq #lưu lại
Restart samba and xinetd services
[root@sambaserver ~]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@sambaserver ~]# service smb restart Shutting down SMB services: [ OK ] Starting SMB services: [ OK ] [root@sambaserver ~]# service nmb restart Shutting down NMB services: [ OK ] Starting NMB services: [ OK ]
Mở trình duyệt Web từ Client và gõ địa chỉ “http://server-ip-address:901” . Sau đó thì có thể cấu hình Samba server ở đây!
Chúc các bạn thành công! 🙂