In my dotfiles I have the following setup to make ssh connections using my gpg key:


export GPG_TTY=$(tty)
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
gpgconf --launch gpg-agent


default-cache-ttl 600
max-cache-ttl 7200

and calling ssh-add -l shows me my correspondig gpg key.

This does obviously not translate into tramp/eshell. I tried setting the SSH_AUTH_SOCK variable inside .config/emacs/eshell/profile:

(let ((ssh_auth_sock (shell-command-to-string "gpgconf --list-dirs agent-ssh-socket")))
(setenv "SSH_AUTH_SOCK" ssh_auth_sock))

but this does not work and ssh-add -l only displays the error

Error connecting to agent: No such file or directory

What is the correct way of getting this setup working inside tramp/eshell?

  • What is the value of the SSH_AUTH_SOCK variable in the shell? Does it have a newline at the end of it? Does the socket filename on disk have a newline at the end of its name?
    – db48x
    Commented Feb 10, 2022 at 1:32
  • Hi thanks for your feedback, no there is not a newline at the end of the value: /run/user/1000/gnupg/S.gpg-agent.ssh
    – Reza
    Commented Feb 11, 2022 at 9:09
  • Are you sure? What do you get if you type ls $SSH_AUTH_SOCK into eshell?
    – db48x
    Commented Feb 11, 2022 at 15:23
  • Sorry for the late reply, this is the output: ~ $ ls $SSH_AUTH_SOCK /run/user/1000/gnupg/S.gpg-agent.ssh : No such file or directory
    – Reza
    Commented Feb 13, 2022 at 16:40
  • 1
    I am in the middle of writing one :)
    – db48x
    Commented Feb 13, 2022 at 21:53

1 Answer 1


As we determined in the comments, ssh-add cannot open the socket file because that value of the SSH_AUTH_SOCK has a newline in it. This newline is coming from your call to gpgconf. Every well–behaved program will ensure that the final line it prints ends in a newline character so that your prompt always ends up at the start of a new line. In this case that is perhaps unfortunate, because what you wanted it to print was not a well–formed line of text but a filename. Luckily removing the unwanted newline character is not hard. Incidentally, you should check the man page for gpgconf as there is an additional level of encoding here that could trip you up in the future, but which I will ignore for the purpose of answering this question.

As you found, there are a number of ways to remove this extraneous newline character after calling shell-command-to-string and before putting it into the environment. I like this one, possibly because I learned Perl at an early age:

(defun chomp (str)
  "Chomp tailing whitespace from STR."
  (replace-regexp-in-string (rx (* (any " \t\n")) eos)

You could use it like this:

(let ((ssh_auth_sock (chomp (shell-command-to-string "gpgconf --list-dirs agent-ssh-socket"))))
  (setenv "SSH_AUTH_SOCK" ssh_auth_sock))
  • 2
    For the records, the upcoming Emacs 28 has a new function string-chop-newline. Commented Feb 14, 2022 at 7:34
  • I used (car (process-lines "gpgconf" "--list-dirs" "agent-ssh-socket")) at tthe end
    – Reza
    Commented Feb 14, 2022 at 18:52
  • That’s a good choice.
    – db48x
    Commented Feb 14, 2022 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.