The work of operations is often helped by using the combinations of tools. The tools you choose to use will depend heavily on your work style. Over time you may even change the tools you use.
This is a list of commonly used tools, we recommend trying as many of them as you have time for. Finding the right tool is often like finding a good pair of gloves - they have to fit just right!
If your operating system has a good terminal emulator available (eg, Linux, BSD,
OSX), then the ssh
command line utility is by far the best tool to use for
SSH interactions.
If you use Windows as your primary work space, PuTTY is possibly one of the best SSH clients available.
Connectbot is a free app available in the Google Play store, and offers most of the functionality you would need from an SSH client.
Operations work regularly involves connecting to remote servers (there will be times when you do nothing but work on remote servers - parts of this curriculum were even typed on remote servers rather than contributors desktops and laptops!).
There are however two limitations to working this way:
- You'll often need to be connected to more than one remote system at a time. Opening a whole new terminal each time can result in a lot of windows cluttering up previous screen space.
- When happens if your internet connection stops working? All of your connections are reset. Any work you might have been doing on the remote servers can be lost.
Multiplexers are a good solution to this. They allow you to run multiple "virtual" windows inside a single windows. For example:
Bob works on 10 remote servers, all of which run Linux. Bob's internet connection at work is questionable. To work around this, Bob connects to
server1
which is at his data centre. It is a reliable server which is close to the other servers Bob works on. Onserver1
, but starts a multiplexer. The multiplexer gives Bob a regular looking command prompt, and Bob continues his work.If Bob's internet connection drops, he can reconnect to
server1
, and then re-attach to the multiplexer he started previously. His session is in the same state he left it before being disconnected, and he can continue his work.The multiplexer also lets Bob open more than one command prompt and switch between them as he needs to. Bob can now connect to many servers and see them all in one window.
screen
is one of the longest lived multiplexers. Almost everyone who has
used a multiplexer has used screen, and you can't go far wrong with it.
.. todo:: Explain how to use ``screen``
tmux
[1] is relatively new compared to
screen
. It covers the same basic feature set and has added a few
more advanced features. It is recommended you get comfortable with
screen
first before attempting to use tmux
.
In this chapter you will learn to start a tmux session, get to know a few first keyboard shortcuts and detach from and re-attach to the session.
tmux is available on Debian and its descendants like Ubuntu or Mint with the command:
aptitude install tmux
On RedHat-style distributions you will have to use the :term:`EPEL` repo to get a pre-built package, and install with the command:
yum install tmux
On MacOS you can use Homebrew to install via:
brew install tmux
tmux
is usually started with the command tmux
in a
terminal window. Depending of your version of tmux you will see either
a line at the bottom of the screen or nothing at all. tmux
is
controlled with keyboard shortcuts, the default shortcut usually is
ctrl-b
. If you press ctrl-b
and then a t
in the newly
started tmux window you should see the local time displayed as a large
digital clock. If you hit ctrl-b
and c
you should see a new
empty window with an empty input prompt.
If you want to detach from the session you have to hit ctrl-b
and
d
. The tmux
window will disappear and you will see a message
[detached]
in your terminal window. All the shells and processes
you started onside the tmux
session continue to run, you can see
this with a simple
ps -ef | grep tmux
You should see something like the following:
cdrexler 13751 1 0 Nov30 ? 00:00:41 tmux
You will notice that the tmux
process has a parent process id of 1
which means that it is not a child process of the shell you started it
in anymore. Accordingly you can leave your working shell, start a new
one and attach to the running tmux process again which is very handy
if your connectivity is flaky or you have to work from different
locations. If you check the process table for the process id of the
tmux process
ps -ef | grep 13751
you will find that is the parent process of the two shells you created in the beginning of the chapter:
cdrexler 4525 13751 0 17:54 pts/2 00:00:00 -zsh
cdrexler 4533 13751 0 17:54 pts/5 00:00:00 -zsh
If you want to get an overview of the running tmux processes on your system you can use the command
tmux ls
It will list all available tmux
sessions on your system [2]. If there
is only one you can attach to it with the command:
tmux att
If there is more than one session the output of tmux ls
will look like this:
0: 3 windows (created Fri Nov 30 18:32:37 2012) [80x38]
4: 1 windows (created Sun Dec 2 17:44:15 2012) [150x39] (attached)
You will then have to select the right session with the -t
command line switch:
tmux att -t 4
tmux
runs as a server process that can handle several sessions so
you should only see one tmux process per user per system.
You should see the original session with the two shells again after running this command.
tmux
is configured via a
config file which is usually called :file:`.tmux.conf` that should live in
your $HOME
directory.
A typical :file:`.tmux.conf` looks like this:
#set keyboard shortcut to ctrl-g
unbind C-b
set -g prefix C-g
bind C-g send-prefix
bind g send-prefix
#end of keybord shortcut setting
# Highlight active window
set-window-option -g window-status-current-bg red
# Set window notifications
setw -g monitor-activity on
set -g visual-activity on
#automatically rename windows according to the running program
setw -g automatic-rename
#set scroll back buffer
set -g history-limit 10000
set -g default-terminal "xterm-256color"
set -g base-index 1
set -g status-left '#[fg=green]#H
This illustrates a method to change the default keybinding and some useful settings.
Please note that you can force tmux
to use another configfile with
the -f
command line switch like so:
tmux -f mytmuxconf.conf
There is a nifty cheat sheet [3] for the most important
screen
and tmux
keybindings or even a whole book about tmux [4].
.. todo:: - describe advantages of meta-multiplexers like ``byobu`` [#]_ that can use different backends. - describe scrollback and copy and paste
[1] | http://tmux.sourceforge.net/ |
[2] | Please note that tmux ls will only list tmux sessions that belong to your userid! |
[3] | http://www.dayid.org/os/notes/tm.html |
[4] | http://pragprog.com/book/bhtmux/tmux |
[5] | https://launchpad.net/byobu |
As you read in :doc:`shells_101`, your shell is your primary tool during the work day. It's also incredibly customisable to suit your needs. Let's look at some changes you can make.
Your shell's configuration is stored in its rc
file. For bash, this file is
~/.bashrc
. Each time you edit this, you can reload the configuration by
typing:
source ~/.bashrc
Your default prompt probably looks something like this:
bash-3.2$
That's pretty plain and doesn't tell you much. In fact, all it does tell you is
that you're using Bash version 3.2, and that you are not the root user (the
$
at the end signifies a regular user, whereas if you were root, you would
see a #
instead).
Let's change this up a little. Edit your ~/.bashrc
file, and add this line
to the end:
PS1="\u@\h \w> "
Save, quit, and then reload your .bashrc
file. Your prompt should change to
something like this:
avleen@laptop ~>
Much better! Now your know your username, the name of the machine you're on (in
this case "laptop
"), and the directory you're in ("~
" is your home
directory).
The PS1
variable has a lot of different options you can use to customise it
further.