Tuesday, July 23, 2024

Linux Container (LXC) 101

LXC is a userspace interface for the Linux kernel containment features. Through a powerful API and simple tools, it lets users easily create and manage system or application containers.

The main usage of LXC in my scenario is, to test out any application in linux before deploying to the real environment, without disturbing my host linux. I used to use virtualbox, but LXC is lighter in terms of resources usage, but only applicable to linux. 

To install lxc in an ubuntu machine:
$ sudo apt update && sudo apt install lxc -y

Once installed, you now have access to multiple lxc-* commands. 



If our ufw firewall is turned on, we need to allow traffic to and from the bridge, and also allow traffic forwarded to the bridge. The name of the bridge is usually lxcbr0
$ sudo ufw allow in on lxcbr0
$ sudo ufw route allow in on lxcbr0
$ sudo ufw route allow out on lxcbr0

To create a container, use lxc-create command. For example, to create a container named as u1, using a template from https://images.linuxcontainers.org/, in an interactive mode (where you get to select distribution, release and architecture interactively), use below command
$ sudo lxc-create -n u1 -t download






















To create a same almalinux container, named u2, using a template from https://images.linuxcontainers.org/ but in a non interactive mode, use below command
$ sudo lxc-create -n u2 -t download -- -d almalinux -r 8 -a amd64 







To list out all created containers, use below command 
$ sudo lxc-ls





To get a better listing, use fancy mode (-f)
$ sudo lxc-ls -f






To start the containers, just use lxc-start
$ sudo lxc-start u1
$ sudo lxc-start u2


After a while, the containers will get ip addresses






To access the shell of the containers, we use lxc-attach
$ sudo lxc-attach u1





To exit the shell, just type exit





To destroy the containers, we need to stop the container first.
$ sudo lxc-stop u1
$ sudo lxc-destroy u1





Wednesday, May 22, 2024

Multiple ways to extract zip files in linux terminal

I downloaded a big zip file (5.9GB) for an application, and as usual, to unzip it, just use unzip command.

$ unzip mybigfile.zip

I received this error, which is unusual.

Error when extracting using unzip command












Then I search around to find any other zip application that can extract the file. I am pretty confident that file is fine, since the provider has provided a md5sum file, and it matches. I ended up trying "jar" command. To install jar in ubuntu, simply run below command:
$ sudo apt update && sudo apt install fastjar -y

Once installed, extract the zip file using below command
$ jar xvf mybigfile.zip

Unfortunately, jar is also unable to extract the file, with below error:
Error when extracting using jar command





The I encounter another robust archiver called 7z, which can be installed in ubuntu using below command:
$ sudo apt update && sudo apt install 7zip -y

To extract a zip file, simply run this command:
$ 7z x mybigfile.zip

Even though 7z reported some error as well, but it managed to extract the file. 
Error when extracting using 7z command



There you go, 3 commands to extract a zip file. So do not worry if you are unable to extract any zip file, you can always try a few commands to get the job done.

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=#