Cài đặt và cấu hình samba trên linux

Như bạn đã biết, Samba là một nền tảng mã nguồn mở, và là một gói phần mềm miễn phí cung cấp các dịch vụ file và máy in đến cho cho các máy Clients. Samba cho phép người dùng chia sẻ file, folders và máy in giữa các máy Linux và máy Windows. Với phương án sử dụng Samba, bạn có thể dựng một máy chủ domain controller trên nền tảng Unix/Linux, và tích hợp máy con chạy Windows vào hệ thống Domain Controller này.

An Ninh Mạng sẽ mô tả cho bạn từng bước để cấu hình một máy chủ Samba cơ bản trên hệ điều hành CentOS 7. Hướng dẫn này cũng có thể thực hiện được trên các distro khác như Redhat Enterprise Linux và Scientific Linux 7.

Để chuẩn bị tiến hành cho bài Lab, cần 2 máy tính với hệ thống như sau :

–  Máy 1 : Samba Server

Hệ điều hành : CentOS 7 Server

Hostname : server.anninhmang.edu.vn

Địa Chỉ IP :   192.168.1.1/ 255.255.255.0

–  Máy 2 : Windows 7 Ultimate

Hostname : Windows 7

Địa chỉ IP : 192.168.1.2/ 255.255.255.0

Tiến hành cài đặt Samba :

–  Kiểm tra các gói Samba đã được cài đặt trên máy bằng dòng lệnh :

Rpm –qa | grep samba

Yum list installed | grep samba

–  Nếu có gói Samba nào đã được cài đặt, gỡ bỏ nó bằng câu lệnh :

Yum remove samba*

–  Sau khi hoàn thành, bắt đầu tiến hành cài đặt Samba :

Yum install samba* -y

++ Cấu hình truy cập share cho bất kì tài khoản nào ( anonymous ) :

Với tùy chọn này, bất kì tài khoản nào cũng có thể thực thi quyền Read/Write trên thư mục được share. Nào chúng ta cùng bắt đầu cấu hình :

–  Tạo một thư mục tại địa chỉ : /samba/anonymous_share và tiến hành set full permission cho thư mục. Bạn cũng có thể đặt tên thư mục tùy theo ý thích của mình.

mkdir -p /samba/anonymous_share
chmod -R 0777 /samba/anonymous_share

–  Edit file cấu hình Samba :

vi /etc/samba/smb.conf

–  Tìm đến những dòng này, và tiến hành thay đổi như bên dưới :

[...]




## Add the following lines under [global] section ##

unix charset = UTF-8

dos charset = CP932




## Change the to windows default workgroup ##

workgroup = WORKGROUP




## Uncomment and set the IP Range ##

hosts allow = 127. 192.168.1.




## Uncomment ##

max protocol = SMB2




## Uncomment, and change the value of 'Security' to 'user' ##

security = user




## Add the following line ##

map to guest = Bad User




## Add the following lines at the bottom ##

[Anonymous share]

path = /samba/anonymous_share

writable = yes

browsable = yes

guest ok = yes

guest only = yes

create mode = 0777

directory mode = 0777

– Tiến hành khởi động dịch vụ Samba, và để Samba tự khởi động sau mỗi lần Restart máy :

systemctl start smb
systemctl start nmb
systemctl enable smb
systemctl enable nmb

– Test cấu hình Samba Server :

Lệnh Testparm được sử dụng khi muốn thực hiện test Samba Server :

Testparm

–  Kết quả sẽ ra như sau :

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 "[Anonymous share]"

Loaded services file OK.

WARNING: You have some share names that are longer than 12 characters.

These may not be accessible to some older clients.

(Eg. Windows9x, WindowsMe, and smbclient prior to Samba 3.0.)

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions




[global]

    dos charset = CP932

    netbios name = UNIXMEN SAMBA SERVER

    server string = Samba Server Version %v

    map to guest = Bad User

    log file = /var/log/samba/log.%m

    max log size = 50

    server max protocol = SMB2

    idmap config * : backend = tdb

    hosts allow = 127., 192.168.1.

    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




[Anonymous share]

    path = /samba/anonymous_share

    read only = No

    create mask = 0777

    directory mask = 0777

    guest only = Yes

    guest ok = Yes

–  Cấu hình Firewall :

Cấu hình Firewall là việc cần thiết nếu bạn muốn máy con có thể truy cập được file share trên Samba Server. Tiến hành thêm những dòng lệnh sau :

firewall-cmd --permanent --add-port=137/tcp
firewall-cmd --permanent --add-port=138/tcp
firewall-cmd --permanent --add-port=139/tcp
firewall-cmd --permanent --add-port=445/tcp
firewall-cmd --permanent --add-port=901/tcp

–  Restart lại Firewall để những thay đổi có hiệu lực :

firewall-cmd --reload

–  Cấu hình SE Linux :

Chỉnh giá trị samba_enable_home_dirs thành On nếu bạn muốn share thư mục home qua Samba :

setsebool -P samba_enable_home_dirs on

Nếu bạn tạo một thư mục mới, chẳng hạn như là các thư mục ở gốc, hãy gắn nhãn chúng là samba_share_t, khi đó SELinux sẽ để cho Samba đọc và viết trên nó. Hãy nhớ đừng gắn chúng với các nhãn như /etc/ /home/.

Ở trường hợp của chúng ta, đã tạo thư mục anonymous, vậy hãy gắn nhãn nó như bên dưới :

chcon -t samba_share_t /samba/anonymous_share/

Còn nếu chúng ta không muốn bị bối rối với SELinux, hãy disable nó theo hướng dẫn bên dưới :

Mở file SELinux theo đường dẫn /etc/sysconfig/selinux :

vi /etc/sysconfig/selinux

–  Set giá trị của SELinux thành disable :

# 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 - No SELinux policy is loaded.

SELINUX=disabled

# SELINUXTYPE= can take one of these two values:

# targeted - Targeted processes are protected,

# mls - Multi Level Security protection.

SELINUXTYPE=targeted

–  Reset máy để thay đổi có hiệu lực.

++  Test Samba Shares :

Bây giờ chúng ta bắt đầu chuyển sang máy Client. Ở ví dụ này, người viết dùng Windows 7.

Bấm vào Start -> run. Điền vào địa chỉ IP của máy Samba server theo hình bên dưới.

Cài đặt và cấu hình samba trên linux

Bạn đã có thể truy cập được vào thư mục share.

Cài đặt và cấu hình samba trên linux

Bạn có thể tiến hành thêm, sửa, xóa, tùy chỉnh các file/folder bên trong thư mục. Ví dụ, người viết tạo một folder anninhmang bên trong thư mục share.

Cài đặt và cấu hình samba trên linux

–  Kiểm tra folder vừa được tạo với lệnh :

ls -l /samba/anonymous_share/

–  Được kết quả như sau :

total 0

drwxrwxrwx. 2 nobody nobody 6 Sep 26 17:55 anninhmang

Nếu hiện kết quả như vậy đồng nghĩa với việc folder đã được tạo trong folder /samba/anonymous_share/ .

  1. Tạo thư mục share yêu cầu chứng thực :

ở hướng dẫn này, chúng ta sẽ cùng nhau tìm hiểu cách cấu hình để Samba yêu cầu chứng thực tài khoản nếu muốn truy cập :

–  Tạo một user với tên là “security” và group tên “smbgroup” :

useradd -s /sbin/nologin security
groupadd smbgroup

–  Gán user security vào group smbgroup và set samba password cho user :

usermod -a -G smbgroup security
smbpasswd -a anninhmang

–  Tạo một folder tên “/samba/secure_share” và gán quyền :

mkdir /samba/secure_share
chmod -R 0755 /samba/secure_share
chown -R security:smbgroup /samba/secure_share

–  Cấu hình file Samba Config :

vi /etc/samba/smb.conf

–  Thêm vào những dòng sau :

[secure_share]

path = /samba/secure_share

writable = yes

browsable = yes

guest ok = no

valid users = @smbgroup

–  Test lại bằng lệnh :

Testparm

–  Kết quả sẽ ra như sau :

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 "[Anonymous share]"

Processing section "[secure_share]"

Loaded services file OK.

WARNING: You have some share names that are longer than 12 characters.

These may not be accessible to some older clients.

(Eg. Windows9x, WindowsMe, and smbclient prior to Samba 3.0.)

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions




[global]

    dos charset = CP932

    netbios name = UNIXMEN SAMBA SERVER

    server string = Samba Server Version %v

    map to guest = Bad User

    log file = /var/log/samba/log.%m

    max log size = 50

    server max protocol = SMB2

    idmap config * : backend = tdb

    hosts allow = 127., 192.168.1.

    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




[Anonymous share]

    path = /samba/anonymous_share

    read only = No

    create mask = 0777

    directory mask = 0777

    guest only = Yes

    guest ok = Yes




[secure_share]

    path = /samba/secure_share

    valid users = @smbgroup

    read only = No

Gán nhãn samba_share_t vào thư mục /samba/secure_share để SELinux cho phép Samba đọc và viết trên nó :

chcon -t samba_share_t /samba/secure_share/

Restart lại samba service

systemctl restart smb

systemctl restart nmb

++ Test Samba shares :

Bây giờ đi đến máy Windows, kiểm tra thư mục secure_share

Cài đặt và cấu hình samba trên linux

Click đúp chuột vào thư mục secure_share, bạn sẽ được yêu cầu nhập username và password để truy cập được vào folder :

Cài đặt và cấu hình samba trên linux

Sau khi nhập đúng username : security, password : anninhmang, bạn sẽ có thể truy cập được vào thư mục secure_share.

Cài đặt và cấu hình samba trên linux

Như vậy là samba share đã sẵn sàng để được sử dụng.

Xem thêm : Cấu Hình Samba Primary Domain Controller Trên CentOS 7