Mount an SMB file system
Linux has limited compatibility with the SMB protocol. We recommend mounting an SMB file system on a Linux client only when you need to share data across different operating systems. This topic describes how to mount an SMB file system on a Linux ECS instance for read and write operations.
Prerequisites
-
A Linux ECS instance is available in the same region as the SMB file system. For more information, see Create an ECS instance.
Important-
For Linux clients, we recommend mounting NFS file systems. Due to the limited compatibility of Linux with the SMB protocol, mount an SMB file system on a Linux client only when you need to share data across different operating systems.
-
The SMB clients in some earlier versions of Linux have defects in certain scenarios. If you use a Linux operating system version that is not officially supported, Alibaba Cloud cannot guarantee the reliability of the SMB file system.
This topic applies only to the following officially supported Linux distributions:
Operating system
Version
CentOS
CentOS 7.6 64-bit: 3.10.0-957.21.3.el7.x86_64 and later
Alibaba Cloud Linux
Alibaba Cloud Linux 2.1903 64-bit: 4.19.43-13.2.al7.x86_64 and later
Alibaba Cloud Linux 3.2104 64-bit: 5.10.23-4.al8.x86_64 and later
Debian
Debian 9.10 64-bit: 4.9.0-9-amd64 and later
Ubuntu
Ubuntu 18.04 64-bit: 4.15.0-52-generic and later
openSUSE
openSUSE 42.3 64-bit: 4.4.90-28-default and later
SUSE Linux
SUSE Linux Enterprise Server 12 SP2 64-bit: 4.4.74-92.35-default and later
CoreOS
CoreOS 2079.4.0 64-bit: 4.19.43-coreos and later
-
-
An SMB file system has been created. For more information, see Create a General-purpose NAS file system.
-
A mount target has been created. For more information, see Create a mount target.
-
Network connectivity has been established.
-
Ensure that the Linux ECS instance and the SMB file system are in the same VPC.
-
Check the security group associated with the file system to ensure that the Linux ECS instance can access the SMB file system.
-
Ensure that TCP port 445 is open. SMB file systems communicate over TCP port 445.
If port 445 is not open, add a security group rule that allows access to port 445 to the security group of the target ECS instance. For more information, see Add a security group rule.
-
Install a CIFS client
Before you can mount an SMB file system on a Linux client, you must install a CIFS client. This is a one-time configuration that you need to perform on each Linux server.
-
Connect to the Linux ECS instance. For more information, see Connection methods.
-
Install the CIFS client.
Ubuntu and Debian
Use the apt-get package management tool.
sudo apt-get update sudo apt-get install cifs-utilsRHEL, CentOS, and Alibaba Cloud Linux
Use the yum package manager.
sudo yum install cifs-utilsOpenSUSE and SLES12-SP2
Use the zypper tool.
sudo zypper install cifs-utilsCoreOS
Use the following method to install the client:
-
Configure SELinux.
sed -i 's/SELINUXTYPE=mcs/SELINUXTYPE=targeted/' /etc/selinux/config -
Manually compile the CIFS client tools on CoreOS.
You can follow these steps to start a Fedora container to compile the CIFS client tools. Alternatively, download the official Alibaba Cloud CIFS toolkit for CoreOS and copy it to the /tmp/ or /bin directory.
-
On CoreOS, start a Fedora container to compile cifs-utils.
docker run -t -i -v /tmp:/cifs fedora /bin/bash -
In the Docker environment, run the following commands in sequence.
yum groupinstall -y "Development Tools" "Development Libraries"yum install -y bzip2curl https://download.samba.org/pub/linux-cifs/cifs-utils/cifs-utils-6.9.tar.bz2 --output cifs-utils-6.9.tar.bz2;bunzip2 cifs-utils-6.9.tar.bz2 && tar xvf cifs-utils-6.9.tarcd cifs-utils-6.9; ./configure && makecp mount.cifs /cifs/exit
-
-
Mount the SMB file system
On Linux, you can mount a NAS SMB file system either manually or automatically. A manual mount is temporary and must be repeated each time the server restarts. An automatic mount is persistent and does not need to be repeated after a restart. To ensure the mount persists after a reboot, we recommend configuring an automatic mount after you successfully mount the file system manually.
Manual mount
To mount the NAS SMB file system to a Linux ECS instance, you must use its mount target address.
-
Connect to the Linux ECS instance. For more information, see Connection methods.
-
Run the following command to mount the file system.
sudo mount -t cifs //file-system-id.region.nas.aliyuncs.com/myshare /mnt -o vers=2.1,guest,uid=0,gid=0,dir_mode=0755,file_mode=0755,mfsymlinks,cache=strict,rsize=1048576,wsize=1048576The command syntax is:
sudo mount -t cifs //<mount-target-address>/myshare <mount-directory> -o <mount-options>Parameter
Description
File system type
When you mount an SMB file system on a Linux operating system, you must specify the
-t cifsparameter.Mount target address
The mount target address that is automatically generated when you create a mount target for the file system. Replace the placeholder with your actual value. To obtain the mount target address, log on to the NAS console, click the ID of the target file system, click Mount Targets, and then hover over the
icon in the Mount Target column.myshare
The name of the shared directory of the SMB file system. This name cannot be changed.
Mount directory
The local directory where you want to mount the file system, such as /mnt/sharepath.
Mount options
Specify required mount options by adding the
-oparameter:-
vers: The SMB protocol version. Versions 2.1 and later are supported.
-
guest: Enables guest access, which does not require a password. This option is required for clients that use NTLM authentication.
-
rsize: The maximum size of data in bytes for each read request. We recommend that you set this to 1048576 (1 MB).
-
wsize: The maximum size of data in bytes for each write request. We recommend that you set this to 1048576 (1 MB).
Specify optional mount options by adding the
-oparameter:-
uid: The user ID of the file owner after the file system is mounted. If this option is not specified, the default value is 0.
-
gid: The group ID of the file owner after the file system is mounted. If this option is not specified, the default value is 0.
-
dir_mode: The permissions for directories. The value must start with 0, such as 0755 or 0644. If this option is not specified, the default value is 0755.
-
file_mode: The permissions for regular files. The value must start with 0, such as 0755 or 0644. If this option is not specified, the default value is 0755.
-
mfsymlinks: Enables support for symbolic links.
-
cache:
-
cache=strict: Enables client-side caching for the SMB file system. This is the default setting if the cache option is not specified.
-
cache=none: Disables client-side caching for the SMB file system.
-
-
atime|relatime: If your business is not highly sensitive to file access times, do not use the atime option. By default, the file system is mounted with the relatime option.
Note-
An authorized administrator has full control over the SMB file system.
-
You can run the
mount | grep cifscommand to query your mount target information. -
If you use an unsupported Linux distribution, we strongly recommend that you use a kernel version of 3.10.0-514 or later. If the Linux kernel version is 3.7 or earlier, you must set the
cache=strictparameter in the mount options. You can run theuname -acommand to check the current kernel version.
-
-
Run the
mount -lcommand to check the mount result.If the output is similar to the following, the mount was successful.
debugfs on /sys/kernel/debug type debugfs (rw,relatime) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime) mqueue on /dev/mqueue type mqueue (rw,relatime) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime) tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=800928k,mode=700) //xxx-cn-hangzhou.nas.aliuncs.com/mushare on /mnt type cifs (rw,relatime,vers=2.1,sec=none,cache=strict,domain=,uid=0,noforceuid,gid=0,noforcegid,addr=xxx,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1)After a successful mount, you can run the
df -hcommand to view the capacity information of the current file system.If the mount fails, troubleshoot the issue. For more information, see Troubleshoot SMB mount failures on Linux.
-
After the file system is mounted, you can access it from the Linux ECS instance to perform read or write operations.
You can access and use the NAS SMB file system as a regular directory. Example:
[root@ixxxx ~]# mkdir /mnt/dir1 [root@ixxxx ~]# mkdir /mnt/dir2 [root@ixxxx ~]# touch /mnt/file1 [root@ixxxx ~]# echo 'some file conent' > /mnt/file2 [root@ixxxx ~]# ls /mnt dir1 dir2 file1 file2 tmp
(Optional) Automatic mount
You can configure the /etc/fstab file on the Linux ECS instance to automatically mount the SMB file system when the instance restarts.
Before you configure automatic mounting, make sure that the manual mount is successful. This helps prevent the ECS instance from failing to start.
-
Open the /etc/fstab configuration file and add the automatic mount configuration.
-
Replace <file-system-id.region.nas.aliyuncs.com> with the actual mount target address of the NAS file system, for example,
237*******-*****.cn-hangzhou.nas.aliyuncs.com. -
Replace </mnt> with the local mount directory, for example,
/mnt.
file-system-id.region.nas.aliyuncs.com/myshare /mnt cifs vers=3,guest,uid=0,gid=0,dir_mode=0755,file_mode=0755,mfsymlinks,cache=strict,rsize=1048576,wsize=1048576 0 0For a description of the main parameters in the example, see the parameter table for the manual mount command. The other parameters are described as follows.
Parameter
Description
0 (the first value after wsize)
A non-zero value indicates that the file system should be backed up by the dump command. For a NAS file system, this value must be 0.
0 (the second value after wsize)
The order in which the fsck command checks file systems at startup. For a NAS file system, this value must be 0, which indicates that fsck should not run at startup.
-
-
Run the
rebootcommand to restart the ECS instance.ImportantRestarting the ECS instance interrupts your services. We recommend that you perform this operation during off-peak hours.
-
Verify that the automatic mount was successful.
NoteWithin one minute after the ECS instance restarts, run the
df -hcommand to confirm that the NAS file system is mounted.
Use cases
You can configure different mount options for different use cases to achieve optimal file system performance. The following sections describe common use cases and their recommended mount option configurations.
Shared access for Linux
Multi-user home directories
Shared access for web servers
Shared access for Windows and Linux
Access control
Alibaba Cloud NAS SMB file systems support user authentication and file system-level permission control based on an Active Directory (AD) domain. By accessing an SMB file system as a domain user, you can implement file- and directory-level access control.
For more information about how to implement user authentication and file system-level access control based on an AD system, see Join an SMB mount target to an AD domain and Mount and use an SMB file system on a Linux client as an AD domain user.
Related documents
-
If you want to back up the data in an SMB file system, see Back up a General-purpose NAS file system.
-
You can also enable the recycle bin. If you accidentally delete files from a General-purpose NAS file system, you can use the recycle bin to restore these files and their metadata, such as UIDs, GIDs, and ACLs. For more information, see Recycle bin.
-
If you want to mount and access an SMB file system from a local server or an on-premises data center, see Access a file system from an on-premises data center.
-
If you need to mount a file system across VPCs, regions, or accounts, you can use Cloud Enterprise Network (CEN). For more information, see Use CEN to mount a NAS file system across VPCs in the same region or Mount a NAS file system across accounts and regions by using CEN.