CentOS 7.2 和 Ubuntu 16.04 安装配置samba

时间:2021-08-14 15:38:59

CentOS 7.2

系统信息

[lipl@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[lipl@localhost ~]$ 

要确保虚拟机中centOS能够上网,
主机能够被ping通

安装samba

安装

sudo yum -y install samba

查看

[root@localhost samba]# rpm -qa | grep samba
samba-4.6.2-12.el7_4.x86_64
samba-client-libs-4.6.2-12.el7_4.x86_64
samba-common-4.6.2-12.el7_4.noarch
samba-common-libs-4.6.2-12.el7_4.x86_64
samba-libs-4.6.2-12.el7_4.x86_64
samba-client-4.6.2-12.el7_4.x86_64
samba-common-tools-4.6.2-12.el7_4.x86_64

创建共享目录

[lipl@localhost ~]$ sudo mkdir /home/share
[lipl@localhost ~]$ sudo chmod 777 /home/share 

测试中发现,如果共享目录建立在用户目录下,windows会无法访问。后来发现在smb.conf的[global]里配置force user后可以解决该问题。

samba配置

备份

[lipl@localhost ~]$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orign

编辑

[lipl@localhost ~]$ sudo vi /etc/samba/smb.conf

末尾添加

[global]
        workgroup = SAMBA
        security = user

        passdb backend = tdbsam
        map to guest = Bad User

        force user = lipl
        force group = lipl

        hosts allow = 127. 192.168.10.

        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw


# 中间部分省略
[Share]
        path = /home/share
        writable = yes
        guest ok = yes
        guest only = yes
        create mode = 0777
        directory mode = 0755

lipl是我的centOS的用户名和用户组。如果没有设置force user 和 force group, windows下匿名创建的文件的归属为nobody。 lipl用户没有权限修改该文件和文件夹。
hosts allow 中的192.168.10为我的虚拟机的网段

启动

[lipl@localhost ~]$ systemctl start smb nmb
[lipl@localhost ~]$ systemctl enable smb nmb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/nmb.service to /usr/lib/systemd/system/nmb.service.
[lipl@localhost ~]$ 

配置防火墙

注意,centOS 7.2 使用了firewall作为防火墙,而不是iptables。

[lipl@localhost ~]$ sudo firewall-cmd --add-service=samba --permanent
success
[lipl@localhost ~]$ sudo firewall-cmd --reload
success
[lipl@localhost ~]$ 

配置SELinux

[lipl@localhost ~]$ sudo  setsebool -P samba_enable_home_dirs on 
[lipl@localhost ~]$ sudo restorecon -R /home/share

windows 尝试访问

直接在资源管理器输入\\192.168.10.142, 无需输入用户名密码。

Ubuntu 16.04

系统信息

lipl@ubuntu:/etc/samba$ uname -a
Linux ubuntu 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
lipl@ubuntu:/etc/samba$ 

要确保虚拟机中centOS能够上网,
主机能够被ping通

安装samba

安装

sudo apt install samba*

samba 配置

lipl@ubuntu:/etc/samba$ sudo cp smb.conf smb.conf.orig
lipl@ubuntu:/etc/samba$ sudo vi smb.conf

具体内容参考 centOS7.2的配置

创建共享目录

lipl@ubuntu:~$ sudo mkdir /home/share
lipl@ubuntu:~$ sudo chmod 777 /home/share

启动

lipl@ubuntu:~$ sudo /etc/init.d/samba restart

windows下访问

相关文章