引自:https://www.cnblogs.com/kevingrace/p/8550810.html
关于Linux和Windows系统之间的文件传输,很多人选择使用FTP,相对较安全,但是有时还是会出现一些问题,比如上传文件时,文件名莫名出现乱码,文件大小改变等问题。相比较来说,使用Samba作为文件共享,就省事简洁多了。Samba服务器通信协议(Server Messages Block)就是是为了解决局域网内的文件或打印机等资源的共享服务问题,让多个主机之间共享文件变成越来越简单。下面简单介绍下,在Centos7下部署Samba服务的操作记录(测试机192.168.10.204):
1)安装Samba
1 2 3 4 |
[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [[email protected]samba-server ~]# rpm -qa|grep samba [[email protected] ~]# yum install -y samba |
2)安全角度考虑,需要设置防火墙策略(不要关闭防火墙)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
添加samba服务到防火墙策略中 [[email protected] ~]# firewall-cmd --add-service samba --permanent success 重启防火墙 [[email protected] ~]# firewall-cmd --reload success 查看samba服务是否添加到防火墙中: [[email protected] ~]# firewall-cmd --list-all|grep samba services: ssh dhcpv6-client samba 记住:一定要关闭selinux(否则会造成windows客户机连接Samba失败) [[email protected] ~]# vim /etc/sysconfig/selinux ..... SELINUX=disabled [[email protected] kevin]# setenforce 0 [[email protected] kevin]# getenforce Permissive |
3)配置Samba服务文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
[[email protected] ~]# cp /etc/samba/smb.conf /etc/samba/smb.conf.bak [[email protected] ~]# 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 = root create mask = 0664 directory mask = 0775 [kevin] //这个是共享文件夹标识,表示登录samba打开时显示的文件夹名称。配置了多少个共享文件夹标识,登录samba时就会显示多少文件夹。 comment = please do not modify it all will //comment是对该共享的描述,可以是任意字符串 path= /home/kevin //共享的路径 writable = yes //是否写入 public = no //是否公开 |
4)添加kevin账号(如上配置中添加的内容)
1 2 |
设置为不予许登入系统,且用户的家目录为 /home/kevin(相当于虚拟账号)的kevin账号。 [[email protected] ~]# useradd -d /home/kevin -s /sbin/nologin kevin |
5)pdbedit 命令说明
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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用户 |
6)将kevin添加为samba用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[[email protected] ~]# id kevin uid=1001(kevin) gid=1001(kevin) groups=1001(kevin) [[email protected] ~]# pdbedit -a -u kevin new password: //设置kevin使用的samba账号密码,比如123456 retype new password: //确认密码 Unix username: kevin NT username: Account Flags: [U ] User SID: S-1-5-21-33923925-2092173964-3757452328-1000 Primary Group SID: S-1-5-21-33923925-2092173964-3757452328-513 Full Name: Home Directory: \\samba-server\kevin HomeDir Drive: Logon Script: Profile Path: \\samba-server\kevin\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: Mon, 12 Mar 2018 18:07:58 CST Password can change: Mon, 12 Mar 2018 18:07:58 CST Password must change: never Last bad password : 0 Bad password count : 0 Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 接着修改samba用户的家目录权限 [[email protected] ~]# chown -Rf kevin.kevin /home/kevin |
7)启动Samba服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[[email protected] ~]# systemctl start smb [[email protected] ~]# systemctl enable smb Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to/usr/lib/systemd/system/smb.service. [[email protected] ~]# systemctl restart smb [[email protected] ~]# systemctl status smb ● smb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2018-03-12 18:11:20 CST; 3s ago Main PID: 977 (smbd) Status: "smbd: ready to serve connections..." CGroup: /system.slice/smb.service ├─977 /usr/sbin/smbd ├─978 /usr/sbin/smbd ├─979 /usr/sbin/smbd └─980 /usr/sbin/smbd Mar 12 18:11:19 samba-server systemd[1]: Starting Samba SMB Daemon... Mar 12 18:11:19 samba-server systemd[1]: smb.service: Supervising process 977 which is not our child. We‘ll most likely not... exits. Mar 12 18:11:20 samba-server smbd[977]: [2018/03/12 18:11:20.065982, 0] ../lib/util/become_daemon.c:124(daemon_ready) Mar 12 18:11:20 samba-server systemd[1]: Started Samba SMB Daemon. Mar 12 18:11:20 samba-server smbd[977]: STATUS=daemon ‘smbd‘ finished starting up and ready to serve connections Hint: Some lines were ellipsized, use -l to show in full. |
8)开始测试
先往共享路径/home/kevin里添加点内容
1 2 3 4 |
[[email protected] kevin]# touch test1 test2 test3 [[email protected] kevin]# mkdir a1 a2 a3 [[email protected] kevin]# ls a1 a2 a3 test1 test2 |
接着再windos 客户机本地测试。”Win+E键"打开,在最上面的"网络"地址栏输入“\\192.168.10.204”,然后回车,输入上面设置的samba账号kevin及其密码,就能共享到linux上的/home/kevin下的文件了
连接上后,就可以在windows和linux直接进行文件夹的共享操作了,可以让里面放点测试文件
如果在windows客户机上连接Samba出现网络错误:Windows 无法访问\\192.168.10.204\kevin,解决办法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
查看上下文的安全关系 [[email protected] ~]# semanage kevin -a -t samba_share_t -bash: semanage: command not found 如果系统出现上面的报错 ,说明你系统里没有安装 semanage命令,下面开始安装semanage: [[email protected] ~]# yum provides /usr/sbin/semanage Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.0x.sg * epel: mirror.dmmlabs.jp * extras: mirror.0x.sg * updates: mirror.0x.sg policycoreutils-python-2.5-17.1.el7.x86_64 : SELinux Repo : base Matched from: Filename : [[email protected] ~]# yum install -y 然后再执行一次,执行完成后,不要忘了刷新上下文关系 [[email protected] ~]# semanage fcontext -a -t [[email protected] ~]# restorecon -Rv /home/kevin 允许SElinux对于SMB用户共享家目录的布尔值 重启Samba [[email protected] ~]# systemctl restart smb |
如何在windows本机访问samba时用切换另一个用户登录
1 2 3 4 |
方法如下: 1)按键ctrl+r,打开"运行",输入"cmd" 2)输入命令"net 3)输入切换账号的命令"net |
重置samba账号密码
1 |
[[email protected] ~]# smbpasswd -a kevin //即重置kevin密码 |
======================================================
清理windows下连接linux的samba服务缓存
在安装配置linux服务器samba服务之初,samba服务难免会经过多次修改配置/重启,在期间win的系统或许早已连接上了samba。samba修改配置修改后,特别是用户权限,再次从win登录就很容易出现因缓存的权限原因导致不允许访问或者操作。
这时一般要等很久会清理缓存,另外重启win也会清理。但这效率很低。用以下手动的方法可以实时清理。
清理步骤:
1)打开win的命令行(ctrl+R,输入cmd)。
2)在命令行里输入net use,就会打印出当前缓存的连接上列表。
3)根据列表,一个个删除连接: net use 远程连接名称 /del;
或者一次性全部删除:net use * /del。
这样再次命令行输入samba服务地址的时候,就会重新让你输入访问的账户和密码了。
======================================================
可以在一个samba环境下建立多个业务组的共享目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
比如: 创建一个运维部门的samba共享磁盘,可以看到所有的共享内容; 创建一个产品风控组的samba共享磁盘,只能看到自己组的共享内容; [[email protected] ~]# cd /etc/samba/ [[email protected] samba]# ls lmhosts ops.smb.conf smb.conf [[email protected] samba]# diff smb.conf smb.conf.bak 103d102 < [[email protected] samba]# cat ops.smb.conf [信息科技部-运维小窝] comment = path= /data/samba public = valid users = printable = write list = [[email protected] samba]# cat chanpinfengkong.smb.conf [产品风控组共享目录] comment = path= /data/samba/产品风控组 public = valid users = printable = write list = useradd创建以上的几个用户,并设置好用户家目录 [[email protected] ~]# useradd wangshibo -d /data/samba -s /sbin/nologin [[email protected] ~]# useradd linan -d /data/samba -s [[email protected] ~]# useradd xiaomin -d /data/samba/产品风控组 -s /sbin/nologin [[email protected] ~]# useradd haokun -d /data/samba/产品风控组 -s /sbin/nologin [[email protected] ~]# cat /etc/passwd ...... wangshibo:x:507:508::/data/samba:/sbin/nologin lijinhe:x:508:509::/data/samba:/sbin/nologin ...... xiaomin:x:1006:1006::/data/samba/产品风控组:/sbin/nologin haokun:x:1007:1007::/data/samba/产品风控组:/sbin/nologin chanpinfengkong:x:1010:1010::/home/chanpinfengkong:/bin/bash 将这几个用户添加到samba里 [[email protected] ~]# pdbedit -a -u wangshibo [[email protected] ~]# pdbedit -a -u linan [[email protected] ~]# pdbedit -a -u xiaomin [[email protected] ~]# pdbedit -a -u haokun [[email protected] ~]# pdbedit -L wangshibo:507: linan:510: xiaomin:1006: haokun:1007: 创建chanpinfengkong组,将xiaomin和haokun添加到这个组内 [[email protected] ~]# useradd chanpinfengkong [[email protected] ~]# usermod -G chanpinfengkong xiaomin [[email protected] ~]# usermod -G chanpinfengkong haokun 创建samba共享目录 [[email protected] ~]# cd /data/ [[email protected] data]# mkdir samba [[email protected] data]# mkdir samba/产品风控组 [[email protected] data]# chown -R samba.samba samba [[email protected] data]# chmod -R 777 samba [[email protected] data]# setfacl -R -m g:chanpinfengkong:rwx 赋权脚本 [[email protected] ~]# cat /opt/samba.sh #!/bin/bash while [ "1" = "1" ] do /bin/chmod -R /usr/bin/setfacl -R -m done [[email protected] ~]# nohup sh -x /opt/samba.sh & [[email protected] ~]# ps -ef|grep samba.sh root root 185455 117471 0 如上配置后,登录samba: 1)用wangshibo,linan账号登录samba,能看到"/data/samba"下面所有的共享内容。 2)用xiaomin,haokun账号登录samba,只能看到"/data/samba/产品风控组" 下面的共享内容 3)如果还需要分更多的组,就如上面的"产品风控组"一样进行配置即可! |
原文地址:https://www.cnblogs.com/huo-fenghuang/p/10283693.html