首页 > 系统相关 >Ubuntu通过NFS实现文件共享

Ubuntu通过NFS实现文件共享

时间:2022-12-03 23:38:15浏览次数:67  
标签:文件共享 sudo share 192.168 etc nfs Ubuntu NFS root


搭建基础 NFS 服务

服务端(192.168.1.1)

# 安装 NFS Server 服务
sudo apt-get install nfs-kernel-server -y

# 创建共享目录并设置权限
cd /data
mkdir -p share
chmod 777 share

# 编辑配置文件(/data/share 为共享目录)
sudo vim /etc/exports
/data/share *(insecure,rw,sync,no_root_squash)
# 或者指定客户端 ip 为 192.168.1.2 才能挂载
# /data/share 192.168.1.2(insecure,rw,sync,no_root_squash)

# 开启 nfs 服务
sudo /etc/init.d/nfs-kernel-server start
# 或者
sudo service nfs-kernel-server start | restart | status

###################################
# 辅助命令
###################################
# 列出 服务器(192.168.1.1)上 nfs 服务的共享目录
showmount -e 192.168.1.1
showmount -e localhost

# 将配置文件中的目录全部重新export一次!无需重启服务。
sudo exportfs -rv

# 查看 nfs 的运行状态
sudo nfsstat

# 查看 nfs 是否启动(nfs 主端口是 2049)
rpcinfo -p

 

客户端(192.168.1.2)

# 安装 nfs-common 工具
sudo apt-get install nfs-common

# 挂载远程目录
sudo mount 192.168.1.1:/data/share /data/share

# 查看挂载盘信息
df -h

###################################
# 辅助命令
###################################
# 取消挂载
umount -l /data/share

 

问题排查

# 问题1 
# 执行
sudo mount 192.168.1.1:/data/share /data/share
# 报错
mount.nfs: access denied by server while mounting 192.168.1.1:/data/share
# 解决, 增加 insecure 选项
sudo vim /etc/exports
/data/share *(rw,sync,no_root_squash)
# 改为
/data/share *(insecure,rw,sync,no_root_squash)
# 重启服务
sudo service nfs-kernel-server restart


# 问题2
# 客户端开机没有共享文件夹
# 重启rpcbind
sudo /etc/init.d/rpcbind restart
# 重启nfs
sudo /etc/init.d/nfs-kernel-server restart
sudo mount 192.168.1.1:/data/share /data/share

 

​附录​

1. /etc/exports 文件中的 Host 格式

​/etc/exports​​​ 文件的格式为:​​Directory Host(Options ...) Host(Options) #comment​

其中的 Host 项用来指定可访问对应共享目录的主机,其格式可分为以下几种:

# 1. 单个主机, Host 项可以为一个或多个单独的 TCP/IP 主机名或 IP 地址
admin
admin.starky.net
192.168.56.101

# 2. IP 子网
10.0.0.0/255.0.0.0
172.16.0.0/255.255.0.0
192.168.56.0/24

# 3. TCP/IP 域, 通过使用通配符,可以指定某个特定域中的全部或部分主机
*.starky.net
*craft.starky.net
???.starky.net

# 4. NIS 组
可以指定某个 NIS 组中所有主机的访问权限,使用 @group

 

2. /etc/exports 文件中的 Options

选项

描述

ro

只读权限

rw

读写权限(默认)

rw=list

通过 list 指定具有写权限的客户端主机,其他主机则为只读权限

root_squash

将 UID 0 和 GID 0 映射到 anonuid 和 anongid(即 Ubuntu 系统中的 nobody 和 nogroup)

no_root_squash

允许需要 root 权限的文件操作,有安全风险

all_squash

将所有的 UID 和 GID 映射到它们的匿名形式,主要针对不信任的主机

anonuid=xxx

指定客户端 root 权限的操作需要映射到的 UID(默认是65534)

anongid=xxx

指定客户端 root 权限的操作需要映射到的 GID(默认是65534)

insecure

允许通过任意端口的远程访问

async

服务器可以在写入硬盘之前响应客户端的写入请求

wdelay

通过延迟同步多个客户端对文件的更新

sec=flavor

指定共享目录的安全验证方法,包括 ​​sys​​​(UNIX 验证),​​dh​​​ (DES),​​krb5i​​​,​​krb5p​​​ 和 ​​none​​(匿名访问)

 

3. NFS 挂载选项

选项

描述

rw

以读写模式挂载文件系统(rw 也需在服务端定义)

ro

以只读模式挂载文件系统

bg

如挂载失败(服务器无响应),在后台继续尝试并执行其他挂载请求

hard

如果服务器无响应,重复发送请求直到服务器回复

soft

如果服务器无响应,重复发送请求,超过一定时间后返回错误,而不会一直阻塞

intr

允许用户中断阻塞的文件操作(并返回错误)

nointr

不允许用户中断客户端的文件操作请求

retrans=n

在 soft 模式下,指定返回错误前重复发送请求的次数

timeo=n

设置超时后重复发送请求的时间间隔(单位 1/10 秒)

rsize=n

设置读取 buffer 大小为 n bytes

wsize=n

设置写入 buffer 大小为 n bytes

sec=flavor

设置安全验证方法

proto=proto

设置传输协议,NFSv4 必须为 TCP

 

4. /etc/exports示例

/var/nfs/gernel  192.168.56.0/24(rw,insecure,sync,no_subtree_check)
/var/nfs/public *(ro,insecure,sync,no_subtree_check)
/home/starky 192.168.56.1(rw,insecure,no_root_squash,sync,no_subtree_check)
  • 第一条纪录表示 192.168.56.0/24 子网中的所有主机都可挂载 ​​var/nfs/gernel​​ 目录并拥有读写(rw)权限
  • 第二条纪录表示所有主机都可挂载 ​​/var/nfs/public​​ 目录且拥有只读ro)权限
  • 第三条纪录表示客户端 IP 地址为 192.168.56.1 的主机可以挂载 ​​/home/starky​​ 目录并拥有读写权限,而且任何 root 权限(UID=0 , GID=0)的文件操作都不默认映射给 nobody:nogroup,而保持属主(组)仍为 root(no_root_squash
  • insecure 选项:允许通过任意端口的远程访问
  • sync 选项:强制 NFS 服务器在响应请求之前将文件的改动写入磁盘(强调客户端和服务端文件内容的一致性,但会降低文件操作的效率)。
  • no_subtree_check 选项:禁用 subtree_check 。subtree_check 用来设置服务器在收到请求时,检查该文件是否在指定目录结构中依旧可用(该选项会在某些情况下发生错误:重命名某文件的同时,该文件在客户端打开)。

 

​​Ubuntu 设置开机自启动​​

方式1

# rc.local 脚本是一个 ubuntu 开机后(重启后)会自动执行的脚本
sudo vim /etc/rc.local

 

方式2

# 在 /etc/init.d 下新建脚本 auto-script
touch /etc/init.d/auto-script
# 编辑脚本内容
vim /etc/init.d/auto-script

# 设置脚本权限
chmod 755 auto-script
# 将脚本添加到启动脚本
update-rc.d auto-script defaults 90

# 移除开机脚本
update-rc.d -f auto-script remove

 

3. systemctl 方式

在 ​​/etc/init.d/​​ 下新建脚本 auto-script 并编辑脚本内容后, 执行以下命令设置开启自启动

systemctl enable auto-script
# 或者
systemctl enable auto-script.service

适用于Ubuntu 和 Centos

systemctl 命令

# 启动服务
systemctl start nginx.service

# 停止服务
systemctl stop nginx.service

# 重启服务
systemctl restart nginx.service

# 查看服务状态
systemctl status nginx.service

# 设置开机自启动
systemctl enable nginx.service

# 停止开机自启动
systemctl disable nginx.service

# 查看所有服务(包括启动失败的,已经启动的,退出运行的)
systemctl list-units --type=service

 

4. 查看开机启动项

systemd-analyze blame

 

参考


标签:文件共享,sudo,share,192.168,etc,nfs,Ubuntu,NFS,root
From: https://blog.51cto.com/u_15891990/5908773

相关文章

  • cpp mysql ubuntu
    1.Installlibmysqlcppconn-devsudoapt-getinstalllibmysqlcppconn-dev2.//MySQLHelper.h#include<iostream>#include<mysql_connection.h>#include<mysql......
  • Ubuntu18.04安装docker
    一、安装1.更新源sudoapt-getupdate2.安装依赖:sudoapt-getinstallapt-transport-httpsca-certificatescurlgnupg2software-properties-common3.信任Do......
  • Ubuntu 22.10 阿里云源
    Ubuntu22.10更换阿里云源sudovim/etc/apt/sources.listdebhttp://mirrors.aliyun.com/ubuntu/kineticmainrestricteduniversemultiversedeb-srchttp://mirro......
  • linux(ubuntu/debian)安装QT
    开源版和商业版最新的QT是付费的商业软件,但是其社区提供了开源的免费版本,供学习使用。完整版安装使用QT安装脚本,但是opensource版最新的只支持到5.14.2(目前),下载地址:......
  • 解决 Ubuntu 22.04.1 LTS 初次安装时无法访问网络问题
    背景:在VM中安装了Ubuntu22.04.1LTS,但是其无法访问网络。解决措施:编辑/etc/NetworkManager/NetworkManager.conf文件,将managed=false设置为managed=true关闭网络......
  • Ubuntu下安装Atom编辑器并画PlantUML图
    系统版本:Ubuntukylin15.101.安装Atom编辑器打开终端,然后使用下面的命令行:sudoadd-apt-repositoryppa:webupd8team/atomsudoapt-getupdatesudoapt-getinstallat......
  • 阿里云的堡垒机无法使用秘钥登录Ubuntu服务器
    服务器是阿里云上的Ubuntu22.04堡垒机是用的阿里云提供的秘钥对也是阿里云生成的但是,堡垒机始终无法用秘钥登录该服务器,只能使用密码登录查看日志/var/log/auth.log,有如下......
  • Ubuntu22.04安装CUDA深度学习环境&&cuda principle
    environment:neofetch&&uname-a|lolcatinstallnvidiaGPUdriver:sudoadd-apt-repositoryppa:graphics-drivers/ppa#加入官方ppa源sudoaptupdate#检查软件包......
  • Ubuntu22.04 Server安装
    本篇主要记录在OracleVmVirtualBox中安装Ubuntu22.04Server,并设置静态IP1.下载VirtualBox下载地址https://www.virtualbox.org/wiki/Downloadsubuntu下载地址ht......
  • ubuntu18.04下cmake的安装
    一.使用安装命令sudoaptinstallcmake这种方式安装最为简单,但是,这种方式安装的不是最新版本的Cmake。我此次安装cmake是因为要编译fastdds,其实之前系统中有cmake,但......