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

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.

Related

Tmux session does not always open with expected mount bindings

I want to run unshare, mount a few directories, and run a tmux session in the new mount namespace. Here is my setup
$ cat run
#!/bin/bash
mount --bind ~/a ~/b
tmux
$ unshare -r --mount ~/run
When I run this command, I get dropped into the tmux session but without the binding. What is more strange to me is that if I rerun the mount command in the tmux session and exit, the next time I run unshare -r --mount ~/run the binding is there!
I could get consistent behavior by always running the mount command in the tmux session but I would like to understand why the behavior depends on if the binding existed in a previous session.
Replacing tmux with /bin/bash to poke at the mounted directory shows that the binding always happens, as expected. Once running the mount command in tmux, all subsequent runs have the binding until I move ~/a. Then the problem is back.
I am now unable to reproduce my own problem. I suspect the solution was to power cycle. I do not think I had done so between installing tmux and running into this issue, so that may have been the root cause.

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

In tmux I only have 2 groups

In tmux I only have 2 groups, as opposed to the expected 5:
$ groups
username sudo staff website1 website2
$ tmux
$ groups
username sudo
Why is this and how do I fix it?
Perhaps your tmux server was started before you were added to the additional groups. The server process and any processes which it starts will only have the permissions that were in place when the server was started.
You can fix this by closing all sessions and starting a new server. Once you've quit any programs that you care about which are running inside tmux sessions you can use tmux kill-server to ensure that the old server process is ended. Then when you run tmux again it will automatically start a new server which should have all of your current permissions.

How do you detach a remote screen session in byobu (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.

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