Foreground and background jobs – Linux

Foreground and background jobs – Linux

A tool that is for sure not used enough is fg / bg.

Example: Did you ever start editing a file and suddenly remembered that you needed to copy the values of the previous command? Well this take care of this problem and many others!

First of all, executing a command / job in background when in a shell is as simple as adding an “&” at the end of the command:

watch -d ls -l &

Another way to do this is by using the “bg” command, or by pressing “CTRL+Z”:

watch -d ls -l bg

In order to see all background running jobs use the “jobs” command:

jobs
[1]  + suspended (signal)  nano ca.crt

And to bring one of them back to foreground, or use “CTRL+Z”:

fg %1

Where “%1” is the number of the job. Using this syntax you can also kill jobs:

kill %1

This is a very simple feature which can make your live easier if you learn to use it.

Cheers,

Vincent

What driver is my NIC using?

What driver is my NIC using?

Hello people,

in this small blog post I wanted to show you a nice script to see which drivers are used by the Network Interfaces of a Linux machine.

#!/bin/bash
for f in /sys/class/net/*; do
    dev=$(basename $f)
    driver=$(readlink $f/device/driver/module)
    if [ $driver ]; then
        driver=$(basename $driver)
    fi
    addr=$(cat $f/address)
    operstate=$(cat $f/operstate)
    printf "%10s [%s]: %10s (%s)\n" "$dev" "$addr" "$driver" "$operstate"
done

I find this very useful when you need to find out if your interface is running at 10gb or only 1gb. The output looks like this:

      eno1 [2c:60:0c:da:7b:fc]:        igb (up)
  eno1.361 [2c:60:0c:da:7b:fc]:            (up)
  eno1.362 [2c:60:0c:da:7b:fc]:            (up)
  ens255f0 [2c:60:0c:f1:e6:5b]:      ixgbe (up)

So you can see that ens255f0 is a 10gb interface and en1 is only 1 gb.

This script has not been written by me but by an Stackexchange user, all credits goes to him. For reference here’s the link to the Stackexchange question.

Bests,

DOV

Working with systemd

Working with systemd

Hi there,

in this small post we are going to see how systemd works and what are the difference from the old init.d system.

A problem I recently had to solve was a mongodb instance which was starting with the wrong arguments. To debug it I used the following commands:

vdo@ubuntu:/lib/systemd/system$ systemctl status mongod.service 
- mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2016-11-04 14:24:21 CET; 3 days ago
 Main PID: 2870 (code=exited, status=100)

vdo@ubuntu:/lib/systemd/system$ systemctl status mongodb
- mongodb.service - LSB: An object/document-oriented database
   Loaded: loaded (/etc/init.d/mongodb; bad; vendor preset: enabled)
   Active: active (running) since Mon 2016-11-07 15:14:44 CET; 1min 23s ago
  Process: 16456 ExecStop=/etc/init.d/mongodb stop (code=exited, status=0/SUCCESS)
  Process: 16474 ExecStart=/etc/init.d/mongodb start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mongodb.service
           +-16486 /usr/bin/mongod --config /etc/mongodb.conf

So basically I had mongod and mongodb. But which was I suppesed to use and how should I start it?

By googling around I found that mongodb was renamed to mongod after version 2.4. Now that I was sure of the version I was supposed to use I could then enable the correct start script. If you have no startup script you can create one follwing this template:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --replSet singleNodeRepl --quiet --config /etc/mongodb.conf

[Install]
WantedBy=multi-user.target

To enable the script:

sudo systemctl enable mongod.service

this command creates a symlink to your service file:

mongod.service -> /lib/systemd/system/mongod.service
Installing ZSH and Oh-My-Zsh with my favorite theme: agnoster!

Installing ZSH and Oh-My-Zsh with my favorite theme: agnoster!

Hi guys,

In this small post I’m showing you how to install the ZSH shell interpreter and one of my favorite themes Agnoster. Firstly, the terminal of choice for this article (and also my personal choice) is Terminator. You can find it in any repository for most distributions.

In Ubuntu, these simple commands install Terminator:

sudo apt-get install -y terminator

Next, to install ZSH run the following command:

sudo apt-get install -y zsh

Once installed, the default terminal is automatically changed to zsh. If this is not the case, use the following commands to set it as default:

#first check the location
which zsh
/bin/zsh

#now change shell from /bin/bash to /bin/zsh
chsh
Password: 
Changing the login shell for root
Enter the new value, or press ENTER for the default
	Login Shell [/bin/bash]: /bin/zsh

Now if you restart your terminator instance it should open using ZSH! It’s now time to install Oh-My-Zsh.

Oh-My-Zsh is a github project framwork for managing your Zsh configuration. It includes 180+ optional plugins and more than 120 themes to spice up your journey. For complete information, please visit the official Git home page : robbyrussell – oh-my-zsh.

To install Oh-My-Zsh run the following command:

wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh

As I’ve mentioned, my favourite theme is “agnoster”. You can check out other themes at this URL.

To set up Oh-My-Zsh to use the agnoster theme just edit the ~/.zshrc configuration file (unique for each user) and add the name of the theme to the ZSH_THEME variable:

ZSH_THEME="agnoster"

Once changed the theme, specific Fonts have to be installed in order to fully support it:

#clone repo
git clone https://github.com/powerline/fonts.git
cd fonts
#start install script
sudo ./install.sh

And now, open Terminator and go under the Preference menu (right-click -> Preferences). Under Profiles you should find the General tab with the possibility to change the font. Replicate the one set up in the image here below:
terminalFont

The last step is to set up the syntax highlighting plugin:

#move to zsh plugin folder
cd ~/.oh-my-zsh/plugins
#clone repo
git clone https://github.com/zsh-users/zsh-syntax-highlighting
# enable plugin in zshrc
nano ~/.zshrc
#find the plugin and add syntax
+plugins=(git zsh-syntax-highlighting)

#source modif
source .zshrc

And there you go. End result should look something like this:

final_zsh

Enjoy!

iptables – add, remove and backup your rules!

iptables – add, remove and backup your rules!

Hi,

today, in this post, we are going to talk about iptables. It’s user-space application that allows to configure the tables provided by the Linux kernel firewall (implemented as different Netfilter modules) and the chains and rules it stores.

The first basic command to learn is the one allowing to list all rules in all chains, “iptables -L” or “iptables –list”. In iptables by default three different chain exist: INPUT, FORWARD and OUTPUT. Each one of these chains is related to an “action”, INPUT handles the rules for input connections and so on.

Empty iptbales rules listing:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Now let’s try to add a simple rule allowing ssh access only form a certain IP range:

iptables -A INPUT -s 10.0.0.0/8 -p tcp -m multiport --dports 22 -m comment --comment "010 ssh" -m state --state NEW -j ACCEPT

The meaning of each option is described here below:

  • -A : append the rule to a chain, in this case to the INPUT chain
  • -s : address/mask source specification (the source of the connection), in this case only 10.x.x.x addresses are allowed to use ssh
  • -p : the connection protocol used, in this case TCP
  • -m multiport: matches multiple ports
  • –dports : the destination port or port range
  • -m comment –comment : adds a comment to the rule
  • -m state : allow filter rules to match based on connection state, in this case NEW connections
  • -j : jump to the specified target, in this case ACCEPT (accept connection)

Instead if we want to remove the rule the -D option can be used:

iptables -D INPUT -s 10.0.0.0/8 -p tcp -m multiport --dports 22 -m comment --comment "010 ssh" -m state --state NEW -j ACCEPT

Another useful command is the one allowing the dump of all the rules:

sudo iptables-save > iptables-export.bak

All rules of all chains are written to the file, so that you can restore them later.

To make any change persistent after a reboot of the iptables service the “iptables-save” command has to be executed.

This concludes this small overview of iptables!

CYA,

Vincenzo

Writing files with dd

Writing files with dd

Hello,

like the SSH config post this is going to showcase two or three commands which I use very often when I’m in need of creating a file with a specific size.

To acheive this the command used on pretty much all Linux distribution is “dd”. dd is designed to copy blocks of data from an input file to an output file.

Let’s start with a few examples:

this first command is used to write a 1 Gb file using dd:

dd if=/dev/zero of=1g.img bs=1 count=0 seek=1G

The arguments are:

  • if= -> if the interface is present, this is the source of the data.
  • of= -> the name and path of the file to write
  • bs= -> the block size which is read each time
  • count= -> number of blocks to write
  • seek= -> skips n° blocks from the beginning of the file

Let’s look at another example, writing 10Mb:

dd if=/dev/zero of=10m.img bs=1 count=0 seek=10M
#or
dd if=/dev/zero of=10m.img bs=10M count=1 

The source can be changed depending on what kind of data you need:

  • /dev/zero : writes zeros
  • /dev/random : writes random data
  • /dev/urandom : writes more secure random data

One final thing to know about dd is that it can also be used to write images. For example, when you need to create a bootable USB with a Linux image. Let’s see how:

#first download your image...
wget http://cdimage.debian.org/debian-cd/8.5.0/amd64/iso-cd/debian-8.5.0-amd64-netinst.iso
#then write it to USB
dd if=/path/to/debian-8.5.0-amd64-netinst.iso of=/dev/sdbX

And that’s it! You have now a bootable USB with a Debian image.

Cheers,

Vincenzo

SCP – Transfering files with SSH

SCP – Transfering files with SSH

This is one thing that I don’t use that often but when I do I alwasy have to look for the syntax of the command on google. To avoid this step I’ve finally decided to document it in a small blog post!

If you want to copy a file from your pc to a remote machine use this command:

scp <file> <username>@<IP address or hostname>:<Destination>

This works well if you have to transfer only one file, but when a folder is involved recursion has to be used:

scp -r /folder/ <username>@<IP address or hostname>:<Destination>

And finally, to instead copy files from the remote machine to your laptop:

scp -r <username>@<IP address or hostname>:<Source> <Destination>

# for example, copying "files_to_copy" to current directory
scp -r vince@192.168.1.10:/home/vince/files_to_copy/ .

This concludes the blog post about SCP. Hopefully the next time I have to use it I’ll remember the synthax or at least know where to find it!

Peace out,

Vincenzo

Working with Fuel and Openstack (part 1)

Working with Fuel and Openstack (part 1)

Hello everybody,

during the last few weeks I’ve been working on Fuel, an open source deployment and management tool for OpenStack.

In this blog post I’m going to present Fuel, how it works, what it offers and I’m also going to talk about a few personal thoughts derived from my personal experience with Fuel.

What’s Openstack and what’s Fuel?

OpenStack is a free and open-source software platform for cloud computing. The software platform consists of interrelated components that control hardware pools of computing, storage, and networking resources throughout a data center. It basically allows the end user to spawn Virtual Machines on-demand. The image here below gives an overview of its architecture:

openstack-software-diagram
Openstack architecture (Compute, Networking and Storage)

Fuel, on the other hand, is a tool developed by Mirantis that provides a faster, less complicated and nicer way to deploy OpenStack environments. When installing a software plaform like OpenStack a lot of complicated and repetitive task have to be made, for example: the object storage, the identity manager, the networking services, the compute service etc. have to be configured and deployed. This makes deploying OpenStack a nightmare for many user. In order to solve this problem and also have a more scalable infrastructure, the Fuel software has been developed.

How does Fuel work?

Fuel is composed of a CentOS image which is called the Fuel Master. Iside this CentOS image there are 11 docker container (EDIT: Fuel v9 doesn’t use containers anymore), each one running a dedicated service such as: keystone for autenthication, niginx for the web GUI, postgres for the database etc. Thanks to these containers the Fuel Master, after having configured the correct networks, is able to add and remove “nodes” on the fly and without having to reconfigure the OpenStack environment. These “nodes” are of three types, Controller, Storage and Compute.

openstack_env
Fuel architecture

Controller nodes, which handles the orchestration activities and provides the OpenStack dashboard and the external API access. Compute nodes are used to create, manage and terminate Virtual Machines. And finally Storage nodes which are used to store the Virtual Machine data.

As mentioned before another Fuel functionality is the Network configuration. A total of four network have to be defined and configured in the Fuel Master. One is the Public network, which allows inbound connections to VMs (Controllers and Tenant VMs) from external networks (e.g., the Internet) as well as outbound connections from VMs to the external networks. The second one is the Storage network which is used to provide storage services such as replication traffic or migrating a disk. The third network is the Management which handles all nova-api and other api traffic. Thanks to this separation one can be sure that when doing operation on a VM the other traffic isn’t affected. Finally, the last one is the Private network which is used for communication between each tenant’s VMs.

 

This is the first part of a series of posts about Fuel. Stay tuned for the next parts!

SSH Config file

SSH Config file

Tired of writing hostnames and ports when using ssh? Then start using ssh config file!

I’ve recently found out that there is the possibility to define frequently used hosts in an ssh configuration file which get’s looked up each time you use ssh.

Here’s an example of a ssh config entry:

Host raspberry
    HostName 192.168.1.10 # hostname used by ssh
    User root
    IdentityFile ~/.ssh/rasp.key

#or

Host ftpserver
    HostName myhostname.domain.me
    User vince

With this configuration, connecting to the raspberry host is going to be as simple as:

ssh raspberry

Of course there are a lot of other options, one that I use frequently is the ProxyCommand, which allows you to specify the ssh command to be executed. In my case I use this when I need to connect to a host using a second machine, so for example : my_pc -> jump_machine -> final_destination. Also showed in the image here below:

 

sshConfig
Using ssh_config to bypass hosts

This can be achived with this set-up:

Host final_destination
    User vince
    ProxyCommand ssh -q -l myusername -W final_destination:22 jump_machine

So there you go! A complete list of possible options/attributes for your ssh config file can be found here.

Bye