Skip to content

Instantly share code, notes, and snippets.

@Wes974
Forked from ryuheechul/1.SSH.md
Created October 12, 2022 18:14
Show Gist options
  • Save Wes974/571be5ffc0ee1ba264257cfb01ef9ace to your computer and use it in GitHub Desktop.
Save Wes974/571be5ffc0ee1ba264257cfb01ef9ace to your computer and use it in GitHub Desktop.
Networking (ssh, mosh, etc)

SSH

https://smallstep.com/blog/ssh-agent-explained/ https://gist.github.com/ryuheechul/494f4e6f08eaca34ef00ab8b238eca2a#ssh-server

Tunneling

Jump Host

Examples of using public keys as IdentityFile

Connections issuses

https://gist.github.com/ryuheechul/494f4e6f08eaca34ef00ab8b238eca2a#troubleshoot-send-disconnect-broken-pipe

Long lasting ssh-agent

Enter Keychain, https://www.funtoo.org/OpenSSH_Key_Management,_Part_2

https://esc.sh/blog/ssh-agent-windows10-wsl2/ or
# maybe in your .bashrc or .zshrc
eval $(keychain [--quiet] --eval --agents ssh [id_rsa])

Accessing Clipboard

assuming pbcopy & pbpaste is what is used for clipboard on both systems

From Remote Host

ssh remote-host pbpaste | pbcopy

To Remote Host

pbpaste | ssh remote-host pbcopy

not strictly related to above, but I enjoyed reading this article, https://andrewbrookins.com/technology/synchronizing-the-ios-clipboard-with-a-remote-server-using-command-line-tools/ and hopefully iPad gets to have macOS one day, lol.

Sharing Clipboard

Even beyond simply accessing it, sharing clipboard is also possible via something called X11 forwaring.

A nice explanation is at https://askubuntu.com/a/305681.

# client side at ./ssh/config
  ForwardX11 yes # make sure this is on
# server side at /etc/ssh/sshd_config
  X11Forwarding yes # make sure this is on - this might be blocked by default on macOS host
  
  # When an error occur like this
  # ```
  # Remote: No xauth program; cannot forward
  # X11 forwarding request failed on channel 0
  # ```
  # and the host is macOS and xquartz is installed, this should help with error above
  XAuthLocation /opt/X11/bin/xauth # or the path what `which xauth` returns

On macOS, there is no X11 server by default but it can be installed

I would install it by one of these ways:

  • nix-env -iA nixpkgs.xquartz
  • brew install --cask xquartz

Another important thing is that we need to know how to use clipboard on linux and that information can be found at https://ostechnix.com/access-clipboard-contents-using-xclip-and-xsel-in-linux/.

I prefer to use xsel over xclip.

And having xsel is enough to make these two plugins work:

I also would make an alias to help my muscle memory used to macOS.

# bring `pbcopy` and `pbpaste` to linux
alias  pbcopy='xsel -i -b'
alias pbpaste='xsel -o -b'

Now things copied inside ssh and vice versa!

Trouble Shooting

XQuartz Installation

Using after initial installation may require reboot (let me know if not)

xeyes command to see if it's working (and it should run the app if it wasn't)

A timing could be little tricky that when it actually start working.

xsel on Host

Check to see if xsel (or xclip if you prefer) is installed on the host system.

$DISPLAY

But bascially, when you run printenv DISPLAY, you should see something like below.

# on the client side - in this case, macOS
$ printenv DISPLAY
/private/tmp/com.apple.launchd.xYzAbCde/org.xquartz:0

# on the ssh host side - in this case, linux
$ printenv DISPLAY
localhost:10.0

Wrong Selection

The selection should be CLIPBOARD not PRIMARY nor SECONDARY.

These should work

  • xclip [-i] [-o] -s clipboard
  • xclip [-i] [-o] -s c
  • xsel [-i] [-o] --clipboard
  • xsel [-i] [-o] -b

but not these:

  • xclip [-i] [-o]
  • xsel [-i] [-o]

pbcopy, pbpaste on macOS host

pbcopy and pbpaste seems not work out of the box to forwarded X11 on the host side. (when it is on the client side it works)

xsel [-i] [-o] -b still works though

Even after running XQaurtz on the host side and make sure it syncs clipboards. My speculation is that it's because even if it syncs, it syncs with the host X11 not the client's which makes sense to me.

I'm looking for an way to mitigate this as this could be annoying that plugins that I use automatically select pbcopy|paste when they exist.

I inveted a workaround that uses these wrapper scripts:

They worked as I hoped for my use cases!

And now these plugins works on macOS host.

Just make sure those scripts are exported to $PATH.

But this might break the sync between macOS clipboard and xsel -b. I'm looking into it. I'm fine with the workaround for now.

Mosh

Remote terminal application that allows roaming, supports intermittent connectivity, and ... The project can be found here, https://mosh.org/. In addition to SSH, Mosh can be a great fit with mobile applications like Blink Shell since the condition of connection will be constanly changing (assuming you are on the move with your mobile devices).

It use SSH to initialize (including authenticating) connection so not much additional overhead should be necessary. It doesn't even need you to run addtional daemon process (sonn you will know about what it means after your first connection).

Install

nix-env -i mosh

Usage

instead of ssh ..., mosh [--server=/your/path/for/mosh-server] ...

True Color Issue

blinksh/blink#609 (comment) can be dealt with brew install --head mosh

Connection Issue

Impressively Mosh maintains connection even after a client machine's rebooting. However if you rely on VPN to connect a server and if your machine like iPad can't connect (or seems to not respond for a while), it's probably not Mosh's issue. Double check the network connectivity and configure it properly and it should be good to go.

Locale issue

mobile-shell/mosh#793 (comment)

Maybe just run with LC_ALL=en_US.UTF-8 mosh ...? I'm not sure what's the best way to do it for now.

with Tmux

I would like to have a dedicated session for Mosh and setting Mosh Command for [bash -c -l '] tmux a -t mosh-session || tmux new -s mosh-session should work butit somehow does not work with Blink.

So I just place a helper script like this in the host and set command in Blink like below.

/path/to/tmux-attach-or-new.sh mosh-session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment