10. Linux中的文件服务

154 阅读3分钟

本系列都是是基于RedHat体系的,所以CentOS也可以用,但是Debian系列的可能会有些命令上的出入。

1. 文件服务(FTP、NFS)

例如邮件发放、文件发给所有的员工,就涉及到了文件服务。如果不成功,可能是防火墙问题。

1.1 FTP

File Transport Protocol,文件传输协议。logo是vsFTP(very secure FTP)。

1. FTP Server

服务器端软件是vsftpd

CentOS下,所有可以共享给其它主机的文件都需要位于/var/ftp/目录。Ubuntu可以参考Ubuntu vsftpd User Direcoty,其实是在/etc/vsftpd.conf文件中配置local_root=/path/to/ftp

# CentOS 7 安装
$ yum install vsftpd
# Ubuntu 安装
$ apt install vsftpd

# 启动
$ systemctl start vsftpd
$ systemctl enable vsftpd

# 修改配置文件之后要重新启动
$ systemctl restart vsftpd

2. FTP Client

  • 客户端软件是lftp

    # 登录
    $ lftp <server-ip>
    
    # 下载,位置在登录的当前目录
    # 下载文件
    $ get <file_name>
    # 下载目录
    $ mirror <dir_name>
    
  • wget

    $ wget ftp://<server-ip>/path/to/file
    # -O:指定保存位置
    $ wget <source-file-net-path> -O <target-file-local-path>
    # -m:mirror,下载目录
    $ wget -m <source-dir-net-path> -O <target-dir-local-path>
    
  • 浏览器

3. 启用用户上传

  1. 配置文件:

    CentOS/etc/vsftpd/vsftpd.conf, Ubuntu/etc/vsftpd.conf

  2. 检查禁用匿名用户登录开启:

    anonymous_enable=NO,默认可以使用匿名用户,改为NO,接下来需要使用账户名密码来访问,默认的登录后的文件夹位于用户的家目录。

    然后重启程序:systemctl restart vsftpd

  3. 配置上传指令

    anon_upload_enable=YES			# 启动文件上传功能
    anon_mkdir_write_enable=YES		# 启动创建目录功能
    
  4. 创建上传目录

    $ mkdir /var/ftp/upload
    $ chmod 777 /var/ftp/upload
    
  5. 上传测试

    # 测试机
    $ lftp 192.168.7.40
    lftp> cd upload
    lftp/upload> put 234.txt
    lftp/upload> mirror 
    

1.2 NFS

Network File System,网络文件系统,*nix系统之间共享文件的一种协议,支持多接点同时挂载以及并发写入。提供文件共享服务,为Web服务器配置集中的后端存储。

  1. 存储端(NAS)

    NAS,Network Attached Storage,网络附加存储,是一种存储设备。

    # 安装
    $ yum install nfs-utils							
    $ mkdir /webdata								# 发布资源的目录
    $ echo "nfs test" >> /webdata/index.html		# 测试网页
    
    # 配置
    $ vim /etc/exports								# nfs的主配置文件
    	/webdata		192.168.7.0/24(rw)		# /webdata目录可读可写,后面是可以访问的网段
    	
    # 启动
    $ systemctl start nfs-server					# 启动服务器
    $ systemctl enable nfs-server					# 开机自启
    $ exportfs -v									# 查看可以被访问的目录
    
  2. 客户端

    # 查询可用NFS服务目录
    $ showmount -e <nfs-server-ip>
    Export list for 192.168.7.40:
    /webdata 192.168.7.0/24
    
    # 手动挂载
    # mount -t(ype)   nfs <nfs-server-ip>:/path/to/data    mount-point
    $ mount -t        nfs 192.168.7.40:/webdata           /var/www/html
    # 取消挂载
    $ umount /var/www/html
    
    # 查看挂载
    $ df
    $ ls /var/www/html
    

2. 网站服务(HTTP)

2.1 静态站点

Apache,CentOS中软件叫做httpd,服务端口80(tcp/http), 443(tcp/https),配置文件/etc/httpd/conf/httpd.conf,子配置文件/etc/httpd/conf.d/*.conf

Ubuntu中软件叫做apache2,配置文件/etc/apache2/apache2.conf

# 安装
$ yum -y install httpd
# 关闭防火墙
$ systemctl stop firewalld
# 关闭开机自启
$ systemctl disable firewalld
$ setenforece 0

# 验证安装
$ httpd -v

/var/www/html是源网站文件所在目录,可以修改,修改位置位于/etc/httpd/conf.d/下面同名的网站名.conf文件。

例如,/var/www/a.org储存www.a.org的网站源码,而CentOS中/etc/httpd/conf.d/a.org.conf存储配置信息,(Ubuntu /etc/apache2/sites-enabled/a.org.conf,还需要source /etc/apache2/envvars)配置信息中写入:

<VirtualHost *:80>
        ServerName www.a.org
        DocumentRoot /var/www/a.org
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

还需要在访问的客户端主机的/etc/hosts中添加域名和IP地址的对应。然后重启CentOSsystemctl restart httpd,Ubuntusystemctl restart apache2

访问还要注意一点,Google Chrome 会自作聪明的加上https的访问,请改成http的访问。

如果网站的源代码不放在/var/www目录下,则需要授权。

<Directory "/var/b.org">
       Require all granted
</Directory>

字符浏览器:elinks

2.2 动态站点

LAMP: Linux、Apache、MySQL、PHP。

3. 域名服务(DNS)

本机域名IP对应关系:/etc/hosts

FQDN: Fully Qualified Domain Name

主机名.[...].二级域.顶级域.(根域)

/etc/resolv.conf这个文件不是用来改的,而是生成的,修改的是/etc/network/interfaces

Debian Network Configuration