Ubuntu中挂载使用nas服务器

建好群晖nas后,在Windows上做映射很简单,但在Ubuntu上使用,还是耗费了一些时间。
尝试过如下三种方法:

  1. smb
  2. Ubuntu自带的网络共享-afp
  3. mount

最终选择的方法是mount到本地,读写及用Python代码等直接访问很方便。

1. smb(尝试一,不用)

之前一直用smb在Windows和Ubuntu之间共享数据,所以第一个尝试使用samba连接nas服务器。
结果是可以访问,可以读写,但自己写代码读取数据比较困难,应该是有方法的,但没深究。

1.1 安装samba环境

环境安装参考:Ubuntu 18.04安装Samba服务器及配置

  1. 更新当前软件
sudo apt-get upgrade 
sudo apt-get update 
sudo apt-get dist-upgrade
  1. 安装samba服务器
sudo apt-get install samba samba-common

设置共享文件夹略…

1.2 访问nas

打开文件管理器,在【other locatios】中的【connect to server】中输入nas地址,弹出用户验证框输入用户名及密码即可。

2. Ubuntu自带的网络共享(尝试二,不用)

打开Ubuntu文件管理器,在【other locatios】中的【Networks】列表中即可找到nas共享文件夹,点击在弹出的验证框中输入用户名及密码即可。

3. mount到本地(非常好用)

前边两种方法对于读写来说都很好用,但是我要写代码与nas做数据交互,都比较困难,所以考虑直接mount。但中途又遇到了一些问题,最耗时的就是mount完后,归属与root,在普通用户下只能读,不能写,找了好久才解决。

3.1 安装cifs-utils

sudo apt install cifs-utils

3.2 新建一个目标文件夹

将nas挂载到/mnt/nas下

cd /mnt/
sudo mkdir nas

3.3 执行挂载操作

sudo mount -t cifs -o uid=***,username=***,password=***,iocharset=utf8 nas地址 本地地址

各参数解析如下:

  • uid:最关键,可以指定所有者,解决只能root权限的问题,这里=Ubuntu用户名
  • username:在nas上的用户名
  • password:nas上用户名对应的密码,可以不写,回车后会有密码输入行
  • iocharset:路径中如有中文则添加此项,支持中文路径
  • nas地址:如"//192.168.0.1/share"
  • 本地地址:如"/mnt/nas"

3.4 设置开机自动挂载

sudo vim /etc/fstab

添加一行信息

nas地址 本地挂载地址 cifs uid=***,username=***,password=***,iocharset=utf8 0 0

保存退出后,输入

sudo mount -a

可以看到,已经挂载成功。

3.5 配置/etc/fstab后还不能开机自动挂载的解决方法

一般不需要进行此项配置,在执行3.4后,重启后不自动挂载,可以执行此操作。

  1. 开启服务
systemctl start remote-fs.target
systemctl enable remote-fs.target
  1. 提升用户权限为root和sudo
sudo vi /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root	ALL=(ALL:ALL) ALL
# add user
[username] ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL
# add user
[username] ALL=(ALL:ALL) NOPASSWD:ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

添加两处[# add user],[username]为当前Ubuntu用户名称。

  1. 开机执行命令
cd && vi .bashrc

在最后一行加入下面命令

sudo mount -a

保存退出。

  • 14
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值