Posted on 09/09/2014

In almost all cases, when mounting a CIFS-share on a Linux host, you will need to supply some credentials. Either you could enter the credentials by hand every time you need the share or add the credentials to /etc/fstab to automatically mount the share. Entering the password manually is secure but not comfortable, leaving the password in /etc/fstab is comfortable but not secure since the file /etc/fstab is world readable.

Generally, it’s a good idea to password protect shares since you don’t want everyone to freely have access to a share. The “problem” you have with that, if you want to automatically mount the share on your Linux-system, is that the password needs to be saved somewhere or entered manually. For obvious reasons, entering the password every time you need the share isn’t very convenient. Especially not when you want the share to be automatically mounted on boot. This article is about how to avoid manually mounting a Windows share and still keep the credentials secure.

Installing CIFS support

A share created on a Windows-machine can be used on a Linux box by using the CIFS file system. CIFS (Common Internet File System) is a dialect of SMB (Server Message Block).

First thing to do before we are able to use a CIFS-share on our Linux machine is to make sure that it understands how to talk CIFS and thus has support for the CIFS file system.

To check which file systems are supported on your machine:

[jensd@cen ~]$ cat /proc/filesystems

nodev sysfs

nodev rootfs

nodev bdev

nodev proc

nodev cgroup

nodev cpuset

nodev tmpfs

nodev devtmpfs

nodev debugfs

nodev securityfs

nodev sockfs

nodev pipefs

nodev anon_inodefs

nodev configfs

nodev devpts

nodev ramfs

nodev hugetlbfs

nodev autofs

nodev pstore

nodev mqueue

nodev selinuxfs

xfs

nodev rpc_pipefs

nodev nfsd

nodev binfmt_misc

As you can see in the above list, CIFS is not there. This means that we’ll have to install the necessary packages to support CIFS. In case you were wondering (as I did), the nodev option means that such filesystem doesn’t require a block device but can be used as a virtual fs.

To install CIFS-support on RHEL/CentOS/SL and variants:

[jensd@cen ~]$ sudo yum install cifs-utils

...

Complete!

For Debian/Ubuntu/Mint and variants:

jensd@deb:~$ sudo apt-get install cifs-utils

When checking the entries in /proc/filesystems after installation, you should see CIFS:

[jensd@cen ~]$ cat /proc/filesystems |grep cifs

nodev cifs

On some Linux distro’s, filesystems do not appear in /proc/filesystems before the first use, even if it’s installed. In that case you can check which kernel modules are available for filesystems:

To look at the full list:

[jensd@cen ~]$ ls /lib/modules/$(uname -r)/kernel/fs/*/*ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/btrfs/btrfs.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/cachefiles/cachefiles.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/cifs/cifs.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/cramfs/cramfs.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/dlm/dlm.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/exofs/libore.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/ext4/ext4.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/fat/fat.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/fat/msdos.ko

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/fat/vfat.ko

...

To look for CIFS-support:

[jensd@cen ~]$ ls /lib/modules/$(uname -r)/kernel/fs/*/*ko|grep cifs

/lib/modules/3.10.0-123.el7.x86_64/kernel/fs/cifs/cifs.ko

Mount the CIFS share manually

After installing the packages and checking the filesystem support, our system should be able to mount a Windows/CIFS-share. The best way to be sure is simply to mount a CIFS-share:

[jensd@cen ~]$ sudo mount -t cifs //192.168.202.2/drive_e /mnt -o user=jensd

Password for jensd@//192.168.202.2/drive_e: **********

[jensd@cen ~]$ mount

...

//192.168.202.2/drive_e on /mnt type cifs (rw,relatime,vers=1.0,cache=strict,username=jensd,domain=TEST,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.202.2,file_mode=0755,dir_mode=0755,nounix,rsize=61440,wsize=65536,actimeo=1)

As you can see in the above output, we had to enter the password manually when mounting. To avoid entering the password, it is possible, next to the username, to supply the password directly on the command but this means it’s readable by everyone looking at your screen or previously entered commands.

Automatically mount the CIFS share

What we really want is to automatically mount the share on boot. For that, we basically have two options:

  • The first option is to create a small script with the above mount-command, including the password, and let it run on boot. The positive thing with this option would be that the script can be protected from being read by other users by changing the permissions. The negative part is that a simple mount or re-mount won’t work anymore since our mountpoint isn’t in /etc/fstab and that this isn’t really considered as a best practice solution.
  • The second, and best, option, is to add the mountpoint to /etc/fstab. The only problem we have there is that we will have to find a way to supply the credentials. The file /etc/fstab is readable by everyone, so to put the password directly in /etc/fstab isn’t really a good idea.

To continue with the second option, we’ll provide the credentials required in an external file. The file only contains the required username and password and we can restrict the file to be only readable by root. The fstab-entry contains only the path to the file.

The file providing the credentials which is made only readable by root:

[jensd@cen ~]$ sudo vi /root/.smbcred

[jensd@cen ~]$ sudo cat /root/.smbcred

username=jensd

password=secret

[jensd@cen ~]$ sudo chmod 400 /root/.smbcred

[jensd@cen ~]$ sudo ls -al /root/.smbcred

-r--------. 1 root root 36 Sep 9 15:43 /root/.smbcred

The line to automatically mount the share on boot in /etc/fstab:

[jensd@cen ~]$ cat /etc/fstab|grep /mnt

//192.168.202.2/drive_e /mnt  cifs  credentials=/root/.smbcred  0 0

The line in /etc/fstab consists out of 6 parts:

  • the remote location (//192.168.202.2/drive_e)
  • the local mountpoint (/mnt)
  • the type of filesystem (cifs)
  • the options (credentials=/root/.smbcred)
  • dump-option (0)
  • check/pass-option (0)

After adding the above line, we can simply mount our share without providing credentials. On top of that, the share should be mounted at boot time automatically

[jensd@cen ~]$ sudo mount /mnt/

The above seems to be a simple solution, and it is, but I still see too often that password are simply entered in /etc/fstab or that a “work-around-boot-script” is used in order to prevent other from knowing precious Windows-share passwords.

This entry was posted in CentOS, Debian, Linux, Red Hat, RHEL, Security, Windows by jensd. Bookmark the permalink.

Mount Windows (CIFS) shares on Linux with credentials in a secure way的更多相关文章

  1. Linux开发环境搭建三 使用mount -t cifs 挂载windows共享目录方法与问题解决

    转载链接:https://blog.csdn.net/fuyuande/article/details/82915800 嵌入式开发通常是在linux环境下编译,windows下开发,这就需要在lin ...

  2. Linux mount Windows目录

    [问题描述] Windows 机器192.168.1.103共享了 /share/yasi 目录,并且赋予了写的权限,在Windows机器下可以用 yasi/pass 登录.在一台CentOS 6.3 ...

  3. 创建cifs系统案例之“实现将Windows磁盘共享至Linux”

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  4. smbclient和mount -t cifs共享win的共享文件夹? autocad小记

    插入U盘没有反应? 首先,打开设备管理器, 发现usb大容量设备为黄色感叹号 其次, 将这个usb大容量设备先卸载, 然后点击"自动扫描硬件变化",就可以重新自动安装usb的驱动. ...

  5. [转帖]windows CIFS sabma协议识

    windows CIFS sabma协议识别 https://www.cnblogs.com/tcicy/p/9992871.html 公司的一个共享服务器就是 win2003的 mount 的时候 ...

  6. How to mount Windows network disk in WSL

    Backgroud Mount samba directly in wsl like linux is difficult Password for root@//filesystem.domain/ ...

  7. VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录

    VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录 一.描述 系统描述:win7旗舰版64位系统+VMware Workst ...

  8. windows下能读写linux分区的软件 转

    1. ext2ifs 这个工具与explore2fs都是John Newbigin使用Delphi写的,explore2fs Copyright (C) 2000,Ext2IFS v0.3 Copyr ...

  9. windows nfs server for linux

    摘要 在开发嵌入式系统的过程中,为了方便调试与文件共享,需要使用到nfs,即网络文件系统,这位板子的调试测试带来了很大的方便.之前在linux系统下开发,与ARM11核心板 linux系统对接共享也比 ...

随机推荐

  1. Twisted & Treq

    1. Install treq:pip install treq 2. If twisted install failed, please reinstall itpip install twiste ...

  2. Python-sympy科学计算与数据处理(数学表达式)

    数学表达式 from sympy import * 1/2+1/3 S(1)/2+1/S(3) Out[4]: 5/6 Rational(5,10) Out[5]: 1/2 x,y = symbols ...

  3. excel怎么设置密码保护?Excel文件添加密码保护教程

    excel怎么设置密码保护?Excel文件添加密码保护教程 众所周知,Excel具有强大的数据处理和数据分析能力,广泛应用于加工学统计及金融统计中.特别是金融统计需要较高的安全性,那么就一定要为Exc ...

  4. 修改Mysql 数据库的密码

    1.修改my.cnf 在这个文件里面加入 skip-grant-tables 修改之后,保存.然后重启数据库 2.由于我的系统是Centos7 ,数据库是mariadb ,所以执行命令如下: 3.重启 ...

  5. Java 基础篇之注解

    注解 注解,其实是代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行相应的处理.通过使用注解,可以在不改变原有逻辑的情况下,在源文件中嵌入补充一些信息. Annotation 提供了一 ...

  6. 【miscellaneous】海康相机RTSP连接代码分析

    海康相机RTSP连接代码分析 最近在做海康相机rtsp连接获取音视频的工作,现在介绍一下分析过程和源码. [源码在我上传的共享资料中: http://download.csdn.net/detail/ ...

  7. vs2015试用到期,不能输入序列号

    如果是社区版,登录账号即可, 如果不能登录账号,可以执行修复再登录账号: 控制面板-程序和应用-vs2015(我写的是简称)-右键-更改-修复-输入序列号

  8. Stream系列(十四)parallet方法使用

    并发 视频讲解:https://www.bilibili.com/video/av78408286/ EmployeeTestCase.java package com.example.demo; i ...

  9. poj1155 TELE (树上分组背包)

    题目链接:https://vjudge.net/problem/POJ-1155 题意:给定一颗以1为根的边权树,有n个结点,其中m个叶子结点,每个叶子结点有一个价值.要求从m个叶子结点中选最多的结点 ...

  10. [转帖]SSL延迟有多大?

    SSL延迟有多大? http://www.ruanyifeng.com/blog/2014/09/ssl-latency.html 转帖 作者: 阮一峰 日期: 2014年9月24日 感谢 腾讯课堂N ...