Archive

Archive for the ‘bash’ Category

how to configure a Digital Ocean (or Linode, etc.) Ubuntu box

March 21, 2016 Leave a comment

I wrote a doc about it on GitHub: https://github.com/jabbalaci/DigitalOceanNotes . Following this guide I can set up a virtual private server (VPS) in 30-40 minutes.

tmux: a screen alternative

March 18, 2016 Leave a comment

A few days ago I wrote about the awesome tool screen. Then, some of my readers namely “i90rr _” and Rodnee suggested that I should try tmux (thanks for the tip). Actually, I tried it years ago, but since I didn’t have to work with SSH too much, it was not integrated in my daily routine. However, a few days ago I bought a Digital Ocean VPS to do some Python webapp development and now I do need ssh :) And without screen / tmux it would be a pain.

So, after screen, let’s see tmux.

tmux: an advanced and friendly terminal multiplexer

The scenario is the same:

ssh
From machine A, which is your local machine, you want to SSH to a remote server.

Log in with SSH:

ssh -p PORT USER@1.2.3.4

Specify the port if the SSH daemon is not on port 22. In the case of screen I used screen twice, but now I think it’s not necessary. First just use “ssh”. To keep the connection alive between machines A and B, it’s a good idea to use this alias:

alias ssh='ssh -o ServerAliveInterval=60'

More info about it here.

Once you are logged in to machine B, make sure that you have a recent version of tmux installed. More info here. And now just start tmux and use it :)

My config file

A big advantage of tmux over screen is its sane configation system. You can find my ~/.tmux.conf file here, I won’t copy it here.

Screenshot

My tmux looks like this:

tmux_screenshot
In the top right corner I also display my hostname and IP address, but it’s blurred out on the screenshot.

Detach

A very special feature of screen / tmux is the ability to detach. It means that you have several tabs open within tmux (tmux calls them “windows”), and pressing “Ctrl-b d” you can close tmux. However, the programs in its tabs are still running, they are not terminated! You can close SSH, even restart your local machine, it doesn’t matter. When you log in again to machine B with SSH, you can attach to this tmux session and voilรก, there you have your tabs and every program is running.

For instance, you launch a program on the server that runs for hours. Just detach, and attach a few hours later to see the result of this program. Or, you log in to a remote server from home, launch some programs, detach, shutdown your local machine, go to your workplace, log in again with SSH and attach to tmux, and you continue where you left off at home.

I don’t want to write a tmux tutorial because others have already done it much better. So let’s see some links:

Links

Appendix

I use two simple scripts to make re-attaching easier. The first one is called “tm”:

#!/usr/bin/env bash

# tm

tmux list-sessions
echo "# tmux attach -t 0"

The second one is called “tm0”:

tmux attach -t 0

If you don’t give your tmux session a name (I don’t) then your session has ID 0. When I log in again to the remote server first I run “tm” to see the running sessions. Normally I only have one session, so its ID is 0. With “tm0” I can attach to it and continue my work in tmux. Easy peasy :)

Summary: to detach, press “Ctrl-b d”, and close SSH. To continue your work, log in with SSH, check sessions with “tm”, and attach with “tm0”. Easy peasy :)

Categories: bash Tags: , ,

upgrade tmux to the latest version

March 17, 2016 Leave a comment

What I love in Manjaro is that it contains the latest software versions. For instance, I installed tmux and on Manjaro it’s version 2.1.

On Ubuntu 14.04 it’s still version 1.8 in the reposotories…

Problem
How to upgrade tmux 1.8 to 2.1 on Ubuntu?

Solution
Install these packages:

$ sudo apt-get install exuberant-ctags cmake libevent-dev libncurses5-dev

Then download the source of tmux from the official home page. Then build and install:

$ ./configure && make
$ sudo make install

This tip is from here.

Categories: bash, ubuntu Tags: , ,

keep SSH connections alive

March 17, 2016 Leave a comment

Problem
You connect to a server via SSH, you take a coffee, and when you come back your terminal is stuck. If you are inactive for a while, your connection is cut by the server.

Solution
I use a very simple solution. Add this to your ~/.bashrc file:

alias ssh='ssh -o ServerAliveInterval=60'

This way the client sends a null packet to the server every minute, just to keep the connection alive.

You could also do this in different configuration files (see here).

Categories: bash Tags: , ,

screen: a game changer tool for ssh

March 15, 2016 3 comments

Recently I discovered the tool screen and it’s simply awesome. It’s “a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program.” (source)

Here, in this post I want to show how to use screen with ssh and how it can simplify your daily work.

Problem
If you ssh to a server and you are inactive for a while, the server disconnects you. It depends on the server, but most of them are configured this way. Annoying. Or, if you launch a process on the server in the foreground and you want to do something else on the server, what do you do? Log in again in another terminal?

Solution
Screen can solve all these problems for you. Here is the figure of an SSH connection:

ssh

We will use screen twice. First, we will launch it on machine A. This screen instance will guarantee that the connection won’t be broken even if you are inactive. Fine. Second, we will launch screen on machine B too. This second screen will allow us to open virtual tabs on the server! For instance, you want to work with two Midnight Commanders. You start copying something that takes a long time. No problem, just open a new “tab” and continue working on the server. You don’t need to log in again!

Preparation
You will need two configuration files. Here is the first one that must be copied to machine A, to your HOME folder. Here is the second one that you need to put on machine B, also in your HOME folder. Don’t forget to rename them to “.screenrc”.

Log in with SSH
For logging in I use this script:

screen -t SESSION_TITLE ssh -p REMOTE_PORT -t USERNAME@REMOTE_HOST screen
clear
echo "# screen closed"

For instance, you have a VPS whose IP is 1.2.3.4. You installed SSH on port 2222. Then, the first line could be “screen -t my_vps ssh -p 2222 -t me@1.2.3.4 screen“. It will start screen on localhost, but upon login it will also start screen on the remote server.

Here is a screenshot of the bottom left corner:

screen

By default, screen commands can be activated with Ctrl-a. However, we have two screen instances, so on machine B I changed the shortcut to Ctrl-b. It’s also displayed in the corner, so there is no need to memorize it. As can be seen on the figure, on the server I have two virtual tabs open.

Use cases
Now, what can we do with all this? If you want to open a new virtual tab on the server, press “Ctrl-b c” (hold Ctrl and press b, release both and press c). To close a tab just press “Ctrl-d” (like in any terminal).

Change between tabs: Ctrl-b-b (hold Ctrl, then press b twice). Or, use the ID of the tabs: “Ctrl-b 0” (first tab), “Ctrl-b 1” (second tab), etc.

You can also detach screen, but I will write about that later.

Links

Categories: bash Tags: ,

set up your firewall with UFW

March 15, 2016 Leave a comment

Problem
You have a Digital Ocean VPS with Ubuntu and you want to set up a firewall on it. However, iptables is too complicated. What to do?

Solution
Use UFW, the Uncomplicated Firewall. Say you want to open the SSH port and you have a webapp running on a specific port that you want to make accessible. Here is a basic setting:

#!/usr/bin/env bash

sudo ufw reset
sudo ufw disable

# SSH is on a custom port
sudo ufw allow 12345/tcp
# the webapp is listening here
sudo ufw allow 1234/tcp

sudo ufw default deny incoming
sudo ufw default allow outgoing

sudo ufw enable

sudo ufw status verbose

Warning! Make sure to add your SSH port! Otherwise you won’t be able to log in anymore!

Outgoing connections are allowed. Incoming connections are blocked except: 1) port 12345, and 2) port 1234.

Links

Categories: bash, linux Tags: ,

[mongodb] mongorestore crashes

March 15, 2016 Leave a comment

Problem
Yesterday I bought a Digital Ocean VPS with MongoDB preinstalled. I chose the cheapest one, which is $5 / month. With FileZilla, using the SFTP protocol, I uploaded a Mongo dump that I wanted to restore with the “mongorestore” command. Mongorestore terminated halfway and it also made the MongoDB server crash! WTF?

Solution
It turned out that mongorestore didn’t have enough memory, but it didn’t print an error message about it! It just stopped and said that the server is not running. Yeah, it was not running because it just crashed because of you, you dumb program…

So, the cheapest DO VPS has 512 MB RAM, which is not much for MongoDB. By default, this cheapest VPS has no swap at all. So the solution is the following: add a swap file. I created a swap file of size 2 GB (maybe 1 GB is also enough) and retried mongorestore. This time it was successful. During the import process I was checking “htop” and the swap file was used indeed.

How to create a swap file?
Check out this article: http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/. It explains the process step by step.

Follow

Get every new post delivered to your Inbox.

Join 87 other followers