Effective SSH Connections With Emacs

Following this tutorial will lead to have the following:

  1. Extremely easy SSH connection to servers using Emacs, to edit and manipulate files and folders.

Tramp mode

tramp-mode allow us to create SSH connection with a remote server.

In Emacs we can execute ido-find-file then write /ssh:user@host: and RET.

This will open the root directory of the server. And since dired-mode will work on a remote connection, we can create files and folders and also manipulate them.

Making it effective

Tramp configuration

1
2
3
4
5
(tramp-set-completion-function "ssh"
                               '((tramp-parse-sconfig "/etc/ssh_config")
                                 (tramp-parse-sconfig "~/.ssh/config")))

(setq tramp-default-method "ssh")

Now the file ~/.ssh/config will have following contents.

1
2
3
4
5
6
7
Host short-name-one
    HostName your-host-name-one.com
    User your-user-one

Host short-name-two
    HostName your-host-name-two.com
    User your-user-two

There we can add as many hosts as we want.

Now on next time we call ido-find-file we just write /ssh:short-name-one: and RET.

Passwords

Create the following file ~/.authinfo.

The contents of this file will be.

1
machine short-name-one port ssh login your-user-one password your-password

Now the password is on plain text, we want to avoid that.

Call epa-encrypt-file and select the ~/.authinfo file, RET on OK and set a password for decrypting the file.

You’ll end up with the following file ~/.authinfo.gpg, remove the previously created ~/.authinfo.

Emacs by default will search on this file to get passwords.

Next time we try to connect to a server Emacs will ask the password for decrypting ~/.authinfo.gpg from which Emacs can take the passwords to connect to the specified host.

Known problems

tramp-mode and Projectile does not play well together.

Add the following to your configuration to avoid that.

1
2
(defadvice projectile-project-root (around ignore-remote first activate)
  (unless (file-remote-p default-directory) ad-do-it))

which-function-mode play bad with tramp-mode as well.

I would suggest to use emacs-powerline instead.

Posted by Carlo Eduardo Rodríguez Espino