关于Linux和Windows系统之间的文件传输,很多人选择使用FTP,相对较安全,但是有时还是会出现一些问题,比如上传文件时,文件名莫名出现乱码,文件大小改变等问题。相比较来说,使用Samba作为文件共享,就省事简洁多了。Samba服务器通信协议(Server Messages Block)就是是为了解决局域网内的文件或打印机等资源的共享服务问题,让多个主机之间共享文件变成越来越简单。下面简单介绍下,在Centos7下部署Samba服务的操作记录(测试机192.168.0.122)
[root@samba-server ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@samba-server ~]# rpm -qa|grep samba
[root@samba-server ~]# yum install -y samba
[root@samba-server ~]# setenforce 0
[root@samba-server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@samba-server ~]# systemctl stop firewalld
[root@samba-server ~]# systemctl disable firewalld
[root@samba-server ~]# cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@samba-server ~]# vim /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global] //全局配置
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers] //共享打印机配置
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
[durant] //这个是共享文件夹标识,表示登录samba打开时显示的文件夹名称。配置了多少个共享文件夹标识,登录samba时就会显示多少文件夹。
comment = this is the info //comment是对该共享的描述,可以是任意字符串
path= /home/durant //共享的路径
writable = yes //是否可写入
public = no //是否公开
设置为不予许登入系统,且用户的家目录为 /home/durant(相当于虚拟账号)的durant账号。
root@samba-server ~]# useradd -d /home/durant -s /sbin/nologin durant
pdbedit 命令用于管理Samba服务的帐户信息数据库,格式为:"pdbedit [选项] 帐户"
第一次把用户信息写入到数据库时需要使用-a参数,以后修改用户密码、删除用户等等操作就不再需要了。
pdbedit -L :查看samba用户
pdbedit -a -u user:添加samba用户
pdbedit -r -u user:修改samba用户信息
pdbedit -x -u user:删除samba用户
samba服务数据库的密码也可以用 smbpasswd 命令 操作
smbpasswd -a user:添加一个samba用户
smbpasswd -d user:禁用一个samba用户
smbpasswd -e user:恢复一个samba用户
smbpasswd -x user:删除一个samba用户
[root@samba-server ~]# pdbedit -a -u durant
new password: //设置durant使用的samba账号密码,比如123456
retype new password: //确认密码
Unix username: durant
NT username:
Account Flags: [U ]
User SID: S-1-5-21-3966910846-3390734216-1763763463-1001
Primary Group SID: S-1-5-21-3966910846-3390734216-1763763463-513
Full Name:
Home Directory: \\samba-server\durant
HomeDir Drive:
Logon Script:
Profile Path: \\samba-server\durant\profile
Domain: SAMBA-SERVER
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Thu, 13 Jun 2019 16:26:31 CST
Password can change: Thu, 13 Jun 2019 16:26:31 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
接着修改samba用户的家目录权限
[root@samba-server ~]# chown -Rf durant:durant /home/durant
[root@samba-server ~]# systemctl start smb
[root@samba-server ~]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[root@samba-server ~]# systemctl restart smb
[root@samba-server ~]# systemctl status smb
先往共享路径/home/durant里添加点内容
[root@samba-server ~]# cd /home/durant/
[root@samba-server durant]# touch test1 test2 test3
[root@samba-server durant]# mkdir a1 a2 a3
[root@samba-server durant]# ls
a1 a2 a3 test1 test2 test3
1.将samba共享目录挂载到windows上
1)”Win+E键"打开,右键“此电脑”选择“映射网络驱动器”
2)输入samba的地址,点击“完成”
3)输入凭据,用户名durant,密码123456,点击“确定”
可以看到,samba上的durant目录已经挂载到了windows的Z盘上。
4)连接上之后,就可以测试windows和linux之间的文件共享了
在windows端上传两个文件
在linux上也有了
2.将samba共享目录挂载到linux上(192.168.0.123)
以下操作都在客户机192.168.0.123上执行
[root@fdfs-3 ~]# mkdir -p /data/test
[root@fdfs-3 ~]# mount -t cifs //192.168.0.122/durant -o username=durant,password=123456 /data/test
[root@fdfs-3 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 1.6G 19G 8% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.6M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
tmpfs 183M 0 183M 0% /run/user/0
//192.168.0.122/durant 20G 1.5G 19G 8% /data/test
可以看到在192.168.0.123上已成功挂载上samba的durant共享目录。
参考文章:https://www.cnblogs.com/kevingrace/p/8550810.html