📜 ⬆️ ⬇️

Convenient remote control of the linux console from under Windows

So, friends, some subset of us (people) somehow feel the need to remotely control a computer (usually a server) on linux (the method can work in BSD and Mac OS X, but I have not tried it) using ssh. Another subset uses Windows as the OS on the host machine. The intersection of these subsets is addressed to my article.

Utility, for which all conceived is started:
1. Save the connection when the connection is broken. This item is the most important for me and played a decisive role in setting up such a system. The fact is that I live in a hostel with a very, very unstable Internet, and my communication breaks (on bad days) can occur every minute. And putty requires a sequence of actions to reconnect each time.
2. Saving the session, including the current directory, command history and output. Often, the only task when connecting to the server is to go into the same directory and write git pull. In order not to remember the path to this directory every time, you need to save the session.
3. The terminal supports tabs. Most importantly, this terminal can also be used for cmd, for PS, and for git bash console. Therefore, do not have to keep several different terminals open.
4. Pluses tmux: multiple windows in one session and split

image

The recipe for success will include several points:
  1. Terminal - ConEmu code.google.com/p/conemu-maximus5
  2. Session Manager - tmux tmux.sourceforge.net
  3. Connector between client and server - mosh mosh.mit.edu
  4. Running * nix applications under windows - cygwin www.cygwin.com

')
Also for carrying out all the manipulations we also need a similar set of software:

  1. Putty or another ssh client;
  2. Remote server (I use ubuntu on aws, although this is not significant).


What do we get in the end?


As a result, we get a terminal with the ability to access a remote console. When the connection is broken (the Internet is broken, the ip-address is changed, the routing is changed, the connection is not stable, the notebook goes to sleep, etc.) you will not have to reconnect, and when you reconnect, the work context will not be lost.

Now in detail.

Configuration


We will be setting up our system in stages, starting with a remote server. Connect to it via ssh and set ...

Tmux


Tmux is a session manager, as they say, screen on steroids. My tmux distribution was already bundled, and if not, we put it:

sudo apt-get install tmux 

Or we collect from source codes:

 git clone git://git.code.sf.net/p/tmux/tmux-code tmux cd tmux sh autogen.sh ./configure && make 


A brief cheat sheet on tmux
Copied from http://habrahabr.ru/post/126996/

A very good way to run tmux:
tmux attach || tmux new - by doing so, you first try to connect to an existing tmux server, if it exists; if this is not yet - create a new one.

After that you get to the full console.
Ctrl + bd - disconnect. (In the same way, you disconnect if the connection is disconnected. How to connect back and continue working - see above.)

In one session there can be any number of windows:
Ctrl + bc - create a window;
Ctrl + b 0 ... 9 - go to such a window;
Ctrl + bp - go to the previous window;
Ctrl + bn - go to the next window;
Ctrl + bl - go to the previous active window (from which you switched to the current one);
Ctrl + b & - close the window (or you can just type exit in the terminal).

In one window there can be many panels:
Ctrl + b% - split the current panel into two, vertically;
Ctrl + b "- split the current panel into two, horizontally (this is a quotation mark, which is near Enter, not Shift + 2);
Ctrl + b → ← ↑ ↓ - move between panels;
Ctrl + bx - close the panel (or you can just type exit in the terminal).

Disadvantage - scrolling becomes unusual:
Ctrl + b PgUp - enter the "copy mode", then:
PgUp, PgDown - scrolling;
q - exit from the "copy mode".


In the config, which is located in ~ / .tmux.conf, we add the following:

new-session
set-window-option -g mode-mouse on
set -g history-limit 25000


The first line means that when you try to connect, if there is no active session, a new one is created. The second line includes mouse support, and the third sets the size of the story.

Mosh


Mosh in our bundle is a system that provides uninterrupted connection. When the connection is broken, the program waits until connectivity is restored and continues to work from the point of stop. It also helps with a slow / unstable connection.

Install from source to get a version that supports all the necessary features:

 git clone https://github.com/keithw/mosh.git cd mosh/ sudo apt-get build-dep mosh ./autogen.sh && ./configure && make sudo make install 


No special configuration required.

Cygwin


Now go to the client side.

Cygwin is an alternative implementation of linux api, which allows using linux utilities from under windows. It is installed from the official site (https://cygwin.com/install.html). I have a version of x86_64. Download, set.
When choosing packages (besides the main ones), you should note mosh and ssh:

image

Configuration is not required.

Conemu


The last item is ConEmu . Wonderful terminal under windows. Supports a bunch of features, such as:

  1. tabs and split screns;
  2. working with console applications using the console API;
  3. flexible configuration and management;
  4. work with flowers;
  5. ... and much more, you can read more about this terminal in the developer’s article .

ConEmu is placed from the official site. Download, install (or unpack), launch. When you first start leaving a tick stand on the ground. Open the settings for Win + Alt + P. We are interested in the item Tasks.

Here is the cygwin startup task:

image

Create such an item at your place, and you may have to replace the path with your installation path:

 C:\cool\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico - 

Save, close the settings, select our cygwin in the drop-down menu next to the plus sign in the upper right corner.
Now we need to get an ssh connection to our server.

My team looks like this:

 ssh -i key2014.pem -p 22 

key2014.pem - my ssh private access key to the instance in aws. It may be worth copying from the windows drive (which is mounted in / cygdrive) to the root folder.

The next step is to connect via Mosh:

 mosh <username>@<hostname> --ssh=\"ssh -i key2014.pem -p 22\" -p 55505 -- tmux a 

55505 is a UDP port that I previously opened for incoming connections in the security rules in the aws admin panel. Now all connections will be made only through it (besides the initial connection establishment - it goes through port 22).
tmux a is a command that is executed when connecting to the server (attach to a working tmux session).
We are checking. Works? Sumptuously! The final task is to create a new Task in ConEmu.

image

Specifically:

 C:\cool\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico /bin/bash -l -c "mosh <username>@<hostname> --ssh=\"ssh -i key2014.pem -p 22\" -p 55505 -- tmux a" 

I think everything is clear. Pay attention to slashes before quotes. They distinguish the degree of nesting quotes.

Well, perhaps that's all. Now, when we want to connect to the server, run ConEmu, poke the arrow next to the plus sign and select our Task.

Addition


Another simplification that I use is that I added all the tasks I need to call directly from Totcmd:

image

Normal cmd:

image

Connect to aws:

image

PS
I suggest everyone who has read this article to express suggestions on what else can be included in this scheme. Thanks for attention.

Pps
This article is a compilation of other articles taken from different places and own developments. The only article I remember is http://xakep.ru/komfort-shell/

Source: https://habr.com/ru/post/243613/


All Articles