文件服务器在企业中应用非常广泛,具有文件存储、文件共享和安全控制的功能,我们可以可以搭建一台文件服务器来集中存储自己的文件,下面是我搭建文件服务器的过程。
搭建文件服务器用的是Samba这个开源软件,它可以实现linux与windows之间的文件交互,能够提供安全、稳定、快速的文件与打印服务。windows客户端可以通过samba来共享文件,或者连接到打印机上,而不用每台windows客户端都安装打印机程序。
安装过程:
1、关闭防火墙与SELinux
由于iptables与SELinux的默认策略会阻止远程用户对Samba的访问,所以首先要暂时关闭这些服务。
关闭防火墙:
[root@localhost ~]# systemctl stop firewalld.service #停止
关闭SELinux:
修改 /etc/sysconfig/selinux 文件,将把 SELINUX=enforcing 修改为SELINUX= disable 退出保存,并且重启服务器。
2、安装Samba软件
[root@localhost ~]# yum -y install samba
3、修改配置文件
在默认的配置文件下,Samba已经配置为允许远程用户共享访问自己的家目录,同时具有写权限,这里我们不再增加新的共享目录。如果以后要增加新的目录,只需要创建一个共享目录,并且设置为合适的权限,然后在Samba配置文件的最后追加对这个共享目录的共享设置,就可以实现对新目录的共享。修改后的配置文件如下,供大家参考(下载地址:https://github.com/xinxingli/conf/tree/master/samba)。
[root@localhost ~]# vim /etc/samba/smb.conf
# This is the main Samba configuration file. For detailed information about the # options listed here, refer to the smb.conf(5) manual page. Samba has a huge # number of configurable options, most of which are not shown in this example. # # The Official Samba 3.2.x HOWTO and Reference Guide contains step-by-step # guides for installing, configuring, and using Samba: # http://www.samba.org/samba/docs/Samba-HOWTO-Collection.pdf # # The Samba-3 by Example guide has working examples for smb.conf. This guide is # generated daily: http://www.samba.org/samba/docs/Samba-Guide.pdf # # In this file, lines starting with a semicolon (;) or a hash (#) are # comments and are ignored. This file uses hashes to denote commentary and # semicolons for parts of the file you may wish to configure. # # Note: Run the "testparm" command after modifying this file to check for basic # syntax errors. # #--------------- #======================= Global Settings ===================================== [global] # ----------------------- Network-Related Options ------------------------- workgroup = WORKGROUP server string = Samba Server Version %v netbios name = SMBServer ; interfaces = lo eth0 10.12.8.196/24 192.168.13.2/24 ; hosts allow = 127. 192.168.12. 192.168.13. ; max protocol = SMB2 # --------------------------- Logging Options ----------------------------- # log files split per-machine: log file = /var/log/samba/log.%m # maximum size of 50KB per log file, then rotate: max log size = 500 # ----------------------- Standalone Server Options ------------------------ security = user passdb backend = tdbsam # ----------------------- Browser Control Options ---------------------------- # # local master = when set to no, Samba does not become the master browser on # your network. When set to yes, normal election rules apply. # # os level = determines the precedence the server has in master browser # elections. The default value should be reasonable. # # preferred master = when set to yes, Samba forces a local browser election at # start up (and gives itself a slightly higher chance of winning the election). # ; local master = no ; os level = 33 ; preferred master = yes #----------------------------- Name Resolution ------------------------------- ; wins support = yes ; wins server = w.x.y.z ; wins proxy = yes ; dns proxy = yes # --------------------------- Printing Options ----------------------------- load printers = yes cups options = raw ; printcap name = /etc/printcap # obtain a list of printers automatically on UNIX System V systems: ; printcap name = lpstat ; printing = cups # --------------------------- File System Options --------------------------- ; map hidden = no ; map read only = no ; map system = no ; store dos attributes = yes #============================ Share Definitions ============================== [homes] comment = Home Directories browseable = no writable = yes ; valid users = %S ; valid users = MYDOMAIN\%S [printers] comment = All Printers path = /var/spool/samba browseable = no guest ok = no writable = no printable = yes
4、创建访问账号
访问Samba服务需要独立的Samba 账号和密码,这个Samba 账号和密码只能用于文件共享,不能登陆文件服务器,增加了服务器的安全性。在修改了Samba的配置文件后,还需要使用 smbpasswd 将账户添加到 Samba 中,下面创建一个账户为:smbuser.
[root@localhost ~]# useradd -s /sbin/nologin smbuser [root@localhost ~]# smbpasswd -a smbuser
根据提示连续两次输入密码即可。
小提示:
smbpasswd命令的常用方法
选项:-a 增加用户(要增加的用户必须以是系统用户)
-d 冻结用户,就是这个用户不能在登录了-x 删除SMB账户
-e 恢复用户,解冻用户,让冻结的用户可以在使用
-n 把用户的密码设置成空.
5、启动Samba服务
[root@localhost samba]# service smb start Redirecting to /bin/systemctl start smb.service
[root@localhost samba]# chkconfig smb on
6、windows下访问samba服务
在windows中使用win+R调出运行,输入 \\10.12.8.196 然后回车,输入刚才添加的SMB账户名和密码,就可以使用Samba服务了。
到这里一台文件服务器就搭建好了,可以在文件服务器中存储文件了!
无法连接Samba共享服务的常见解决方法:
1、无法连接服务器
解决方法:
关闭SELinux,通过
[root@localhost ~]# vi /etc/sysconfig/selinux
把 SELINUX=enforcing 修改为SELINUX= disable 退出保存,并且重启服务器。就可以实现访问了,当然,也可以通过配置selinux策略来实现该功能。
原创文章,转载请注明: 转载自科技爱好者博客
本文链接地址: Linux中使用Samba搭建文件服务器详解 (https://www.tujing.site/1347)
如果博客对您有帮助,请给我 赞助
非常详细,给赞一个。
谢谢!