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

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

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

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.