Mount remote files through sshfs/shfs In Debian

Source: Internet
Author: User

 

I tried the first method. Note that the remote host. Ssh/authorized_keys needs to be generated through the ssh-keygen gesture. The previous steps were successful, but the configuration was still not successful after the instance was started.

After the password is removed, an executable file is directly created in the main folder.

 

#/Bin/sh

#

# Mountsshfs Mount SSH filesystems.

#

# Version: @ (#) mountsshfs 1.00-1 15-apr-2005 andot@ujn.edu.cn

#

 

Path =/sbin:/usr/sbin:/bin:/usr/bin

Export path

 

#

# Mount SSH file systems in/etc/sshfstab.

#

Echo-n "mounting SSH filesystems ..."

Leon@192.168.19.105:/home/Leon/serviceleon

Echo "done"

 

: Exit 0

 

 

You may know that you have used SMB and NFS to share remote file systems in Linux, but do you know that you can share remote file systems through SSH. You may say that there is something strange about this, not SSH remote login, or SFTP or SCP. In short, these things are not familiar enough. But what I want to talk about here is not these things. What I want to talk about is to mount the file system of the remote host to the local through sshfs, then you can use the remote file system just like using a local file system. Are you interested in this? Next, let's take a look at how we can achieve this.

In fact, there are more than one method to mount a remote host using sshfs. Currently, I know three methods. The first one is through the lufs sshfs subsystem, the second is through the fuse-based sshfs program, and the third is directly through the shfs module supported by the kernel. After testing, the remote host file system mounted by the second method is much more stable than the first method, and the third method is the simplest and the best way to connect with the system. The first method works with autofs to mount a remote file system more conveniently. However, it may be difficult to directly open files such as songs or movies on such a mounted file system, when I listen to songs on a system mounted in this way, if I operate other files on the file system, the songs will stop playing. The system mounted through the last two methods does not have this problem. So here we only describe the last two methods. The first method can be studied by yourself if you are interested.

The Debian system is used to Debian. I have not performed experiments on other Linux systems, but other Linux systems should also work.

1. Mount the remote host file system through sshfs

First, download sshfs. This step is simple:

# Apt-Get install sshfs

In addition, it is based on the fuse module. Therefore, you must ensure that this module is already available in your kernel module. The kernel of the Debian system does not compile this module by default, however, installing this module in Debian is very easy. First, download the fuse source code package and toolkit and compile the program to be used.

# Apt-Get install fuse-source fuse-utils
Debconf-utils debhelper dpatch gettext
Html2text intltool-Debian po-debconf devscripts
Kernel-package dpkg-dev module-Assistant

Set environment variables:

# Export kvers = $ (uname-R)

# Export ksrc =/usr/src/kernel-headers-$ kvers

Download the kernel header file:

# Apt-Get install kernel-headers-$ kvers

Now, you can find the header file and the source code package of the fuse module in/usr/src, and decompress and compile the package.

# Cd/usr/src

# Tar jxvf fuse.tar. JZ

Compile the fuse module:

# Cd/usr/src/modules/Fuse

# Debian/rules binary_modules

After compilation, you can find the compiled Debian package in/usr/src/modules/(or/usr/src.

Install the compiled fuse module.

# Dpkg-I/usr/src/modules/fuse *. Deb

Now the first step is complete. Next we can test whether sshfs can be used.

First, create a mount point (here I use the remotehost name as an example, you can use any name you like ):

# Mkdir/mnt/remotehost

 

Then, try mounting the remote host file system. First, we need to have a machine that can remotely log on via ssh. Suppose this machine is 192.168.1.1. It does not need to be set, as long as the SSH service is enabled, we can mount the file system.

# Sshfs root@192.168.1.1: // MNT/remotehost-o
Sshfs_sync-O default_permissions, allow_other,
Allow_root, kernel_cache, hard_remove

Now it may prompt you to enter the password, and enter the root account password of the remote host required for SSH login. Now let's see if the mounting is complete.

# Cd/mnt/remotehost
# Ls

If the mounting is successful, you can now see the remote machine file. In addition, if you use X Window, you can browse and view files through the file manager, just as convenient as using a local hard disk.

It is also easy to uninstall the file system, just enter:

# Umount/mnt/remotehost

You can. How are you doing?

Why are you still in trouble? Yes, now you have to use the command line to mount and unmount the file, and you have to enter the password each time. I am also in trouble. If it can be automatically mounted at startup, it is convenient to automatically unmount it upon shutdown and restart. Don't worry. Here is a way to achieve this. This method is original and I hope you will criticize and correct the shortcomings.

The first thing to solve is to avoid entering the password. This is simple and can be found in section 9.5.3 of the Debian reference manual-establishing connections with fewer passwords. This will not be reproduced here. Here is an example.

First, make sure that "pubkeyauthentication yes" is set in/etc/ssh/sshd_config on 192.168.1.1, and then perform the following operations locally with the root account, the root account is used to mount the file system.

# Ssh-keygen-T RSA
# Cat ~ /. Ssh/id_rsa.pub
| SSH root@192.168.1.1 "cat->. Ssh/authorized_keys"

When the first command is executed, you may enter passphrase. leave it blank and press Enter. When executing the second command, you need to enter the password of the remote system root account. In this case, you must enter the correct password to succeed.

Now, we can try to see if the above operation works.

# SSH root@192.168.1.1

If you have logged on to the remote system without a password, congratulations! Now return to your system to continue the most important work below. Create an Automatic startup script.

Go to the/etc/init. d directory and create the following two files.

The first file is mountsshfs. The following is the file content.

#! /Bin/sh
#
# Mountsshfs Mount SSH filesystems.
#
# Version: @ (#) mountsshfs 1.00-1 15-apr-2005 andot@ujn.edu.cn
#

Path =/sbin:/usr/sbin:/bin:/usr/bin
Export path

#
# Mount SSH file systems in/etc/sshfstab.
#
Echo-n "mounting SSH filesystems ..."
If [-F/etc/sshfstab]; then
(CAT/etc/sshfstab; echo) | sed-e '/^ #/d'-E'/^ $/d' | (
While read host mount_point sshfs_options fuse_options
Do
Sshfs $ host $ mount_point-o $ sshfs_options-o $ fuse_options
Done
)
Fi
Echo "done"

: Exit 0

The second file is umountsshfs. The following is the file content.

#! /Bin/sh
#
# Umountsshfs unmount SSH filesystems.
#
# Version: @ (#) umountsshfs 1.00-1 15-apr-2005 andot@ujn.edu.cn
#

Path =/sbin:/usr/sbin:/bin:/usr/bin
Export path

#
# Unmount SSH file systems in/etc/mtab.
#
Echo-n "unmounting SSH filesystems ..."
If [-F/etc/mtab]; then
(CAT/etc/mtab; echo) | sed-e '/^ #/d'-e'/^ $/d' | (
While read host mount_point options
Do
If echo $ host | awk '{If (substr ($0, 1, 6)
= "Sshfs #") Exit 0; else Exit 1} '; then
Umount $ mount_point
Fi
Done
)
Fi
Echo "done"

: Exit 0
 
Now, create an sshfstab file in the/etc directory. This file function is similar to fstab. However, this file only contains the sshfs file system that needs to be mounted to boot, does not include information about other file systems. We still use the above example to create this file:

#/Etc/sshfstab: SSH file system information.
#
# <[User @] HOST: [dir]>
Root@192.168.1.1: // MNT/remotehost sshfs_sync
Default_permissions, allow_other, allow_root,
Kernel_cache, hard_remove

The system to be mounted in this file can contain multiple lines. The mount point must be created, and the above STARTUP script does not contain the step of creating a mount point.

Now you can use:

#/Etc/init. d/mountsshfs

To mount all sshfs remote file systems. Then use:

#/Etc/init. d/umountsshfs

You can detach all mounted sshfs remote file systems. Now we need to add it to startup. Create a soft connection pointing to/etc/init. d/umountsshfs in/etc/rc0.d and/etc/rc6.d respectively. Note that the soft connection name is related to the execution sequence.

# Cd/etc/rc0.d
# Ln-S ../init. d/umountsshfs s15umountsshfs
# Cd/etc/rc6.d
# Ln-S ../init. d/umountsshfs s15umountsshfs

In/etc/rc2.d,/etc/rc3.d,/etc/rc4.d,/etc/rc5.d, create a point to/etc/init respectively. d/mountsshfs soft connection. Note that the soft connection name is related to the execution sequence.

# Cd/etc/rc2.d
# Ln-S ../init. d/mountsshfs s85mountsshfs
# Cd/etc/rc3.d
# Ln-S ../init. d/mountsshfs s85mountsshfs
# Cd/etc/rc4.d
# Ln-S ../init. d/mountsshfs s85mountsshfs
# Cd/etc/rc5.d
# Ln-S ../init. d/mountsshfs s85mountsshfs

Okay. Now you can restart the machine and find that the remote file system is now your "local" file system!

2. Mount the remote host file system through shfs

Although the method described above can be automatically mounted at startup, The sshfs file system can be automatically uninstalled after Shutdown and restart, but it cannot be directly specified by the-t parameter in mount, therefore, it cannot be directly added to the/etc/fstab type, and DF cannot be used to list such partitions. Therefore, you may wish to mount the sshfs file system just like mounting a common partition. This is not impossible. The method described below can satisfy our desire. It is implemented through the shfs module of the kernel. It supports kernels above 2.4.10 and 2.6. I think this method is the most convenient. Let's take a look at how to use it.

First, make sure that your system has installed the shfs module. If you are using the default Debian kernel, it does not have this module, but don't worry, we can compile this shfs module like the fuse module compiled above. The method is similar. Therefore, only the commands are listed below and will not be explained in detail.

# Apt-Get install shfs-source shfs-utils debconf-utils
Debhelper dpatch gettext html2text intltool-Debian
Po-debconf kernel-Package Module-Assistant

# Export kvers = $ (uname-R)

# Export ksrc =/usr/src/kernel-headers-$ kvers

# Apt-Get install kernel-headers-$ kvers

# Cd/usr/src

# Tar jxvf shfs.tar. JZ

# Cd/usr/src/modules/shfs

# Debian/rules binary_modules

# Dpkg-I/usr/src/modules/shfs *. Deb

Now that the shfs module has been compiled and installed, you can use the mount command to mount the partition.

# Mount-T shfs root@192.168.1.1: // MNT/remotehost-O rmode = 755, preserve

Assume that the/mnt/remotehost directory has been created according to the previous method. Now, you can use/mnt/remotehost to access the file system of the remote host. Is it convenient?

All right, if you want to start and mount it, it is more convenient to shut down and restart and uninstall it. The first step is to avoid entering the SSH login password. This is the same as described in the above method. The following assumes that this step has been completed, and you only need to modify/etc/fstab. Instead of writing mountsshfs and umountsshfs scripts, you do not need to create the/etc/sshfstab file.

Root@192.168.1.1: // MNT/remotehost shfs rmode = 755, preserve 0 0

Add the above line to/etc/fstab, And you can mount the line directly, shut down and restart and unmount it, which is very simple.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.