ssh.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. Truly convenient SSH
  2. =========================================
  3. .. only:: man
  4. Overview
  5. ----------------
  6. * Automatic :ref:`shell_integration` on remote hosts
  7. * Easily :ref:`clone local shell/editor config <real_world_ssh_kitten_config>` on remote hosts
  8. * Automatic :opt:`re-use of existing connections <kitten-ssh.share_connections>` to avoid connection setup latency
  9. * Make the kitten binary available in the remote host :opt:`on demand <kitten-ssh.remote_kitty>`
  10. * Easily :opt:`change terminal colors <kitten-ssh.color_scheme>` when connecting to remote hosts
  11. * Automatically :opt:`forward the kitty remote control socket <kitten-ssh.forward_remote_control>` to configured hosts
  12. .. versionadded:: 0.25.0
  13. Automatic shell integration, file transfer and reuse of connections
  14. .. versionadded:: 0.30.0
  15. Automatic forwarding of remote control sockets
  16. The ssh kitten allows you to login easily to remote hosts, and automatically
  17. setup the environment there to be as comfortable as your local shell. You can
  18. specify environment variables to set on the remote host and files to copy there,
  19. making your remote experience just like your local shell. Additionally, it
  20. automatically sets up :ref:`shell_integration` on the remote host and copies the
  21. kitty terminfo database there.
  22. The ssh kitten is a thin wrapper around the traditional `ssh <https://man.openbsd.org/ssh>`__
  23. command line program and supports all the same options and arguments and configuration.
  24. In interactive usage scenarios it is a drop in replacement for :program:`ssh`.
  25. To try it out, simply run:
  26. .. code-block:: sh
  27. kitten ssh some-hostname-to-connect-to
  28. You should end up at a shell prompt on the remote host, with shell integration
  29. enabled. If you like it you can add an alias to it in your shell's rc files:
  30. .. code-block:: sh
  31. alias s="kitten ssh"
  32. So now you can just type ``s hostname`` to connect.
  33. If you define a mapping in :file:`kitty.conf` such as::
  34. map f1 new_window_with_cwd
  35. Then, pressing :kbd:`F1` will open a new window automatically logged into the
  36. same host using the ssh kitten, at the same directory.
  37. The ssh kitten can be configured using the :file:`~/.config/kitty/ssh.conf` file
  38. where you can specify environment variables to set on the remote host and files
  39. to copy from the local to the remote host. Let's see a quick example:
  40. .. code-block:: conf
  41. # Copy the files and directories needed to setup some common tools
  42. copy .zshrc .vimrc .vim
  43. # Setup some environment variables
  44. env SOME_VAR=x
  45. # COPIED_VAR will have the same value on the remote host as it does locally
  46. env COPIED_VAR=_kitty_copy_env_var_
  47. # Create some per hostname settings
  48. hostname someserver-*
  49. copy env-files
  50. env SOMETHING=else
  51. hostname someuser@somehost
  52. copy --dest=foo/bar some-file
  53. copy --glob some/files.*
  54. See below for full details on the syntax and options of :file:`ssh.conf`.
  55. Additionally, you can pass config options on the command line:
  56. .. code-block:: sh
  57. kitten ssh --kitten interpreter=python servername
  58. The :code:`--kitten` argument can be specified multiple times, with directives
  59. from :file:`ssh.conf`. These override the final options used for the matched host, as if they
  60. had been appended to the end of the matching section for that host in
  61. :file:`ssh.conf`. They apply only to the host being SSHed to by this invocation,
  62. so any :opt:`hostname <kitten-ssh.hostname>` directives are ignored.
  63. .. warning::
  64. Due to limitations in the design of SSH, any typing you do before the
  65. shell prompt appears may be lost. So ideally don't start typing till you see
  66. the shell prompt. 😇
  67. .. _real_world_ssh_kitten_config:
  68. A real world example
  69. ----------------------
  70. Suppose you often SSH into a production server, and you would like to setup
  71. your shell and editor there using your custom settings. However, other people
  72. could SSH in as well and you don't want to clobber their settings. Here is how
  73. this could be achieved using the ssh kitten with :program:`zsh` and
  74. :program:`vim` as the shell and editor, respectively:
  75. .. code-block:: conf
  76. # Have these settings apply to servers in my organization
  77. hostname myserver-*
  78. # Setup zsh to read its files from my-conf/zsh
  79. env ZDOTDIR=$HOME/my-conf/zsh
  80. copy --dest my-conf/zsh/.zshrc .zshrc
  81. copy --dest my-conf/zsh/.zshenv .zshenv
  82. # If you use other zsh init files add them in a similar manner
  83. # Setup vim to read its config from my-conf/vim
  84. env VIMINIT=$HOME/my-conf/vim/vimrc
  85. env VIMRUNTIME=$HOME/my-conf/vim
  86. copy --dest my-conf/vim .vim
  87. copy --dest my-conf/vim/vimrc .vimrc
  88. How it works
  89. ----------------
  90. The ssh kitten works by having SSH transmit and execute a POSIX sh (or
  91. :opt:`optionally <kitten-ssh.interpreter>` Python) bootstrap script on the
  92. remote host using an :opt:`interpreter <kitten-ssh.interpreter>`. This script
  93. reads setup data over the TTY device, which kitty sends as a Base64 encoded
  94. compressed tarball. The script extracts it and places the :opt:`files <kitten-ssh.copy>`
  95. and sets the :opt:`environment variables <kitten-ssh.env>` before finally
  96. launching the :opt:`login shell <kitten-ssh.login_shell>` with :opt:`shell
  97. integration <kitten-ssh.shell_integration>` enabled. The data is requested by
  98. the kitten over the TTY with a random one time password. kitty reads the request
  99. and if the password matches a password pre-stored in shared memory on the
  100. localhost by the kitten, the transmission is allowed. If your local
  101. `OpenSSH <https://www.openssh.com/>`__ version is >= 8.4 then the data is
  102. transmitted instantly without any roundtrip delay.
  103. .. note::
  104. When connecting to BSD hosts, it is possible the bootstrap script will fail
  105. or run slowly, because the default shells are crippled in various ways.
  106. Your best bet is to install Python on the remote, make sure the login shell
  107. is something POSIX sh compliant, and use :code:`python` as the
  108. :opt:`interpreter <kitten-ssh.interpreter>` in :file:`ssh.conf`.
  109. .. note::
  110. This may or may not work when using terminal multiplexers, depending on
  111. whether they passthrough the escape codes and if the values of the
  112. environment variables :envvar:`KITTY_PID` and :envvar:`KITTY_WINDOW_ID` are
  113. correct in the current session (they can be wrong when connecting to a tmux
  114. session running in a different window) and the ssh kitten is run in the
  115. currently active multiplexer window.
  116. .. include:: /generated/conf-kitten-ssh.rst
  117. .. _ssh_copy_command:
  118. The copy command
  119. --------------------
  120. .. include:: /generated/ssh-copy.rst
  121. .. _manual_terminfo_copy:
  122. Copying terminfo files manually
  123. -------------------------------------
  124. Sometimes, the ssh kitten can fail, or maybe you dont like to use it. In such
  125. cases, the terminfo files can be copied over manually to a server with the
  126. following one liner::
  127. infocmp -a xterm-kitty | ssh myserver tic -x -o \~/.terminfo /dev/stdin
  128. If you are behind a proxy (like Balabit) that prevents this, or you are SSHing
  129. into macOS where the :program:`tic` does not support reading from :file:`STDIN`,
  130. you must redirect the first command to a file, copy that to the server and run :program:`tic`
  131. manually. If you connect to a server, embedded, or Android system that doesn't
  132. have :program:`tic`, copy over your local file terminfo to the other system as
  133. :file:`~/.terminfo/x/xterm-kitty`.
  134. If the server is running a relatively modern Linux distribution and you have
  135. root access to it, you could simply install the ``kitty-terminfo`` package on
  136. the server to make the terminfo files available.
  137. Really, the correct solution for this is to convince the OpenSSH maintainers to
  138. have :program:`ssh` do this automatically, if possible, when connecting to a
  139. server, so that all terminals work transparently.
  140. If the server is running FreeBSD, or another system that relies on termcap
  141. rather than terminfo, you will need to convert the terminfo file on your local
  142. machine by running (on local machine with |kitty|)::
  143. infocmp -CrT0 xterm-kitty
  144. The output of this command is the termcap description, which should be appended
  145. to :file:`/usr/share/misc/termcap` on the remote server. Then run the following
  146. command to apply your change (on the server)::
  147. cap_mkdb /usr/share/misc/termcap