This topic describes how to mount a Network File System (NFS) file system on a Linux Elastic Compute Service (ECS) instance. If an NFS client is installed on a Linux ECS instance, you can manually mount the NFS file system or automatically mount it.

Configure a Linux ECS instance

To mount an NFS file system in a Linux operating system, you must configure a Linux ECS instance. After you configure each Linux ECS instance for the first time, you no longer need to configure the instance each time you mount a file system.

  1. Connect to the ECS instance. For more information, see Connection methods.
  2. Install an NFS client.
    • If CentOS, Red Hat Enterprise Linux (RHEL), or Alibaba Cloud Linux is running on the ECS instance, run the following command to install the NFS client:
      sudo yum install nfs-utils
    • If Ubuntu or Debian is running on the ECS instance, run the following commands to install the NFS client:
      sudo apt-get update
      sudo apt-get install nfs-common
  3. Increase the number of concurrent NFS requests.
    Run the following command to set the maximum number of concurrent NFS requests to 128. For more information, see How can I modify the maximum number of concurrent NFS requests?.
    if (lsmod | grep sunrpc); then
    (modinfo sunrpc | grep tcp_max_slot_table_entries) && sysctl -w sunrpc.tcp_max_slot_table_entries=128
    (modinfo sunrpc | grep tcp_slot_table_entries) && sysctl -w sunrpc.tcp_slot_table_entries=128
    fi
    (modinfo sunrpc | grep tcp_max_slot_table_entries) && echo "options sunrpc tcp_max_slot_table_entries=128" >> /etc/modprobe.d/alinas.conf
    (modinfo sunrpc | grep tcp_slot_table_entries) && echo "options sunrpc tcp_slot_table_entries=128" >> /etc/modprobe.d/alinas.conf

Manually mount an NFS file system

You can use the mount target of an NFS file system to mount the file system on an ECS instance.

  1. Mount the NFS file system.
    • To mount a Capacity or Performance NAS file system, run the following command.
      Note
      • To ensure optimal performance, we recommend that you mount a file system by using the NFSv3 protocol.
      • If your application depends on file locks and you need to edit a file on multiple Linux ECS instances at the same time, use the NFSv4 protocol to mount the file system.
      Use the NFSv3 protocol to mount a file system:
      sudo mount -t nfs -o vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.nas.aliyuncs.com:/ /mnt
      Use the NFSv4 protocol to mount a file system:
      sudo mount -t nfs -o vers=4,minorversion=0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.nas.aliyuncs.com:/ /mnt                                
    • To mount an Extreme NAS file system, run the following command:
      sudo mount -t nfs -o vers=3,nolock,noacl,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt
    The following table lists the parameters that are used in the mount command.
    Parameter Description
    Capacity or Performance NAS: file-system-id.region.nas.aliyuncs.com:/ /mnt

    Extreme NAS: file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt

    <Domain name of the mount target>:<Name of the shared directory> <Path of the mount directory>. Replace the domain name of the mount target, the name of the shared directory, and the path of the mount directory based on your business requirements.
    • Domain name of the mount target: Log on to the NAS console and click File System List in the left-side navigation pane. On the page that appears, click Manage in the Actions column on the right of the file system. Then, click the Mount Targets tab to view the domain name of the mount target. For more information, see Manage mount targets.
    • Name of the shared directory: specifies the root directory / or a subdirectory. If you specify a subdirectory such as /share, make sure that the subdirectory exists.
      Note The shared directory of an Extreme NAS file system must start with /share, for example, /share and /share/subdir.
    • Path of the mount directory : the root directory / of the Linux ECS instance or a subdirectory such as /mnt. If the mount directory is a subdirectory, make sure that the subdirectory exists.
    vers The protocol version of the file system.
    • vers=3: uses NFSv3 to mount the file system.
    • vers=4: uses NFSv4 to mount the file system.
    Note
    • Capacity NAS and Performance NAS file systems support NFSv3 and NFSv4.
    • Extreme NAS file systems support only NFSv3.
    Mount options

    When you mount a file system, you can select multiple mount options. These mount options are separated by commas (,). For example, you can select the following options:

    • rsize: specifies the size of data blocks that the client reads from the file system. Recommended value: 1048576.
    • wsize: specifies the size of data blocks that the client writes to the file system. Recommended value: 1048576.
      Note To prevent performance degradation, we recommend that you specify the maximum value (1048576) for both the rsize mount option and the wsize mount option.
    • hard: specifies that applications stop accessing a file system when the file system is unavailable, and wait until the file system is available. We recommend that you enable the hard parameter.
    • timeo: specifies the period in deciseconds (tenths of a second) for which the NFS client waits before it retries to send a request. Recommended value: 600 (60 seconds).
      Note If you need to modify the timeo mount option, we recommend that you specify 150 or a greater value. The timeo mount option is measured in deciseconds (tenths of a second). For example, the value 150 indicates 15 seconds.
    • retrans: specifies the number of times the NFS retries to send a request. Recommended value: 2.
    • noresvport: specifies that a new TCP port is used to ensure network continuity between the file system and the ECS instance when the network recovers from failure. We recommend that you enable the noresvport parameter.
    Notice
    • To prevent data inconsistency, we recommend that you do not use the soft mount option. Use of the soft option is at your own risk.
    • We recommend that you use the default values for other mount options. Performance degradation may occur due to changes in several mount options. These mount options include the size of the read or write buffer or the use of attribute caching.
  2. Run the mount -l command to view the mount result.
    The command output in the following figure indicates a successful mount. View the mount resultAfter the file system is mounted, you can run the df -h command to view the capacity of the file system.

    If the file system fails to be mounted, troubleshoot the issue. For more information, see Fix mount issues.

  3. After the NAS file system is mounted, you can read data from and write data to the NAS file system on the Linux ECS instance.
    You can access the file system the same way you access a local directory. The following figure shows an example.Read and write operations

Automatically mount the NFS file system

When you restart the ECS instance to which the file system is mounted, the information about all the file systems actually mounted may be lost. To prevent the loss of such information, you can edit the /etc/fstab configuration file in the Linux ECS instance to enable automatic mounting of NFS file system at startup.

Note Before you configure automatic mounting, make sure that the preceding manual mounting is successful.
  1. To mount an Extreme NAS file system, run the following command:
    To mount a Capacity or Performance NAS file system, skip this step and go to Step 2.
    vi /etc/systemd/system/sockets.target.wants/rpcbind.socket
    Open the /etc/systemd/system/sockets.target.wants/rpcbind.socket configuration file, and comment out the rpcbind parameter that is related to IPv6 in the following figure. Otherwise, the rpcbind service fails to run at startup. Comment out IPv6-related rpcbind parameters
    If you want to enable an automatic mounting in CentOS 6.x, perform the following steps:
    1. Run the chkconfg netfs on command to enable the netfs service at startup.
    2. Open the netconfig file in the /etc/ directory, and comment out inet6-related information.
  2. Open the /etc/fstab configuration file.
    • If you mount a Capacity or Performance NAS file system, run the following command
      • Use the NFSv3 protocol to mount a file system:
        file-system-id.region.nas.aliyuncs.com:/ /mnt nfs vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev,noresvport 0 0
      • Use the NFSv4 protocol to mount a file system:
        file-system-id.region.nas.aliyuncs.com:/ /mnt nfs vers=4,minorversion=0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev,noresvport 0 0
    • To mount an Extreme NAS file system, run the following command:
      file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt nfs vers=3,nolock,noacl,proto=tcp,noresvport,_netdev 0 0
    Note
    • If you want to enable an automatic mounting in CentOS 6.x, run the chkconfg netfs on command to enable the netfs service at startup.
    • If you want to enable an automatic mounting in Ubuntu, run the following command:
      [ ! -f /etc/rc.local ] && echo '#!/bin/bash' > /etc/rc.local; echo "mount -a -t nfs" >> /etc/rc.local; chmod +x /etc/rc.local
      Add ,x-systemd.automount next to the automatic mount parameter noresvport and retain "0 0" in the command.
    • If you want to enable an automatic mounting in Alibaba Cloud Linux, run the following command:
      [ ! -f /etc/rc.local ] && echo '#!/bin/bash' > /etc/rc.local; echo "mount -a -t nfs" >> /etc/rc.local; chmod +x /etc/rc.local
      Add ,x-systemd.automount,x-systemd.requires=systemd-resolved.service,x-systemd.after=systemd-resolved.service next to the automatic mount parameter noresvport and retain "0 0" in the command.
    For more information, see Mount parameters. The following table describes the parameters that are not included in the preceding table.
    Parameter Description
    _netdev Prevents the automatic mounting before the network is connected.
    0 (the first value after noresvport) Specifies whether to back up the file system by running the dump command. If a value is not zero, it indicates that a file system is backed up. For a NAS file system, the default value is 0.
    0 (the second value after noresvport) Indicates the order in which the fsck command checks file systems at startup. For a NAS file system, the default value is 0. It indicates that the fsck command is not run at startup.
  3. Run the reboot command to restart the ECS instance.
    Note Before you restart the ECS instance, make sure that the manual mounting is successful. Otherwise, the ECS instance may fail to restart. If the automatic mounting is enabled, you can view the mounted NAS file systems by running the df -h command after the ECS instance is restarted.