SIGN IN / UP
    opened image

    In order to make the process of entering commands that allow connecting to servers faster and more convenient, there is a useful function for auto-completing SSH commands. This function utilizes configuration files. The best and most efficient way is to set up auto-completion for network nodes specified in .ssh/config, which significantly simplifies the connection to the necessary servers. In this material, we will explore how to implement this setup on Linux and macOS operating systems.

     

    The .ssh/config file plays an important role in structuring the list of hosts and facilitating access to them when managing multiple remote servers. However, the constant manual input of server names each time is inefficient, so this process can be simplified by setting up auto-completion.

     

    .ssh/config - what is it?

     

    The .ssh/config text document is a file that contains settings for SSH connections. With .ssh/config, you can utilize keys, specify standard ports, set aliases for servers, and define other connection parameters. Each host in this configuration can have its unique alias, which simplifies the SSH connection process.

     

    The .ssh/config file can be easily opened with your preferred text editor. This can be, for example, gedit, vim, or nano. If the .ssh/config document does not exist, it will be automatically created when you use the command.

     

    How to use the file with the mentioned graphical editors?

     

    Editor nano:

    nano ~/.ssh/config

    Example vim:

    vim ~/.ssh/config

    Example gedit (for graphical interface lovers):

    gedit ~/.ssh/config &

    If there is no graphical editor, it can be installed using the following command:

    sudo snap install gedit


     

    Sample text file .ssh/config:

    nano ~/.ssh/config

    Host example-server

    HostName 192.168.1.100

    User user1

    Port 2222


     

    Next, to avoid further manual entry of the full command for connection:

    ssh [email protected] -p 2222

    You can use the following option:

    ssh example-server


     

    Why is auto-completion necessary?

     

    With several practical advantages that significantly enhance usability, the SSH auto-completion feature on Ubuntu reduces the time spent entering commands and improves interaction with remote servers. Here are the main arguments in favor of auto-completion:

     

    • Simplifying work. Auto-completion allows you to write the necessary commands faster, which is especially important when working with long paths or complex commands. You no longer have to constantly type them manually.

    Consider the situation where you need to enter the address of a specific host. To avoid typing the following:

    ssh [email protected]

    You only need to type:

    ssh use[TAB]

    After that, the name will be automatically completed by the system to its full value.

     

    • Reducing errors. With auto-completion, the likelihood of typos and incorrect input of complex and long commands decreases, leading to less frequent failures. Automatic input offers correct options by referring to previously configured hosts in .ssh/config.

     

    • Optimizing SSH work. Auto-completion recognizes entries with connection parameters to different hosts, provided your .ssh/config file is already set up, and then offers these entries when typing the ssh command. This auto-suggestion system allows for quick searching of already configured servers and connecting to them.

     

    How this can be recorded in the .ssh/config file:

    Host myserver

    HostName myserver.com

    User myusername

    Port 2222

     

    As a result, based on your configurations, after entering ssh myser[TAB], the system will recommend you auto-completion options.

     

    • Ease of working with multiple servers. When interacting with several servers at once, the auto-completion feature significantly simplifies the management process. There is no need to remember every combination for domains or IP addresses — you can rely on the .ssh/config document configuration, and the system will suggest possible options on its own.

     

    • Time-saving. When using SSH auto-completion, users can save time on server connections by not entering the entire command from scratch, but simply duplicating already existing commands or paths. This feature is extremely useful for those who frequently access multiple remote servers, such as developers and system administrators.

     

    • Increasing productivity: Auto-completion allows you to focus on more important tasks, minimizing routine actions when working with scripts and in the terminal. Additionally, auto-completion can also be applied in other cases; if set up correctly, it can significantly reduce the amount of work performed and boost productivity.

     

    These advantages make auto-completion an indispensable tool for both experts and beginners working with SSH on Ubuntu.

     

    Configuring bash-completion for SSH

     

    First step: setup

    To set up SSH auto-completion, you will need the bash-completion package installed. This package is usually pre-installed in modern Linux distributions, but if it is not, it can be installed.

     

    Installing bash-completion on Ubuntu and Debian platforms:

    sudo apt update

    sudo apt install bash-completion


     

    Installing bash-completion on CentOS and Fedora platforms:


    sudo yum install bash-completion

    For Arch Linux: sudo pacman -S bash-completion

    Activating:

    After installation, you should check the activation of auto-completion by adding the following line to the .bashrc or .bash_profile file:

    nano ~/.bashrc

    if [ -f /etc/bash_completion ]; then

        . /etc/bash_completion

    fi

    Once you check the activation of the completion, restart the terminal or use the following command:

    source ~/.bashrc

     

    Second step: Setting up SSH auto-completion

     

    In the second step of configuration, you need to add auto-completion to the hosts in the .ssh/config document.

    To perform this procedure, we use the completion script:

    In the /etc/bash_completion.d/ folder, create or edit an existing auto-completion file. The following script will help to automatically load the required hosts from the .ssh/config document.

    Create the file /etc/bash_completion.d/ssh:

    sudo nano /etc/bash_completion.d/ssh


    Insert the following code into the opened file:

    _ssh_hosts() {

        COMPREPLY=($(compgen -W "$(awk '/^Host / {print $2}' ~/.ssh/config)" -- "${COMP_WORDS[COMP_CWORD]}"))

    }

    complete -F _ssh_hosts ssh


     

    This script extracts host names from the .ssh/config file and passes them for auto-completion.

    Save the file and close the editor.

    Activate the changes:

    source /etc/bash_completion.d/ssh

    Now, when you enter the ssh command and start typing the host name, the system will suggest auto-completion options.

     

    Checking the functionality

    To check the functionality of auto-completion, simply start typing the SSH command and press the Tab key. If everything is set up correctly, the system will offer a list of available hosts from your .ssh/config file.

    Example:

    ssh exa<Tab>

    Result:

    ssh example-server


     

    Support for auto-completion for other shells

    Auto-completion is possible not only for Bash. If you are using other shells, such as Zsh, auto-completion can also be configured.

     

    Configuring auto-completion for Zsh

    For Zsh users, the setup process is slightly different. You need to add the following line to the .zshrc file to ensure the activation of auto-completion:

    autoload -U compinit && compinit 

    The next step is to create the auto-completion mode:

    _ssh_hosts_zsh() {

        local hosts

        hosts=(${(f)"$(awk '/^Host / {print $2}' ~/ssh/config)"})

        _describe -t hosts 'SSH Hosts' hosts

    }

    compdef _ssh_hosts_zsh ssh

    After performing these actions, save and restart your terminal:

    source ~/.zshrc

    Successfully completing the entire installation process, auto-completion in Zsh will be operational.

     

    Interacting with multiple configuration files

     

    In some situations, it can be useful to distribute SSH settings across multiple files. For example, this may be related to different groups of servers. In this case, the auto-completion setup should take into account all configuration files.

    To do this, you can add multiple configuration sources:

    _ssh_hosts() {

        COMPREPLY=($(compgen -W "$(awk '/^Host / {print $2}' ~/.ssh/config ~/.ssh/config.d/*)" -- "${COMP_WORDS[COMP_CWORD]}"))

    }

    complete -F _ssh_hosts ssh

    Now auto-completion will work for all hosts specified in all files in the ~/.ssh/config.d/ directory.

     

     

    Conclusion

    Configuring SSH command auto-completion for nodes specified in the .ssh/config document is a key step that enhances work efficiency and reduces the likelihood of errors when interacting with remote servers. This mode will simplify your connections and allow you to focus on working with key tasks, regardless of whether you are using Zsh or Bash.

    It is important to remember to keep the .ssh/config text file up to date and to constantly refresh it. This will provide you with the ability to manage large server infrastructures smoothly and easily, without needing to memorize complex connection commands.