[SSH]

In one month you learn nothing new about the tools you are using and in one day you discover a few new awesome things. The first one is about ssh; it may be not very useful but I did not know about it. Since a lot of people has already posted[4][5] about that I'm not going to repeat everything here and link some posts instead. The new feature I did not know about is the scape sequences in SSH. Imagine you do "ssh 10.0.0.1" and while you are connected you want to put the SSH connection in background. You can do that just by pressing: ~^Z (being ^Z  Control+Z). This way you'll be in the host form where you launched the connection. You can get you console with SSH back issuing "bg". Don't know if it can come handy but.... you never know!

Another one. You can issue ~q to completely kill the SSH session if is blocked. The other only way to kill the SSH link is by killing the process from another console. You can do that from the blocked console itself. If you want to know more options type ~? or read the posts I link below.

[Move long-running processes to screen session]

This one is awesome and can come VERY handy sometimes (and I've always wanted to do it). Context: You connect to a remote server, and run a command that takes more than you expeced to finish. Like hours. And is time to go home. And you cannot close the ssh session because, lazy you, forget to start screen or tmux and you cannot detach the session. There is hope, fellow sysadmin, and it comes with the name of reptyr [1]. You can install it from the debian repo, and is a tool for "re-ptying" programs.

Open a SSH connection to a server and issue "watch -n1 date". Imagine that this is the command that will take a while to end (in fact, it will...). Then open another SSH connection to the server, open a screen or tmux session and issue something like "reptyr $(pidof watch)". And here we go! The process on the first connection is now in background process (do not try to "fg" it, you'll not see anything), and in screen you'll see the output of the process happily running. You can now close the fist connection safely and detach the screen/tmux session from the second connection and the process will be up and running until it finishes (in fact, you don't really need two terminals for doing all that process, I've just used to for the sake of the explanation).

[Sharing screen sessions]

If you want to share a screen session with another user if you issue:

$ sudo su - root

$ sudo su - other_username_with_no_privileges

$ screen

Cannot open your terminal '/dev/pts/16' - please check.

While screen allows you to create a named screen session and using ACLs [2] give permission to another user to connect to your screen[3] (setting a SUID flag to the screen binary!!!!)  you can use tmux insted that will work like a charm at the first try.

[Bash - last arg completion]

OK, lets see the last one with a very simple exampe. You are in bash, creating a directory like:

mkdir -p /this/is/the/logest/path/you_will/every/type

If you want to "cd" this directory after creating it, you can hit the TAB key thousands of times ooooor issue one of the following commands:

cd <EscKey> .

cd <AltKey + .>

The fist one you press the EscKey and then the dot, and the second one you use the Alt+. at the same time. After pressing one of this two combinations you'll see the last argument of the previous command (the long directory) on as an argument. If you keep pressing AltKey+. you will go back in your history showing the last arguments of all the commands. It is very useful when you get used to it. I promise. What is you want the second argument of the last command? Its a little bit more typing but it would be something like: AltKey+2AltKey+. Easy. Try to use it and you'll see the benefits in no time. You opened a log with "tailf" instead of less? Don't edit the previous command line, just "less Alt+." it!

If you have an interesting/useful tip for us just leave a comment below!

Regards.

  • [1] https://github.com/nelhage/reptyr
  • [2] http://aperiodic.net/screen/multiuser
  • [3]  https://stackoverflow.com/questions/13962495/how-to-make-gnu-screen-share-1-screen-for-all-users
  • [4] https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen
  • [5] https://blog.nelhage.com/2011/01/reptyr-attach-a-running-process-to-a-new-terminal/