Thursday, November 30, 2023

Draws ASCII Shapes Around Text in Linux Terminal

Boxes is an application to draw boxes and shapes around text in linux terminal. For those who cannot imagine, below is one example of how boxes is used:







To install boxes, simply run below command in any debian like linux distro:
$ sudo apt update && sudo apt install boxes -y

To use it, simply echo any text and pipe it to boxes
$ echo "this is linuxwave" | boxes

Boxes comes with 59 designs by default. We can list all the styles by typing:
$ boxes -l

To use the designs, simply append -d flag, and provide the name of the design. For example, to use unicornsay style:
$ echo "this is linuxwave" | boxes -d unicornsay

And you will get something like this













As usual, to know more about a command, simply use man command
$ man boxes

Monday, November 20, 2023

Install clustercontrol v2 on ubuntu 22.04 using podman

Clustercontrol is a database cluster management system, developed by severalnines.com, that ease up the work of database cluster deployment and management, using a nice web interface. Please follow below steps to install clustercontrol v2 using podman.

First, install podman
$ sudo apt update && sudo apt install podman -y

Configure podman registries. Add below lines into the end of /etc/containers/registries.conf
[registries.search]
registries = ['docker.io']

Pull the latest clustercontrol image
$ podman pull severalnines/clustercontrol

Create necessary directories
$ mkdir -p clustercontrol/{backups,cmon.d,cmonlib,datadir,prom-conf,prom-data,sshkey}

Create ssh key, and save them into clustercontrol/sshkey
$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/myuser/.ssh/id_ed25519): /home/myuser/clustercontrol/sshkey/id_ed25519    
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
...

Start clustercontrol
$ podman run -d --name clustercontrol \
-h clustercontrol \
-p 5000:80 \
-p 5001:443 \
-p 9443:9443 \
-p 19501:19501 \
-e DOCKER_HOST_ADDRESS=192.168.10.10 \
-v $PWD/clustercontrol/cmon.d:/etc/cmon.d \
-v $PWD/clustercontrol/datadir:/var/lib/mysql \
-v $PWD/clustercontrol/sshkey:/root/.ssh \
-v $PWD/clustercontrol/cmonlib:/var/lib/cmon \
-v $PWD/clustercontrol/backups:/root/backups \
-v $PWD/clustercontrol/prom-data:/var/lib/prometheus \
-v $PWD/clustercontrol/prom-conf:/etc/prometheus \
severalnines/clustercontrol

Create username, and set password for the user
$ podman exec -it clustercontrol bash
# s9s user --create --generate-key --controller="https://localhost:9501" --group=admins myuser
# s9s user --change-password --new-password=anypassword myuser

Open a web browser, and browse to the ip address of the server with port 5001 for https























Login using the user and password created above


Friday, November 10, 2023

Using psql without entering password

Sometimes, we need to run psql command without entering the password, even though the account is protected with password. The usual situations are, when we are running the psql command in a script, or we have to constantly monitor the output of psql commands using watch. Here is the method on how to achieve that.

Create a .pgpass file inside the user's home directory who's going to access psql without password

$ touch ~/.pgpass

Follow below format to add the user's details into pgpass

hostname:port:database:username:password

$ echo "10.10.10.10:5432:mydatabase:myuser:mysuperlongpassword" > ~/.pgpass

We can also use wildcard, such as *. If you password contains ":" or "\", use "\" to escape them

$ echo "*:*:mydatabase:myuser:mysuperlongpassword" > ~/.pgpass

Give a proper permission to the file, nobody accept the owner of the home directory is allowed to use the file
$ chmod 0600 ~/.pgpass
Now we should be able to use psql to mydatabase as myuser, without entering any password
$ psql -h localhost -U myuser mydatabase
postgres=# 

Saturday, November 4, 2023

Postgresql 16 streaming replication on ubuntu 22.04

Streaming Replication is a feature in PostgreSQL that allows continuous shipping and application of WAL XLOG records to standby servers to keep them current.

Below is the steps to configure streaming replication using postgresql 16 on ubuntu 22.04, with one master and one slave.

First, install postgresql in all nodes by referring here.


In master node, do below steps:

- add this configuration (uncomment wherever necessary) in /etc/postgresql/16/main/postgresql.conf. For one slave node, set max_wal_senders to 3, and add 2 for every additional slave nodes, according to percona.
listen_addresses = '*'
wal_level = replica
max_wal_senders = 3
 - create a user for replication
$ sudo su - postgres
postgres@master:~$ createuser --replication -P replicauser 
- allow replication from slave's ip address. Add below line in /etc/postgresql/16/main/pg_hba.conf, assuming our slave node's ip address is 172.17.0.4
host    replication     replicauser             172.17.0.4/32                 scram-sha-256
- Restart postgres
$ sudo systemctl restart postgresql


In slave node, do below steps:

- remove postgresql data directory which is /etc/postgresql/16/main (we can also rename it to save as a backup)

$ sudo su - postgres

postgres@slave:~$ rm -rf /etc/postgresql/16/main

- set a proper permission to the data directory

postgres@slave:~$ chmod 700 /etc/postgresql/16/main

- copy data from master (assuming master's ip address is 172.17.0.3)

postgres@slave:~$ pg_basebackup -h 172.17.0.3 -U replicauser -D /var/lib/postgresql/16/main/

- add standby.signal file inside postgresql data directory, to tell postgresql that this is a standby node

postgres@slave:~$ touch /var/lib/postgresql/16/main/standby.signal

- add below configuration (uncomment wherever necessary) /etc/postgresql/16/main/postgresql.conf

listen_addresses = '*'

hot_standby = on

primary_conninfo = 'host=172.17.0.3 port=5432 user=replicauser password=1' 

- restart postgresql

$ sudo systemctl restart postgresql


Verify the replication is working 

- in master, check pg_stat_replication table

$ sudo su - postgres

postgres=# select client_addr, state from pg_stat_replication where usename like 'replicauser';






- Check if walsender process is running in master

$ ps -ef | grep wal


 




- Check if walreceiver is running in slave

$ ps -ef | grep wal




We can also create a database in master, and verify that the same database appear in slave almost instantly .

Wednesday, November 1, 2023

Postgresql 16 installation on ubuntu 22.04

Postgresql 16 is the latest postgresql version, released on Sept 2023. To install postgresql 16 on ubuntu 22.04, just follow below steps


First, create a postgresql repository file
$ sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Then, import repository signing key
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Update the package list
$ sudo apt update

Install postgresql
$ sudo apt install postgresql-16 -y

Configure postgresql to listen on all interfaces rather than just localhost
- Edit /etc/postgresql/16/main/postgresql.conf
$ sudo vi /etc/postgresql/16/main/postgresql.conf
- Add below configuration
listen_addresses = '*'
- Save and exit

Start postgresql
$ sudo systemctl start postgresql

Enable postgresql on boot
$ sudo systemctl enable postgresql

Allow postgresql port (5432) on ufw
$ sudo ufw allow 5432/tcp

Access to postgresql database and check version
$ sudo su - postgres
postgres@host:~$ psql
postgres=# select version();

The output should look like below








Type \q to exit psql


Friday, September 29, 2023

View information about audio or video files in command line

One of the command to accomplish this, is called mediainfo. It is not installed by default, so we have to install it first.


To install mediainfo in ubuntu, simply run
$ sudo apt update && sudo apt install mediainfo -y

Once installed, we can check the information of any audio / video file using the command
$ mediainfo myfile.mp4

Some of the information that we can get from a video file



















Some of the information that we can get from an audio file


Thursday, September 21, 2023

Extracting audio from video using ffmpeg

To extract audio from a video, here is the command

$ ffmpeg -i video.mp4 -q:a 0 -map a audio.mp4

whereby we supply the name of our video to using -i and put the name of the audio file at the end of the command.

If we want to extract only some part of the audio from the video, we can use below command
$ ffmpeg -i video.mp4 -ss 00:03:00 -t 00:00:45.0 -q:a 0 -map a audio.mp4

where -ss is the start time of the video, that we want to extract, and -t is the duration of how much time we want to extract from the -ss time. In the above example, the output will be an audio extract from a video called video.mp4, starting from the third minute, until 45 seconds after the third minute.  

Monday, September 18, 2023

Converting rpm into deb, tgz and vice versa using alien

To convert rpm to deb, or vice versa, we can use a tool called alien. To install alien in an ubuntu machine:
$ sudo apt install alien -y

To install alien in redhat based distro:
$ sudo dnf install alien -y

Once installed, we can easily convert any rpm into deb:
$ sudo alien --to-deb myapp.rpm

A file called myapp.deb will be created once alien finished doing its magic.

We can also use alien to convert deb to rpm:
$ sudo alien --to-rpm myapp.deb

A file called myapp.rpm will be created.

We can also convert to tgz package:
$ sudo alien --to-tgz myapp.deb

To generate multiple packages format:
$ sudo alien --to-rpm --to-deb myapp.tgz

Sunday, September 10, 2023

Determine if your system is using systemd

Systemd is a suite of basic building blocks for a Linux system that provides a system and service manager that runs as PID 1 and starts the rest of the system. It works as a replacement for sysvinit.

Almost all popular distros are using systemd, but if you need to use old version of the popular distros, or simply some non popular distros, here is how you can determine whether the linux your are having is using systemd, or not.

First, check which process is running PID 1.
$ ps 1

The output, most of the time will be /sbin/init. 






Now we need to determine if /sbin/init is a symlink to something else. There are 2 commands we can use to achieve that:
1. use stat
$ stat /sbin/init
2. use readlink
$ readlink /sbin/init

In a systemd based system, you will be getting output showing that /sbin/init is actually a symlink to /lib/systemd/systemd, where as in other system, you will not get that output.




Monday, September 4, 2023

Remove background from picture using command line

Removing a picture's background is sometimes a necessity, especially if you need the picture to integrate into other document or other picture. Here I will show how we can achieve that in linux using a python application called rembg.


First, make sure we have python installed. Most linux will have it installed by default, but just check to make sure
$ python -V






In order avoid installing python libraries globally, we will create a virtual environment to keep the rembg application files. You can read more about virtual environment, by clicking here. So we will create a virtual environment called rembg
$ python3 -m venv rembg

Activate the virtual environment
$ cd rembg
$ source bin/activate

Update pip
(rembg) $ pip install --upgrade pip

Install rembg[cli] package
(rembg) $ pip install rembg[cli]

Once installed, we can use "--help" to see what are the options available for rembg
$ rembg --help

Now we can use rembg to remove background. Let's say we have a picture called mypicture.jpg, and we want to remove its background, and save the new picture as mypicture-nobg.jpg:
(rembg) $ rembg i mypicture.jpg mypicture-nobg.jpg

Verify that the background has been removed in the new picture.

rembg also comes with http-server, for those who wanted a web interface to remove background from picture. Just run rembg with "s" flag to launch the rembg http server
$ rembg s

A web app will be launched on http://localhost:5000, where you can upload the file to be background removed, and click submit to get the output.

























Simply press "ctrl + c" to cancel rembg's http server, when you are done.

Thursday, August 31, 2023

Installing python's virtual environment in ubuntu linux

Python's virtual environment, is a way that developer can use to separate python's packages for different projects, in a single machine. The packages installed in this lightweight virtual environment, would be available in other virtual environment, thus removing the possibility of packages, org packages' version clashing inside a single machine.


First, we need to determine which python version is currently installed in our machine
$ python -V

 




Then, we need to install python-venv
$ sudo apt update && sudo apt install python3.10-venv -y

Once we have venv, we can start creating virtual environment. Let's say we wanted to have a virtual environment for a project called myproject, run this command
$ python3 -m venv myproject

The above command will create a directory called myproject, with necessary files in it. To activate the virtual environment, and start using it, we can run below commands
$ cd myproject
$ source bin/activate

How do we know that we are already inside the virtual environment? We will see the name of the virtual environment at our bash prompt, like below






We can now install any package that we need inside the virtual environment using pip, and that packages can only be used if we activate "myproject" virtual environment









To exit from the virtualenvironment, simply run deactivate command, and we will get back our standard bash prompt
$ deactivate


Wednesday, August 23, 2023

Reducing disk usage by journalctl logs

Systemd is using journalctl as the log keeper for its services. The log location is at /var/log/journal.


To check the disk usage of journalctl log
$ sudo journalctl --disk-usage

To check the logs date, just run 
$ sudo journalctl 

To flush active logs to file, so it can be cleared (vacuum)
$ sudo journalctl --flush

To clear the log, we can use "journalctl --vacuum-size". This below command will reduce the total size used by journalctl log to 1G. This command however, will only work on archived logs, and not active logs.
$ sudo journalctl --vacuum-size=1G

We can also vacuum by date, this command will remove data older than a month. Same with vacuum-size, this will work only to the archived logs.
$ sudo journalctl --vacuum-time=1month 

We can also ask journalctl to rotate its log files, using --rotate. The rotation criteria can be set in /etc/systemd/journald.conf, by editing MaxFileSec and MaxRetentionSec configuration.
$ sudo journalctl --rotate

Tuesday, August 15, 2023

Enable server time sync in ubuntu with chrony

Chrony is an implementation of the Network Time Protocol (NTP). It is an alternative to ntpd, a reference implementation of NTP. It runs on Unix-like operating systems (including Linux and macOS) and is released under the GNU GPL v2. It is the default NTP client and server in Red Hat Enterprise Linux 8 and SUSE Linux Enterprise Server 15, and available in many Linux distributions.


To enable chrony in ubuntu, we can install it first
$ sudo apt update && sudo apt install chrony -y
Start chrony once it is installed
$ sudo systemctl start chrony
Enable chrony on boot
$ sudo systemctl enable chrony
We can change the server that chrony sync from, by editing /etc/chrony/chrony.conf. By default, chrony in ubuntu will use ntp.ubuntu.com







To check the status of time sync
$ sudo chronyc sources -v

We can see that from the above example, the current best source of time is 144.126.242.176.

Make sure there is at least one server with ^* status.

If the time is skewed, we can force resync using below command 
$ sudo chronyc makestep
To install and configure chrony in redhat based linux, please refer to this article

Tuesday, August 8, 2023

Tuning kernel parameters in linux

For most people, the kernel provided with our favorite linux distro is good enough for daily use. But for some other people, they need more performance out of their linux machine, so they need to tune some kernel parameters, to suit whatever need they have in their hands.


We can add the parameter to the kernel runtime (would not survive reboot) by using sysctl command
$ sudo sysctl <tunable class>.<tunable>
To make the change permanent, we can add the kernel parameter to /etc/sysctl.conf or /etc/sysctl.d/filename.conf (best practice) by using any text editor

We can also add the parameter by using 
$ sudo sysctl -w <tunable class>.<tunable> >> /etc/sysctl.conf
$ sudo sysctl -w <tunable class>.<tunable> >> /etc/sysctl.d/filename.conf
Load kernel parameters from /etc/sysctl.conf or /etc/sysctl.d/filename.conf 
$ sudo sysctl -p 
$ sudo sysctl -p /etc/sysctl.d/filename.conf
We can also load from all system configuration files. System configuration files are /etc/sysctl.d/*.conf, /run/sysctl.d/*.conf, /usr/local/lib/sysctl.d/*.conf, /usr/lib/sysctl.d/*.conf, /lib/sysctl.d/*.conf and /etc/sysctl.conf
$ sudo sysctl --system
View all the kernel parameters currently available by using
$ sudo sysctl -a
We can also set pattern for the displayed results. For example, 
$ sudo sysctl -a --pattern 'kernel'


What are the available tunable classes? According to Red Hat, these are the ones available in RHEL.

- abi: execution domain and personalities 

- crypto: cryptographic interfaces

- debug: kernel debugging interfaces

- dev: device specific information

- fs: global and specific filesystem tunables

- kernel: global kernel tunables

- net: network tunables

- sunrpc: sun remote procedure calls (NFS)

- user: user namespace limits

- vm: tuning and management of memory, buffer and cache


How to get information about the tunables and its usage? Refer to https://www.kernel.org/doc/Documentation/

Tuesday, August 1, 2023

Lenovo Thinkpad X260 touchpad scrolling not smooth in Linux Mint 21.2

I just installed Linux Mint 21.2 onto a Lenovo Thinkpad X260. Everything is good, except that the scrolling using the touchpad is not smooth. After reading around, some people suggested to change the drive to synaptics, instead of the default libinput. Here is how to achieve that:


Get the id of the touchpad
$ xinput list | grep -i touchpad

 
Check properties of the touchpad, my id for the touchpad is 11, the id might differ. You should be seeing a lot of libinput inside the output (refer picture), to show that the touchpad is using libinput driver
$ xinput list-props 11

We can also determine which driver is being used, by using below command
$ grep -i "using input" /var/log/Xorg.0.log

 
Install synaptics driver
$ sudo apt update && sudo apt install xserver-xorg-input-synaptics -y

Logout and log in to switch the driver from libinput to synaptics






















Check the driver again, you should see a lot of synaptics, like below 
$ xinput list-props 11

















Your scrolling with touchpad will be much better compared to the previous driver. 
 
With both synaptics and libinput installed, synaptics will take priority. 
 
If you want to change back to libinput, simply uninstall synaptics
$ sudo apt remove xserver-xorg-input-synaptics

Then logout and log in again to activate libinput.