Console Into Mass. The Transit to the Light Side. Part Two.
I’d used screen for quite a long time before I knew it was not enough for me anymore and I started to look for alternatives. Around the same time I found tmux, a software application described by many users as a screen based on steroids. I couldn’t understand this strange comparison until I tried the app for myself. Honestly speaking it didn’t happen instantly that I started to work with tmux only. After three-four attempts I still got back to using screen. Eventually, though, tmux has won.
The important note: I’d like to make a point that a reader is expected to have no access to the graphic shell. All tools he/she has got are a terminal (CLI) and an array of commands to enter.
So, let’s get to the tmux installation. Actually, the installation requires entering the first command and after that a user can launch it on each next input of the command
tmux in the terminal.
Basic Key Combinations
Same for when using screen, you don’t need to learn these key combinations by heart. Follow this link and start to use them instead. The only thing which might confuse you from the start is that the key combinations are different from those used for screen. If needed, however, they can be changed to the ones you prefer in the configuration file.
Tmux boasts quality default settings out-of-the-box. There’s no need to customize the arrangement and backlight of the control pane how you’d have to do it in screen. But if you’d need a configuration file, keep in mind that it’s got to have a name
.tmux.conf; it ought to be transferred to a home user’s directory.
I’d like to tell you about a useful setting that saved me the trouble of creating a new tmux session every time I used it. Let me describe it in details. I’ve got a Windows OS driven PC and to connect with the remote server I use putty and pagent (for adding keys). With each new key being added to pagent, I need to enter a password (a passphrase that you provide when creating ssh keys). In putty each new connection needs selecting this checkbox:
These are the peculiarities of the work of Windows + putty + remote connection via SSH. Putting the tick in this checkbox is required to make SSH-agent forwarding work.
Ok, let’s get back to the problem. In case you happen to lose a connection with the network or if the power in the office is gone, tmux will save your session and after you reconnect, will prompt you to enter your password to the key.
This is the scenario that will take place all the time. This also means that SSH-agent forwarding will stop to work. What you can do, though, is to end the current tmux session and reconnect to the remote server. It isn’t a critical issue, of course, but looking at it in a long perspective, this can become an irritating extra step to do. Well, there’s a solution, you only need to open your home directory, find the file
.bashrc and change there a few settings.
I will try to explain what happens when you connect to the remote server with the SSH-agent forwarding working. The moment an ssh-client (putty in our case) connects with the remote server, it creates a
socket which a user gets the key through, with a variable
$SSH_AUTH_SOCK inherited by the running shell (that processes user’s commands) during the current session. Besides the said variable, the client creates
$SSH_TTY variables to store information about the current ssh-connection. Yet, the next time we will connect to the remote server, the client will create a new socket with new variables. That’s why we need to prompt shell of the data we’d like it to save and use on the next connection (if there are any).
How tmux Works
Now, it’s time to look into how tmux works. You already know how to launch it — it’s enough to enter
- Working area. Pane.
- A status bar with tabs and other elements.
- A tab with the default name.
This is the layout you’ll see on opening tmux. If you’ve read my previous post, you’ve probably noticed that tmux and screen look pretty much similar with the only difference that in tmux a status bar comes by default. Ok, let’s see how tmux works when implementing a more complicated task.
For this purpose we will need to use repository. If you haven’t cloned it on your PC yet, enter the following command:
After that we need to launch tmux. Let’s work with the tabs first. We will need two-three tabs at a start. In order to create a new tab, you can use the following key combination:
The next step requires renaming tabs. To do this, use:
To switch between the tabs, use either this key combination:
Now, after we’ve practiced with tabs a bit, let’s split the pane into two independent ones. Keep in mind, though, the layout isn’t going to be the same for the rest of the tabs. For each new tab tmux creates its unique layout. So, in order to split the pane, we need to press:
Open some application (In my example I use midnight commander) in the left pane by inputting the command:
Shifting the panes can be done by pressing:
In the right pane let’s run our test app. It’s a simple chat written in node.js and socket.io and taken from socket.io official website. What I added to it is a bit of logging in order to show you that it works. Let’s open directory now
%repoName%/tmux/chat-example and enter the following commands:
%repoName% — the directory’s name to which we’ve cloned repository before.
If you’ve done everything correctly, you will see the next line on the screen:
Open a browser and enter
localhost:3000 in a new tab. Type some message in the entry line and press “Enter”. See? The same message is displayed in the terminal, in the pane where you entered the
node index.js command.
Like we’ve done with screen (described in the previous post), let’s simulate disconnection, click the cross to close the terminal but leave the browser running. Let’s type more messages in the entry line. They all keep appearing on the screen despite the terminal being closed a second ago. Everything works!
The current session along with all processes have been saved. Screen can save them too. So, what’s different then? Let’s reconnect to the session. Launch the terminal and enter the command:
You’ve probably noticed that the layout we’ve created before (vertically splitted) has been saved too and that’s already a great advantage of tmux over screen.
Useful Links to Work With tmux
screen vs. tmux
Drawing the line between screen and tmux, I’d say, use screen only when tmux isn’t available. E.g. you can neither build or download it. In all other cases it’d be much more sensible to start working with tmux.
Let’s make a summary of what’s been said above. We’ve seen one more terminal manager in work — tmux. We’ve learnt how to install and configure it. We’ve practiced some shortcut keys and seen how tmux works by giving it some task. We’ve also seen some of its obvious advantages over screen.
Of course, you can find a lot more terminal managers but screen and tmux, compatible with different OS to your liking, so it’s totally up to you which one to use.
That’s it for now. Thanks for your attention and time. Special thanks to the readers who’ve read the post to the end and see you soon, on my next post release.