文件共享服务器

一、vsftp服务器

FTP概述

文件传输协议(file transfer protocol,FTP),基于该协议 FTP 客户端与服务端可以实现共享文件、上传文件、下载文件。

FTP 基于 TCP 协议生成一个虚拟的连接,主要用于控制 FTP 连接信息,同时再生成一个单独的 TCP 连接用于 FTP 数据传输。

用户可以通过客户端向 FTP 服务器端上传、下载、删除文件,FTP 服务器端可以同时提供给多人共享使用。

FTP 服务是 client/server(简称 C/S)模式,基于 FTP 协议实现 FTP 文件对外共享及传输的软件称之为 FTP 服务器源端,客户端程序基于 FTP 协议,则称之为 FTP 客户端,FTP 客户端可以向 FTP 服务器上传、下载文件。

vsftp连接类型
  • 控制连接(持续连接) → TCP 21(命令信道) → 用户收发FTP命令

  • 数据连接(按需连接) → TCP 20(数据信道) → 用于上传下载数据

vsftp工作模式

主动模式(port):

  • FTP 客户端首先和服务器的 TCP 21 端口建立连接;用来发送命令,

  • 客户端需要接收数据的时候在这个通道上发送 PORT 命令。PORT 命令包含了客户端用什么端口接收数据。

  • 在传送数据的时候,服务器端通过自己的 TCP 20 端口连接至客户端的指定端口发送数据。

  • FTP server 必须和客户端建立一个新的连接用来传送数据。

被动模式(passive):

  • FTP 客户端首先和服务器的 TCP 21 端口建立连接,用来建立控制通道发送命令,

  • 但建立连接后客户端发送 Pasv 命令。

  • 服务器收到 Pasv 命令后,打开一个临时端口(端口大于 1023小于 65535)并且通知客户端在这个端口上传送数据的请求;

  • 客户端连接 FTP 服务器的临时端口,然后 FTP 服务器将通过这个端口传输数据。.

  • 注意:由于VSFTP的被动模式是随机端口进行数据传输,所以在设置防火墙时需要刻意放行-->这里一定要放心vsftp的服务。

VSFTP 传输模式
  • Binary模式:不对数据进行任何处理,适合进行可执行文件、压缩文件、图片等

  • ASCII模式:进行文本传输时,自动适应目标操作系统的结束符,如回车符等

  • Linux的红帽发行版中VSFTP默认采用的是Binary模式,这样能保证绝大多数文件传输后能正常使用

  • 切换方式:在ftp>提示符下输入ascii即转换到ACSII方式,输入bin,即转换到Binary方式。

登录验证方式
匿名用户验证
  • 用户账号名称:ftp或anonymous

  • 用户账号密码:无密码

  • 工作目录:/var/ftp

  • 默认权限:默认可下载不可上传,上传权限由两部分组成(主配置文件和文件系统)

复制代码
​
客户端-->匿名用户登录-->匿名账号:ftp或者是anonymous 密码是空密码
复制代码
    默认的家目录:/var/ftp/目录下
本地用户验证
  • 用户账号名称:本地用户(/etc/passwd)

  • 用户账号密码:用户密码(/etc/shadow)

  • 工作目录:登录用户的宿主目录

  • 权限:最大权限(drwx------)

复制代码
​
客户端-->本地用户登录-->本地登录涉及到:普通用户和普通用户的家目录
                            /etc/passwd
复制代码
    /etc/shadow文件
虚拟(virtual)用户验证
  • 创建虚拟用户用来代替本地用户,减少本地用户曝光率

  • 使用本地用户作为虚拟用户的映射用户,为虚拟用户提供工作目录和权限控制

  • 能够设置严格的权限(为每一个用户生成单独的配置文件)

复制代码
​
客户端-->虚拟用户登录-->这个是最复杂的;
                    需要人为创建,生成数据库文件;
                    找一个系统用户作为虚拟用户的映射用户,借助系统用户的家目录作为默认的登陆点,/home下的其中一个用户名
                    每一个虚拟用户的权限都可以单独指定的

vsftp安装

复制代码
​
###关闭防火墙及SElinux###
###配置静态IP地址###
###安装vsftpd软件包###
###配置yum源###
###安装软件包:vsftpd:服务段;ftp:客户端###
[root@localhost ~]# yum install -y vsftpd  ftp
###配置文件目录####
[root@localhost vsftpd]# pwd
/etc/vsftpd

配置文件解析

复制代码
[root@localhost vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh
ftpusers文件

该文件中的所有用户不能访问FTP服务器,相当于ftp的黑名单。

复制代码
​
​
[root@localhost vsftpd]# cat ftpusers 
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
user_list文件

该文件中的用户是否可以登录取决于/etc/vsftpd/vsftpd.conf文件中的配置项:userlist_deny=YES/NO;如果时YES,则文件中用户不可登录;如果为NO,则可登录。

复制代码
[root@localhost vsftpd]# cat user_list 
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
vsftpd.conf文件

vsftpd服务的主配置文件。

复制代码
[root@localhost vsftpd]# cat vsftpd.conf 
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
#chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
vsftpd.conf文件常用配置项
作用范围 配置项 说明
匿名用户 | 虚拟用户 anonymous_enable=YES 是否允许匿名用户访问
anon_upload_enable=YES 是否允许匿名用户上传
anon_mkdir_write_enable=YES 是否允许匿名用户创建目录
anon_umask=022 设置匿名用户上传文件的权限掩码
anon_root=/var/ftp 设置你名用户根目录
anon_other_write_enable=YES 设置匿名用户的其他写入权限
本地用户 local_enable=yes 是否允许本地用户访问
local_umask=022 设置本地用户上传文件的权限掩码
local_root=$HOME 设置本地用户根目录,默认用户家目录
chroot_local_user=yes 是否禁锢宿主目录
local_max_rate=0 设置传输速度,0表示不限制
全局配置 listen=YES 设置IPv4监听,与IPv6监听不能同时开启
listen_ipv6=NO 设置IPv6监听
listen_address=0.0.0.0 设置监听地址
listen_port=21 设置监听端口号
write_enable=yes 设置是否开启所有写入权限
download_enable=yes 设置是否可以下载
userlist_enable=yes 设置是否启用user_list列表
userlist_deny=yes 设置user_list列表中用户是否能登录
allow_writeable_chroot=yes 允许对目录具有写入权限的用户锁定宿主目录
user_config_dir=/etc/vsftpd/vusers_dir 设置虚拟用用户权限目录

匿名用户访问实验

核心配置
复制代码
anonymous_enable=YES        #启用匿名访问
anon_umask=022              #匿名用户所上传文件的权限掩码
anon_root=/var/ftp          #匿名用户的 FTP 根目录
anon_upload_enable=YES      #允许上传文件
anon_mkdir_write_enable=YES #允许创建目录
anon_other_write_enable=YES #开放其他写入权(删除、覆盖、重命名)
anon_max_rate=0             #限制最大传输速率(0 为不限速,单位:bytes/秒)
启动服务
复制代码
[root@localhost ~]# systemctl start vsftpd
[root@localhost ~]# systemctl enable vsftpd 
客户端验证
复制代码
[root@Node1 ~]# ftp 192.168.115.132
Connected to 192.168.115.132 (192.168.115.132).
220 (vsFTPd 3.0.2)
Name (192.168.115.132:root): ftp        #输入用户名:ftp
331 Please specify the password.
Password:                               #直接回车,空密码
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

本地用户访问验证

核心配置
复制代码
anonymous_enable=NO
local_enable=YES            #是否启用本地系统用户
local_umask=022             #本地用户所上传文件的权限掩码
local_root=/var/ftp         #设置本地用户的 FTP 根目录
chroot_local_user=YES       #是否将用户禁锢在主目录
allow_writeable_chroot=yes  #允许对目录具有写入权限的用户锁定宿主目录
local_max_rate=0            #限制最大传输速率
ftpd_banner=Welcome to blah FTP service #用户登录时显示的欢迎信息
userlist_enable=YES & userlist_deny=YES #禁止/etc/vsftpd/user_list 文件中出现的用户名登录 FTP
userlist_enable=YES & userlist_deny=NO  #仅允许/etc/vsftpd/user_list 文件中出现的用户名登录 FTP
###注意:禁止/etc/vsftpd/ftpusers 文件中出现的用户名登录 FTP,权限比 user_list 更高,即时生效###
服务端创建登录用户
复制代码
#创建一个用于vsftpd的本地账户
[root@localhost ~]# useradd  zhangsan
[root@localhost ~]# echo 1 | passwd --stdin zhangsan
重启服务
复制代码
[root@localhost ~]# systemctl restart vsftpd
客户端验证
复制代码
[root@localhost ~]# ftp 192.168.115.132
Connected to 192.168.115.132 (192.168.115.132).
220 (vsFTPd 3.0.2)
Name (192.168.115.132:root): zhangsan       
331 Please specify the password.
Password:                           #输入密码
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

二、samba服务器

概述

samba 是基于SMB协议(ServerMessage Block,信息服务块)的开源软件,samba也可以是SMB协议的商标。SMB是一种Linux、UNIX系统上可用于共享文件和打印机等资源的协议,这种协议是基于Client\Server型的协议,Client端可以通过SMB访问到Server(服务器)上的共享资源。当Windows是 Client,CentOS是服务器时,通过Samba就可以实现window访问Linux的资源,实现两个系统间的数据交互。

Samba的应用环境
  • 文件和打印机共享:文件和打印机共享是 Samba的主要功,能,通过 SMB 进程实现资源共享,将文件和打印机发布到网络之中,以供用户访问。

  • 身份验证和权限设置:smbd服务支持user mode和 domain mode等身份验证和权限设置模式,通过加密方式可以保护共享的文件和打印机。

  • 名称解析:Samba遇过 nmbd 服务可以搭建 NBNS(NetBIOS Name Service)服务器,提供名称解析,将计算机的NetBIOS 名解析为IP地址。

  • 浏览服务:局域网中,samba服务器可以为本地浏览服务器(LMB),保存可用资源列表,当使用客户端访问 Windows网上邻居时,会提供浏览列表,显示共享目录、打印机等资源。

Samba的工作原理

Samba 服务功能强大,这与其通信基于SMB 协议有关。SMB不仅提供目录和打印机共享,还支持认证、权限设置。在早期,SMB运行于NBT协议(NetBIOS over TCP/IP)上,使用UDP的137、138及TCP的139端口,后期 SMB经过开发,可以直接运行于 TCP/IP上且没有额外的 NBT 协议,使用TCP的 445端口。

smaba服务安装

复制代码
[root@localhost ~]# yum -y install samba

配置文件解析

复制代码
[root@localhost ~]# cat /etc/samba/smb.conf
[global]                 #全局配置
    workgroup = SAMBA        #设置服务器加入的域或工作组
    security = user【server、domain】#设置验证方式,默认本地用户验证
    username map = /etc/samba/smbusers #指定虚拟用户文件
    passdb backend = tdbsam     #建立samba用户数据库,用户必须是系统用户 
    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
    irectory mask = 0775
        
[smbtool]               #共享名
    comment = private 
    path = /smbtools        #共享路径,在启动samba服务之前创建
    browseable = no         #是否可被浏览
    valid users = lisi,@root    #有效访问用户或组,@表示组
    invalid users = zhangsan,@wang  #禁止访问的用户或组,不要与valid users同时存在
    read only = yes         #只读访问
    write list = lisi,@root #允许写入的用户
    public = yes            #是否允许guest用户访问
    writable = yes          #是否允许写入
    create mask = 0664      #上传文件的默认权限
    directory mask = 0775   #创建目录的默认权限
    guest ok = yes          #当配置虚拟用户访问时,需使用该选项

samba用户添加

复制代码
[root@localhost ~]# pdbedit -a username #新建Samba账户。
[root@localhost ~]# pdbedit -x username #删除Samba账户。
[root@localhost ~]# pdbedit -L  #列出Samba用户列表。

访问samba服务器

linux客户端访问

复制代码
###需要安装samba-client软件包
[root@localhost ~]# smbclient -L //192.168.115.132 -U lisi #查看共享名称
[root@localhost ~]# smbclient //192.168.115.132/共享名称 -U lisi

windows客户端访问

复制代码
#####win+R,打开运行框,输入UNC路径:###
\\192.168.115.132

samba服务案例

项目需求

复制代码
lisi,zhangsan两个用户都能访问samba服务器,lisi可下载和上传,zhangsan只能下载

配置过程

复制代码
[root@localhost ~]# useradd lisi
[root@localhost ~]# useradd zhangsan
[root@localhost ~]# pdbedit -a lisi
[root@localhost ~]# pdbedit -a zhangsan
[root@localhost ~]# mkdir /testdir
####配置文件修改:#######
[root@localhost ~]# tail -7 /etc/samba/smb.conf
[test]
        comment = Printer Drivers
        path = /testdir
        valid users = lisi,zhangsan
        read only = yes
        write list = lisi
        create mask = 0644
相关推荐
A小辣椒1 天前
TShark:Wireshark CLI 功能
linux
A小辣椒1 天前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式