How do you detach a remote screen session in byobu (tmux)? - tmux

I am currently in a byobu-tmux session and am ssh'ed into a screen session. How do I detach the remote screen session without detaching byobu-tmux session? Some things to note, I can't run byobu-config because I'm on osx and don't have python-newt (w/ snack) installed. And, I've run byobu-ctrl-a in Emacs mode, but that doesn't seem to allow me to ctrl-a d out of the remote screen session.

It is easy when you use tmux commands:
byobu-tmux detach
byobu-tmux
or even just:
byobu detach
byobu

You should be able to double-escape with Ctrl-a.
To send a detach message to the inner byobu-screen session, press:
Ctrl-a Ctrl-a d
Full disclosure: I am the author and maintainer of Byobu.

Try letting go of ctrl after the first a, so the sequence is ctrl-a, a, d. Man screen:
C-a C-a (other) Toggle to the window displayed
previously. Note that this
binding defaults to the command character typed twice, unless
overridden. For instance, if you use the option "-e]x", this
command becomes "]]".
C-a a (meta) Send the command character (C-a) to
window. See escape com‐
mand.
Or if you're using tmux instead of screen for Byobu, try just ctrl-a d. Byobu's default prefix key is ctrl-b, so if you're using that default, doubling up the ctrl-a keystroke would not be necessary.
Source: https://askubuntu.com/a/309215/106100

I was able to do this by listing all clients inside the current client:
$ byobu list-clients
/dev/pts/67: 1 [80x24 xterm] (utf8)
/dev/pts/70: 1 [157x48 xterm-256color] (utf8)
Then detach the remote client (determined based on screen size):
$ byobu detach -t /dev/pts/67
Now I can use my full window size

perhaps not relevant to tmux but for byobu, I found the following command to be very helpful: detach all sessions except the current one:
/usr/lib/byobu/include/tmux-detach-all-but-current-client
hope this helps

You need to switch the prefix of your local session if it conflicts with the remote session. For example, if both are using CTRL+A then you'd be in trouble. You can either send a raw command (there's a sequence for that, but I can't remember it), or go the easy route and remap your local session to Ctrl+B, then you can input Ctrl+A that will get routed to the remote session. Also not related to tmux but the ssh connection itself you can input "~." and it'll disconnect from the ssh session. Hope it helps.

I've been an avid user of byobu on Linux for the best part of a decade. After struggling with configuring the brew install of byobu on OSX for most of these years, I finally managed to setup my byobu configs in a round about way. First I executed this:
echo '/usr/local/lib/python2.7/site-packages' | sudo tee /Library/Python/2.7/site-packages/homebrew.pth
Then I ran the byobu config file:
byobu-config
Finally I cleaned up
sudo rm /Library/Python/2.7/site-packages/homebrew.pth
Python crashed along the way with a few pop-ups, however, byobu now works for me as it should. I do need to repeat these steps when I want to change config again though... still looking for a cleaner solution.

Related

Is it possible to quit tmux, while "keeping" the window in the terminal

Use case
I run command over a bastion in SSH and since the connection can be dropped we are using tmux or screen.
For short commands I'm using send-keys, let's say I'm using a command like kubectl get pods. I'd like to keep the output of this command but terminate the tmux session.
Using remain-on-exit is not what I'm looking for as it keeps a dead window.
I've seen those question already
https://unix.stackexchange.com/questions/17116/prevent-pane-window-from-closing-when-command-completes-tmux
TMUX: how to make new window stay when start shell-command quits?
I'd like to keep the output of this command but terminate the tmux session.
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
I'm not sure it's exactly what you are looking for, but you could use the capture-pane command to save the contents of a pane (subject to what is still in the pane's history) to a file.

how to add commands in .ssh/authorized_keys

I read on man sshd one can add post-login processing when a user logs in using a particular key:
environment="FOO=BAR" ssh-rsa AAA... keytag
But when I try to ssh into the system, the target host does not register the line and instead asks for a password. What is the right way of adding this? I would like to do something like
command="echo|mail -s ${USER},${HOSTNAME} a.monitored.email#example.com" ssh-rsa AAA... keytag
I am using Suse SLE 11 SP2.
Thanks
Dinesh
First, according to the documentation command = "command":
That specifies the command is executed Whenever This key is used for authentication. The command supplied by the user (if any) is ignored. The command is run on a pty if the client requests a pty; Otherwise it is run without a tty. If an 8-bit clean channel is required, one must not request a pty or specify no-pty Should. A quote May be included in the command by quoting it with a backslash. This option might be useful to restrict Un certain public keys to perform just a specific operation. An example might be a key That Permits remote backups but nothing else. Note That May specify the client TCP and / or X11 forwarding Explicitly UNLESS they 'are prohibited. The command originally supplied by the client is available in the SSH_ORIGINAL_COMMAND environment variable. Note That This option Applies to shell, command or subsystem execution. Also note This command That May be superseded by Either a sshd_config (5) ForceCommand directive or a command embedded in a certificate.
Using this option, it is possible to enforce execution of a given command when this key is used for authentication and no other.This is not what you're looking for.
To run a command after login you can add in the file ~/bashrc something like this:
if [[ -n $SSH_CONNECTION ]] ; then
echo|mail -s ${USER},${HOSTNAME} a.monitored.email#example.com"
fi
Second, you need to verify the permissions of the authorized_keys file and the folder / parent folders in which it is located.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
For more information see: https://www.complang.tuwien.ac.at/doc/openssh-server/faq.html#3.14

How to copy a tmux session from one machine to another?

Is it possible to export/import tmux sessions?
At the university we have identical machines (same binaries and file structure) and sometimes I want to move a tmux session from one machine to another. Is there a way to do this?
As pointed on the comments, it is very unlikely that you would be able to restore all the state of tmux session.
If your purpose is to access the tmux session from a different machine (hostB), while the tmux session is still running on the original machine (hostA), you could simple access it through ssh:
hostB $ ssh hostA
hostA $ tmux attach
The link you mentioned contains a discussion about how to re-create a tmux session. If you are interested in starting a similar tmux session, you should try tmuxinator:
# ~/.tmuxinator/sample.yml
name: sample
root: ~/
windows:
- editor:
layout: main-vertical
panes:
- vim
- guard
- server: ssh serverX
- logs: tail -f log/development.log
By issuing mux start sample you will have a tmux session with three windows:
the first with two panes, the first one running vim
the second with a ssh to a given server
the third displaying the tail of a log file
Using this file you could start similar sessions on different machines.

How do I disconnect all other users in tmux?

I've got a tmux session where the window is too small because some user somewhere is connected.
How do I tell tmux to disconnect all connected users?
You can use <prefix> D (where prefix is C-b by default), to chose which clients to detach; it will also list they col/lines as well as the last used time. Note the uppercase D, i.e. Shift+d.
You could also use tmux's detach-client option
detach-client [-P] [-a] [-s target-session] [-t target-client]
(alias: detach)
Detach the current client if bound to a key, the client specified
with -t, or all clients currently attached to the session speci-
fied by -s. The -a option kills all but the client given with
-t. If -P is given, send SIGHUP to the parent process of the
client, typically causing it to exit.
either from <prefix>:followed by detach [options] or on the command line inside tmux with tmux detach [options]
tmux a -dt <session-name>
a=attach
d=detach other clients (so only you can attach to this session)
t=target
I'll break out #PEdroArthur's great comment into a separate answer since it directly answers the most common use case: "being within a tmux session and wanting to disconnect all other sessions"
Run this from within your tmux session:
tmux detach-client -a

How can I launch an x-window from emacs ess when running R on a server?

I am using emacs-snapshot with the ssh.el package, following the instructions from the ess manual.
There are a few ways to open an R session, but this is how I do it:
open emacs
C-x C-f /server:dir/file.R this puts me in ESS [S] mode
Type 'plot(1)'
C-c C-n to run
emacs asks for starting directory, and I choose the /server:dir/
I would like for a figure to pop up but it wont.
This also doesn't work when using ess-remote in shell or tramp mode, but it does work if I set the starting directory to my local desktop.
Any advice much appreciated. My current workaround is to print the file to pdf and then open pdf in DocView mode, but this takes a few extra steps and is slow.
I do it the other way around:
ssh -X some.server.com to connect to a remote server with x11 forwarding.
emacsclient -nw to restart an Emacs session that is already running
plot(cumsum(rnorm(100))) in R as usual
Then the plot windows appears on the initial machine I ssh'ed away from.
Edit: As a follow-up to the comment: This works for any emacs, either emacs or emacs-snapshot. For a long time I used (server-start) in the ~/.emacs but now I prefer that (just once) lauch emacs --daemon after which I can then connect to via emacsclient (which also exists as emacsclient-snapshot). I really like this -- it gives me Emacs around R in a persistent session that I connect, disconnect and reconnect to.
I selected Dirk's answer because he pointed me in the right direction, and especially for lowering the energy of activation required to visualize my data, but here I am going to give the details of how I got this to work on my desktop.
1) set ssh keypairs (I had previously done this, full instructions for Ubuntu here)
mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa
ssh-copy-id username#hostname
2) include the following in ~/.ssh/config
Host any_server_nickname
HostName hostname
User username
ForwardX11 yes
3) open emacs on local machine
4) C-x C-f
5) /any_server_nickname:dir/file.R for files in home directory or /any_server_nickname:/path/to/file.R
6) plot(1)
7) C-x C-b to evaluate entire buffer.

Resources