Pàgina 2 de 2

Tmux

Tmux is a terminal multiplexer. This is the kind of tools where you don’t know that you need. This tool is useful when you may work with remote servers via terminal.

This allows you to:

  • Have multiple terminals on the same connection
  • Be able to disconnect from the terminal without terminating the running
  • Share terminal with other users

How to install

To install tmux you can run this command:

apt-get install tmux

How to use:

You can create a new tmux session this command:

tmux

You can attach to an existing running session :

tmux attach 

or a shorter version:

tmux a

Once you are on a tmux session you have the following shortcuts:

  • Control+b+c: Open a new window.
  • Control+b+d: Detach of a tmux session.
  • Control+n: Move to the next window.
  • Control+p: Move to the previous window.
  • Control+b+number: Move to the number window.
  • Control+b+”: Slit the window horizontally.
  • Control+b+%: Split the window vertically.
  • Control+b+$: Rename session
  • Control+b+s: Change the session

Share a tmux session

You can share a tmux session with another ssh session with the same user via ssh.

Also if you can’t share the ssh connection you can use tmate.

You can install tmate with this command:

apt-get install tmate

Once tmate is installed you can run the command:

tmate

This will give you a list of ways to share the tmux session

Fail2ban

Usually,when you deploy a service on a server you, will receive a lot of strange requests trying to discover the services running on your server or attacking it, for example trying to login in the ssh of your server.

Apart from having a strong password and the updated software, fail2ban mitigates this type of attacks blocking the ips that does this request.

How it works

Fail2ban reads the logs of various services that you can configure like ssh or Nginx, and looks for suspicious usages .

Once fail2ban detects incorrect login attempts, fail2ban blocks the ip using iptables

You can check the banned ips using this iptables command:

iptables -L
Example of the output of iptables

How to install and configure

You can install it with:

sudo apt-get install fail2ban

After this, you can configure the services that you want to monitor.

On the DEFAULT section there are the following parameters to configure:

  • bantime: the seconds to ban the ip
  • findtime: Window of time to check the intents of login
  • maxretry: Number of intents

Tip

The time is specificatied in by default seconds, but you can add an m at the end to specificate minutes

After this you can see different sections to enable the services to monitor, for example:

[sshd]

# To use more aggressive sshd modes set filter parameter "mode" in jail.local:
# normal (default), ddos, extra or aggressive (combines all).
# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.
#mode   = normal
port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

Finally you can add the tag enable = true to enable fail2ban to enable to monitor ssh

Remote debug

Usually when you develop an application it will not run on the same computer where you are developing. This can caused unexpected behaviors when you deploy it,usually cause by permissions, software/package versions,configuration,ect.. or just that we write an error on our code that we didn’t notice. In those cases, it’s difficult to know what is failing but we can use remote debug to check the application state when it fails.

Tip:

If you are trying to debug a python package and the debugger doesn’t stop, try to install it with the -e flag

I use two tools in these cases:

Visual studio code remote ssh

To remote debug (and develop) with Visual Studio code you mus to install Remote ssh extenison

Once you install it, you can add a host that looks like this:

Add a new host on remote ssh.

And finally open a remote project:

Connect to a remote ssh and run a script.

After this, you can use Visual Studio code like on your own machine

PUDB

Pudb is a package to debug python applications via CLI. If you are used to and feel comfortable using CLI tools this can be a good tool to debug your python application.

To debug it with pudb you need to install the package with pip:

pip install pudb

On your code , add the following code where you want to stop:

import pudb;pu.db

Once the execution reaches the previous line, you will get a window that looks like this:

You can use the following shortcuts to interact with the pudb:

  • Ctrl+x: Go to command line or return to the code
  • n: Step to the next instruction
  • s: Step into
  • c: Continue
  • b: Toggle breakpoint

My Python development environment

Python environment

To develop different python applications and avoid problems with the requirements of each application I use virtualenvs. To have all the virtualenvs o organized you can use virtualenvwrapper. To install virtualenv you can execute:

pip install virtualenv
pip install virtualenvwrapper

IDE

I tried a several IDES to develop in Python lik Pycharm or PyDev and my favorite is Visual Studio Code because it can be used with multiple languages and it is also lightweight.

To install Visual Studio you can run the following commands:

# Install requirements to install
sudo apt install software-properties-common apt-transport-https
# Add Microsoft keys
sudo wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
# Add visual studio repository
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
# Update apt and install Visual Studio Code
sudo apt update
sudo apt install code

Extensions

  • Python extension: This extension adds Python support for Visual Studio Code
  • Remote SSH : Very useful if you have to debug or test code on a remote server

Whoami

Hi, my name is Xevi, I’m a software developer interested in Python, GIS, OpenStreetmap and learning.

I created this blog to share developer tricks and interesting things but also to get feedback and learn.

So I hope that it can help you

Entrades més recents »

© 2021 Another dev

Tema de Anders NorenAmunt ↑