0

I use OS X and a private SSH key stored in a Yubikey to SSH to remote servers. Each time after my Mac sleeps and wakes, I need to run gpg-connect-agent updatestartuptty /bye. This answer said I can put this command in ~/.ssh/config like so

Match host * exec "gpg-connect-agent updatestartuptty /bye"

But this doesn't work because I need to set the GPG_TTY env var in the context of this exec to my current shell's tty value. I use fish shell and have this in ~/.config/fish/config.fish

set -x GPG_TTY (tty)

But when the ssh exec runs, tty returns not a tty. These are all my env vars.

cat ~/.ssh/config

...
Match host * exec "env > /tmp/t"
...

[run ssh some-host command]

cat /tmp/t

PWD=/Users/dxia
COLORFGBG=12;8
LC_TERMINAL=iTerm2
LOGNAME=dxia
GPG_TTY=not a tty
LANG=en_US.UTF-8
XPC_FLAGS=0x0
TERM=xterm-256color
TMPDIR=/var/folders/x1/f9sjnv7j43z73sdv5lsk3r8h0000gp/T/
EDITOR=/usr/bin/vim
COLORTERM=truecolor
LC_TERMINAL_VERSION=3.4.10
XPC_SERVICE_NAME=0
__CF_USER_TEXT_ENCODING=0x1F6:0x0:0x0
HOME=/Users/dxia
ITERM_PROFILE=Default
TERM_PROGRAM=iTerm.app
TERM_PROGRAM_VERSION=3.4.10
DISPLAY=/private/tmp/com.apple.launchd.6iiZkY4Tos/org.xquartz:0
USER=dxia
SHLVL=1
COMMAND_MODE=unix2003
TERM_SESSION_ID=w0t0p1:5945A63C-850F-4DCF-A605-F72860D9D72C
__CFBundleIdentifier=com.googlecode.iterm2
SHELL=/usr/local/bin/fish
ITERM_SESSION_ID=w0t0p1:5945A63C-850F-4DCF-A605-F72860D9D72C
PATH=/opt/homebrew/sbin:/opt/homebrew/bin:/Users/dxia/.pyenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/sbin:/usr/local/opt/fzf/bin:/opt/homebrew/opt/fzf/bin:/Users/dxia/.jenv/shims:/Users/dxia/.jenv/shims:/Users/dxia/.rbenv/shims:/Users/dxia/google-cloud-sdk/bin
SSH_AUTH_SOCK=/Users/dxia/.gnupg/S.gpg-agent.ssh

How can I get Match host * exec ... to inherit the tty? Or is there an altogether better way to make OS X work with Yubikey + SSH after sleep/wake?

1 Answer 1

1

According to the OpenSSH source code, commands invoked for the "Match exec" feature will have their standard input and output connected to /dev/null. Standard error, however, is left alone. If you run ssh from a TTY, the standard error of the exec command should still be the TTY.

The tty command reports on the TTY connected to its standard input. You could use command-line redirection to connect the tty command's stderr to its stdin. I'm not a fish user, but under bash for example you'd run something like this:

tty <&2
2
  • Amazing, that worked! Thank you 🙏 Match host * exec "set -x GPG_TTY (tty <&2) && gpg-connect-agent updatestartuptty /bye"
    – David Xia
    Commented Oct 26, 2021 at 13:15
  • This helped me a TON (ty) however my command needs user input as well as output. Any way to make that work? I posted my question here: serverfault.com/questions/1117873/…
    – Rino Bino
    Commented Dec 15, 2022 at 0:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .