7

I’m a new PhD student and I’m dealing with a serious issue. I tried to map a directory from Server A (/home/lab/Desktop) to Server B (/home/usr/labDesktop) because Server B has better computing power. While working, all the data in the directory on Server A disappeared.

Here’s a part of the sequence of commands I ran, which might have caused the problem:

$ sudo sshfs lab@ipaddress:/home/lab/Desktop /home/usr/labDesktop [sudo] password for usr: The authenticity of host 'ipaddress (ipaddress)' can't be established. XXX key fingerprint is XXX. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes lab@ipaddress's password: $ ls labDesktop  examples.desktop $ cd labDesktop -sh: 44: cd: can't cd to labDesktop $ ls labDesktop  examples.desktop $ sudo sshfs lab@ipaddress:/home/lab/Desktop /home/usr/labDesktop lab@ipaddress's password: read: Interrupted system call $ fusermount -u /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab $ ls labDesktop  examples.desktop $ cd labDesktop -sh: 49: cd: can't cd to labDesktop $ rm -r labDesktop rm: cannot remove 'labDesktop': Permission denied $ sudo rm -r labDesktop                           rm: cannot remove 'labDesktop': Device or resource busy $ mount | grep /home/usr/labDesktop lab@ipaddress:/home/lab/Desktop on /home/usr/labDesktop type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0) $ fusermount -u /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab $ fusermount -uz /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab $ sudo umount /home/usr/labDesktop $ mount | grep /home/usr/labDesktop $ rm -r /home/usr/labDesktop $ ls examples.desktop 

As you can see, "sudo rm -r" threw an error message, "rm: cannot remove". So I don't believe this actually deleted the data. Moreover, I tried to unmount using "fusermount -u" before running "sudo rm -r". If "fusermount -u" failed with an error message, shouldn’t "sudo rm -r" have also failed with an error message?

  1. Could this sequence of commands have caused the files on Server A to be deleted? Or is it likely something else caused the data loss?
  2. How to recover them?
2
  • 8
    Please stop putting sudo in front of random commands. It's bad practice, and it's what caused your current situation. Understand when you need elevated privileges, and only use sudo then. Commented Dec 30, 2024 at 15:34
  • 3
    sshfs is designed to be usable by a regular user without needing sudo access on a properly configured system. It works out of the box on my Ubuntu desktop as a regular user. However, if it is used as root, then only root can see the files that were mounted. Commented Dec 31, 2024 at 0:02

1 Answer 1

32
$ sudo sshfs lab@ipaddress:/home/lab/Desktop /home/usr/labDesktop [sudo] password for usr: The authenticity of host 'ipaddress (ipaddress)' can't be established. XXX key fingerprint is XXX. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes lab@ipaddress's password: 

This establishes a sshfs connection: for user root on server B, the directory /home/usr/labDesktop allows access to server A's /home/lab/Desktop directory with the access rights of user lab on server A.

Specifically, this does not mean user lab on server B would have any access to /home/usr/labDesktop.

$ cd labDesktop -sh: 44: cd: can't cd to labDesktop 

So you cannot cd to the directory as your regular user lab. This is as expected.

$ sudo sshfs lab@ipaddress:/home/lab/Desktop /home/usr/labDesktop lab@ipaddress's password: read: Interrupted system call 

Trying to mount it again fails, probably because it's already mounted.

$ fusermount -u /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab 

More specifically, there is no mount entry for /home/usr/labDesktop that the user lab would be able to unmount, because the initial mounting was done by user root, not lab. So this command does nothing.

$ rm -r labDesktop rm: cannot remove 'labDesktop': Permission denied 

Because the mount was done by user root, the user lab has no write access to labDesktop at all. This is as expected.

$ sudo rm -r labDesktop rm: cannot remove 'labDesktop': Device or resource busy 

This command says: "As user root, delete everything within directory labDesktop, including the directory itself." This is the mistake.

Since server A's directory is perfectly accessible for user root through the sshfs mount, the command will delete everything within the directory. But deleting the directory itself fails, because the directory is actually a mount point. However, that error message only appears after everything else within has already been deleted.

$ mount | grep /home/usr/labDesktop lab@ipaddress:/home/lab/Desktop on /home/usr/labDesktop type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0) 

This confirms that server A's directory is indeed still mounted and accessible to root only.)

$ fusermount -u /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab $ fusermount -uz /home/usr/labDesktop fusermount: entry for /home/usr/labDesktop not found in /etc/mtab 

User lab has no permission to unmount something root has mounted for themselves.

$ sudo umount /home/usr/labDesktop 

By using sudo, you issue the unmount request as user root, and so it will succeed.

$ rm -r /home/usr/labDesktop 

And after a successful unmount, you can delete the labDesktop directory, because it is now just a regular empty directory.

(A rmdir /home/usr/labDesktop would have been sufficient. It would be a good habit to use rmdir whenever you expect to be deleting an empty directory, as it would do nothing and produce an error message if the directory was unexpectedly non-empty.)

  1. Could this sequence of commands have caused the files on Server A to be deleted?

Definitely, once you ran the rm -r command as the same user that had done the mount on local server B, and the remote user specified in the sshfs command was presumably the owner of the directory on the remote server A, so was expected to have full access.

With sudo, you had full access to the mount, and so server B did exactly what you asked and deleted everything it could access within the mount point.

  1. Is there any way to recover them?

Command-line rm will not "move files to trash can". It is a command to actually delete the specified file(s) immediately.

Do you have good backups of that directory of server A?

If not, and you want to attempt to recover deleted files, then it's important to stop using that filesystem on server A immediately so the blocks that used to contain the deleted files won't get overwritten with newer data. The filesystem should be unmounted on server A, and then appropriate data recovery utilities could be run on it. Any recovered files should be saved to some other filesystem: it is important that the filesystem with the lost data is not modified any further until no further recovery actions are needed/worthwhile.

If the filesystem on server A is on a SSD, it is also important to ensure no periodic TRIM optimizations are executed on that entire SSD until recovery operations are concluded. If the filesystem gets TRIMmed, any deleted files will most likely be irrevocably gone.

1
  • 4
    Thank you so much. I learned so much from you. Commented Dec 29, 2024 at 14:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.