Linux Multiplexer with VNC, Screen and Tmux

Overall, tmux is the best among all selections here, but you should know how do others work in case tmux is unavailable. 你还需要理解VNC, Screen, tmux 的原理是什么? 为什么使用它们则SSH 断开之后进程仍可以继续运行呢? 哪里在收集和记录状态。

Run in Background

The most straightforward way is put long running task in background, redirect the stdout/stderr to files or /dev/null, for example:

1
2
./task.sh &> log.$(date "+%Y-%m-%d") &
# then working on it by jobs, bg, fg and ctrl+z commands

Note that ctrl+z also works when editting Vim file, it will suspend editing, put it in background and bring you back to terminal.

To detach a job from the current jobs list, using disown %<job id> command. The detched job will still be running but cannot bring it back to jobs.

Nohup

Why the work gets lost when SSH session drops is because the signal SIGHUP will be sent to the foreground process. If we let the process ignore this signal, then it can keep running when remote SSH connection is gone.

1
2
3
4
5
6
# nohup will redirect the stdin from /dev/null
# stdout/err to FILE $HOME/nohup.out
nohup [bash|sh] <script> &
# similar to
# &> is the modern version of 2>&1
<script> </dev/null &>/dev/null &

You can use jobs, bg and fg command to operate it.

VNC

之前用VNC的目的之一是为了keep SSH long running task on remote machine , prevent lost of work, the alternative is screen and tmuxcommand(best).

Virtual Network Computing (VNC) is a graphical desktop-sharing system that uses the Remote Frame Buffer protocol (RFB) to remotely control another computer. It transmits the keyboard and mouse events from one computer to another, relaying the graphical-screen updates back in the other direction, over a network.

Popular uses for this technology include remote technical support and accessing files on one’s work computer from one’s home computer, or vice versa.

I usually use it to do remote development on Fyre, I have a Fyre central control machine with VNC installed, after vnc to that machine I use it to SSH to other machines within the same internal network(usually connections will not break), you can also install IDE in VNC to do general programming work rather than developing locally.

Note, there are other remote terminal tools such as Termius.

Install VNC Server

The settings are varies on different Linux distros.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
yum update -y

# seems no need this:
yum install open-vm-tools

# if you don’t have desktop installed, note linux has many
# different desktop system themes, only install core packages
# of KDE or GNOME, KDE is better
yum groupinstall 'X Window System' 'KDE'
yum groupinstall 'X Window System' 'GNOME'

yum list | grep tiger
yum -y install tigervnc-server

# Fyre firewalld default is inactive, no need to deal with firewall
# if it’s enable, may need to config
systemctl status firewalld
firewall-cmd --permanent --zone=public --add-service vnc-server
firewall-cmd --reload

# start vnc
# select yes then create password: 123456
# first time it will open port 1
# then you will get the address and port to login:
# centctl1.fyre.ibm.com:1
vncserver

Note you will get new vnc session everytime you run vncserver, check with

1
2
3
4
ps aux | grep vnc

root 2682 0.0 3.0 284860 116840 ? Sl May27 1:38 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop mycentctl1.fyre.ibm.com:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
root 9199 1.4 1.6 231660 62388 pts/3 Sl 08:59 0:00 /usr/bin/Xvnc :2 -auth /root/.Xauthority -desktop mycentctl1.fyre.ibm.com:2 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5902 -rfbwait 30000

you can kill it by running:

1
vncserver -kill :2

If the VNC is broken due to Fyre maintenance, open a new vnc session again by vncserver.

Install VNC viewer

Install VNC viewer on your local laptop, then connect by for example:

1
centctl1.fyre.ibm.com:1

Copy and Paste

If you want to copy from local to viewer, sometimes it’s malfunction, kill the klipper process:

1
2
ps aux | grep klipper
kill -9 <PID of klipper>

You can adjust shortcuts in VNC viewer for copy and paste: Settings -> Configure Shortcuts -> copy / paste

Other Settings

Other settings useful: Settings -> Edit Current Profile -> Mouse -> copy on select / Trim trailing space

Adjust font size the themes: Settings -> Manage Profiles -> Edit Profile -> Appearabce -> Black on Random Light -> check Vary the background color for each tab

Also change the text size under Appearance.

Screen resolution

If the screen open by VNC viewer is small, you can change the resolution, run:

1
xrandr -s 1920x1080

on the terminal in your remote machine.

Screen

Create virtual terminal lives beyond your terminal session.

How To Use Linux Screen Screen or GNU Screen is a terminal multiplexer. In other words, it means that you can start a screen session and then open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected, the work or progress will not get lost! 所以说可以不用& 后台运行了。

screen command cheat sheet, or see help in screen: crtl+a ?, hit enter to quit. you can use either screen or ctrl+a(key binding).

Note that install and run screen on target machine, VNC 应该是同样的.

1
2
3
4
5
6
7
8
apt update
apt install -y screen
# or
yum install -y screen

# start a screen session with description
# and enter into it
screen -S remote_playbook

You can create multiple windows in one session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Ctrl+a c         Create a new window (with shell)
Ctrl+a p Go back to previous screen
Ctrl+a n Go to next screen
Ctrl+a Ctrl+a Toggle between the current and previous region or screen

Ctrl+a A Rename the current window

Ctrl+a " List all window
Ctrl+a 0/1/2/3 Switch to window 0 (by number )


Ctrl+a S Split current region horizontally into two regions
Ctrl+a | Split current region vertically into two regions
Ctrl+a tab Switch the input focus to the next region
Ctrl+a Q Close all regions but the current one
Ctrl+a X Close the current region

Ctrl+a k Kill current window or `exit`
Ctrl+a \ Kill all windows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# list running sessions
screen -ls

# detach session
screen -d [session name]
# detach current session
Ctrl+a d

# attach session
screen -r [session name]
# attach to a running session
screen -x

# detach and logout all sessions
# will not terminate session
Ctrl+a D D

# quit and terminate current session
Ctrl+a :quit
# or kill all window on sessions
Ctrl+a k

How do I know I am in screen and which session:

1
2
3
4
5
6
7
8
9
10
11
# show session id if you are in screen
# for example: 3384204.screen-es-upgrade
echo $STY

# return with screen prefix if you are in screen
# for example: screen.xterm-256color
echo $TERM

# press ctrl+a with t
# otherwise nothing will show
# for example: 22:26:35 Mar 04 chengdolcent 0.36 0.28 0.2

Tmux

Tmux 相比Screen 有terminal底部的提示,不容易和host terminal搞混.

Note that tmux has 3 terms(same as Screen): session -> window -> pane. you can have multiple sessions, each session can have mutliple windows and each window can have several panes. Usually rename the session and window with meanful symbol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# version
tmux -V
# start a session, notice the bottom line for window status
tmux
tmux new -s <session name>

# rename session, by default no meanful name
Ctrl+b $

# list tmux sessions
tmux ls

# switch list sessions
# use arrow key to expand each session and select
Ctrl+b s

# detch tmux session
Ctrl+b d
# attch again
tmux [attach|a] -t <session name>
# attach most recent one
tmux [a]ttach|a]

# kill all sessions but current
tmux kill-session -a
# kill all sessions but my session
tmux kill-session -t <my session>

In the bottom line, * means current window, you will see the windows list and name here.

Some key bindings for window:

1
2
3
4
5
6
7
8
9
10
11
12
13
Ctrl+b ?         help, press `q` to quit

Ctrl+b c create new window
Ctrl+b , rename window
Ctrl+b w list windows

Ctrl+b <id> switch to other window
Ctrl+b n go to next window
Ctrl+b p go to previous window

Ctrl+b & kill and close the window

Ctrl+b t clock display, press any key goes back

Add panes to windows, active one has the green color.

1
2
3
4
5
6
Ctrl+b "         horizontal split
Ctrl+b % vertical split
Ctrl+b o switch pane
Ctrl+b arrow switch pane by arrow key
Ctrl+b ; switch between most recently panes
Ctrl+b x close pane

You can also adjust the pane size or even promote the pane to a its own window.

1
2
Ctrl+b [esc + arrow]    连续多次esc + arrow resize the pane
Ctrl+b ! promote window

Tmux also has command mode to accomplish the same task by key bindings, similar to Vim. 比如说monitor-activity设置,在有输出变化时会提示高亮. You can pre-set tmux config: .tmux.conf, there are a lot examples on Internet.

0%