CentOS 7.3 安装配置Samba服务器

1背景

转到Linux有段时间了,vim操作还不能应对工程代码,之前一直都是Gnome桌面 + Clion 作开发环境,无奈在服务器上没有这样的环境,

看同事是(Windows)Source Insight + WinSCP + Linux 开发,来回同步文件有点麻烦,所以想尝试搭个Samba服务器做共享文件。

不过希望以后还是要转到vim上来。

2环境

CentOS系统

[root@linuxidc.com ~]# cat /etc/RedHat-release 
CentOS Linux release 7.3.1611 (Core) 

Samba服务器

[root@linuxidc.com ~]# rpm -qi samba
Name        : samba
Epoch       : 0
Version     : 4.4.4
Release     : 9.el7
Architecture: x86_64
Install Date: Sun 18 Dec 2016 11:59:56 PM CST
Group       : System Environment/Daemons
Size        : 1869290
License     : GPLv3+ and LGPLv3+
Signature   : RSA/SHA256, Mon 21 Nov 2016 04:38:30 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : samba-4.4.4-9.el7.src.rpm
Build Date  : Mon 07 Nov 2016 06:31:03 PM CST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.samba.org/
Summary     : Server and Client software to interoperate with Windows machines
Description :
Samba is the standard Windows interoperability suite of programs for Linux and
Unix. 

3安装

[root@base ~]# yum -y install samba samba-client

4配置

进入samba配置目录

[root@base ~]# cd /etc/samba/

备份smb.conf

[root@base samba]# mv smb.conf smb.conf.origin

新建smb.conf

[root@base samba]# vim smb.conf

内容如下,保存并退出

[global]
        workgroup = WORKGROUP
        server string = Ted Samba Server %v
        netbios name = TedSamba
        security = user
        map to guest = Bad User
        passdb backend = tdbsam

[FileShare]
        comment = share some files
        path = /smb/fileshare
        public = yes
        writeable = yes
create mask = 0644
directory mask = 0755 [WebDev] comment = project development directory path = /smb/webdev valid users = ted write list = ted printable = no create mask = 0644 directory mask = 0755

注释:

workgroup 项应与 Windows 主机保持一致,这里是WORKGROUP

security、map to guest项设置为允许匿名用户访问

再下面有两个section,实际为两个目录,section名就是目录名(映射到Windows上可以看见)。

第一个目录名是FileShare,匿名、公开、可写

第二个目录吗是WebDev,限定ted用户访问

默认文件属性644/755(不然的话,Windows上在这个目录下新建的文件会有“可执行”属性)

创建用户

[root@base samba]# groupadd co3
[root@base samba]# useradd ted -g co3 -s /sbin/nologin
[root@base samba]# smbpasswd -a ted
New SMB password:
Retype new SMB password:
Added user ted.
[root@base samba]# 

注意这里smbpasswd将使用系统用户。设置密码为1

创建共享目录

[root@base samba]# mkdir -p /smb/{fileshare,webdev}
[root@base samba]# chown nobody:nobody /smb/fileshare/
[root@base samba]# chown ted:co3 /smb/webdev/

注意设置属性,不然访问不了。

启动Samba服务,设置开机启动

[root@base samba]# systemctl start smb
[root@base samba]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[root@base samba]# 

开放端口

[root@base samba]# firewall-cmd --permanent --add-port=139/tcp
success
[root@base samba]# firewall-cmd --permanent --add-port=445/tcp
success
[root@base samba]# systemctl restart firewalld
[root@base samba]# 

或者直接把防火墙关了也行。

5使用

本机测试

可以使用testparm测试samba配置是否正确

[root@base samba]# testparm 
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[FileShare]"
Processing section "[WebDev]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
    netbios name = TEDSAMBA
    server string = Ted Samba Server %v
    map to guest = Bad User
    security = USER
    idmap config * : backend = tdb


[FileShare]
    comment = share some files
    path = /smb/fileshare
    guest ok = Yes
    read only = No


[WebDev]
    comment = project development directory
    path = /smb/webdev
    create mask = 0644

valid users = ted write list = ted [root@base samba]#

root用户的话,不用密码可直接查看samba服务器情况

[root@base samba]# smbclient -L localhost 
Enter root's password: 
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.4.4]

    Sharename       Type      Comment
    ---------       ----      -------
    FileShare       Disk      share some files
    WebDev          Disk      project development directory
    IPC$            IPC       IPC Service (Ted Samba Server 4.4.4)
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.4.4]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
[root@base samba]# 

Linux访问

Samba服务端配置

[root@linuxidc.com webdev]# vim /etc/samba/smb.conf

内容如下

[global]        
        workgroup = WORKGROUP
        server string = Samba Server Version %v
        netbios name = MYSERVER
        security = user
        passdb backend = tdbsam

[tvms]
        path = /root/tvms-test
        public = yes
        valid user = root
        writeable = yes
        printable = no
        create mask = 0644
        directory mask = 0755

在局域网内另外一台机器上,

挂载目录

安装cifs

[root@linuxidc.com ~]# yum -y install cifs-utils

挂载smb目录

[root@linuxidc.com ~]# mkdir /mnt/tvms
[root@linuxidc.com ~]# mount //192.168.118.133/tvms /mnt/tvms
Password for root@//192.168.118.133/tvms:  *
[root@linuxidc.com ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        18G  988M   17G   6% /
devtmpfs                devtmpfs  479M     0  479M   0% /dev
tmpfs                   tmpfs     489M     0  489M   0% /dev/shm
tmpfs                   tmpfs     489M  6.7M  483M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  117M  381M  24% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0
//192.168.118.133/tvms  cifs       18G  8.5G  9.1G  49% /mnt/tvms
[root@linuxidc.com ~]# ll /mnt/tvms/
total 3952
drwxr-xr-x. 10 root root       0 Dec 21 03:40 suricata-3.1.2
-rw-r--r--.  1 root root 4042824 Dec 21 03:22 suricata-3.1.2.zip
[root@linuxidc.com ~]# 

输入密码:1

卸载目录

[root@linuxidc.com mnt]# umount /mnt/tvms/

如果出现

umount: /mnt/tvms: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

可以使用fuser卸载

[root@linuxidc.com ~]# yum -y install psmisc
[root@linuxidc.com ~]# fuser -m -v -i -k /mnt/tvms
                          USER        PID ACCESS COMMAND
/mnt/tvms:                root     kernel mount /mnt/tvms
                          root       2760 ..c.. bash
Kill process 2760 ? (y/N) y

Connection closed by foreign host.

Disconnected from remote host(192.168.118.132) at 19:46:12.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

Connecting to 192.168.118.132:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed Dec 21 03:27:53 2016 from 192.168.118.1
[root@linuxidc.com ~]# umount /mnt
[root@linuxidc.com ~]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  18307072 1011444  17295628   6% /
devtmpfs                   490236       0    490236   0% /dev
tmpfs                      500680       0    500680   0% /dev/shm
tmpfs                      500680    6836    493844   2% /run
tmpfs                      500680       0    500680   0% /sys/fs/cgroup
/dev/sda1                  508588  118860    389728  24% /boot
tmpfs                      100136       0    100136   0% /run/user/0

自动挂载目录

[root@linuxidc.com ~]# vi /etc/fstab 

添加如下内容

//192.168.118.133/tvms  /mnt/tvms               cifs    defaults,username=root,password=1       0 0

重新挂载

[root@linuxidc.com ~]# mount -a
[root@linuxidc.com ~]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  18307072 1012316  17294756   6% /
devtmpfs                   490236       0    490236   0% /dev
tmpfs                      500680       0    500680   0% /dev/shm
tmpfs                      500680    6836    493844   2% /run
tmpfs                      500680       0    500680   0% /sys/fs/cgroup
/dev/sda1                  508588  118860    389728  24% /boot
tmpfs                      100136       0    100136   0% /run/user/0
//192.168.118.133/tvms   18307072 8819380   9487692  49% /mnt/tvms
[root@linuxidc.com ~]# 
上一篇:Linux到Windows – 可以列出smb共享但无法连接


下一篇:如何跨SMB安装进行PHP复制