1. Overview

Auto-completion or Bash completion is a very useful feature in Linux. It helps us to quickly complete commands, file names, and other data to run in the terminal. This is implemented by a set of scripts in the /etc/bash_completion.d folder.

This all works well in a local machine. However, if we need to enable auto-completion for the files on a remote machine, we need to further configure it. In this tutorial, we’ll see how to enable auto-completion for files and folders on a remote machine while using the scp command.

2. The Missing Configuration

When using the scp command, the auto-completion feature uses the /usr/share/bash-completion/completions/scp script to complete the command line arguments.

If we take a look at the script, we can see an ssh command is used to retrieve the folder list when auto-completion is invoked:

ssh -o 'Batchmode yes' $userhost ls -aF1dL "$path*"

We can see this line inside the function _scp_remote_files. This line tries to execute the ls command on the remote machine using the ssh command.

But when executing an ssh command, we need to enter the password to create the connection. Otherwise, the ls command will fail.

Another way is to set up a passwordless login to the remote machine. With such a login, the ssh connection is created without us entering a password. Additionally, the ls command is executed, and the folder list is retrieved from the remote machine.

3. Enabling Passwordless Login

Now we know the missing configuration is to set up a passwordless login. Let’s look at how can we set up a passwordless login to the remote machine.

First, we need to generate an SSH key pair:

$ ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
$ ssh-copy-id -i ~/.ssh/id_rsa [email protected]

The first command generates the key pair. The second one installs the public key in the remote machine with IP 192.168.1.11.

While installing the public key, we’ll have to enter the password to the remote machine when prompted. Once we run these commands, the passwordless login to the remote machine 192.168.1.11 is configured.

Now, let’s run the scp command with tab completion:

$ scp 192.168.1.11:/home/bluelake/[Tab][Tab]
/home/bluelake/Desktop/                  /home/bluelake/Music/                    /home/bluelake/Public/                   /home/bluelake/test/                     
/home/bluelake/Documents/                /home/bluelake/Videos/                   /home/bluelake/Pictures/                 /home/bluelake/Templates/                
/home/bluelake/Downloads/                

We can see that by pressing the tab key twice files and folders from the remote machine are listed.

With that, we’ve got the basic configuration to list files from a remote machine upon tab completion. However, communication with a remote machine always incurs some delays. And it takes some time to list the files after we press the tab key. It might not be noticeable when we are working with machines within a LAN. But over the internet, it’s clearly visible. So let’s look at how we can speed this up.

3.1. Speed up File Listing

The main cause of the delay is the time taken to establish a secure connection for each tab completion. Only after a successful connection can we run the ls command and retrieve the file list. If we can reuse an already-established connection to get the file listing, it will become more responsive. Luckily, SSH provides a feature for creating a master connection and sharing it.

The idea behind the master connection is to establish a main connection to the remote host. Then, the main connection is shared for all the subsequent communications to that remote host. This makes the data exchange to the remote machine faster.

Let’s take a look at how we can set up a master connection. Firstly, we need to create a config file inside the ~/.ssh folder.

Here’s a sample content for the config file:

$ cat ~/.ssh/config 
Host *
	ControlMaster auto
	ControlPath ~/.ssh/controlmasters/%r@%h:%p
	ControlPersist yes

Let’s look at the parameters in each line:

  • Host * option sets the connection parameters for any host we’re trying to connect
  • ControlMaster option enables sharing a connection using a Unix domain socket
  • ControlPath specifies the folder path where the control socket file is saved. %r, %h, and %p are placeholders for the remote username, hostname, and port number
  • ControlPersist parameter instructs keeping the main connection open even though the initial connection has been closed

Secondly, we need to create the controlmasters folder:

$ mkdir -p ~/.ssh/controlmasters

With that, the setup for the master connection is ready. From this point, when establishing an SSH connection, it  checks for the socket file in the ~/.ssh/controlmasters folder. If a socket file for that user, host, and port exists, that will be used to establish the connection. Otherwise, it creates a new one.

Finally, when we invoke tab completion for the scp command, it lists the files faster than before.

4. Using sshfs

Another way to list files from a remote machine on tab completion is to use sshfs.

sshfs is a special protocol that helps us mount a remote file system on our machine. After mounting, we can access the remote file system through the mount point as if it was in the local device. Then, we can invoke tab completion to list files on the remote machine while running any command.

Even though this is very handy, it doesn’t come installed by default. For Debian-based distributions, we use the apt command to install a package.

Here are the commands to install and configure sshfs:

$ sudo apt install sshfs
$ sudo mkdir /mnt/pacific
$ sudo sshfs -o allow_other,default_permissions bluelake@pacific:/home/bluelake /mnt/pacific

With these above commands, we’ve installed the package and created a directory to mount the remote file system.

The first command installs the package on our machine. The second one creates the mount point for the remote file system. The last command mounts the remote file system to the local machine.

Now with any command, tab completion will list files from the remote machine.

5. Conclusion

In this article, we’ve taken a look at enabling tab completion to list files on a remote machine. We’ve also explored how to reduce the delays in the file listing and seen how we can use sshfs to achieve similar functionality.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.