Ubuntu

(Ubuntu 19.04) NFS 共享不會在啟動時自動掛載:‘mount.nfs: Network is unreachable’

  • August 13, 2020

我不知道如何在啟動時掛載 NFS 共享。我嘗試了幾種不同的方法(如下所列),但沒有任何效果。在這兩種情況下,他們都需要network-online.target,但他們似乎並沒有在開始之前等待網路上線。是的,我確實可以與遠端機器通信,mount -a登錄後我可以執行,共享將被掛載。我完全不知所措。

嘗試#1:我將以下內容放入/etc/fstab

10.0.10.10:/volume1/Media /mnt/media nfs x-systemd.automount,noauto,nofail,x-systemd.requires=network-online.target,suid,dev,exec,rw,sync,nouser 0 0

重啟後的結果:

root@ubuntu-svr:~# journalctl -b -u mnt-media.mount
-- Logs begin at Mon 2019-08-12 00:00:56 UTC, end at Fri 2019-09-20 22:05:16 UTC. --
Sep 20 21:36:29 ubuntu-svr systemd[1]: Mounting Mounts the '/mnt/media' directory...
Sep 20 21:36:29 ubuntu-svr mount[1528]: mount.nfs: Network is unreachable
Sep 20 21:36:29 ubuntu-svr systemd[1]: mnt-media.mount: Mount process exited, code=exited, status=32/n/a
Sep 20 21:36:29 ubuntu-svr systemd[1]: mnt-media.mount: Failed with result 'exit-code'.
Sep 20 21:36:29 ubuntu-svr systemd[1]: Failed to mount Mounts the '/mnt/media' directory.

嘗試#2:我註釋掉了 fstab 條目並創建了一個 systemd mnt-media.mount 文件並啟用了 systemd 單元以在啟動時啟動。這是mnt-media.mount文件的內容:

root@ubuntu-svr:~# cat /etc/systemd/system/mnt-media.mount
[Unit]
Description=Mounts the '/mnt/media' directory
Wants=network-online.target
Requires=network-online.target
#After=network-online.target

[Mount]
Where=/mnt/media
What=10.0.10.10:/volume1/Media
Options=auto,x-systemd.automount,nofail,suid,dev,exec,rw,sync,nouser
Type=nfs

[Install]
WantedBy=multi-user.target

我通過systemctl enable mnt-media.mount. 我重新啟動,並檢查了它的狀態:

root@ubuntu-svr:~# systemctl status mnt-media.mount
● mnt-media.mount - Mounts the '/mnt/media' directory
  Loaded: loaded (/etc/systemd/system/mnt-media.mount; enabled; vendor preset: enabled)
  Active: failed (Result: exit-code) since Fri 2019-09-20 22:28:51 UTC; 1min 12s ago
   Where: /mnt/media
    What: 10.0.10.10:/volume1/Media

Sep 20 22:28:51 ubuntu-svr systemd[1]: Mounting Mounts the '/mnt/media' directory...
Sep 20 22:28:51 ubuntu-svr mount[1206]: mount.nfs: Network is unreachable
Sep 20 22:28:51 ubuntu-svr systemd[1]: mnt-media.mount: Mount process exited, code=exited, status=32/n/a
Sep 20 22:28:51 ubuntu-svr systemd[1]: mnt-media.mount: Failed with result 'exit-code'.
Sep 20 22:28:51 ubuntu-svr systemd[1]: Failed to mount Mounts the '/mnt/media' directory.

解決了:

原來我需要啟用該systemd-networkd-wait-online.service服務。現在我嘗試的兩種嘗試(/etc/fstab 和 mnt-media.mount)都按預期工作。

對於其他為此苦苦掙扎的人,取決於您是否使用systemd-networkdor NetworkManager,您需要確保您的系統上啟用了systemd-networkd-wait-online.serviceor 。NetworkManager-wait-online.service否則,即使您的掛載明確需要,您的系統也不會等待網路啟動。

我通過這個連結找到了這個答案:NFS/Troubleshooting - Network is unreachable

引用自:https://unix.stackexchange.com/questions/542925