November 03, 2013

Useful Linux commands

Backup your data

$ SOURCEDIRS=".ssh .gnupg .gitconfig bin Documents Desktop logs stuff"
$ TARGETDIR="/media/external/BACKUP"

$ mkdir ${TARGETDIR}
$ cd ~/
$ du -smxc ${SOURCEDIRS}
$ rsync -avhpzx --progress --stats ${SOURCEDIRS} "${TARGETDIR}/"

Suspend the system

Lock the screen and suspend the system (to RAM)

$ gnome-screensaver-command -l && dbus-send --print-reply --system --dest=org.freedesktop.DeviceKit.Power --type=method_call --reply-timeout=6000 /org/freedesktop/DeviceKit/Power org.freedesktop.DeviceKit.Power.Suspend

(Alternative) Suspend to RAM

# cat /sys/power/state
freeze mem disk
# echo -n mem > /sys/power/state

Converting images

Resize images and strip EXIF data

arno@homepc:~/photos$ for i in $(ls); do convert $i -resize 2800x1800 -quality 80 -strip ${i}-converted.jpg ;done

Updating packages

When you updated your system packages check for deleted file descriptors and make sure that libraries loaded in memory are not old. If necessary, restart the service.

# lsof -n |grep DEL

Enable timestamp logging for the dmesg output

# diff /etc/rsyslog.conf.20121204 /etc/rsyslog.conf
38c38
< #kern.*                                                 /dev/console --- > kern.*                                                 /var/log/kern.log

# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

# cat /var/log/kern.log
Dec  4 09:37:31 hostname kernel: imklog 5.8.10, log source = /proc/kmsg started.

The yesterday’s date

$ date +"%Y%m%d"
20121204
$ TZ=XYZ+24 date +"%Y%m%d"
20121203
$ date --date='yesterday' +"%Y%m%d"
20121222
$ date --date='5 days ago' +"%Y%m%d"
20121218

Compiling mplayer in RHEL6

# svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
# cd mplayer
# svn update
(Optional) # ./configure --yasm='' --extra-cflags="-O3"
# ./configure --yasm=''
# cd ffmpeg/
# ./configure --disable-yasm
# cd ..
# make
# ./mplayer

SSH

Automatic ssh login without keys

# yum install expect
$ expect -c 'spawn ssh admin@server.com;expect password;send "Y0uRPassword\n";interact'

With ssh keys (in case when /etc/sudoers setup in use)

$ ssh-copy-id admin@server.com    # Do this once. This adds your ~/.ssh/id_rsa.pub to server's ~/.ssh/authorized_keys file
$ expect -c 'spawn ssh admin@server.com;expect admin;send "sudo su -\n";expect password;send "Y0uRPassword\n";interact'

Testing SSH port via SOCKS proxy

$ /usr/bin/nc -X 5 -x 120.70.70.77:1080 -vz 19.100.2.51 22
Connection to 19.100.2.51 22 port [tcp/ssh] succeeded!

Establishing SSH connection to a remote server behind proxy

# ssh -o "ProxyCommand /usr/bin/nc -X 5 -x 120.70.70.77:1080 %h %p" user@19.100.2.51

To make user SSH configuration, edit yours ~/.ssh/config config as follows.

# Customer1
Host 18.100.*
   ProxyCommand /usr/bin/nc -X 5 -x 120.70.70.77:1080 %h %p

# Customer2
Host 19.100.*
   ProxyCommand /usr/bin/nc -X 5 -x 120.70.70.77:1080 %h %p

After that you can directly connect remote servers behind the proxy, for example: ssh user@19.100.2.51

Disable screensaver/blank while watching a movie

When watching a movie you don’t want your screen to blank or screensaver to lock.

#!/usr/bin/env sh
# moviemode.sh
trap 'echo Exiting..; xset +dpms ; xset s on; exit' SIGINT SIGQUIT

set -x
xset -dpms
xset s off
gnome-screensaver-command -i

List those ZIP archives which have file(s) with the pattern we are looking for

# find /path/to/ -type f -name "*.zip" |xargs -I% sh -c '(unzip -l % *jpg 2>/dev/null|grep Pattern) && echo "ZIP: "%'

Append a file with dd

[root@rhel ~]# cp /etc/motd ./test
[root@rhel ~]# hexdump -C test |tail -5
000003e0  0a 0a 3d 3d 3d 3d 3e 20  54 65 78 74 20 6f 66 20  |..====> Text of |
000003f0  74 68 69 73 20 6d 65 73  73 61 67 65 20 69 73 20  |this message is |
00000400  69 6e 20 2f 65 74 63 2f  6d 6f 74 64 20 3c 3d 3d  |in /etc/motd <==|
00000410  3d 3d 0a                                          |==.|
00000413

[root@rhel ~]# dd if=/dev/zero of=/root/test ibs=1M count=10 obs=1M oflag=append conv=notrunc
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0208541 s, 503 MB/s

[root@rhel ~]# hexdump -C test |tail -5
00000410  3d 3d 0a 00 00 00 00 00  00 00 00 00 00 00 00 00  |==..............|
00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00a00410  00 00 00                                          |...|
00a00413
  • Metalink should be supported by server
$ wget -c http://download.documentfoundation.org/libreoffice/stable/4.1.2/rpm/x86_64/LibreOffice_4.1.2_Linux_x86-64_rpm.tar.gz.meta4 -O - 2>/dev/null|grep url

Different ways of checksums calculation

Besides coreutils, openssl also gives you a way to calculate various checksums (see ‘man dgst’)

Using OpenSSL

$ openssl dgst -md5 /etc/services
MD5(/etc/services)= 35435ea447c19f0ea5ef971837ab9ced

$ openssl dgst -sha256 /etc/services
SHA256(/etc/services)= f0c4af9261e7e6193db0bbb8f335c0f0c094d8d7aa428e5d8cbb674f2e13b2ef

Using perl

$ cat bin/md5sum.pl
#!/bin/env perl
use Digest::MD5;
use IO::File;

my $chk = Digest::MD5->new();

foreach my $file (@ARGV)
{
  $chk->addfile(IO::File->new($file));
  print "$file -> ",$chk->hexdigest,"\n";
}
$ md5sum.pl /etc/services /etc/motd
/etc/services -> 35435ea447c19f0ea5ef971837ab9ced
/etc/motd -> 7bd665b73c9f9f2938c561175e5f8db1

In AIX

$ csum -h MD5 /etc/services

Feel free to improve this article ! ;)

Ubuntu tips

Restore a network connection if gone

This happened to me after upgrading to Ubuntu 13.10

If you started your system after it was suspended and there seem to be no network after wake-up, then you should run following

root@homepc:~# nmcli nm status
root@homepc:~# nmcli nm sleep false

Adjust mouse speed on resume/thaw after sleep/hibernate

root@homepc:~# cat /etc/pm/sleep.d/20_mouse-speed
#!/bin/sh

#
# Adjust mouse speed on resume/thaw after sleep/hibernate.
# Should work for all active users in the system using X.
#

case $1 in
     resume|thaw)
        /usr/bin/who |/usr/bin/sort -u -k1,1 |/usr/bin/awk '{print $1, $NF}' | /usr/bin/tr -d ')''(' |while read USER DISPLAY; do /bin/su ${USER} -c "(sleep 3 && DISPLAY=${DISPLAY} /usr/bin/xset m 1) &" ; done
     ;;
esac

Ubuntu, black screen while using SSD

SSD boosts system startup, however lightDM needs a little while to start properly in such case. Below is the workaround, just add “sleep 2” or more seconds to make sure that you won’t have black screen while booting Ubuntu 13.04.

root@homepc:~# diff -u /etc/init/lightdm.conf.arnobkp /etc/init/lightdm.conf
--- /etc/init/lightdm.conf.arnobkp	2013-04-23 11:10:26.000000000 +0200
+++ /etc/init/lightdm.conf	2013-08-04 18:14:39.105140142 +0200
@@ -43,6 +43,7 @@
 	fi
     fi

+    sleep 2  # workaround for SSD disk so that no black screen experienced. http://www.youtube.com/watch?v=402YWC2LkjA
     exec lightdm
 end script