Start GUI program on the remote machine using your local display through ssh

This article is probably not only for absolute beginners, so many Linux guys do not know they can just execute a GUI program on their remote machine but the output of the graphics to be drawn in the local display, it’s like your local display is connected to the remote computer through the network. And you do not need any special software like VNC Client/Server, TeamViewer – all you need is the OpenSSH package you probably have when managing a remote machine. And it is absolutely secure because all the communication is performed within ssh.
Suppose you have a machine, which has some GUI capabilities (you know if we speak for servers, not all servers need the GUI, but in some cases we need it) and you want to execute a graphical program on the remote.
Here is how you can login to be sure you can execute the program remotely:

ssh -o ForwardX11=yes -o ForwardAgent=yes -o ForwardX11Trusted=yes [USER]@[IP]

And you can also execute a remote command with the ssh:

ssh -o ForwardX11=yes -o ForwardAgent=yes -o ForwardX11Trusted=yes myuser@192.168.0.245 'virtualbox'

This command will execute virtualbox on your remote machine with login myuser@192.168.0.245 and the output the display on your local machine.

Here are some examples. Gentoo Desktop using KDE with konsole get to the remote machine on our local network 198.160.245:

SCREENSHOT 1) Login in your remote machine and there type firefox. The firefox will be executed on the remote machine, but the graphics will be output on your local display.

main menu
Remote execute Firefox through ssh

Keep on reading!

List all kernels available to install under Ubuntu

Here is the fast command to see which Linux kernels are available to install from the Ubuntu package management system:

apt update
apt list linux-*image-*

Always update to download the latest metadata files for the available packages and the list with a pattern. In our case we want the Ubuntu kernels and they start with “linux-“, before it was easy linux-image-*, but now there are two types of kernels:

  1. Signed Linux kernel with name starting “linux-signed-image-” (and alias is “linux-image-“) – “Signed with the Ubuntu EFI key”, which means you should have UEFI BIOS and unsigned (with two words – “more security”)
  2. Good old kernel format (so no signing) with name starting “linux-image-unsigned-” – to be sure you pull the unsigned version

And you can use:

apt search linux-*image-*

to see the kernels available and their descriptions.
Keep on reading!

Missing pkg-config results in configure error: possibly undefined macro

This small article is for you who wonder why a configure script under Linux failed with an error like:

configure.ac:204: error: possibly undefined macro: AC_MSG_ERROR
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:216: error: possibly undefined macro: AC_CHECK_LIB
configure.ac:328: error: possibly undefined macro: AC_CHECK_HEADER
autoreconf: /usr/bin/autoconf failed with exit status: 1
 FAILED 

So autoreconf failed with missing macros, but you just saw you had the latest version and no clue what is wrong with the code you tried to compile!
In most cases the problem is trivial – you are missing the

pkg-config

It is a helper tool used when compiling. It helps to have the correct compiler options and not to hard-coded them in your files. So search for this tool in your Linux distro and install it.

Under CentOS 7

yum install pkgconfig

Under Ubuntu/Debian

apt-get install pkg-config

Under Gentoo

emerge -v dev-util/pkgconfig

Close socket as if the remote closed the connection

If you have a hung process and it happened to be in this state because of the network, for example your client or server program is in read timeout state, you can use

lsof and gbg

to close the network socket simulating the other (remote) end closed it and the process will continue operating normally.

In our case there is a couple of nrpe process hung in read from a network socket:

[root@srv ~]# lsof -n -p 9948
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF    NODE NAME
nrpe    9948 nrpe  cwd    DIR                9,2     4096       2 /
nrpe    9948 nrpe  rtd    DIR                9,2     4096       2 /
nrpe    9948 nrpe  txt    REG                9,2    69960 1053396 /usr/sbin/nrpe
nrpe    9948 nrpe  mem    REG                9,2    62184 1053312 /usr/lib64/libnss_files-2.17.so
nrpe    9948 nrpe  mem    REG                9,2   402384 1051943 /usr/lib64/libpcre.so.1.2.0
nrpe    9948 nrpe  mem    REG                9,2   155784 1057231 /usr/lib64/libselinux.so.1
nrpe    9948 nrpe  mem    REG                9,2   144792 1051919 /usr/lib64/libpthread-2.17.so
nrpe    9948 nrpe  mem    REG                9,2   106848 1053314 /usr/lib64/libresolv-2.17.so
nrpe    9948 nrpe  mem    REG                9,2    15688 1051678 /usr/lib64/libkeyutils.so.1.5
nrpe    9948 nrpe  mem    REG                9,2    58728 1051843 /usr/lib64/libkrb5support.so.0.1
nrpe    9948 nrpe  mem    REG                9,2    90664 1051808 /usr/lib64/libz.so.1.2.7
nrpe    9948 nrpe  mem    REG                9,2    19776 1053308 /usr/lib64/libdl-2.17.so
nrpe    9948 nrpe  mem    REG                9,2   210840 1051701 /usr/lib64/libk5crypto.so.3.1
nrpe    9948 nrpe  mem    REG                9,2    15920 1051682 /usr/lib64/libcom_err.so.2.1
nrpe    9948 nrpe  mem    REG                9,2   963576 1051755 /usr/lib64/libkrb5.so.3.3
nrpe    9948 nrpe  mem    REG                9,2   320408 1051956 /usr/lib64/libgssapi_krb5.so.2.2
nrpe    9948 nrpe  mem    REG                9,2  2173512 1051792 /usr/lib64/libc-2.17.so
nrpe    9948 nrpe  mem    REG                9,2    42520 1051997 /usr/lib64/libwrap.so.0.7.6
nrpe    9948 nrpe  mem    REG                9,2   117680 1053310 /usr/lib64/libnsl-2.17.so
nrpe    9948 nrpe  mem    REG                9,2  2512832 1051648 /usr/lib64/libcrypto.so.1.0.2k
nrpe    9948 nrpe  mem    REG                9,2   470360 1051690 /usr/lib64/libssl.so.1.0.2k
nrpe    9948 nrpe  mem    REG                9,2   164240 1049135 /usr/lib64/ld-2.17.so
nrpe    9948 nrpe    0r   CHR                1,3      0t0    1028 /dev/null
nrpe    9948 nrpe    1w   CHR                1,3      0t0    1028 /dev/null
nrpe    9948 nrpe    2w   CHR                1,3      0t0    1028 /dev/null
nrpe    9948 nrpe    3u  unix 0xffff961d48d37000      0t0   19091 socket
nrpe    9948 nrpe    6u  IPv4          261850576      0t0     TCP 10.10.10.10:5666->10.10.10.254:39056 (ESTABLISHED)

As you can see the FD column shows the File Descriptor number of the opened file (network resource here) and you can use it with

gdb

to simulate closing the network socket as if the remote close it but from the same machine.

[root@srv ~]# gdb -p 9948
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Attaching to process 9948
Reading symbols from /usr/sbin/nrpe...Reading symbols from /usr/sbin/nrpe...(no debugging symbols found)...done.
(no debugging symbols found)...done.
....
....
Loaded symbols for /lib64/libpcre.so.1
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libnss_files.so.2
0x00007f91e8295c70 in __read_nocancel () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install nrpe-3.2.0-6.el7.x86_64
(gdb) call shutdown(6, 0)
$1 = 0
(gdb) quit
A debugging session is active.

        Inferior 1 [process 9948] will be detached.

Quit anyway? (y or n) Y
Detaching from program: /usr/sbin/nrpe, process 9948

Just call

call shutdown(FileDescriptorID, 0)

and quit the gdb. In our case the FileDescriptorID is 6, so we executed

call shutdown(6, 0)

And the network socket between this machine and the remote one will be terminated, so the process nrpe could continue its execution.
Of course, in your cases you can look for a specific network connection among many other, but lsof is the tool you can use to identify the connection and the right file descriptor number to use in gdb.

Delete files in a directory with checking for the free space with find and stat command – effective and fast approach

If you have a big storage for let’s say your cache proxy and you may want to delete some files fast you could use

find

linux command to delete files. There are plenty examples in the Internet how to do it but many of them use pipes and sorts or other manipulation of the output, which could require the whole output before running some of the piped commands and if you have millions of files you could wait for an hour or two before the command to run and then to see it is not efficient for you case!
So we need a command (or commands), which begins to delete files immediately with some sort of simple selection. We can use linux command “find” with the most used options like

  • “-type f” – only files
  • “-mtime +/-N” – only files older or newer than N days. “-mtime +5” – files or directory older than 5 days and “-mtime -5” files or directory newer than 5 days
  • “-name ‘*.jpg'” – files or directories with patterns in the name *.jpg, for example “sample.jpg”

So we’ll use

  1. “find” to delete files
  2. and a while cycle periodically to check for the free space
  3. and if the free space gets greater than we would like we will kill the find command.

This approach is probably the most effective one, because we run only once the “find” command – if you have multiple runs of the find command in directory with many sub-directories it will check them on every execution in the same order – you may imagine what is like to have hundreds of thousands sub-directories full of files!

The goal is to execute only one find command and to stop it when we reach the desired free space!

So here an example:

echo "STARTING WITH /mnt/cache/"
find /mnt/cache/ -type f -name '*.jpg' -mtime +60 -delete &>/dev/null &
PID=$!
stime=60

while kill -0 "$PID" >/dev/null 2>&1; do
    FREESPACE=$(($(stat -f --format="%a*%S/1024" "/mnt/cache/")))
    if [[ $FREESPACE -gt 50000000 ]]
    then 
        kill "$PID"
        break
    fi
    echo "SLEEPING FOR $stime"
    sleep ${stime}s
done
echo "TERMINATING"
exit 0

The above piece of bash code will find in /mnt/cache/ only files with names ‘*.jpg’ and older than 60 days and will delete them. In parallel we check if the find command is still executing (because it could return finding nothing or just a small amount of files) and sleep for a 60 seconds. If there is 50000000 kylobites (50Gbytes) it will kill the find command!
Someone would argue we could have used

timeout command,

but this will kill “find” every time the check time passes and on every execution of find there will be the same files to check from the previous run!!! And if the first “find” command passed 10000 files and deleted 100, the second time it will check again this 9900 not deleted files and then will continue with the new one and if you execute it again and again you could enter in a endless loop, because the time is used only for checking the files previously checked and not deleted.

Here is the bash script with two parameters for the command line:

#!/bin/bash

if [ "$1" == "" ]
then 
        echo "USAGE: <script> <path> <time=60s>"
        exit 0
fi
if [ "$2" == "" ] || [ "$2" == 0 ]
then
        stime=60
else
        stime=$2
fi

sleep ${stime}s

echo "STARTING WITH $1"
find "$1" -type f -delete &>/dev/null &
PID=$!

while kill -0 "$PID" >/dev/null 2>&1; do
    FREESPACE=$(($(stat -f --format="%a*%S/1024" "$1")))
    if [[ $FREESPACE -gt 50000000 ]]
    then 
        kill "$PID"
        break
    fi
    echo "SLEEPING FOR $2"
    sleep ${stime}s
done
echo "TERMINATING"
exit 0

Check for the missing two parameters. The second parameter is not mandatory and the default value is 60s. Probably you can tune the script for your needs:

  • the find commands to look for specific files with name/mtime/type and so on.
  • third parameter in the command line to set minimum free space to check.
  • the time of the second parameter to be passed with the argument for s=seconds, m=minutes, h=hours and so on.

Replace an old IP with new one in all files of all sub-directories recursively

Here is a quick Linux tip for those who want to replace their old IP with new one for all files in a given directory and all its sub-directories recursively:

find [path-to-directory] -type f -print0 | xargs -0 sed -i 's[old-IP-escape-dot]/[new-IP]/g'

Quick example:

find /etc/nginx/ -type f -print0 | xargs -0 sed -i 's/192\.168\.10\.124/10.10.10.224/g'

As you can see the directory is “/etc/nginx” and replace it with the directory where are your (configuration) files are. We are replacing old IP=192.168.10.124 with the new one 10.10.10.224, so after the execution of the above line you’ll get modified files with IP 10.10.10.224.
You must escape the dot “.” in the IP!

Send coins in Ethereum network with geth console

Here are the steps needed to make a secure transfer of your funds under linux distro of Ubuntu. To be able to transfer money safely without any 3rd party involved (no online wallet) just funds from your personal Ethereum wallet address on your computer to another Ethereum wallet address we need the official command line interface for running a full ethereum node:

geth

STEP 1) Install and run an Ethereum node with geth

Here you can follow this tutorial – “Running an Ethereum node from source under Ubuntu 16 LTS”.
The Ethereum node must be running and synced with the Ethereum network.

STEP 2) Attach to the console of geth tool

ubuntu@srv.local:~/go-ethereum/build/bin$ ./geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.2-unstable-b574b577/linux-amd64/go1.9.4
coinbase: 0x30d8810bb1a74e808b46788b2316bd93cf056517
at block: 5172179 (Wed, 28 Feb 2018 15:59:17 UTC)
 datadir: /home/ubuntu/.ethereum
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

STEP 3) Check how many funds you have in your Ethereum wallet address

While you are in the geth console paste the following:

> web3.fromWei(eth.getBalance('30d8810bb1a74e808b46788b2316bd93cf056517'), "ether")
0.105
> 

You can see there are 0.105 Ethers in the Ethereum wallet address 30d8810bb1a74e808b46788b2316bd93cf056517, replace the address with your public address.

STEP 3) Unlock your account

As you remember your wallet private key is encrypted with a password, so before you can use the private key to send funds you must decrypt the key with:

> personal.unlockAccount('30d8810bb1a74e808b46788b2316bd93cf056517', '<your_password>',300)
true

So your key will stay dectrypted for 300 seconds, this is the purpose of the third parameter. It can be omitted.

STEP 4) Send the amount you like to the Ethereum wallet address you like

, BE CAREFUL the operation is irreversible, if you put wrong address or unknown or unexciting one, you’ll lose your Ether coins!!
Every transaction has a cost, which is computed by the miners. That’s why the gas price is dynamic. The price is determined by the miner of a block and it can change from block to block. You can check the gas price here on http://ethgasstation.info. Determining and computing the price in Ether is very difficult there could be at least 3 new howtos about that. The best and the easiest way is just to check a site (like http://ethgasstation.info) for the average gas price in dollars and when executing a transaction of sending Ethereum funds make sure the same amount of Ether in dollars will stay in your wallet address to be used for the gas.

> eth.sendTransaction({from: '30d8810bb1a74e808b46788b2316bd93cf056517', to: '9009a960fb38c379813a4a2e26c5b94909dc8599', value: web3.toWei(0.104, "ether")})
"0x31b4f213f88f2831756bd9cc37b12e80c6dfe413a3b3423659ca91be352961fc"

If you see the transaction ID, it means the you have executed the command successfully and you can check the status of the transaction.

STEP 5) Check the transaction status.

The status of your transaction could be checked in sites offering Ethereum block explorer sites like

https://etherscan.io/

Here is the check URL for our transaction:

https://etherscan.io/tx/0x31b4f213f88f2831756bd9cc37b12e80c6dfe413a3b3423659ca91be352961fc

Here is the screenshot of the status page:

main menu
Transaction details in https://etherscan.io

And verify the other address has the coins:

ubuntu@ip-172-31-30-78:~/go-ethereum/build/bin$ ./geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.2-unstable-b574b577/linux-amd64/go1.9.4
coinbase: 0x30d8810bb1a74e808b46788b2316bd93cf056517
at block: 5195693 (Sun, 04 Mar 2018 15:40:58 UTC)
 datadir: /home/ubuntu/.ethereum
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> web3.fromWei(eth.getBalance('9009a960fb38c379813a4a2e26c5b94909dc8599'), "ether")
0.104
>

And how many coins remained at “30d8810bb1a74e808b46788b2316bd93cf056517”:

> web3.fromWei(eth.getBalance('30d8810bb1a74e808b46788b2316bd93cf056517'), "ether")
0.000811

So as you see the Gas was 0.000189 Ethers as you can see in the block explorer page.

Here is the whole output of the “geth” tool:

> ubuntu@ip-172-31-30-78:~/go-ethereum/build/bin$ ./geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.2-unstable-b574b577/linux-amd64/go1.9.4
coinbase: 0x30d8810bb1a74e808b46788b2316bd93cf056517
at block: 5193986 (Sun, 04 Mar 2018 08:48:41 UTC)
 datadir: /home/ubuntu/.ethereum
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> web3.fromWei(eth.getBalance('30d8810bb1a74e808b46788b2316bd93cf056517'), "ether")
0.105
> personal.unlockAccount('30d8810bb1a74e808b46788b2316bd93cf056517', '<the_password_of_the_address>',300)
true
> eth.sendTransaction({from: '30d8810bb1a74e808b46788b2316bd93cf056517', to: '9009a960fb38c379813a4a2e26c5b94909dc8599', value: web3.toWei(0.104, "ether")})
"0x31b4f213f88f2831756bd9cc37b12e80c6dfe413a3b3423659ca91be352961fc"
> web3.fromWei(eth.getBalance('30d8810bb1a74e808b46788b2316bd93cf056517'), "ether")
0.000811
> 

network time synchronization status with ntpq

There is a handy program called

ntpstats

, which shows the network time synchronization status of your server under Ubuntu 16, but the older version or other linux distros do not have this program. The output of the program is:

srv@local # ntpstat
synchronised to NTP server (129.250.35.250) at stratum 3 
   time correct to within 133 ms
   polling server every 1024 s

As you can see the time is correct within some interval of milliseconds. In other words your time in the server could be trusted only within these milliseconds! If your server is synchronized with the program ntp your time of the server will be correct within a tiny fractions of a second, closer to a time server much smaller interval of correctness. The interval is computed according to how away the server is and how much the latency of the network of the server is.
ntpstats compute the output with the formula:

rootdisp + rootdelay / 2.0

  • rootdisp – indicating the maximum error relative to the primary reference source. If your server gets the time from an external source (GPS, radio, high precise clock external device), this value is the estimated maximum error of that clock and if your server gets the time from another NTP server (in most cases), this value is the current NTP server’s root dispersion (to which your server is synced) plus the dispersion added by the network link between your server and the NTP server
  • rootdelay – indicating the total roundtrip delay to the primary reference source. Because it is a roundtrip we divide it to 2 to get the single trip delay of the network

You can get these two values with the ntpq program:

srv@local # ntpq -c "rv 0 rootdisp,rootdelay"
rootdelay=202.882, rootdisp=31.816

So the time correct to within 31.816+(202.882/2) = 133.257 ms, which is the output of ntpstat.

Delete millions of files slowly without loading the server

There a situations when we need to delete a great deal of files from our filesystem and if we just execute

rm -Rf

the server will surely get loaded and the service it provides will degrade! What if you cannot reformat the filesystem, because the server use it extensively, but you need to delete let’s say a couple of millions file from it? We can use find and usleep (in most linux distro this program is installed by an additional package). The idea is to delete files one by one tuning the pause between every delete. Here you can execute this command in the background or a screen:

find /mnt/storage/old/ -type f -exec echo {} \; -exec rm {} \; -exec usleep 200000 \;

usleep accepts microseconds, so 200000 microseconds are 0.2 seconds. You can tune it precisely with a step of just a microsecond. In the real world under the bash console we probably will use values of max 1/10 of a second around above 100000 microseconds. Execute the command and then watch your server load and tune.

  • usleep in CentOS 7 is installed with package “initscripts”, which is installed by default
  • usleep in Ubuntu is missing and probably won’t find any safe place to download a package to install, but it can be sort of replace with “sleep <floating_point_number>s”, GNU sleep could accept floating point number for the delay and when added “s” at the end it could sleep for a fractions of a seconds. So the command for the Ubuntu is slightly changed:
    find /mnt/storage/old/ -type f -exec echo {} \; -exec rm {} \; -exec sleep 0.2s \;
    
  • not GNU version of sleep require NUMBER, so the smallest sleep is only 1 second, which is too big for the purpose. Check your man manual to see if your system has GNU sleep command.

Avoiding Esc (escape) key, a key combination to simulate it

Writing the last howto there was an idea: how to simulate the ESC key, when the key is not available in our keyboard, which is very often these days. Mobile traffic is the ruler coming from mobile devices, of course! So why to limit console users (probably mainly admins?) only to desktop or “qwerty” keyboards? The tables could weight under 500 grams and event smartphones could be used to ssh to a server and what if you do not have ESC key, but need to use it? You can try the following combination:

The escape key can be replaced with pressing and holding CTRL and pressing “[” (left square bracket) = “CTRL+[”

So next time when trying to use ssh or console under Android or iOS or any device try this combination at lease it could be faster than selecting a menu for a predefined keys like ESC key.