Linux Admin Daily Usage

Published on Author gryzliLeave a comment

Shell

 

Calculate the size of all files from a given type/extension

Recently I need something for doing such calculations and end up with the following command line (found on stackexchange), which will calculate the size summary for all “*.jpg” files in “some_directory”.

find some_directory/ -type f -name '*.jpg' -exec du -ch {} + | grep total$

 

 

Using Linux moreutils 

Recently I found out the existence of moreutils . The package could be installed as an additional package in most of the linux distros from the standart repository. 

After installing it, you will get some really interesting programs accessible like:

 

Some nice examples about moreutils:

moreutils examples

 

 

Sponge – Redirect output from a file to the same file in bash

Many times I needed to do something like this in bash: 

# This won't do, what you expect it to 
cat some_file.txt | sed -re "s/change_this/with_that/g" > some_file.txt

But if you try to do this, you will end up with an empty file ! … So don’t do this ^^^^^^^ ! :) 

So here is where sponge can help, it is as simple as this: 

cat some_file.txt | sed -re "s/change_this/with_that/g" | sponge some_file.txt

# Or cleaner, without the `cat`
sed -re "s/change_this/with_that/g" some_file.txt | sponge some_file.txt

This works like a charm !

 

Some useful commands and bash tricks

For more extensive list of bash tips. take a look at my bash tips and tricks

# Replacing one string with another with replace 
# "replace" is part of the mariadb/mysql-server packages 

gryzli@localhost [~/temp]$ echo test | replace "test" "prod" 
prod

 

 

Managing Big Number Of Hosts Through SSH

The following software is a golden piece: 

pssh

pscp

prsync

Combining it with ssh multiplexing, you will have some serious tool in your pocket. 

 

Speeding up SSH connections to remote servers

Some tips for speeding up your SSH connections to remote servers, especially if you are frequently reusing them.

 

Disable DNS resolve on SSHD server

( This should be executed on the host you are connecting to)

In Centos, SSHD by default has dns resolving enabled, which tries to get the reverse dns record from the connecting ip.

Put the following line in your config to disable it:

open /etc/ssh/sshd_config

UseDNS no

Restart sshd server

systemctl restart sshd

/etc/init.d/sshd restart

 

Using SSH Multiplexing

Usefull technique when you connect to big number of servers and frequently re-connect to them.

Perfect for combining with pssh and pscp tools.

The idea of multiplexing is that once you open a ssh connection to a server, openssh will preserve your socket (until a timeout pasts) and every new connection to the same server will reuse the socket, which is very fast.

In order to use Multiplexing you need the following lines in your “~/.ssh/config” file

Host *
        ControlMaster auto

        # Where the sockets will be created (the FS should support sockets)
        ControlPath  /tmp/%r@%h-%p

        # Timeout in seconds
        ControlPersist  600

If you want to connect to some of the already connected hosts but with different settings (Xforwarding or -A for example), you could temporary disable ControlMaster for the new connection: 

# The '-S none' disables the re-use of already existing socket
ssh -S none root@example.com -A 

 

 

 

Usefull settings for ~/.ssh/config
Host *
# Use root as a default user
        User root
        AddressFamily inet
        Protocol 2 

# Compression
        Compression yes

# Disable strict host key checking
        StrictHostKeyChecking no

# Try always first with public key
        PreferredAuthentications=publickey 

 

Removing files which filenames are starting with “–“

# This won't work 
rm -vf '--filename' 
rm -vf "--filename" 

# This should work 
rm -vf ./--filename

 

Search for all files that DOES NOT contain a given string

# get files that doesn't contain string "EXCLUDE" 
grep -L "EXCLUDE" *.txt

 

Change MOTD on Centos to a colorful message

# vim /etc/profile.d/motd.sh 

...
...
#!/bin/bash

echo
echo
echo "##############################################"
echo "#                                            #"
echo -e "# This is \e[1m\e[32mMY_SERVER\e[0m #"
echo "#                                            #"
echo "##############################################"
echo
echo

 

Tools for working with Conntrack table

# Install conntrack-tools 
yum install conntrack-tools 


# List current conntrack table contents
conntrack -L

 

 

Nagios

Execute remote nrpe check

# Check the host 
/usr/local/nagios/libexec/check_nrpe -H 1.1.1.1

# Check some service on this host
/usr/local/nagios/libexec/check_nrpe -H 1.1.1.1 -c check_load

 

Dovecot

 

List parsed list of current Dovecot configuration

Sometimes you may need to parse Dovecot settings with external scripts. Going through the Dovecot config and it’s includes (+ nested syntax), could easily become automation hell.

One very helpful binary, that can help reading settings by other scripts/programs is “doveconf

# List all settings 
doveconf 

# List only non-default settings 
doveconf -n

# List settings in a machine parsable format (perfect for parsing by external scripts)
doveconf -S 

 

 

MySQL 

Repairing MySQL databases/tables 

Repair single database
mysqlcheck --check --extended --auto-repair DATABASE
Repair single table of a database
mysqlcheck --check --extended --auto-repair DATABASE SOME_TABLE
Repair all databases
mysqlcheck --check --extended --auto-repair --all-databases

 

Repair all crashed tables, getting them from mysql error log

# Get all rows for crashed tables for 2019-02-15 day
# - Remove the grep if you don't want to filter by date 
# - Update your mysql error.log path (/var/lib/mysql/server.err) 

grep '2019-02-15' /var/lib/mysql/server.err  | grep crashed | cut -d "'" -f2   > /root/for_repair.txt 



# Iterate through all db->table pairs and issue a repair for them 
for i in $(cat /root/for_repair.txt  | sed -re "s#\./##g"    | sort | uniq ); do b=$(echo $i | sed -re "s#/# #g" ) ;  mysqlcheck --check --extended --auto-repair  $b ;  done 

 

Usefull MySQL Queries

# Select all records which date is more or less than a given period

# Select all which is older than 1 year
SELECT * FROM My_Table WHERE date_field < DATE_SUB(NOW(),INTERVAL 1 YEAR) 

# Select all which is newer than 1 year
SELECT * FROM My_Table WHERE date_field > DATE_SUB(NOW(),INTERVAL 1 YEAR) 

# Select all for the last day
SELECT * FROM My_Table WHERE date_field > DATE_SUB(NOW(),INTERVAL 1 DAY) 

 

MySQL Hide Headers, Column Names And Formatting

By using the -N option, you can skip/hide the column/header information. 

mysql -N -e "select * from some_table" some_database

By adding the ‘-B’ option, you could also remove the column formattings: 

mysql -N -B -e "select * from some_table" some_database

 

SQLite 

Creating database in SQLite3

sqlite3 database.db

Creating tables in SQLite

# Enter the sqlite interface 
sqlite3 database.db

# Execute 
CREATE TABLE server (
    server_id  INTEGER primary key,
    name char(120) NOT NULL,
    ip char(20) NOT NULL,
    alias char(120),
    type char(40) NOT NULL
); 

 

Using Rowid As Primary Key Instead Of Autoincrement

In SQLite table, each row has internal identification number called “ROWID” and is 64 bit unsigned integer (unless you define your table with “WITHOUT ROWID” ). 

So you can use this ROWID instead of explicitly defining auto-increment field for primary key. 

In order to do it, you need to define your PK with ‘INTEGER primary key‘ : 

CREATE TABLE server (
    server_id  INTEGER primary key,
    name char(120) NOT NULL,
    ip char(20) NOT NULL,
    alias char(120),
    type char(40) NOT NULL
); 

It will behave much like auto-increment, but one key difference is that the ID that will be taken for the new record, is not “+1 on whatever existed” but rather “+1 on what currently exists“. 

So if you have records with id’s “1,2,3” and you add new, it will get “4”. But if you delete the row with id=4, and then again create new one, it will again take “4” as id. 

 

Usefull commands

Show current tables .tables
Quit from sqlite console .quit or .q
Turn headers on/off  .headers on|off
Check table structure (describe table) .schema table 

 Executing batch commands 

Execute sqlite batch commands, without the need of going into the interactive sqlite shell.

# List tables in servers.sqlite db 
qlite3 servers.sqlite ".tables" 

# Execute a select query
sqlite3 servers.sqlite "select * from server" 

# Execute multiple commands 
# This will execute both .headers on  and the select statement 
echo -e ".headers on \n select * from server " | sqlite3 servers.sqlite

 

Iptables 

 

Showing all current rules + interface + traffic counters 

iptables -L -n -v 

 

Adding comments inside iptables rules 

Comments are very very usefull thing inside iptables. They not only could hint you why this rule is there, but also could be used as UNIQUE IDENTIFIERS, for checking if a rule exists, before trying to add it again (which could save you from duplicate rules). 

iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.1.1 -m comment --comment "SSH connection from my pc" -j ACCEPT

You can add multiple comments: 

iptables -m comment --comment "ID_555" -t filter -I INPUT -s 192.168.6.6 -m comment --comment "My home router" -j ACCEPT

The line above will add 2 comments inside the rule. 

 

Using multiport module for adding multiple ports to a rule

The multiport iptables module is another very useful module to use. By default iptables gives you the ability to define port ranges easily with the ‘–dports’ flag like ‘–dports 100:200’.

But sometime you want a rule for multiple ports, which are not continuous as a range. 
Let’s add rule for ports 55 and 77: 

iptables -t filter -I INPUT -p tcp -m multiport --dports 55,77 -j ACCEPT

 

Protecting from iptables xtables lock error (Another app is currently holding the xtables lock)

When you invoke iptables, it first tries to acquire a xtables lock in order to give you consistent result. If you use iptables a lot in scripts (cron jobs), there is a big chance to hit this problem, why you try to execute some rule. 

The worse situation is if you have multiple cron jobs, and they are hitting the xtables lock without you even know about it.

That’s why –  Always check the execution status of your iptables commands. 

If you have newer version of iptables, you should be able to use the lock-safe wait option:

iptables -w -W 100 [...some arguments....] 

Yum

List packages with lower versions inside yum:

yum --showduplicates list httpd

Search for packages which provide some file by using “whatprovides

This could be used for both full path and wildcards.

yum whatprovides */bin/gcc

 

 

NetBeans

Make smooth fonts in Netbeans 8.2 under Linux (Fedora in my case).

Just run netbeans with the following additional options:

netbeans --laf Nimbus -J-Dswing.aatext=true -J-Dawt.useSystemAAFontSettings=lcd

 

AWK

Calculate the sum of a given column (from output). The current example calculates the sum for the first column.

# If your output looks like this
# 1
# 2
# 3
# ....

# You could calc the sum of it by: 
awk '{ sum += $1 } END { print sum } '

 

Leave a Reply

Your email address will not be published. Required fields are marked *