2022
我们一起努力

Linux搭建文件服务器

Linux如何搭建文件服务器?在一个团队或者公司层面上,做一个本地的文件服务器,将网上的资源下载到本地,是有必要的。这将节省其他人的很多下载时间,本篇文章为大家分享一下Linux搭建文件服务器具体方法,有需要的小伙伴可以参考一下。

Linux搭建文件服务器

Samba服务

 作用:共享目录(**b协议)
 软件:samba 服务端, samba-client 客户端
 配置文件:/etc/samba/**b.conf
 服务:**b, nmb
 端口:**b ---> 139/tcp,  445/tcp    提供文件共享功能
    nmb ---> 137/udp,  138/udp  提供解析计算机名称

配置文件:/etc/samba/**b.conf

全局配置

 [global]
 
     workgroup = MYGROUP      >>>设置工作组名称
     server string = Samba Server Version %v   >>>显示samba软件版本信息
 
  interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24   >>>samba服务监听的IP地址
 
  hosts allow = 127. 192.168.12. 192.168.13.   >>>设置仅允许哪些主机可访问
  hosts deny = 192.168.12.  192.168.1.1/24   >>>拒绝哪些主机可访问
 
  security = user   >>> 基于用户认证的访问
 
   share  >>> 匿名访问

共享目录配置

 [共享名称]
  comment =      >>> 描述信息
  path = /bj     >>> 指定目录名称
  browseable = yes    >>> 可下载文件
  writable = yes     >>> 可上传文件
  public = yes     >>> 允许所有用户访问
  write list = user1   >>> 仅允许user1可上传文件

示例:

 环境描述:
  Linux    192.168.122.105  Centos 7.2   文件共享服务器
  Windows/Linux   客户端
 
 
 需求:
 
  通过samba软件将本地的/caiwu目录共享, 客户端可通过martin用户访问,仅允许其下载文件
  1. 关闭SELinux, 防火墙

[root@file-server ~]# setenforce 0 [root@file-server ~]# getenforce Permissive [root@file-server ~]# vim /etc/sysconfig/selinux

[root@file-server ~]# systemctl stop firewalld.service [root@file-server ~]# systemctl disable firewalld.service

  1. 安装软件

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

  1. 编辑配置文件,共享/caiwu目录

[root@file-server ~]# mkdir /caiwu [root@file-server ~]# touch /caiwu/{1..5}.mp3

[root@file-server ~]# vim /etc/samba/**b.conf

     [caiwu]
     comment = It is a test
     path = /caiwu
     browseable = yes
  1. 创建共享用户

[root@file-server ~]# useradd martin [root@file-server ~]# **bpasswd -a martin New SMB password: Retype new SMB password: Added user martin.

[root@file-server ~]# pdbedit -L >>> 查看共享用户 martin:1001: [root@file-server ~]#

  1. 启动服务

[root@file-server ~]# systemctl start **b [root@file-server ~]# systemctl enable **b

[root@file-server ~]# ss -antp | grep **bd LISTEN 0 50 :139 *: users:((“**bd”,pid=2804,fd=38)) LISTEN 0 50 *:445 *:* users:((“**bd”,pid=2804,fd=37)) LISTEN 0 50 :::139 :::* users:((“**bd”,pid=2804,fd=36)) LISTEN 0 50 :::445 :::* users:((“**bd”,pid=2804,fd=35))

  1. 测试访问

Windows客户端:

 \\192.168.122.105
 
 取消用户宿主目录的共享

[root@file-server ~]# vim /etc/samba/**b.conf

[homes] comment = Home Directories browseable = no writable = yes

[root@file-server ~]# systemctl restart **b

Linux客户端:

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

[root@client ~]# **bclient //192.168.122.105/caiwu -U martin

配置允许martin用户可上传文件

  1. 编辑配置文件

[root@file-server ~]# vim /etc/samba/**b.conf

 [caiwu]
  ...
  writable = yes

[root@file-server ~]# systemctl restart **b

  1. 设置目录的本地权限

[root@file-server ~]# setfacl -m u:martin:rwx /caiwu/

示例:

 通过samba软件将本地的/shichang目录共享,允许martin用户下载文件,允许admin用户上传文件
  1. 创建目录,创建共享用户

[root@file-server ~]# mkdir /shichang [root@file-server ~]# touch /shichang/{1..5}.jpg [root@file-server ~]# [root@file-server ~]# useradd admin [root@file-server ~]# **bpasswd -a admin New SMB password: Retype new SMB password: Added user admin. [root@file-server ~]# [root@file-server ~]# pdbedit -L martin:1001: admin:1002: [root@file-server ~]#

  1. 编辑配置文件

[root@file-server ~]# vim /etc/samba/**b.conf

 [shichang]
 path = /shichang
 browseable = yes
 write list = admin

[root@file-server ~]# systemctl restart **b

[root@file-server ~]# chown admin /shichang/ [root@file-server ~]# ls -ldh /shichang/ drwxr-xr-x. 2 admin root 66 2月 21 12:00 /shichang/ [root@file-server ~]#

  1. 测试访问

    清除windows的共享缓存

      net use * /del

windows设置网络映射驱动器访问共享

FTP ——- File Transport Protocol 文件传输协议

FTP协议的连接模式: 主动连接 被动连接

软件:vsftpd 配置文件:/etc/vsftpd/vsftpd.conf 服务:vsftpd 端口:21/tcp 命令连接端口 20/tcp 数据连接端口(主动)

FTP根目录: 用户宿主目录

访问方式: 匿名用户访问(ftp) 用户认证的访问

示例:搭建匿名访问的FTP服务器

  1. 安装vsftpd软件

[root@file-server ~]# yum install -y vsftpd

[root@file-server ~]# systemctl start vsftpd [root@file-server ~]# systemctl enable vsftpd Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

[root@file-server ~]# ss -antp | grep :21 LISTEN 0 32 :::21 ::? users:((“vsftpd”,pid=5748,fd=3))

测试访问:

 Windows:
  ftp://192.168.122.105
 
  FileZilla  FTP客户端软件

允许匿名上传的文件

[root@file-server ~]# chmod o+w /var/ftp/pub/

anon_upload_enable=YES >>>允许上传文件 anon_mkdir_write_enable=YES >>>允许上传目录 anon_other_write_enable=YES >>>允许其他的修改(删除、重命名等)

anon_umask=022 >>>允许其他用户能下载匿名用户的文件

anon_root=/company >>>更改匿名用户的FTP的根目录

本地用户认证的FTP服务

示例: 搭建FTP yum源提供MySQL安装包

[root@file-server ~]# ls /rpm/mysql/ mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm mysql-community-libs-5.7.16-1.el7.x86_64.rpm

[root@file-server ~]# createrepo /rpm/mysql/

[root@file-server ~]# vim /etc/vsftpd/vsftpd.conf

 anon_root=/rpm

[root@file-server ~]# systemctl restart vsftpd

使用ftp源:

[root@client ~]# cat /etc/yum.repos.d/centos.repo

[Centos] name=centos7u2 baseurl=ftp://172.16.8.100/centos7u2 enabled=1 gpgcheck=0

[mysql] name=mysql baseurl=ftp://192.168.122.105/mysql enabled=1 gpgcheck=0

nfs ——– Network File System 网络文件系统

作用:在Linux服务器间实现数据共享

软件: nfs-utils rpcbind

[root@file-server ~]# rpm -q rpcbind rpcbind-0.2.0-32.el7.x86_64

[root@file-server ~]# rpm -q nfs-utils nfs-utils-1.3.0-0.21.el7.x86_64 [root@file-server ~]#

目录导出文件 — /etc/exports

文件格式:

 目录名称   客户端地址(权限)
 
 客户端地址:
  IP地址  192.168.1.1
  网段  192.168.1.0/24
  *
 
 权限:
  ro  只读
  rw  读写
  sync 同步
  async 异步
 
  all_squash  客户端所有用户上传的文件的所属均为nfsnobody
  root_squash  客户端root用户上传的文件的所属会被映射为nfsnobody
  no_root_squash 客户端root用户上传的文件的所属仍为root
 
  anonuid=
  anongid=

示例:

通过nfs共享本地目录/webdata, 允许192.168.122.121以只读方式挂载

[root@file-server ~]# mkdir /webdata [root@file-server ~]# touch /webdata/{1..10}.html

[root@file-server ~]# cat /etc/exports /webdata 192.168.122.121(ro) [root@file-server ~]#

[root@file-server ~]# systemctl restart rpcbind [root@file-server ~]# systemctl restart nfs-server

[root@file-server ~]# systemctl enable nfs-server

[root@file-server ~]# showmount -e localhost Export list for localhost: /webdata 192.168.122.121 [root@file-server ~]#

客户端:

[root@client ~]# mount 192.168.122.105:/webdata /www/

[root@client ~]# ls /www/ 10.html 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html

自动挂载:

*1*|0**vim /etc/fstab**

192.168.122.105:/webdata /www nfs defaults 0 0

示例2:

通过nfs共享本地目录/mysqldata,允许192.168.122.121以读写的方式挂载

[root@file-server ~]# mkdir /mysqldata [root@file-server ~]# touch /mysqldata/{1..10}.sql [root@file-server ~]# chmod o+w /mysqldata/

[root@file-server ~]# vim /etc/exports … /mysqldata 192.168.122.121(rw)

[root@file-server ~]# exportfs -rav exporting 192.168.122.121:/mysqldata exporting 192.168.122.121:/webdata [root@file-server ~]#

客户端:

[root@client ~]# vim /etc/fstab

192.168.122.105:/mysqldata /database nfs defaults 0 0

[root@client ~]# mount -a

[root@client ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 7.3G 4.4G 3.0G 60% / devtmpfs 230M 0 230M 0% /dev tmpfs 245M 0 245M 0% /dev/shm tmpfs 245M 4.7M 240M 2% /run tmpfs 245M 0 245M 0% /sys/fs/cgroup /dev/mapper/centos-home 2.0G 33M 2.0G 2% /home /dev/vda1 512M 141M 372M 28% /boot tmpfs 49M 0 49M 0% /run/user/0 192.168.122.105:/webdata 7.3G 3.6G 3.8G 49% /www 192.168.122.105:/mysqldata 7.3G 3.6G 3.8G 49% /database

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!

137e00002230ad9f26e78-265x300

本文来源:www.lxlinux.net/2526.html,若引用不当,请联系修改。

赞(0)
文章名称:《Linux搭建文件服务器》
文章链接:https://www.fzvps.com/180715.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!