Étiquette : linux

Linux : make an USB bootable thumb with an ISO file

you downloaded the iso of some distrib and you want to make a flash thumbdrive bootable by writing the ISO on the thumb ,

a simple copy and paste is not going to work :

On linux , there is a very simple command line utility to do just that.

first plug your usb thumbdrive on your computer , it will probably automount.

you need to unmount that drive before continuing.

to find out the name of thumbdrive , you can type :

mount

You will get a result that look like that

/dev/sda1 on /media/flash type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=continue)

my flash drive is mounted at /media/flash  the name of the device is /dev/sda

to unmount the drive , jsut do a :

umount /media/flash

then you need to

dd bs=4M if=debian-8.7.1-amd64-netinst.iso of=/dev/sda

after a little while , the utility will exist

your thumbdrive is now a bootable thumbdrive.

Using sed to delete a word from a string

For example in a bash script you need to delete one or more word from the string , you can use sed to do just that

 sed -e "s/word to delete//g"

sed is searching for « word to delete » and replacing it with nothing ,

you can of course pipe to that command ,

example :

the var $visitor contain the string : ‘current number of users: 234’

But only the number (234) is usefull to you ,

your can do :

echo $visitor | sed -e "s/current number of users: //g"

the result is going to be : 234

 

Renewing your IP address on Linux with dhclient

Let’s say you need to change you ip address because there was a change on the network and now ip settings changed,
you can at anytime request with the command : ‘dhclient‘ a new address to a dhcp server ,

if you only have a single network adapter, the command is very simple
just launch the command

dhclient -r

to release the address currently associated to you
then just launch

dhclient

and you computer will broadcast a dhcp request
if you have multiple adapter ,
you can do

dhclient -r wlan0
dhclient wlan0

to spécify that you want a new ip address for you wlan0 adapter
by default the command dhclient is silent , meaning that you will not get any information about what the dhcp server answered.
to add verbosity to the command , just add a -v parameter

dhclient -r -v wlan0
dhclient -v wlan0

Linux epoch time or Unix time


If you need a convenient way to store the date in a database you will certainly use the unix format.
it’s very convenient because it doesn’t change notation from country to country. 

You can later convert it back to a format that’s human readable.

in a shell on linux if you wish to display the current unix time. just type

date +%s

But if you need the unix time for a specific date ,

date –d=’03/14/2016 00:00:01′ +%s
or
date –d=’14 march 2016 00:00:01′ +%s

There is also the function to input an interval

date –d ‘1 hour ago’ +%s
date –d ‘1 month ago’ +%s

 

Grep a line before or after a match in bash

Sometime when you grep a file on Linux you need to not only display the line where the result is found but also several lines before or after the match.

Grep as a useful function permitting you to do just that.

Let’s take an example file.

file.txt:

Title : login issue
Ticket number : 16417
Details : user password is no longer working.

Title : Broken keyboard
Ticket number : 16418
Details : the space key on the user keyboard no longer work

 

If the file is list of hundreds of this informations and you need to see only the information related to ticket 16417 you need to

cat file.txt|grep 16417

Will only return the result Ticket number : 16417  living out the useful informations

If you need to display a line after the grep match

cat file.txt| grep -A1 16417

will return :

Ticket number : 16417
Details : user password is no longer working.

But there is also useful information before  the match

You can associate the 2 options to display the line after and before

cat file.txt| grep -A1 -B1 16417

this will return all the information i need :

Title : login issue
Ticket number : 16417
Details : user password is no longer working.

Mount a network share on linux

On linux you have the ability to mount network share as they were part of your local storage ,
On debian , you need to have a package named cifs-utils specialised to mount network share.

after installation of the package using the command :

apt-get install cifs-utils

You have to create a folder to attach the mount to

mkdir /media/netshare
mount -t cifs "//host-of-the-share/disk/" /media/netshare -o guest,iocharset=utf8,file_mode=0777,dir_mode=0777

if you have to identify yourself to use the network share the command is slightly different :

mount -t cifs "//host-of-the-share/disk/" /media/netshare -o user=username,iocharset=utf8,file_mode=0777,dir_mode=0777

the system should ask you for your password

to check if you mount was successfull , you can check the mount using

df

Change the default SSH port on your linux server

If you run a linux server with with a ssh service on the TCP port 22 , you are likely to get bombarded by automated login attempt by hundreds and hundreds of bots
You can look at the file  getting created by launching

tail -f /var/log/auth.log | grep Failed

the first thing you can do to stop that is to change the default port user by the SSH service on you linux server

for this you just have to edit the config file : /etc/ssh/sshd_config

Find the following lines

 # What ports, IPs and protocols we listen for
Port 22

And put the new port on which you would like to run your OpenSSH server.

After this little change restart the ssh service with the command

service ssh restart

You should see that the numerous and constant attacks sould instantly stop.

Security by obscurity is still security.

 

 

 

 

add custom metrics in influxDB

 

If you have a influxdb server running , there is some data collector like Collectd that can be used to automatically write data in your database.
But collectd is very difficult to personalise. and the documentation is pretty bad, (try using the curl plugin and greping data)
You can’t easily add the result of a piped bash command to the collected data for example.

in my case i’ve decided to ignore these pre made solutions that never completely satisfy my needs or write exactly the value i want in the database,

there is a simple way to use a http request to write in the influxDB database.
first before you even write something in ingluxDB, you have to create a database (you only have to launch this command once)

curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE metrics"

then lets say i want to log every 5min the disk usage in percent of my primary storage device. which is named /dev/mmcblk0p1

the df command is only giving me a rounded value : for example 22%

for my example i want a much more precise percentage ,  this little script does exactly that.

#!/bin/bash
#change mmc by the name of your drive , ex: sdba
df_total=$(df |grep mmc |awk -F' ' '{print $2}')
df_used=$(df |grep mmc |awk -F' ' '{print $3}')
df=$(echo "$df_used * 100 / $df_total" | bc -l | head -c 6)
curl -i -XPOST 'http://localhost:8086/write?db=metrics' --data-binary "disc_space,df=used value=$df"

this is writing in the database metrics the percentage used with 3 decimals if you are using 10% or more and 4 decimals if you use 10% or less
the changing precision is not a concern in my use case.

then if you want a value every 5min for example do a crontab -e  and add the line :

*/5 * * * * bash /root/myscript.sh

 

you can then use grafana for example to graph the data.

install Grafana 4 on a PI

If you have a OrangePi or a raspberry pi ,

there is high chance that the version of grafana package in the repositories of your distribution is outdated
in my case , i use armbian , the repo is from ubuntu xenial .

the version of Grafana in the xenial repo is antique (grafana 2.6.0) and the version is bugged if you use a Headless version of the os ,
the icon are not displayed when you are connected to the grafana interface. (because of a bug in phantomJS).

if you have already the 2.6.0 runing , remove it first

apt-get remove grafana

Then use this page to download a recent version of grafana compiled to run on a arm cpu

wget https://github.com/fg2it/grafana-on-raspberry/releases/download/v4.1.1/grafana_4.1.1-1484347088_armhf.deb
then 
dpkg -i grafana_4.1.1-1484347088_armhf.deb

if the grafana service is not auto starting at the system boot , you can launch that command :

systemctl enable grafana-server.service

you can check with netstat that your grafana service is running if your server is listening on the tcp port 3000

netstat -tcp -l

now start your browser and try to connect to http://ip-of-your-server:3000

 

Orange PI Zero : Control the onboard leds

 

The Orange PI zero is equipped with two onboard leds ,

 

One red , and one green ,
armbian give you the ability to control very easily the state of theses leds.

to turn on the red led you can use this command : echo 1 > /sys/class/leds/red_led/brightness
to turn off the red led you can simply launch : echo 0 > /sys/class/leds/red_led/brightness

to turn on the green led you can use this command : echo 1 > /sys/class/leds/green_led/brightness
to turn off the green led you can simply launch : echo 0 > /sys/class/leds/green_led/brightness

You can put some sleep command between the on and off, and use this functionality to notify you for example that a cron script is running.