8.27 14.4-15.3

14.4 exportfs命令

该命令随nfs-utils包一起被安装

用户首次配置了nfs的共享目录,过一段时间后可能会需要更改或增加某些机器或增加某个共享目录

为了达到上述目的,用户可能需要修改配置文件,再重启nfs服务

此时远程客户端可能正在挂载并使用nfs的共享目录,若此时重启服务会导致远程客户端被挂起,无法提供服务

例:

B服务器的nginx进程可能正在向A共享的目录读写数据,此时若nfs服务停止则该nginx进程会挂起

查看该进程状态会发现ps aux中显示的该进程状态为d(进程不能中断),无法杀死也无法重启

blob.png

想重启nfs服务需要先将BC上挂载的nfs格式的分区或目录卸载,否则可能导致使用该目录或分区数据的服务异常

当有几十台甚至更多的服务器挂载了nfs格式的分区,挂载和卸载会比较困难,此时就会用到exportfs命令

exportfs命令:

常用选项:

-a 全部挂载或卸载

-r 重新挂载

-u 卸载某个目录

-v 显示共享目录

修改配置文件

[root@hyc-01-01 tmp]# vim /etc/exports

/home/nfstestdir 192.168.31.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

/tmp/nfstest 192.168.31.128(rw,sync,no_root_squash)

编辑配置文件使新共享目录生效

[root@hyc-01-01 tmp]# exportfs -arv

exporting 192.168.31.128:/tmp/nfstest

exporting 192.168.31.0/24:/home/nfstestdir

在客户端查看:

[root@hyc-01 ~]# showmount -e 192.168.31.129

Export list for 192.168.31.129:

/home/nfstestdir 192.168.31.0/24

/tmp/nfstest     192.168.31.128

挂载/home/nfstestdir

[root@hyc-01 ~]# mount -t nfs 192.168.31.129:/home/nfstestdir /mnt/

[root@hyc-01 ~]# df -h

文件系统                         容量  已用  可用 已用% 挂载点

/dev/sda3                         18G  1.1G   17G    6% /

devtmpfs                         483M     0  483M    0% /dev

tmpfs                            493M     0  493M    0% /dev/shm

tmpfs                            493M  6.8M  486M    2% /run

tmpfs                            493M     0  493M    0% /sys/fs/cgroup

/dev/sda1                        197M   97M  100M   50% /boot

tmpfs                             99M     0   99M    0% /run/user/0

192.168.31.129:/home/nfstestdir   18G  7.7G   11G   44% /mnt

/home/nfstestdir下创建文件并写数据:

[root@hyc-01-01 nfstestdir]# pwd

/home/nfstestdir

[root@hyc-01-01 nfstestdir]# touch 1212.txt

[root@hyc-01-01 nfstestdir]# echo 'egefrgqer'>1212.txt

查看服务端和客户端的用户权限:

[root@hyc-01-01 nfstestdir]# ls -l 1212.txt

-rw-r--r-- 1 root root 10 8  26 22:12 1212.txt

[root@hyc-01 ~]# ls -l /mnt

总用量 4

-rw-r--r--. 1 root root 10 8  26 22:12 1212.txt

-rw-r--r--. 1 111  111   0 8  26 16:41 hyc.111

先前root用户被限制为id1000的用户和组(hyc.111

目前root用户和组不受限制

[root@hyc-01 ~]# su - 111

[111@hyc-01 ~]$ cd /mnt

[111@hyc-01 mnt]$ touch 111.111

[111@hyc-01 mnt]$ ls -l 111.111

-rw-rw-r--. 1 111 111 0 8  26 22:20 111.111

此时普通用户也不被限制

 

14.5 NFS客户端问题

 

NFS4版本有该问题

不是每次都有,但偶尔会出现

问题:

客户端挂载共享目录后,无论root用户还是普通用户,创建文件时属主、属组均为nobody

解决:

指定nfs的版本为3,不再用4,客户端和服务端均需要指定

方案1

重新挂载:

[root@hyc-01 ~]# mount -t nfs -o,nfsvers=3 192.168.31.129:/home/nfstestdir/ /mnt/

无论先前是否挂载这里都必须先挂载一遍

[root@hyc-01 ~]# mount -t nfs -oremount,nfsvers=3 192.168.31.129:/home/nfstestdir/ /mnt/

然后重新挂载

方案2

[root@hyc-01 ~]# vim /etc/idmapd.conf

  3 # The following should be set to the local NFSv4 domain name

  4 # The default is the host's DNS domain name.

  5 Domain = abc.com

去掉#并修改=后的内容为xxx.com,然后重启rpcidmapd服务(centos7下重启rpcbind服务)

  6 # In multi-domain environments, some NFS servers will append the identity

  7 # management domain to the owner and owner_group in lieu of a true NFSv4

  8 # domain.  This option can facilitate lookups in such environments.  If

15.1 FTP介绍

当传输的文件大小超过4Grzsz命令无法使用

用户可以使用FTP服务将服务器的文件下载到本地或将本地文件上传到服务器

FTPFile Transfer Protocol)文件传输协议,用于在internet上控制文件的双向传输

FTP可以让用户连接一个远程计算机(即运行FTP服务器程序的机器),用户可以查看远程计算机的文件,可以把文件从远程主机复制到本地,或将本地计算机的文件传递到远程计算机

FTP安全性较差,许多大企业不会使用该工具上线程序或网站的代码

Git是一个版本管理工具,许多企业用该工具管理网站程序的版本

15.2 使用vsftpd搭建ftp服务(上)

vsftpdcentos上自带的FTP服务软件包

 

安装软件包:

[root@hyc-01-01 ~]# yum install -y vsftpd

vsftpd软件包可以使用系统级别的用户:

假如linux系统中创建了用户hyc,则此时可以使用vsftpd启动FTP服务,并使用hyc登录,此时用户的登录形式不是ssh而是FTP,登录后会进入hyc用户的家目录

这样使用系统级别的用户登录安全性较差

FTP设置虚拟用户,并将虚拟用户映射为系统中的某个普通用户,可以映射多个虚拟用户,这些虚拟用户无法用于登录linux系统,比较安全

由于服务端和客户端需要上传、下载文件,所以必须有一个linux系统用户的身份:

[root@hyc-01-01 ~]# useradd -s /sbin/nologin virftp

为了FTP传输文件专门创建该用户,-s指定用户的shell/sbin/nologin则该用户无法登录系统,这样比较安全

指定用户名为virftp

编辑虚拟用户密码文件:

[root@hyc-01-01 ~]# vim /etc/vsftpd/vsftpd_login

viruser1

hyc940421

viruser2

hyc940421

[root@hyc-01-01 ~]# chmod 600 /etc/vsftpd/vsftpd_login

为了安全,将密码文件权限设为600

[root@hyc-01-01 ~]# db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db

将文本形式的密码文件转换为计算机识别的二进制密码文件

[root@hyc-01-01 ~]# ls -l /etc/vsftpd/

总用量 36

-rw------- 1 root root   125 8   3 2017 ftpusers

-rw------- 1 root root   361 8   3 2017 user_list

-rw------- 1 root root  5030 8   3 2017 vsftpd.conf

-rwxr--r-- 1 root root   338 8   3 2017 vsftpd_conf_migrate.sh

-rw------- 1 root root    38 8  27 21:12 vsftpd_login

-rw-r--r-- 1 root root 12288 8  27 21:17 vsftpd_login.db

此时在/etc/vsftpd/目录下生成了两个文件vsftpd_loginvsftpd_login.db,其中.db的文件无法直接cat查看

编辑虚拟用户配置文件:

[root@hyc-01-01 ~]# mkdir /etc/vsftpd/vsftpd_user_conf

创建用户配置文件所在目录

[root@hyc-01-01 ~]# cd !$

cd /etc/vsftpd/vsftpd_user_conf

注意:

在用户配置文件所在目录定义用户配置文件时文件名必须与用户名保持一致

[root@hyc-01-01 vsftpd_user_conf]# vim viruser1

local_root=/home/virftp/viruser1

用于定义虚拟用户的家目录,登录系统的虚拟用户会在该目录下读写文件

anonymous_enable=NO 是否允许匿名用户

write_enable=YES 是否允许可写

local_umask=022 指定创建的新文件(目录)的权限,同系统的umask

anon_upload_enable=NO 是否允许匿名用户上传

anon_mkdir_write_enable=NO 是否允许匿名用户创建目录且对目录可写

idle_session_timeout=600 空闲状态下的超时时间

data_connection_timeout=120 数据传输超时时间

max_clients=10 最大客户端连接数

创建虚拟用户家目录:

[root@hyc-01-01 vsftpd_user_conf]# mkdir /home/virftp/viruser1

[root@hyc-01-01 vsftpd_user_conf]# chown -R virftp:virftp /home/virftp

由于FTP虚拟用户最终通过virftp用户上传下载文件,所以需要将读写文件的目录属主属组修改为virftp

[root@hyc-01-01 virftp]# pwd

/home/virftp

[root@hyc-01-01 virftp]# ls -ld

drwx------ 3 virftp virftp 78 8  27 21:46 . 当用户被指定为/sbin/nologin/时,其家目录权限为700

[root@hyc-01-01 virftp]# ls -l

总用量 0

drwxr-xr-x 2 virftp virftp 21 8  27 21:47 viruser1

[root@hyc-01-01 vsftpd_user_conf]# touch /home/virftp/viruser1/hyc.txt

为后期测试创建

定义密码文件位置:

[root@hyc-01-01 virftp]# vim /etc/pam.d/vsftpd

用于认证的文件(登录ftp需要认证),记录登录ftp认证时的认证形式

认证时使用的密码库位置等信息

#%PAM-1.0

auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login 新增两行

注意:centos6系统需要区分32位和64位,32位时标红加粗的部分应写为lib64位时写为lib64,否则相关文件找不到会导致认证失败

account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

session    optional     pam_keyinit.so    force revoke

auth       required     pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed

auth       required     pam_shells.so

auth       include      password-auth

account    include      password-auth

session    required     pam_loginuid.so

session    include      password-auth

编辑vsftpd的主配置文件:

[root@hyc-01-01 virftp]# vim /etc/vsftpd/vsftpd.conf

1 # Example config file /etc/vsftpd/vsftpd.conf

  2 #

  3 # The default compiled in settings are fairly paranoid. This sample file

  4 # loosens things up a bit, to make the ftp daemon more usable.

  5 # Please see vsftpd.conf.5 for all compiled in defaults.

  6 #

  7 # READ THIS: This example file is NOT an exhaustive list of vsftpd options.

  8 # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's

  9 # capabilities.

 10 #

 11 # Allow anonymous FTP? (Beware - allowed by default if you comment this out).

 12 anonymous_enable=NO

 13 #

 14 # Uncomment this to allow local users to log in.

 15 # When SELinux is enforcing check for SE bool ftp_home_dir

 16 local_enable=YES

 17 #

 18 # Uncomment this to enable any form of FTP write command.

 19 write_enable=YES

 20 #

 21 # Default umask for local users is 077. You may wish to change this to 022,

 22 # if your users expect that (022 is used by most other ftpd's)

 23 local_umask=022

 24 #

 25 # Uncomment this to allow the anonymous FTP user to upload files. This only

 26 # has an effect if the above global write enable is activated. Also, you will

 27 # obviously need to create a directory writable by the FTP user.

 28 # When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access

 29 anon_upload_enable=NO 禁止匿名用户上传文件

 30 #

 31 # Uncomment this if you want the anonymous FTP user to be able to create

 32 # new directories.

 33 anon_mkdir_write_enable=NO 禁止匿名用户创建目录并对目录可写

 34 #

 35 # Activate directory messages - messages given to remote users when they

 36 # go into a certain directory.

 37 dirmessage_enable=YES

 38 #

 39 # Activate logging of uploads/downloads.

 40 xferlog_enable=YES

 41 #

 42 # Make sure PORT transfer connections originate from port 20 (ftp-data).

 43 connect_from_port_20=YES

 44 #

 45 # If you want, you can arrange for uploaded anonymous files to be owned by

 46 # a different user. Note! Using "root" for uploaded files is not

 47 # recommended!

 48 #chown_uploads=YES

g out a data connection.

 63 #data_connection_timeout=120

 64 #

 65 # It is recommended that you define on your system a unique user which the

 66 # ftp server can use as a totally isolated and unprivileged user.

81 # ASCII mangling is a horrible feature of the protocol.

 82 #ascii_upload_enable=YES

 83 #ascii_download_enable=YES

 84 #

 85 # You may fully customise the login banner string:

 86 #ftpd_banner=Welcome to blah FTP service.

 87 #

94 # You may specify an explicit list of local users to chroot() to their home

 95 # directory. If chroot_local_user is YES, then this list becomes a list of

 96 # users to NOT chroot().

109 #ls_recurse_enable=YES

110 #

111 # When "listen" directive is enabled, vsftpd runs in standalone mode and

119 # sockets. If you want that (perhaps because you want to listen on specific

120 # addresses) then you must run two copies of vsftpd with two configuration

121 # files.

122 # Make sure, that one of the listen options is commented !!

123 listen_ipv6=YES

124

125 pam_service_name=vsftpd

126 userlist_enable=YES

127 tcp_wrappers=YES

128 chroot_local_user=YES

129 guest_enable=YES 使guest_username生效

130 guest_username=virftp 虚拟用户映射到了系统用户virftp

131 virtual_use_local_privs=YES 当前ftp服务使用虚拟用户

132 user_config_dir=/etc/vsftpd/vsftpd_user_conf 定义虚拟用户配置文件所在路径

133 allow_writeable_chroot=YES

                                         

启动服务:

[root@hyc-01-01 virftp]# systemctl start vsftpd

[root@hyc-01-01 virftp]# ps aux|grep vsftp

root      3980  0.0  0.0  53256   572 ?        Ss   23:31   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

root      3982  0.0  0.0 112720   984 pts/0    R+   23:31   0:00 grep --color=auto vsftp

[root@hyc-01-01 virftp]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      512/rpcbind        

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      935/nginx: master p

…        

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1248/master        

…  

tcp6       0      0 :::54516                :::*                    LISTEN      918/rpc.statd      

tcp6       0      0 :::21                   :::*                    LISTEN      3980/vsftpd         

tcp6       0      0 :::22                   :::*                    LISTEN      909/sshd           

ftp监听21端口

15.3 使用vsftpd搭建ftp服务(下)

安装linux上的ftp客户端软件lftpwindows下可以安装filezila):

[root@hyc-01 ~]# yum install -y lftp

登录测试:

[root@hyc-01-01 pam.d]# lftp [email protected]

口令:

lftp [email protected]:~> ls      

-rw-r--r--    1 1004     1004            0 Aug 27 13:47 hyc.txt

此时可以看到local_root下的文件

lftp [email protected]:/> ?

用户可以敲?后回车查看支持的命令

cat

close

du

glob

jobs

lftp

put

get 获取远程服务端的文件

lftp [email protected]:/> get hyc.txt

lftp [email protected]:/> quit 

[root@hyc-01-01 pam.d]# ls hyc.txt

hyc.txt 登录ftp服务端get文件会把文件下载到当前目录下

 

xshell实现与ftp类似的功能

使用SFTP实现:

新建会话

blob.png

使用该会话连接服务器

输入用户名密码

blob.png

blob.png

成功登录

Connecting to 192.168.31.129:22...

Connection established.

To escape to local shell, press Ctrl+Alt+].

Your current local directory is

C:\Users\Administrator\Documents\NetSarang\Xshell\Sessions

Type `help' to browse available commnands.

sftp:/root>

get一个文件:

sftp:/root> cd /tmp

sftp:/tmp> ls php-fcgi.sock

sftp: permission denied 操作被拒绝

sftp:/tmp> get php-fcgi.sock/

sftp: cannot open /tmp/php-fcgi.sock/ to read 无法读取该文件

sftp:/tmp> get user.sql

Fetching /tmp/user.sql to user.sql

sftp: received 7.02 KB in 0.03 seconds 文件下载成功


 

设置文件存储的路径:

设置后从服务器get的文件都会存储到该路径下

blob.png

使用xftp实现:

使用按键ctrl+alt+f弹出的界面进入xftp下载页

blob.png


猜你喜欢

转载自blog.51cto.com/12216458/2165706