Joseph Zikusooka ~ Zik

A software engineer specializing in open source technologies | Very experienced in building and configuring UNIX/Linux systems and servers. Passionate about developing software applications and hardware for the smart home | Currently serving as the CEO of Jambula Labs and the project leader at JambulaTV, a smart home automation and entertainment platform - https://jambulatv.com | This blog focuses on the following areas: Linux How-Tos and Tutorials ::: IT Security News ::: Free and Libre Open Source Software ::: Smart Home Software ::: Digital Innovations in East Africa https://mastodon.social/@jzik | https://github.com/zikusooka

Linux TIPS

Loading Mastodon feed...

Open source software tools I used while preparing for Creative Freedom Summit 2024

This week I will be presenting at the Creative Freedom Summit 2014. In case you don’t already know about it, the Creative Freedom Summit is a virtual conference dedicated solely to the features and benefits of Open Source creative tools.

My talk at this open source summit will be focused on JambulaTV, a smart home hub I built using only Free/Libre open source software (FLOSS) tools. You can read the details of what my talk is about at:

https://creativefreedomsummit2024.sched.com/event/1Xzv4/using-fedora-linux-to-create-a-private-and-locally-based-smart-home-hub

To prepare for my presentation, and in the spirit of open source software, I exclusively used the following open source tools:

LibreOffice Impress: Used to create the slide show

LibreOffice Impress is part of the LibreOffice suite and is used to create powerful and nice looking slide shows for presentations

https://www.libreoffice.org/discover/impress

OBS Studio: Used to record the video session

OBS Studio is a free and open-source, cross-platform screen-casting and streaming app. It is available and packaged for nearly all the major Linux distributions

https://obsproject.com

GIMP: Used to create or edit the graphics used in the slide show


GNU Image Manipulation Program, commonly known by its acronym GIMP, is a free and open-source raster graphics editor used for image manipulation and image editing, free-form drawing, transcoding between different image file formats, and more specialized tasks.

https://www.gimp.org

FFMpeg: Used to clean up the audio/video output file

FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams.

https://ffmpeg.org

Jitsi: To remotely connect with the summit hosts for the virtual presentation

Jitsi is a free and open-source multiplatform voice, video conferencing and instant messaging applications for the Web platform, Windows, Linux, macOS, iOS and Android.

https://jitsi.org

Linux, Python, Security, and Open Source Software TIPS

  • 💡 Are you having difficulty wrapping your head around the OnCalendar pattern in systemd timers? Simply use the command:systemd-analyze calendar "[TIME-PATTERN]" e.g.systemd-analyze calendar "hourly"#ZikTIPS #Systemd #Sysadmin #CLI #Fedora #Opensource
  • 💡 Use the following command to find out which key has been pressed in Linux#ZikTIPS #CLI #Fedora #Opensourcesudo showkey
  • Alternatives to popular CLI tools: uptime #CLI #opensource https://www.linuxlinks.com/alternatives-popular-cli-tools-uptime/
  • The upstream xz repository and the xz tarballs have been backdoored. Red Hat issues urgent security alert for @fedora Linux 40 & Fedora Rawhide users #security #cybersecurity #infosec #xz #Fedora #opensourcehttps://www.openwall.com/lists/oss-security/2024/03/29/4
  • 💡Use the command line in Linux to mount & un-mount partitions located in an image generated by a tool such as dd #ZikTIPS #Fedora #Debian #CLI #Embedded #opensource sudo losetup -fP –show JambulaOS.imgsudo mount -o loop /dev/loop21pX /mnt(where X = 1, 2 …)sudo umount /mntsudo losetup -D JambulaOS.img
  • Happy to release the latest version of Jambula OS, a custom Linux distribution used primarily on embedded and SoC devices such as the Raspberry Pi. Download it at: https://github.com/zikusooka/Jambula-OS#JambulaOS #JambulaTV #RaspberryPi #OpenSource
  • 💡️Find the version of a Linux kernel source tree that you have unpacked#ZikTIPS #CLI #Fedora cd [linux-source-tree]make kernelversion
  • Use the Linux command line to get the number of days left before that all important day e.g. an upcoming conference @fedora @climagic @Linux @debian #ZikTIPS #CLI #Fedora #OpensourceFUTURE_DATE="2024-03-31"; echo $(( ($(date -d $FUTURE_DATE +'%s')-$(date +'%s')) / 86400))NOTE: You can also use it in reverse e.g. calculate number of days you have been on earth
  • 💡Use the Linux command line to calculate the number of days left before that important & upcoming day @fedora #ZikTIPS #CLI #Fedora #LinuxDAY="2024-03-31"; echo $(( ($(date -d $DAY +'%s')-$(date +'%s')) / 86400))
  • In case you missed my talk at the Creative Freedom Summit 2024, you can watch it at:https://www.youtube.com/watch?v=7CyM5fWq7bQ@fedora @fedora_design @jambula #fedora #jambulaTV
  • My presentation at the Creative Freedom Summit is available at:https://www.youtube.com/watch?v=7CyM5fWq7bQ
  • Join me shortly as I present at the Creative Freedom Summit 2024 @fedora @fedora_design @jambula @jambulatv The live stream starts shortly at: https://www.youtube.com/live/Vob6ZmXE68c?si=E4P_P83M6ilAg42LFor an interactive chat, join us at: https://matrix.to/#/#creativefreedom:fedora.im#creativefreedomsummit #fedora #jambulaTV #smarthome
  • It's the 3rd & final day of the Creative Freedom Summit 2024! Join me today as I speak & share my experiences on using Fedora Linux to create a private & locally based smart home hubhttps://sched.co/1Xzv4@fedora @icinga @homeassistant @jambulatv @jambula#creativefreedomsummit#fedora #opensource #JambulaTV #smarthome #privacy #privacymatters
  • It's Day 2 of the Creative Freedom Summit 2024! Don't miss out on great sessions about open source creative tools @fedora @fedora_design The live stream starts shortly at: https://youtube.com/@fedora/streamsFor an interactive chat, join in using element: https://matrix.to/#/#creativefreedom:fedora.im#CreativeFreedomSummit #opensource
  • Also add '-p mpv ….' to use MPV or another video player instead of VLCYou can select different resolutions depending on your bandwidth. Available streams: 144p (worst), 240p, 360p, 480p, 720p (best)
  • 💡Use the Linux command line to launch a live Youtube stream e.g. To watch the on-going @fedora @fedora_design #CreativeFreedomSummit streamlink https://www.youtube.com/watch?v=sUrTAyksmHA best#ZikTIPS #CLI #Fedora
  • The Creative Freedom Summit 2024 starts today! It's all about open source creative tools and why they're important. Watch live at: https://www.youtube.com/@fedora/streamsAnd for the best experience, join in via Fedora Element: https://matrix.to/#/#creativefreedom:fedora.im#CreativeFreedomSummit #opensource
  • 💡In GNOME, quickly open or switch to an application in your Dash using hot keys e.g. Press WIN+1 to switch to the first app, WIN+2 to the second app, etc.. .#ZikTIPS #GNOME #Fedora #Ubuntu
  • I'm excited to attend the Creative Freedom Summit 2024 which begins in 1 day!#creativefreedomsummit #fedora #Linuxhttps://creativefreedomsummit2024.sched.com/directory/speakers @fedora
  • Join me this week at the Creative Freedom Summit 2024 where I will be speaking and sharing my experiences on using Fedora Linux to create a private and locally based smart home hub @fedora @icinga @homeassistant @jambulatv @jambula #creativefreedomsummit#fedora #jambulaTV #smarthome #privacy #privacymatters https://sched.co/1Xzv4

How to install the latest Python 3 version on Linux using sources

Introduction

Python 3.11.0 was officially released on October 24 2022. However, most Linux distributions have not yet upgraded to the latest Python version i.e. Python 3.11.2 (released on February 8 2023). Please visit this page to see the most recent versions.

The jump from versions 3.10.X to 3.11.X brought with it significant changes and improvements to Python code. Among the improvements is that Python is now 10 – 60% faster than before. That’s a big and noticeable speed boost. So beyond the usual reasons for upgrades i.e. security etc, I really wanted this version on my Linux desktop as soon as the final release was made publicly available.

If you are like me and don’t like waiting for your favourite open-source program to be supported on your Linux distribution, here is how to install the latest Python 3 version on Linux using sources.

Before you begin, check the current version on your Linux system:

python  --version

Currently the version of Python on my Linux system is three (3) releases behind. So it is time for me to upgrade to the most recent version.

CAUTION: Due to the many dependencies on the currently installed Python common package, I highly recommend that you do not remove the existing version. Instead, we will install the latest version in an alternate location e.g. /usr/local

Prerequisites

Please ensure that your Linux system has the following packages installed:

On Ubuntu or Debian:

sudo apt-get install build-essential

sudo apt-get install autoconf automake gdb libffi-dev zlib1g-dev libssl-dev libncurses5-dev libgdbm-dev libnss3-dev libreadline-dev libsqlite3-dev

On Fedora or Rocky Linux:

sudo dnf groupinstall “Development Tools”

sudo dnf install openssl-devel libffi-devel bzip2-devel sqlite-devel

* You will also need wget which is generally included by default on most Linux distributions

Visit the Python downloads page for sources to get the direct link to latest Python source package. In this tutorial I will be using the XZ compressed source tarball i.e. Python-3.11.2.tar.xz

Download the latest Python package to a temporary directory:

wget  -c  -P /tmp  https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tar.xz

Unpack the Python 3 tarball:

tar xvf /tmp/Python-3.11.2.tar.xz -C /usr/src

Compile and build Python:

cd  /usr/src/Python-3.11.2

./configure  --prefix=/usr/local  --enable-shared  --enable-optimizations  --with-system-expat  --with-system-ffi

IMPORTANT: Ensure the prefix i.e. ‘/usr/local’ you use is different from that used by your Linux distribution otherwise you will overwrite the default Python 3 installation!

Optional: You can make some of your modules a permanent part of the Python interpreter. To do so, simply modify the file: Modules/Setup.local before running make. To see my my local setup, see the appendix.

sudo  make

TIP: you can add “– j $(getconf _NPROCESSORS_ONLN)” to the make command in order to speed things up i.e. sudo make -j $(getconf _NPROCESSORS_ONLN)

sudo  make  altinstall

Add the following export command to your shell initialization file:

Using Bash shell:

echo  “export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib”  >>  ~/.bashrc

Then source the file for now:

source  ~/.bashrc

To verify if you now have the latest Python 3 (including pip installer) versions:

/usr/local/bin/python3.11 –version

/usr/local/bin/pip3.11 –version

Congratulations! You now have the latest Python 3 version on your Linux system.

NOTE: When your distribution eventually catches up, you can remove this installation by issuing the ‘make uninstall‘ command in the installation directory i.e. /usr/src/Python3.11

Appendix

Here’s my sample ./Modules/Setup.local file:

array -DPy_BUILD_CORE_MODULE arraymodule.c
_contextvars _contextvarsmodule.c
_struct -DPy_BUILD_CORE_MODULE _struct.c
_random _randommodule.c -DPy_BUILD_CORE_MODULE
_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c
_pickle -DPy_BUILD_CORE_MODULE _pickle.c
_datetime _datetimemodule.c
_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE
_bisect _bisectmodule.c
_asyncio _asynciomodule.c
_json -I$(srcdir)/Include/internal -DPy_BUILD_CORE_BUILTIN _json.c
unicodedata unicodedata.c -DPy_BUILD_CORE_BUILTIN
fcntl fcntlmodule.c
spwd spwdmodule.c
grp grpmodule.c
select selectmodule.c
mmap mmapmodule.c
_csv _csv.c
_socket socketmodule.c
termios termios.c
resource resource.c
_posixsubprocess  -DPy_BUILD_CORE_BUILTIN _posixsubprocess.c
audioop audioop.c
_md5 md5module.c
_sha1 sha1module.c
_sha256 sha256module.c -DPy_BUILD_CORE_BUILTIN
_sha512 sha512module.c -DPy_BUILD_CORE_BUILTIN
_sha3 _sha3/sha3module.c
_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c
syslog syslogmodule.c
binascii binascii.c
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
pyexpat expat/xmlparse.c expat/xmlrole.c expat/xmltok.c pyexpat.c -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DXML_POOR_ENTROPY -DUSE_PYEXPAT_CAPI
_multibytecodec cjkcodecs/multibytecodec.c

How to use Linux GNOME terminal like a Pro

For most users work is typically done in a web browser like Firefox or Chrome. (BTW, if you are not using Mozilla’s Firefox, I can not emphasize enough why you should switch to this opensource browser)
For advanced users i.e. IT admins, software developers, most of their time may be spent in a terminal and on Linux most often gnome terminal

Like regular users who prefer to open multiple tabs in a browser, using tabs in a terminal is necessary for an advanced user if they are to be productive. However, it can get pretty tedious and boring having to open up multiple terminal tabs in every time you start your Linux desktop

Using the command line tool gnome-terminal this process can be automated. This tool allows you to open multiple gnome-terminal tabs like a pro i.e. At desktop startup, open several & different tabs each with its own specified directory path or run command. You can even log into a remote system, if you have setup to allow passwordless SSH connections.

Below is a basic example of gnome-terminal command in action

gnome-terminal --maximize --geometry=80x24+200+200 --display=:0 --tab-with-profile=DAY -t Local --working-directory=/projects --tab-with-profile=NIGHT -t Remote -e "ssh -t jambula@REMOTE \"cd /opt; bash\""

I use this in a script which is activated by a custom keyboard shortcut to start my day

How to monitor the presence of nearby WiFi devices using Icinga2

Introduction

I recently published a Python based plugin for Icinga and Nagios monitoring platforms that monitors the presence of wireless devices in the vicinity.

I really like the stability and reliability Icinga provides when it comes to monitoring the state of hosts or services. There are so many software applications that let you do this, including one I talked about a while back called trackerjacker: https://github.com/calebmadrigal/trackerjacker

Why this way?

At first trackerjacker seemed like what I needed to do this sort of thing, but after several months of testing one issue persisted. The performance hit whenever I started the process was huge. The server, would frequently slow down, as packet capturing and monitoring of WiFi devices took place. So I decided to ditch this tool, and write my own plugin.

I am already using icinga on the server, so I figured, rather than write another app for polling, why not use the existing and well performing icinga2 monitoring software. After all, I had done this with bluetooth devices earlier and presence detection was working so well.

Pre-requisites

You are running a Linux based server with Icinga2 monitoring software installed, configured and working properly. There are several articles about how to install and configure icinga2 including the official documentation at:
https://icinga.com/docs/icinga-2/latest/doc/01-about/

The heavy lifting i.e. scanning is done by the Wireshark based CLI tool ‘tshark’. Therefore, ensure that Wireshark is installed on your system. Also, a separate wireless interface that supports monitor mode is required

To capture packets as a non-root user, run the following command in your terminal:
sudo setcap cap_net_raw,cap_net_admin+eip $(which dumpcap)

If you’re still having trouble capturing packets as a non-root user, check out the following Wireshark wiki page:
https://wiki.wireshark.org/CaptureSetup/CapturePrivileges#Most_UNIXes

You will need to identify the MAC address of each wireless device you plan on monitoring. For phones for example, this information is in the Settings >> General >> About >> WiFi Address for iOS. For Android, go to Settings >> About >> Status >> Wi-Fi MAC address

NOTE: Some phones like the iPhone may have MAC randomization enabled, which makes it difficult to reliably monitor their presence status

Setup Icinga2 plugin

Download icinga2 monitoring plugin named ‘check_tshark’ from my Github page at:
https://github.com/zikusooka/icinga-nagios-plugins

Alternatively, you can clone the entire repository and check out my other plugins too:

git clone https://github.com/zikusooka/icinga-nagios-plugins

Copy the icinga2 plugin i.e. check_tshark to your icinga2 monitoring plugins directory e.g. /usr/lib64/nagios/plugins/

cp -v check_tshark /usr/lib64/nagios/plugins/

Configure Icinga2

Add new check and event command objects to icinga2 i.e. Add and save the following snippets to the file:
/etc/icinga2/conf.d/commands.conf

object CheckCommand "tshark" {
        import "plugin-check-command",

        command = [ PluginDir + "/check_tshark" ]

        arguments = {
                "-i"="$tshark_interface$"
                "-t"="$tshark_timeout$"
                "-a"="$tshark_address$"
        }
}
object EventCommand "wifi-tshark-status-event" {
  import "plugin-event-command"

  command = [ SysconfDir + "/icinga2/scripts/wifi-tshark-status-event.sh" ]

  env = {
    "HOSTALIAS" = "$host.display_name$",
    "HOSTADDRESS" = "$address$",
    "HOSTSTATE" = "$host.state$",
    "HOSTSTATEID" = "$host.state_id$",
    "HOSTSTATETYPE" = "$host.state_type$",
    "HOSTOUTPUT" = "$host.output$",
    "HOSTDISPLAYNAME" = "$host.display_name$",
    "LASTHOSTSTATE" = "$host.last_state$",
    "LASTEHOSTSTATEID" = "$host.last_state_id$",
    "LASTHOSTSTATETYPE" = "$host.last_state_type$",
    "LASTHOSTSTATECHANGE" = "$host.last_state_change$",
    "LASTHOSTCHECK" = "$host.last_check$",
    "LONGDATETIME" = "$icinga.long_date_time$",
  }

}

Add new host templates to icinga2 i.e. Add and save the following snippets to the file:
/etc/icinga2/conf.d/templates.conf

template Host "event-tshark-status" {
  max_check_attempts = 6
  check_interval = 30s
  retry_interval = 5s

  check_command = "tshark"

  enable_event_handler = 1
  event_command = "wifi-tshark-status-event"

  enable_flapping = 1
  flapping_ignore_states = ["Critical"]
}

Finally add the wireless devices you want to monitor to the hosts in icinga2 i.e. Add and save the following snippets to the file:
/etc/icinga2/conf.d/hosts.conf

NOTE: In addition to the MAC address, make sure you specify the WiFi interface used for monitoring

object Host "Phone_Zik" {
  import "event-tshark-status"
  vars.tshark_interface = "wlan1"
  vars.tshark_timeout = 10
  vars.tshark_address = "xx:xx:xx:xx:xx:xx"
}

object Host "Phone_Shushu" {
  import "event-tshark-status"
  vars.tshark_interface = "wlan1"
  vars.tshark_timeout = 10
  vars.tshark_address = "xx:xx:xx:xx:xx:xx"
}

Optional: Add Alert notification using MQTT

In order to send alerts whenever a WiFi device appears or disappears, you will need to set up a MQTT broker server. I personally use mosquitto on Fedora Linux. For detailed instructions on how to setup mosquitto on Fedora Linux, check out this how to: https://nullr0ute.com/2016/05/getting-started-with-mqtt-using-the-mosquitto-broker-on-fedora/

Since, we have already enabled event handling for our monitored devices in icinga2, all that is left is to add an event script that will be triggered by the HOST state of either UP or DOWN. Here’s an example:

Create the following script and make it executable:

cat > /etc/icinga2/scripts/wifi-tshark-status-event.sh <<ET
#!/bin/sh
# Variables
TSHARK_MAC_ADDRESS=$(echo "$HOSTOUTPUT" | grep -oP "(?<=\[).+?(?=\])")
MQTT_TOPIC_PRESENCE_TSHARK=Home/presence/$TSHARK_MAC_ADDRESS
MQTT_PUBLISH_CMD=/usr/bin/mosquitto_pub
MQTT_PUBLISH_OPTS="--quiet -h 127.0.0.1 -p 8883"
# Quit if type of state alert is 'SOFT'
[[ "$HOSTSTATETYPE" = "SOFT" ]] && exit 0

# Publish status via MQTT
if [[ "$HOSTSTATE" = "UP" ]];
then
$MQTT_PUBLISH_CMD $MQTT_PUBLISH_OPTS -t "$MQTT_TOPIC_PRESENCE_TSHARK" -m "Home"

else
$MQTT_PUBLISH_CMD $MQTT_PUBLISH_OPTS -t "$MQTT_TOPIC_PRESENCE_TSHARK" -m "Away"
fi
ET
 
chmod 755 /etc/icinga2/scripts/wifi-tshark-status-event.sh

To check your configuration and that all syntax is correct; run the following command:

icinga2 daemon -C

If all’s OK, restart icinga2 as follows:

systemctl restart icinga2

Conclusion

Next, log into your icinga2 web interface (if you have this setup) and ensure your devices are reporting correctly.

In order to monitor alerting via MQTT run the following command in a terminal:

mosquitto_sub -v -h JambulaTV -p 8883 -t "JambulaTV/#" --insecure  | grep presence

That’s all for now. Until next time!

How to automatically backup your data to an external USB disk on Linux

Introduction

One of the most critical roles that any Systems Administrator must perform on a regular basis is that of running backups for an organization. These days, most big and medium sized offices use Network Attached Storage servers to quickly perform backups on their networks. However, for small offices with limited IT budgets, a DIY solution which uses a simple USB external hard drive is a viable option.

In this setup, the resident “IT guy” is assigned the task of backing up data. This could be from a handful of desktops/laptops, or a central backup server. However, the problem with this setup is that they often will forget to plug in the disk or even initiate the manual backup process, leading to a very unreliable backup scheme.

In this article, I will guide you on how to set up a cheaper, reliable and automated backup scheme. All the operator has to do is to plug in the USB external hard disk any time, and the backup process commences immediately.

This solution takes advantage of a bash script that is triggered once the USB hard disk is inserted into the computer desktop or laptop’s USB ports. The trigger mechanism is handled by udev rules via a systemd unit as will be shown below.

Prerequisites

Operating system: Linux
Desktop: GNOME – mainly for notification purposes
Username: erica (Must have permissions to run ‘sudo’)
Data: /home
Partition is NOT encrypted. See ‘cryptsetup’ tool

Create the following backup script. Save it and make it executable:

vim /opt/backup_2_usb_disk.sh
chmod 755 /opt/backup_2_usb_disk.sh

#!/bin/sh
# Variables
NORMALUSER=erica
VOLUMENAME=BACKUP
USB_DISK_PARTITION=$USB_DISK_PARTITION1
MOUNTPOINT=/run/media/$NORMALUSER/$VOLUMENAME
RSYNC_OPTIONS="-aAXvH”
# Excludes 4 /home
EXCLUDE_FILE_4_HOME_DIR=/tmp/exclude_dirs_in_home_dir
EXCLUDE_DIRECTORIES_IN_HOME_DIR="Videos"
NOTIFY_CMD=/usr/bin/notify-send 
NOTIFY_ICON=/usr/share/icons/gnome/scalable/devices/media-zip-symbolic.svg
NOTIFY_TITLE=""


# Functions
notice () {
su -l $NORMALUSER -c "$NOTIFY_CMD -i $NOTIFY_ICON $NOTIFY_TITLE '$@'"
}

disable_automount () {
su -l $NORMALUSER -c "dbus-launch gsettings set org.gnome.desktop.media-handling automount false"
su -l $NORMALUSER -c "dbus-launch gsettings set org.gnome.desktop.media-handling autorun-never true"
}

sync_data () {
sudo rsync $RSYNC_OPTIONS --delete "$1" "$2"
}

mount_partition () {
# Create mountpoint if it does not exist
[ -d $MOUNTPOINT ] || mkdir -p $MOUNTPOINT
# Mount drive
mount $USB_DISK_PARTITION $MOUNTPOINT
STATUS_MOUNT=$?
if [ "$STATUS_MOUNT" != "0" ];
then
notice "ERROR: mount $USB_DISK_PARTITION $MOUNTPOINT failed"
exit 1
fi
}

backup_thinkpad () {
# Create backupdir directory
[ -d $MOUNTPOINT ] || mkdir $MOUNTPOINT

# Start of backup
notice "Backup Started"

# /home
echo $EXCLUDE_DIRECTORIES_IN_HOME_DIR | tr " " "\n" > $EXCLUDE_FILE_4_HOME_DIR
sleep 3
rsync $RSYNC_OPTIONS --delete --exclude-from=$EXCLUDE_FILE_4_HOME_DIR /home/ $MOUNTPOINT/home/
rm -f $EXCLUDE_FILE_4_HOME_DIR

# other partitions
}

umount_partition () {
umount $USB_DISK_PARTITION
STATUS_UMOUNT=$?
if [ "$STATUS_UMOUNT" != "0" ];
then
notice "ERROR: umount $USB_DISK_PARTITION failed"
exit 1
fi
}

enable_automount () {
su -l $NORMALUSER -c "dbus-launch gsettings set org.gnome.desktop.media-handling automount true"
su -l $NORMALUSER -c "dbus-launch gsettings set org.gnome.desktop.media-handling autorun-never false"
}


# Process the following routines
disable_automount
mount_partition
backup_thinkpad 
umount_partition
enable_automount 

# Notice of completion
sleep 10
notice "Backup completed successfully"

Start/Stop service

Create the systemd unit file:
/etc/systemd/system/backup-usb-disk.service and add the following:

[Unit]
Description=USB Portable Drive

[Service]
User=root
Type=oneshot
Environment=DISPLAY=:0
ExecStart=/opt/backup_2_usb_disk.sh

[Install]
WantedBy=multi-user.target

NOTES:
Reload systemd daemon i.e. ‘systemctl –system daemon-reload’
It is not necessary to enable or start the above unit at this time
since that will be done by the udev rule below.

UDEV rules

Create the udev rules file: /etc/udev/rules.d/97-backup-usb-disk.rules and add the following:

ACTION=="add", ATTRS{idVendor}=="0bc2", ATTRS{idProduct}=="2400", SYMLINK+="disk/by-id/$env{ID_NAME}_$env{ID_SERIAL}", RUN+="/usr/bin/systemctl start backup-usb-disk.service"

ACTION=="remove", ATTRS{idVendor}=="0bc2", ATTRS{idProduct}=="2400", RUN+="/usr/bin/systemctl stop backup-usb-disk.service"

NOTES:
To find your USB disk Vendor and Product ID, run the command:

udevadm info -a -p $(udevadm info -q path -n /dev/sdX) | grep -B2 -i -e idvendor -e idproduct


Change ‘X’ to suit your setup
Run the command ‘udevadm control –reload’ to reload the rules

Conclusion

That’s it! Now when ever, the USB external disk is plugged into the USB port, the backup script will be triggered. You can extend the above mentioned backup script to not only back up other partitions beyond just the /home in our example, but also email the operator whenever the process completes.

Why installing open source packages using sources is cool and how to start today

Until you start installing packages on Linux/UNIX systems using sources, you will always be very dependent on your distribution or vendors to stay up to date.

Installing from sources may not be for everyone, but I recommend it for novice and advanced Linux/UNIX users alike, as it offers tremendous benefits:

  1. Ability to fine tune the package according to your needs
  2. You almost always end up with a much faster application
  3. You get the latest security updates immediately
  4. You learn a whole lot

So how do you install from sources?

  • Download the package from URL upstream

The package may be in several forms: PACKAGE.tar.gz, PACKAGE.tar.bz2, PACKAGE.xz, or clone from a github repository i.e. git clone [URL]

  • Unpack the package inside your sources directory e.g. /usr/src:
    tar zxvf PACKAGE.tar.gz -C /usr/src
    tar jxvf PACKAGE.tar.bz2 -C /usr/src
    tar xvf PACKAGE.tar.xz -C /usr/src

  • IMPORTANT: Read the following two files if available: README (or README.md) and INSTALL. I can not stress how important it is to read the above files. The package
    maintainer will usually include notes on how to compile and install the package

  • Change to source directory of the package
    cd /usr/src/PACKAGE-VERSION/

  • Compile
    ./configure

  • Install
    make && make install

Thats it! If all went well especially during the make, you should be able to run your package. Sometimes, the configure command will specify that
a dependency is missing. You will need to install it before you proceed. This is why it is important that you read the README and INSTALL files.

How to add a printer in Linux using the command line (CLI)

Managing printers in Linux has become easier lately. With just a few clicks in your GNOME, KDE, UNITY, or other desktop, you could be printing away in just seconds. But what about the command line Interface? Here too, a simple command is all you need, and in a few steps your printer should be setup

Before you start, ensure that the CUPS package is installed on your Linux system and if not, install it using your package manager e.g. For Redhat based systems:

dnf install cups

Alternatively, you may download CUPS and PPD files direct from the CUPS website at: https//www.cups.org/

1. Find the Postscript Printer Description (PPD) file for your printer. Typically installed with the cups package and stored under: /usr/share/cups/model/. Also look under /usr/share/ppd/cupsfilters

2. Run the command lpinfo -l to get a list of available printers and drivers i.e. device-uri

3. Add your printer using the following command:

lpadmin -p “HP-LaserJet-CM3530-1” -D “Human Resources Department” -P /usr/share/ppd/cupsfilters/HP-Color_LaserJet_CM3530_MFP-PDF.ppd -E -v file:///dev/PRINTER_PATH

-v represents the device-uri as seen in step 3. For a detailed explanation of the options, type: man lpadmin

Scroll to top