100% found this document useful (1 vote)
370 views100 pages

Linux Magazine Special Editions - 101 Cool Linux Hacks 2020 US

Uploaded by

Tozser Jozsef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
370 views100 pages

Linux Magazine Special Editions - 101 Cool Linux Hacks 2020 US

Uploaded by

Tozser Jozsef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

WELCOME

This Linux Magazine special edition promises you “101 Linux hacks,” and we’ll
certainly deliver – check the table of contents on the following page for a list
of all the gems we present inside. But what exactly is a “hack”?
In today’s world where Windows is the dominant desktop operating system,
the decision to use Linux is the first and most important hack. In addition
to that:
Q If I can use it to solve a problem or get some work done faster than
before, then it’s a hack.
Q If the way it works is unexpected, it’s very certainly a hack.
Q If it teaches me a new and interesting way to use my Linux machine,
it’s a hack.
Q When it makes me grin, because it’s so obvious that only a geeky
Unix person would ever think of doing things this way, I add it to my
list of anecdotes (and yes, it’s a hack).
The definition of a hack is a matter of taste, and so is our choice of hacks
for this collection: Many authors have contributed, and together we have
assembled a mix of new tools, new configuration options for old tools, and
new ways to use familiar software. We’ve organized our hacks into
10 categories so that you’ll find them quickly when you go
back to search for them.
We call the final category “Having Fun,” because we
hope that, in addition to profiting from these clever
new hacks, you’ll also enjoy yourself while you
browse this issue.

Hans-Georg Eßer
Editor-in-Chief

101 LINUX HACKS – 2020 EDITION 3


Desktop Modding Q 6 Getting Stuff Done Q 16
HACK 1 Cure the Caps Lock Disease 7 HACK 11 Find Your Files with DocFetcher 17
HACK 2 Latte Dock: The Best Dock for KDE 8 HACK 12 Organize Your Ideas with Zim, the Desktop Wiki 18
HACK 3 KDE Window Hacking 8 HACK 13 Track Time with Chrono 20
HACK 4 Global Menu for KDE Programs 9 HACK 14 Launch Programs Inside a VirtualBox VM 20
HACK 5 Save Storage with Advanced Image Formats 10 HACK 15 Create a Cheap Timer with sleep 22
HACK 6 Zoom Everything on Your Screen 11 HACK 16 Find and Install Online Fonts 23
HACK 7 Zoom into the Gnome Desktop 12 HACK 17 Sync Up Your Phone and Linux Desktop 23
HACK 8 Use Virtual Desktops, Memorize the Hotkeys 13 HACK 18 Self-Organization with GTG 24
HACK 9 Caffeine Helps Your Linux PC Stay Awake 14
HACK 10 What Would That Resolution Look Like? 14

Keep It Running Q
26
HACK 19 Clone It with Clonezilla 27 HACK 25 Repair the Debian Package Database 34
HACK 20 Write ISOs to USB Sticks 28 HACK 26 Learn the vi Editor Basics 36
HACK 21 Get a Quick Machine Configuration Overview 29 HACK 27 Follow File Updates 37
with inxi HACK 28 Monitor Your System in a Terminal 37
HACK 22 Repair Your Bootloader 30 HACK 29 Visualize ddrescue’s Progress 38
HACK 23 Clean House with Stacer 32 HACK 30 Run dmesg with the Right Options 38
HACK 24 Reset Your Debian-Based Distribution 33 HACK 31 Where Did You Mount That Disk? 39

Security & Privacy Q


40 Networking Q
48
HACK 32 Install and Run Tor Browser 41 HACK 42 Monitor Your Network with Nutty 49
HACK 33 Permanently Wipe Files from Your Hard Disk 41 HACK 43 DIY Network Configuration 49
HACK 34 Disable Webcam and Microphone 42 HACK 44 Share Files on the Local Network 50
HACK 35 Disable Password-Based SSH Logins 43 HACK 45 Dig a Tunnel with sshuttle 51
HACK 36 Encrypt Your Emails with GPG 43 HACK 46 Send Files Without Knowing the Target’s IP 51
HACK 37 Enhance Security with sudo Options 44 HACK 47 Check Your DNS Server with DNSDiag 52
HACK 38 Use Two-Factor Authentication 46 HACK 48 Run a Simple HTTP Server: weborf 53
with Authenticator HACK 49 Use screen in SSH Sessions 53
HACK 39 Start Your Training with Web Security Dojo 46 HACK 50 Access a Remote PC’s Shell Session 54
HACK 40 Lock Your Screen, Always 47 HACK 51 Slow sudo? Check Your Hostname 56
HACK 41 Use ccrypt for Quick 47 Configuration
Encryption HACK 52 Use Network Manager’s CLI 56

4 101 LINUX HACKS – 2020 EDITION


101 LINUX HACKS

Shell Hacks Q
58
HACK 53 Configure Your Shell History 59 HACK 60 Command References at cheat.sh 64
HACK 54 Progress Bars for Standard Tools 60 HACK 61 Highlighting Instead of Grepping 65
HACK 55 Replace top with htop 60 HACK 62 Add File Type Icons to Your File Listing 66
HACK 56 Super-Fast Terminal Emulator 61 HACK 63 Upterm, a Terminal with 67
HACK 57 Universal Unpacker 61 Built-In Shell
HACK 58 List Files with Style 62 HACK 64 The fish Shell 67
HACK 59 ASCII Browsing with Browsh 63 HACK 65 Use a Sub-Shell 68
HACK 66 Start the Right App 68

Publish or Perish Q
70
HACK 67 Install the draw.io Diagram Editor Locally 71
HACK 68 Record Screencasts with Peek 72
HACK 69 Edit Your Videos with VidCutter 73
HACK 70 Create Long Documents with Styles 73 Deep Hacks Q
77
HACK 71 Edit HTML with Live Preview in Brackets 74
HACK 72 Free Wildcard X.509 Certificates for Your Domain 75 HACK 75 Safely Power Off Your Machine 78
HACK 73 Test Alternative CMSs with Docker 76 HACK 76 Make Files Immutable or Append-Only 79
HACK 74 How Fast Is Your Server? 76 HACK 77 Mount .tar.gz and .zip Archives 79
HACK 78 Recover Deleted Files with PhotoRec 80
and TestDisk
HACK 79 Change Root into Second Distro 81
HACK 80 Bind Mount a Directory with New Permissions 81
HACK 81 Run Binaries from a Different Linux Installation 82
HACK 82 Don’t Be So Case-Sensitive 83

Having Fun Q
92

Working with Code 84


HACK 94 Interesting Terminal Output for Your Visitors 93
Q
HACK 95 Scan the Network Like a Script Kiddie 94
HACK 96 Drive Your Moon Lander – in ASCII! 94
HACK 83 Run C Code from the Command Line 85
HACK 97 Run Your Own BBS 95
HACK 84 Edit Binary Files with hx 85
HACK 98 Explore, Expand, Exterminate: Play Star Ruler 2 95
HACK 85 Edit Your Text Files with Textosaurus 86
HACK 99 Edit Files Like It’s the 90s Again 96
HACK 86 Manage Your git Repository with GRV 86
HACK 100 Read Old Unix Books 96
HACK 87 Make git Interactive 87
HACK 101 Pick a Retro Screensaver 97
HACK 88 Replace hexdump with a Colorful Tool 87
HACK 89 Reverse Engineering with Cutter 88
HACK 90 Textadept Works in Graphics and 89 WELCOME 3
Text Mode
MASTHEAD / AUTHORS 98
HACK 91 Perform Dynamic Code Analysis with 89
SystemTap
HACK 92 Count Lines of Code in Your Project 90
HACK 93 Fix Disturbing Indentation and More 90

101 LINUX HACKS – 2020 EDITION 5


Your desktop environment
HACK 1
is more than a window Curing the Caps Lock Disease 7
manager: When you pick the
HACK 2
right tools and know how to Latte Dock: The Best Dock for KDE 8
personalize and use them, HACK 3
you’ll be more productive and KDE Window Hacking 8
do things with your computer HACK 4
that others deem impossible. Global Menu for KDE Programs 9

For starters, get rid of Caps HACK 5


Save Storage with Advanced 10
Lock. Image Formats
HACK 6
Zoom Everything on Your Screen 11
HACK 7
Zoom into the Gnome Desktop 12
HACK 8
Use Virtual Desktops, Memorize the Hotkeys 13
HACK 9
Caffeine Helps Your Linux PC Stay Awake 14
HACK 10
What Would That Resolution Look Like? 14
Image © Lightwise, 123RF.com

6 101 LINUX HACKS – 2020 EDITION


DESKTOP MODDING

Curing the
Caps Lock Disease
HACK 1 Figure 2

Are you old enough to say “the problems


with keyboards started when IBM moved the func-
tion keys from the left (Figure 1) to the top”? That hap-
pened in 1986 when the IBM PC AT replaced the PC XT
and the then-leading personal computer manufacturer
introduced the new keyboard layout, which has re-
mained the standard ever since. New functions in Micro-
soft Windows 95 made keyboard producers add three
new keys, and some modern keyboards have a Fn key in-
spired by such keys on restricted notebook keyboards.
But what’s the
Figure 1
worst problem with
keyboards both old
and new? In my
view, it is certainly The conservative treatment of the
the Caps Lock key. Its “Caps Lock disease” is to transform
purpose is to mess the key into a second Left Shift key.
up text you enter in That way, if you press Caps Lock in-
an Office document CC0 (public domain)
stead of Shift, you will still get a capi-
or on the command talized letter. You can use the xmodmap
line when you type without looking at the screen. command to change the key’s table entry:
Once activated, it will transform every lowercase let-
xmodmap -e "keycode 66 = Shift_L NoSymbol Shift_L"
ter into an uppercase one and vice versa. When you
detect the mistake, you need to get rid of all the en- If instead you want to completely disable the key, use
tered text and type it again (with Caps Lock off). Only the following shorter command:
network trolls who feel like typing whole paragraphs
setxkbmap -option caps:none
in all caps can appreciate that key. (If you rely on ac-
cessibility tools to help you type, Caps Lock is not After that change, pressing Caps Lock will have no ef-
helpful either, since it does not grant access to the fect whatsoever. Notice however that the change only
secondary bindings of non-letter keys. Instead, sticky affects the current X session. In order to make it perma-
Shift, Ctrl, and Alt keys do the job.) nent, you need to use your desktop environment’s au-
The xmodmap tool can inform you about the Caps Lock tostart mechanism. Adding the line to ~/.bashrc will not
key’s current behavior: work (properly): It will generate a lot of error messages,
because both commands expect a working X server
$ xmodmap -pke | grep -w 66
and a properly set DISPLAY variable; they fail when run
keycode 66 = Caps_Lock NoSymbol Caps_Lock
in text mode (see the “Killing Caps Lock in Text Mode”
The culprit is Caps_Lock. There are several ways that you box). Here’s how to make it permanent:
can get rid of it: Some of the desktop environment con- • Under KDE, create a file killcapslock.sh in your ~/bin/
figuration tools let you configure Caps Lock behavior folder and write the command into that file. Then,
(Figure 2 shows how to do it under Linux Mint’s Cinna- from the Start menu, search for “startup,” pick Auto-
mon), but I will focus on two quick solutions that re- start, then click Add Script, and enter the full path to
quire single shell commands. killcapslock.sh.
• Gnome users run the gnome-session-properties com-
Killing Caps Lock in Text Mode mand. Click on Add, then provide an informative title
in the Name field, and enter the command in the
If you happen to work in a text mode session (without X), the Command field.
xmodmap and setxkbmap commands will be useless – they cannot • Under Cinnamon, search for “startup,” pick Startup Ap-
influence the keyboard settings in console mode. Instead, what plications, then click the + button, and choose Custom
you need is loadkeys. Normally, this program is used to switch Command. Provide an informative title in the Name
between alternative local keyboard layouts, such as: field, and enter the command in the Command field.
sudo loadkeys us # US layout Now the future looks bright: Caps Lock won’t trouble
sudo loadkeys uk # UK layout you again.
sudo loadkeys de # German layout
By Hans-Georg Eßer
However, you can also use it to change a single key setting. Use
the following command to make Caps Lock behave like Shift:
echo -e "keymaps 0-127\nkeycode 58 = Shift" | sudo loadkeys -

101 LINUX HACKS – 2020 EDITION 7


DESKTOP MODDING

H AC K
Latte Dock: The Best Dock for KDE 2
One of the best things about KDE – and there are Figure 3
many – is the functionality contained within the panel.
It works well horizontally stretched across the entire
display or as a small panel across half. It works well
vertically, as well as floating in the middle. You can
have more than one, and each one can be configured
to do as much or as little as you want. But the default
KDE panel can still feel a little utilitarian, partly be-
cause it’s nothing like the panel in macOS. This is why
there are perhaps so many panel replacements that
look more like Apple’s similar dock.
Latte Dock is the best alternative I’ve seen for KDE
(Figure 3). Unlike most panel replacements, it has got a
comprehensive set of configuration options that mimic
much of what KDE’s regular panel does. By default, it
will appear in the middle of your screen’s bottom bor-
der when your pointer gets close. The icons for running
applications appear and enlarge as you roll over these
in beautifully animated transitions. The same happens
when you open the configuration panel, which allows
you to change the location of the panel and its align-
ment. You can place it anywhere and get the icons to
center exactly as you wish. You can also control the
transitions and zoom levels, as well as enable or dis- To try it, install the package via sudo zypper in
able the panel background and running application latte-dock (openSUSE) or sudo apt install latte-dock
highlight modes. On a high DPI desktop, it looks abso- (Ubuntu, Mint, and Debian). Then run the latte-dock
lutely fantastic. The SVG icons scale perfectly, and re- command.
placing the old KDE panel with this is a serious tempta-
tion, despite it not fully supporting functional applets By Graham Morrison
https://store.kde.org/p/1169519/
like monitoring tools or desktop pagers.

KDE Window Hacking


KDE’s window manager lets you do lots of crazy
things to windows. Some of them are pretty useful.
Alt+F3 again to open the win-
dow’s configuration menu.
HACK 3
Right-click on any window’s titlebar, and a menu will Within Window Specific Settings (or Special
pop up. Apart from the options to minimize, maxi- Window Settings) and Application Specific Settings
mize, and close the windows, you’ll notice the More (also called Special Application Settings), you have all
Actions option. The Keep Above Others and Keep manner of options to fix the application window’s po-
Below Others options are self-explanatory, but you sition and size. You can make a window stick to a cer-
can also make a window Fullscreen, and it will be tain area of your screen and become unmovable. At
maximized; the application’s titlebar and any other the same time, you can adjust its size to the pixel. You
desktop elements (like panels) will disappear, giving can configure things so that, when you launch a cer-
you maximum workspace. If the application doesn’t tain application, it always opens in a certain place,
offer you a way to exit full screen mode, press Alt+F3 maximized, or shaded. You can make the application
and use the menu to deactivate it. so it won’t close, or you can choose actions from
You can also “shade” the window, which means it dozens of other options.
will roll up like a blind, leaving only the titlebar visible.
Another alternative is to remove the border and titlebar, By Paul Brown
leaving a bare window with no decorations. To recover
borders and the titlebar, select the window and press

8 101 LINUX HACKS – 2020 EDITION


DESKTOP MODDING

Global Menu for KDE Programs


HACK 4
A default KDE Plasma desktop has a panel at the bot- gets. A bar with all the available widgets will show up
tom of the screen, a start button holding menus at the on the screen’s left. You can narrow your search down
bottom left, and a tray on the right – all quite conven- by typing “global” into the search box. When you see
tional, boring, and even Windows-y (see Figure 4). But the Global Menu widget, double-click on it, and it will
Plasma can be configured to look like anything, even be added to the panel. (You may think that the action
like Ubuntu’s defunct Unity, Gnome, macOS, or what- was not successful – at least that’s what happened to
ever else rocks your boat. me. If a small circled “1” appears in a corner of the
widget’s icon, you were suc-
Figure 4
cessful even though the panel
still looks empty.)
Running applications con-
tinue to have their own me-
nubars. Close and reopen
them, and you’ll see how now
their menus have moved from
the application window to the
upper panel you just made.
With KDE Plasma versions be-
fore 5.13, this only works with
native KDE apps, but newer re-
leases support the feature for
other programs, too. This is es-
pecially relevant for GTK-based
applications.
To make the effect even more
striking, click the Start menu on
the bottom panel and pick Sys-
tem Settings or Settings | Con-
Both Unity and macOS use a global menu: It is the figure Desktop. Under Workspace Theme, choose
menu that appears in a bar at the top of the screen and Breeze Dark and click Apply. You will end up with
shows a selected application’s options, instead of hav- something like Figure 5.
ing them in a bar along the top of the application.
To create global menus in Plasma, first right-click in By Paul Brown
any free space on the Plasma
desktop and select + Add Figure 5
Panel | Empty Panel from the
pop-up menu. Usually, the
panel will appear at the top of
the screen, because the bot-
tom is already filled with the
default Plasma panel. If it has
popped up anywhere else,
click on the hamburger menu
(the button with three horizon-
tal lines at one end of the
panel) and then click and hold
the Screen Edge button and
drag the panel to the top.
Once you have placed the
panel, click on the hamburger
menu on the right of the panel
again and click on + Add Wid-

101 LINUX HACKS – 2020 EDITION 9


DESKTOP MODDING

Save Storage with Advanced Image Formats


There are dozens of graphic file formats, but in real life most
HACK 5
flif binary to archive images and recover them later using
photos are stored in JPEG, whereas screenshots and web the command:
graphics look best in Portable Network Graphics (PNG). Let’s
$ ./flif -d input.flf output.png
skip other specialized file formats such as TIFF or numerous
RAW flavors from different camera vendors for the sake of However, it is possible to view the FLIF-encoded images
brevity. Historically JPEG compression was developed as an using a separate tool called UGUI:FLIF, which is based on
optimal trade-off between higher quality and smaller file HTML and JSON.
size, which used to be a headache in the days of dial-up In-
ternet and small hard drives. JPEG is lossy, but it delivers … and JPEG with Lepton
very natural-looking images even with aggressive compres- Lepton is a compression and decompression algorithm that
sion ratios. On the other hand, PNGs have gracefully solved was open sourced by the Dropbox company. The cloud-
the problem of transferring lossless bitmap graphics across based storage provider has proprietary code in its core, but
networks thanks to its built-in compression. otherwise it is very friendly to the Linux community. Lepton
In modern times, the technology has advanced even fur- does with JPEG what FLIF does with PNG. Since the two
ther, and now we have two projects on GitHub that outper- image formats are completely different, we cannot compare
form the above two formats in terms of compression effi- their performance directly.
ciency: Lepton and FLIF. Lepton offers a lossless compression for JPEGs, which re-
sembles convenient archiving. But unlike putting a JPEG file
Replace PNG with FLIF … into a .zip or .tar.gz archive, which makes very little sense for
FLIF is a new lossless image format based on Meta-Adap- saving disk space, Lepton really makes the output file smaller.
tive Near-zero Integer Arithmetic Coding (MANIAC) com- Dropbox published a comprehensive article that describes
pression, which outperforms PNG, FFV1, lossless WebP, how the algorithm works, pointing out optimizations they im-
lossless BPG, and lossless JPEG2000 in terms of compres- plemented in encoding 8x8 pixel blocks (which are used in
sion ratio. To see how to use it, download the code snap- JPEG compression).
shot from the project’s website, and make sure that you Some distributions provide Lepton packages (try sudo apt
have the libpng-dev package (or similar) and then type install lepton on Ubuntu and Mint), but if yours doesn’t,
make. In a few seconds, you’ll get the compiled flif binary, building Lepton from source is very straightforward.
which acts as a FLIF image encoder and decoder. The syn- Download the sources and run the following commands:
tax is very simple:
$ ./autogen.sh; ./configure; make; make check
$./flif input.png output.flf $ sudo make install

and the traditional ./flif --help for the list of options, such Lepton has a very simple syntax, and while you can
as interlacing, palette, quality, and more. Keep in mind that enjoy many of its advanced features, you can start with
FLIF currently can convert only PNG, PAM, and PNM file this template:
formats, so it makes sense to compare FLIF outputs with
$ lepton input.jpg output.lep
PNG as the most widespread format from the above list. In
our tests, lossless FLIF output was smaller than PNG with If your JPEG file uses progressive encoding (Gimp does it
the highest compression ratio and comparable with JPEG by default when exporting to JPEG), you’ll need the appro-
in terms of file size (Figure 6). The only problem is that you priate extra option:
cannot view .flf files, so right now you can only use the
$ lepton -allowprogressive input.jpg output.lep
Figure 6
To restore (decompress) your file, use the same command but
switch the .lep and .jpg order. Lepton is quite effective: It can
trim down your files by 15-20 percent without losing quality.
The reason why you might want to use FLIF, Lepton, or both
is precisely one. The volume of images that have to be stored
is growing faster and faster. Converting some thousands
JPEGs to the new Lepton format lets you free extra disk
space. Lepton files are about 20 percent smaller than their
source JPEGs, so the savings can be be very noticeable.

By Alexander Tolstoy
https://flif.info/
http://flif.info/UGUI_FLIF/
https://blogs.dropbox.com/tech/2016/07/
https://github.com/dropbox/lepton

10 101 LINUX HACKS – 2020 EDITION


DESKTOP MODDING

H AC K
Zoom Everything on Your Screen
Many applications let you zoom the content they dis-
Fix KDE
KDE, like Gnome, needs some
6
play; for example, in Firefox and LibreOffice, you can extra work so you can zoom smoothly. You will need
press the Ctrl key and move the mouse wheel to in- the xbindkeys tool and also dbus-send or qdbus (which
crease or decrease the zoom level. should be preinstalled with KDE).
But sometimes you may need a different kind of Try the following commands:
zooming. Imagine for example that you show a Libre-
qdbus org.kde.kglobalaccel /component/kwin U
Office Impress presentation to your audience, and
invokeShortcut view_zoom_in
there’s a detail you want to draw attention to on your
dbus-send --type=method_call --dest=U
slide. You’ll have to leave presentation mode and
org.kde.kglobalaccel /component/kwin U
zoom into the slide in Impress’ editing mode, which is
org.kde.kglobalaccel.Component.invokeShortcut U
not really professional. Luckily, it is possible to zoom
string:view_zoom_in
the whole desktop. Depending on your desktop envi-
ronment, you may have to activate the feature. If one of them zooms a tiny bit into the desktop,
you’re ready for the next step – on my openSUSE test
Cinnamon Can Do It, Gnome Almost machine only the second command worked. If none
If you use Cinnamon, open Preferences | Accessibility of them works, try to enable the zoom effect as ex-
and click on Enable zoom. Then change the Mouse plained in the “KDE: Enable the Zoom” box.
tracking mode to Cursor pushes contents around: First of all, zoom out again: Just repeat the com-
That way mouse movements will not change what mand that worked, but replace view_zoom_in with view_
you see on the screen – until you reach the borders. zoom_out. Then create (or open) the file .xbindkeysrc in
The default key, called Super, is the Win key next to your home directory and add the following four lines:
Left Alt. So keep Win pressed and use the scroll wheel
to zoom into your desktop and out again. "<WORKING COMMAND ENDING IN view_zoom_in>"
Gnome has a preconfigured zoom feature; it is alt + b:4
one of the accessibility functions. Press Win+Alt+8 "<WORKING COMMAND ENDING IN view_zoom_out>"
to enable zooming, then Win+Alt+= to zoom in, alt + b:5
and Win+Alt+- to zoom out. However, you cannot
configure it to change the zoom in small steps, and The first line contains the command for zooming in,
you cannot use the mouse. If you want that to hap- placed between double quotation marks; the third line
pen, check the hack on the next page – it is more is the same but ends in view_zoom_out. Lines 2 and 4
advanced. hold the mouse/key combinations: alt is the Alt key (it
will work with both the left and
KDE: Enable the Zoom right Alt keys); b:4 and b:5 are
Scroll Up and Scroll Down on
the mouse wheel. (If you want a
If zooming does not work at all or if you the zoom is active and you try to move
want to fine-tune how much KDE the mouse out of the visible area, KDE more complicated combination,
zooms in/out with one wheel move, scrolls so that the mouse pointer stays try control + alt + b:4, for exam-
open KDE’s System Settings and go to visible. But try the other options, too: ple; the xbindkeysrc man page ex-
Workspace | Desktop Behavior. Then It’s a matter of taste. plains how to use the
click on Desktop Effects. Figure 7 other modifier keys.)
Make sure that Zoom Now start xbindkeys (if
(Magnify the entire desk-
you already use it, kill it
top) is activated and click
on the settings icon. first) and check whether
zooming with Alt and
In the new dialog win-
dow (Figure 7), change the mouse wheel works.
the Zoom Factor value If so, make things per-
to something between manent by adding
1.01 and 1.20. My sug- xbindkeys to your auto-
gestion for the Mouse start programs.
Tracking option is to set
it to Push: then, when By Hans-Georg Eßer

101 LINUX HACKS – 2020 EDITION 11


DESKTOP MODDING

HACK 7 Zoom into the


Gnome Desktop
Gnome’s built-in zoom function, which you enable via
Win+Alt+8, lets you zoom in very large steps: from a 1.0
ZOOM_MIN=1.00
ZOOM_MAX=2.00
ZOOM_ADJUST=0.05

into a new file ~/.local/zoom-config. This is where you’ll


zoom factor to 2.0, than 3.0 and so on (Figure 8). While be able to change the behavior of your new super
you can make things large quickly, it lacks the smooth zoom: The ZOOM_ADJUST value will be added or subtracted
transition helpful in a presentation. until you hit the maximum or minimum value.
Luckily, Gnome’s control tool for the command line,
gsettings, lets you query and set the zoom factor with Bring in the Mouse
fine precision. For example, You can already test the new feature by calling ~/bin/
zoom-in and ~/bin/zoom-out from the shell. If ~/bin is in
gsettings set org.gnome.desktop.a11y.magnifier U your PATH, the shell should find the scripts when you
mag-factor 1.15 call them as zoom-in and zoom-out.
But we want mouse support. If you’ve read in the pre-
will set the zoom to 115 percent. If you want to query vious hack how to react to mouse wheel movement,
the current value, replace set with get, and drop the you already know what’s coming: You need the xbind-
number. Now, adjusting the current value (say, in order keys tool that you have to install with your package
to add or subtract 0.01) is not so simple. I’ve created manager. Then create an ~/.xbindkeysrc file with those
two simple Bash scripts which do the job; they use the four lines:
terminal calculator bc to perform the floating-point ad-
ditions, subtractions, and comparisons – the shell can "~/bin/zoom-in"
only work with integers. alt + b:4
Put the code from Listings 1 and 2 into files ~/bin/ "~/bin/zoom-out"
zoom-in and ~/bin/zoom-out, respectively, and make alt + b:5
them executable (chmod a+x ~/bin/zoom-*). (Depending
on your Linux distribution, you may have to create the Like in the KDE zooming hack, this assigns the zooming
~/bin folder.) Then put the three configuration lines commands to the mouse/key combinations, where b:4
and b:5 are Scroll Up and Scroll Down on the mouse
wheel. Check the xbindkeysrc man page for more com-
Listing 1: zoom-in
plex key combinations.
Now start or restart xbindkeys and check whether
#!/bin/bash
zooming with Alt and the mouse wheel works. If so,
[ -r ~/.local/zoom-config ] && source ~/.local/zoom-config
ZOOM_MAX=${ZOOM_MAX:-1.10} make things permanent by adding xbindkeys to your
ZOOM_ADJUST=${ZOOM_ADJUST:-0.01} autostart programs.
SCHEMA=org.gnome.desktop.a11y.magnifier
zoom=$(gsettings get $SCHEMA mag-factor) Negative Zoom
check=$(echo "$zoom < $ZOOM_MAX" | bc -l)
By the way, it’s also possible to use zoom factors below
if [[ $check -eq 1 ]]; then
zoom=$(echo "scale=2; $zoom + $ZOOM_ADJUST" | bc -l) 1.00 (change ZOOM_MAX to 0.5 to try this out), but it
gsettings set $SCHEMA mag-factor $zoom doesn’t make much sense – the desktop will shrink and
zoom=$(gsettings get $SCHEMA mag-factor) fill the upper left corner of the screen and the rest of the
fi monitor displays gets filled with gray.

Listing 2: zoom-out By Hans-Georg Eßer

#!/bin/bash Figure 8
[ -r ~/.local/zoom-config ] && source ~/.local/zoom-config
ZOOM_MIN=${ZOOM_MIN:-1.00}
ZOOM_ADJUST=${ZOOM_ADJUST:-0.01}
SCHEMA=org.gnome.desktop.a11y.magnifier
zoom=$(gsettings get $SCHEMA mag-factor)
check=$(echo "$zoom > $ZOOM_MIN" | bc -l)
if [[ $check -eq 1 ]]; then
zoom=$(echo "scale=2; $zoom - $ZOOM_ADJUST" | bc -l)
gsettings set $SCHEMA mag-factor $zoom
zoom=$(gsettings get $SCHEMA mag-factor)
fi

12 101 LINUX HACKS – 2020 EDITION


DESKTOP MODDING

H AC K
Use Virtual Desktops, Memorize the Hotkeys
Do you work with virtual desktops? If not, try them. As- also the right place if you want to add
8
suming that you do more with your Linux machine than more desktops. Add the Workspace Switcher
run a browser, a mail client, and LibreOffice, it is likely applet to your panel so that you can switch the desktop
that your desktop becomes cluttered with windows. with a single click.
Maybe you minimize some of them to get the chaos If you would prefer to use Ctrl+F1, Ctrl+F2, etc. on Cin-
under control. Connecting one or two extra monitors namon, open Preferences | Keyboard from the menu,
would improve the situation, but that requires free switch to the Shortcuts tab, and pick the Workspaces |
space on your desk (and money). Direct Navigation category: You can now define your
Instead of physical, go for virtual. Linux desktop envi- own hotkeys that will let you switch to a desktop directly.
ronments like KDE, Gnome, and most others have a
feature called virtual desktop. You can have two or Gnome
more independent areas where you can place your win- With a Gnome installation, Ctrl+Alt+Down (or
dows. There’s no need to minimize them; just spread Win+PageDown) will bring you to a fresh, new desktop
them out on as many desktops as you need. – but only, after you’ve opened at least one window on
the current desktop. With Ctrl+Alt+Up (or Win+PageUp)
KDE you can go back to the previous desktop.
On KDE, press Ctrl+F2 to reach a second (empty) desk- By default, Gnome only has a hotkey for switching to
top; Ctrl+F1 brings you back to the original one. If that the first desktop (Win+Home), but when you open the
doesn’t work, virtual desktops are disabled. Open the settings and go to Devices | Keyboard, you can add hot-
start menu, type virtual, and select the Virtual Desktop keys for further desktops. Ctrl+F1 is not available be-
configuration entry. Then set the Number of desktops cause some Gnome applications use it to show the
to some value that makes sense: I like to have eight of shortcuts available in that program.
them (Figure 9). KDE automatically creates more hot- In all desktop environments, each window’s menu
keys (Ctrl+F3, Ctrl+F4, etc.). will let you move a window to a different workspace,
and there may even be hotkeys for doing that without
Figure 9 the mouse. The developers have come up with multiple
ways to configure your desktop’s behavior (e.g., some
program may always open on the second desktop).
It takes some time to get used to multiple desktops,
but once you know the most important hotkeys by
heart, you’ll do many things faster than before.
If you tend to switch between KDE, Gnome, and other
environments or window managers often, it might
make sense to change the settings so that working with
virtual desktops feels the same in every environment –
but if you’ve already chosen your favorite desktop, sim-
ply get to know its default hotkeys: They are likely to
stay the same for future releases.

By Hans-Georg Eßer

If you prefer to move from the current desktop to a Figure 10


“neighboring” desktop, you can assign hotkeys to
those actions. The configuration window, where you
can change the number of desktops, has a Switching
tab where you can add four key combinations for
switching left, right, up, or down – KDE organizes its
desktops in a matrix.

Cinnamon
If you run Cinnamon, press Ctrl+Alt+Right to move to
the next desktop. Cinnamon calls them “workspaces,”
but the concept is the same. Press Ctrl+Alt+Up to view
a summary of all desktop contents (Figure 10). This is

101 LINUX HACKS – 2020 EDITION 13


DESKTOP MODDING

Caffeine
Helps Your
HACK 9 H AC K
Linux PC Stay
Awake
What Would That
Resolution Look Like?
There’s a command-line tool called
10
Sometimes you don’t want your computer to start xrandr that can work magic in configuring your desk-
the screensaver (and possibly lock you out) or acti- top. You may already be aware of its capability of
vate some energy-saving mode and suspend. For ex- changing the resolution via a command similar to
ample, I used to get very annoyed when I brought my
notebook to a classroom and stepped away to ex- xrandr --output DP-1 --mode 2560x1440
plain something on the blackboard – only to find a
few minutes later that the beamer no longer showed and even defining completely new resolutions using
my presentation slides. For me, that’s a problem of a modeline (which you’ll have to create with a tool
the past, because I use Caffeine. There are several like cvt or an online modeline generator).
tools with that name (including ones for macOS and But xrandr can do more. A not so well-known trick
Windows); pick the right one for your desktop! is using a scaling factor to make the desktop bigger
If you work with KDE or Cinnamon, you can install (in pixels) than the maximum resolution offered by
the caffeine package (do not confuse this with Kaf- your monitor. Try the following command:
feine, a KDE media player). Then run caffeine-indica-
tor (if you like it, add it to your auto-start applica- xrandr --output DP-1 --scale 1.5x1.5
tions). This will add a new coffee cup icon to your
panel. Clicking it opens a context menu from which (replace DP-1 with your monitor’s ID). If you see no ef-
you can select Activate (or Deactivate). When Caf- fect, use a number closer to 1.0. On my test machine, I
feine is active, you can see steam rising from the cup could not move beyond 1.7x1.7, but even with that
(Figure 11). scaling factor, I saw an incredible 4352x2448 resolu-
As a Gnome user, visit the Gnome Shell Extensions tion. Of course, the monitor could not display so many
website, search for Caffeine, and enable the exten- pixels, but internally the X server did render an image
sion – this will work best if you have installed the of that size and then created a smaller version for the
chrome-gnome-shell package and the Gnome Shell inte- monitor. I made a screenshot (Figure 12) to check:
gration plugin for Firefox. Using this is even simpler:
Just click it to toggle on/off. $ file megares.png
megares.png: PNG image data, 4352 x 2448, U
Figure 11 8-bit/color RGB, non-interlaced

If you enable zooming, you can even work with the high
resolution and still make the window contents visible.

By Hans-Georg Eßer
https://wiki.archlinux.org/index.php/Xrandr
https://arachnoid.com/modelines/
One last hint: Don’t try an online search for
“Cinnamon+Caffeine” – I did that, and it was com-
pletely useless: Google gave me coffee recipes and Figure 12
health-related articles.

By Hans-Georg Eßer
https://launchpad.net/caffeine
https://extensions.gnome.org/
https://www.zhornsoftware.co.uk/caffeine/
http://lightheadsw.com/caffeine/

14 101 LINUX HACKS – 2020 EDITION


Find a document on your computer, search for a good font
online, organize your project, and track the time you spend
on individual tasks: Nothing in this list is spectacular, but if
it must be done, do it well. We suggest how.

Image © lightwise, 123RF.com

HACK 11
Find Your Files with DocFetcher 17
HACK 12
Organize Your Ideas with Zim, the Desktop Wiki 18
HACK 13
Track Time with Chrono 20
HACK 14
Launch Programs Inside a VirtualBox VM 20
HACK 15
Create a Cheap Timer with sleep 22
HACK 16
Finding and Installing Online Fonts 23
HACK 17
Sync Up Your Phone and Linux Desktop 23
HACK 18
Self-Organization with GTG 24

16 101 LINUX HACKS – 2020 EDITION


GETTING STUFF DONE

H AC K
Find Your Files with DocFetcher 11
I like empty partitions: When I install a new Linux distri- the program, you need Figure 1
bution on a test machine and log in for the first time, I not care about complex
sometimes take a look at the empty Documents folder java command-line op-
and think: How beautiful! Let’s put two or three files in tions, just launch the
that folder and enjoy the clarity. DocFetcher-GTK3.sh script
On my real computers, things are different. The fol- to start the program.
lowing code was run on a notebook that holds only DocFetcher can index
some of my files: all sorts of files (Figure 1),
including PDF, LibreOf-
[esser@m2:~]$ for dir in Data Documents; do printf \ fice, and Microsoft Office
"%-9s: %5d files in %5d folders, " \ files (both classic and
"$dir" \ XML-based). It will also
$(find $dir -type f | wc -l) \ look at ID3 tags embed-
$(find $dir -type d | wc -l); \ ded in MP3 files, so you
du -sh $dir | cut -f1; done can later search for MP3
Data : 96477 files in 10818 folders, 58G songs by title or album
Documents: 1499 files in 335 folders, 14G name even if these meta-
data do not appear in the
(If you like what this command does, put it in a shell filenames. Normally,
script.) Creating or collecting thousands of files means DocFetcher uses the file
trouble when you need to find one of them. Linux gives extensions (like .odt,
you a few built-in options for searching, so you might .docx, etc.) to decide what
want to start with them. kind of file format it is looking at, but you can enable a
By cleverly combining shell commands such as Detect mime type option if you tend to store files with
• ls -lR (recursive listing) non-standard or without file extensions.
• grep (filter search terms) Once you’ve built the index, you can start your
• find (search for files with specific properties) searches. Results will show up in a table, and when you
• locate (use a pre-built database to search) select a particular result, DocFetcher gives you a preview
you can already do a lot. locate is especially helpful, be- (Figure 2). Depending on the file type, it is possible to
cause it gives you instant replies, whereas running find take a look at all search hits in a document: Imagine a
can take many minutes if you throw it at a large folder big PDF file that frequently contains your keyword.
hierarchy. It’s likely that you have to install locate first,
typically from the mlocate package, and you need to By Hans-Georg Eßer
regularly run a file database updater called updatedb. http://docfetcher.sourceforge.net/

More Comfort, More Features Figure 2


All the helpers mentioned so far search
for patterns in the filenames – they com-
pletely ignore the contents. But in order
to be truly helpful, a tool has to find files
that contain some of the searched key-
words. To do this live would mean ana-
lyzing the whole disk for every search,
and that would take much too long. The
solution is obvious: Build an index, like a
search engine does for web pages.
That’s precisely what you get with
DocFetcher. The Java program runs on
all operating systems and needs no clas-
sical installation; you can even put it on
a DVD together with an index that leads
to other files on the DVD. For running

101 LINUX HACKS – 2020 EDITION 17


GETTING STUFF DONE

Organize Your Ideas with Zim, the Desktop Wiki


Install and use Zim, a handy tool that enshrines the
principle of the wiki on your Linux, macOS, or Windows
using File | Save version.
HACK 12
Although Zim is intended for local use and focused on
desktop. The word “wiki” comes from Hawaiian and the single user, the versioning feature does provide an
means “fast.” The term refers to a system in which option for collaboration.
HTML documents on the Internet are both read and ed-
ited using a web browser. The most prominent exam- Use Plugins to Extend Zim
ple of a wiki is the Wikipedia online encyclopedia. You can extend the Zim desktop wiki using around 30
Other wikis inhabit both large and small projects preinstalled plugins. In addition, you will find numer-
within the open source universe. Many wikis are used ous plugins by external developers on GitHub. They in-
for documentation purposes. Debian, Ubuntu, and Arch clude support for formulas, charts, and music notation,
Linux are known for their comprehensive wikis. A wiki’s plus a source code view and a spell checker. Zim can
main task is to organize content in a structured way. “print” to an HTML file and has a table editor, keyword
Many of the popular wiki tools are designed for collabo- management tool, and calculator (Figure 4).
ration over the network, but the wiki concept also pro- You can format text in various ways and supplement
vides benefits for a single user working at a single desk- it with pictures, lists, enumerations, and annexes. In the
top. Zim is a handy desktop wiki that is useful for brain- Tools | Custom tools menu, Zim gives users the oppor-
storming and knowledge gathering, as well as building tunity to create their own extensions. In addition, you
task lists and organizing documents and books. can launch your own web server in the Tools menu and
To operate Zim, you do not need a web server or a thus view your Zim wiki in the browser.
database. The Zim desktop wiki is available for Linux,
Mac OS X, and Windows; it uses the same data format First Launch
across all platforms. As a comparison, WikidPad, a tool When you launch Zim, it asks you where to store the
from the Windows world that also runs on Linux, is data. Zim then creates a first notebook at the defined lo-
conceptually similar, but Zim goes well beyond Wikid- cation, names it Home, and adds the .txt suffix. Home is
Pad’s capabilities. You can find Zim packages in most then opened as a notebook whenever you launch Zim.
Linux distributions’ repositories. In theory, you could now just start typing in Zim’s
WYSIWYG editor; however, it makes sense to define
Markup and Versioning
Every page created in Zim is stored on disk as a text file Figure 4
with a wiki markup (Figure 3). The program organizes
these files into notebooks, which can best be compared
with a traditional wiki’s database. To create a new page,
first create a link to the initially nonexistent page. You
can then add references to other sites to the page’s text.
Zim also supports plugins, including one for version-
ing that stores the editing history for a document in the
form of the Git, Bazaar, or Mercurial version control
systems (VCSs). You need to install the VCS locally on
the computer. As soon as you enable the plugin via the
settings, you can store a document’s current version

Figure 3

18 101 LINUX HACKS – 2020 EDITION


GETTING STUFF DONE

the your wiki’s structure first. For each project (say, a lines using the Tab key. Zim continues these lists until
book project, a collection of links, or a to-do list), you you insert a blank line.
will first want to create a separate notebook to provide If you enable the tasklist plugin, you can expand on
an easy means for distinguishing the content. this function. Supplement the bullet by typing, say
A look at the Format menu clarifies the formatting
[] Task 1 12-23-2019 !
that you can apply to the currently selected text via the
toolbar or the usual keyboard shortcuts. If you do not to include a due date and a priority (each additional ex-
select a text passage explicitly, Zim formats the word clamation mark decreases the priority), and then dis-
under the cursor. In addition to the usual markup fea- play all of the project’s tasks by pressing the Task List
tures (such as bold, italics, underline, and striketh- button in the toolbar.
rough), you will find five levels of headings, and you Internal links to other pages of the notebook you are
can also highlight words or text passages in yellow. currently editing, or to another notebook, mean that Zim
Highlight source code by means of a fixed-width font also has a powerful organizational function. You can ei-
to set it apart from the remaining text. Charts, lists, and ther press Ctrl+L to create a reference to a nonexistent
enumerations complete the picture. If you use the right page (Zim then creates the page automatically) or link to
plugin, you can also embed calculations in Zim and an existing page. The link appears in blue in the text. Al-
represent formulas. The same applies to Gnuplot ternatively, you can create links without dialogs or short-
graphics and images. You can also use an arbitrary cuts using CamelCase or WikiWord notation. For exam-
image editor while working within Zim; launch the ple, entering LinuxMagazine automatically generates a
image editor via an image’s context menu and save the link of the same name and the appropriate page due to
results in Zim. The original image is not changed. the uppercase M. Since this feature can lead to un-
The Toggle notebook editable pen icon in the toolbar wanted links, you have the option of switching off Cam-
lets you enable write protection for the entire notebook. elCase linking through the configuration settings.
In the calendar, clicking on the icon labeled Today in the Zim automatically converts paths such as /etc/de-
active notebook generates the structure of a journal fault into links; clicking on one of these links takes you
with sub-items for the year, month, and day that you to the appropriate directory or opens the associated
can use as a diary. file. Local files can be saved as file attachments using
While you are working, Zim continuously stores the Tools | Attach. Zim assigns the attachments to the arti-
current state. Finished documents can then be exported cle and moves them to the new instance if you copy or
as HTML, LaTeX, or Markdown. The tab bar below the move the original. If you copy the wiki to a second com-
toolbar, which grows to accommodate the increasing puter, the attachments remain in place.
number of projects, shows the state of editing and can- The data is stored in text format with wiki markup,
not be deleted during the session. thus opening up numerous possibilities. For example,
you can use a makefile to generate a website from the
Lists, Links, and Annexes wiki entries. Thanks to the text format with wiki
If you start a line with an asterisk (*) or square brack- markup, you can use the stored information on all three
ets ([]), Zim automatically switches to a mode in supported operating system platforms. The flexible
which it is very easy to create bullet lists or checklists methods for linking let you build rich documents that
(Figure 5). To add a little more structure, indent the you either use as a preliminary stage for processing in
other applications or finalize in Zim.
Figure 5 Zim is easily accessible, revealing the power of its op-
tions only when needed. Once you have discovered the
versatility of Zim, you will not want to be without this
desktop wiki, which has been in development since
2005. Even Linux beginners who are familiar with Mi-
crosoft OneNote are likely to quickly find their way
around Zim.
Zim impresses as a complete package, and if you like
the organizational structure of wikis, you are bound to
feel right at home.

By Ferdinand Thommes
http://www.zim-wiki.org
https://en.wikipedia.org/wiki/WikidPad
https://github.com/jaap-karssenberg/zim-wiki/wiki/Plugins
https://en.wikipedia.org/wiki/Camel_case

101 LINUX HACKS – 2020 EDITION 19


GETTING STUFF DONE

Track Time with Chrono


If you work from home, or work for yourself, a time
HACK 13
Chrono is a command-line time tracker that offers a
tracker is an essential tool that can not only help with comprehensive set of features while remaining simple
productivity, but help when it comes to invoicing cli- and quick enough to slot into even the busiest sched-
ents, estimating the timing and velocity of a project, ules. With the tool installed, you start a project by typ-
and tracking your own efficiency. But to be effective, a ing chrono start <project name> followed by a + tag (Fig-
time tracker has to be as efficient and as transparent ure 6). You can now add comments just as you might
as possible. In particular, it needs to be as simple as with a git commit while programming. You can then
possible. Too complicated, and the effort required to review your comments, and when you’re ready to
maintain a log, or context switch between your work move on to something else, simply type chrono stop.
and your time tracker, makes the tracking process it- After this, start working on a new project with the start
self inefficient. This is especially true if you’re working command. You can also add to a previously created
on the command line and need to switch to a GUI. project by reusing its project name (typing chrono proj-
This makes Chrono ideal. ects lists all the projects about which Chrono knows).
Each new chunk of work is called a
Figure 6 frame, and each has its own
unique identifier, regardless of the
project to which it belongs. This is
great for invoicing, and you can al-
ways go back and add or edit a
specific frame if you need to add
your own notes. The entire work
output can then be displayed by
typing chrono log.

By Graham Morrison
https://github.com/gochrono/chrono

H AC K
Launch Programs Inside a VirtualBox VM
If the Guest Additions are installed in a VirtualBox The parameters are always at
14
guest, you can start programs directly on the guest the end following the two minus signs. In the example,
with VBoxManage. The following command logs into gedit/arg0 indicates that the parameters for the gedit
the virtual machine (VM) named “Fedora 29” as the program follow next. Here, this is just the complete
user tim and a password of 123456: path to the text file that you want gedit to open.
The guestcontrol command gives an insight into the
VBoxManage guestcontrol "Fedora 29" start --exe U
state of the guest system if required:
"/usr/bin/gedit" --username tim --password 123456 U
--putenv "DISPLAY=:0"
VBoxManage guestcontrol "Windows 10" list all
When it gets there, it launches the gedit text editor. The
--exe option is followed by the full path to the program Among other things, guestcontrol also lists the current
you wish to execute. In the case of a Windows VM, you processes. If a process is hanging, you can kill it with
need to enter the backslashes in the path twice, such as closeprocess:
--exe "C:\\ Windows\\System32\\calc.exe". For a graphical
X11 application to launch on a Linux guest, you need to VBoxManage guestcontrol "Fedora 29" closeprocess U
set the DISPLAY environment variable using the --putenv --session-id 4 2022
parameter. You can also add further parameters to the
program: Via --session-id, you provide the ID of the session in
which the process is running. The number at the end
VBoxManage guestcontrol "Fedora 29" start --exe U
represents the troublemaker’s process ID.
"/usr/bin/gedit" --username tim --password 123456 U
--putenv "DISPLAY=:0" -- gedit/arg0 /home/tim/brief.txt By Tim Schürmann

20 101 LINUX HACKS – 2020 EDITION


HACK
Create a Cheap Timer with sleep
15
You call someone on the phone, but they are
busy, so the two of you agree to call again in 10
minutes. But then you forget to call, because
you’ve been too involved with some other task.
Does that sound familiar?
Solve such simple problems with an equally
simple solution: Set a timer. Now, you’ll certainly
find sophisticated timer apps for your desktop
(or smartphone), but how about simply using
tools that are already installed? Just use the fol-
lowing command to get the pop-up window in
Figure 7 after two minutes:

sleep 2m; gdialog --msgbox "$(date +%H:%M) - U


call the doctor now!"

The two minutes are the waiting time that you


asked sleep to wait. Replace the 2m parameter
with something else – for example, 3h for three
hours. Without any suffix, sleep assumes you
mean seconds.
Putting the time in the message window with
$(date ...) has a simple purpose: You might not
be in front of your computer when the message
pops up. With this extra information, you’ll know
how much time has elapsed.
On KDE, try kdialog instead of gdialog. If you
have neither *dialog program on your machine,
there’s always a cheap replacement: Open an
xterm window and launch a shell that displays
your message via

xterm -e bash -c "echo $(date +%H:%M) - call U


the doctor now ; read"

The read command at the end waits for you to


press Return; the terminal window will then close.

By Hans-Georg Eßer

Figure 7

101 LINUX HACKS – 2020 EDITION


GETTING STUFF DONE

HACK
Finding and
16 Sync Up Your HACK 17
Installing Online Phone and Linux
Fonts Desktop
Fonts aren’t something most users spend too much KDE Connect is a utility I fell in love with at first sight.
time contemplating. Most distributions include a You install an app on your Android device and an-
good selection, with even some, such as Ubuntu, other on your Linux desktop, and your phone and
bundling their own fonts designed specifically for computer become part of the same computing envi-
clarity and design aesthetics. But if you spend a lot of ronment. Phone notifications appear on the desktop,
time with code or words, you’ll know that the right desktop notifications on your phone, your music
font doesn’t simply look good; it also helps ease eye player stops playing when you get a call, and you can
strain and aid clarity. For those reasons, spending a browse your phone’s filesystem from the desktop file
little time trying out a few more options than those manager (Figure 9).
provided by your desktop makes sense. There’s even a command-line interface and key-
KDE has a great font-browsing tool that will let board and touchpad emulation from your phone.
you preview fonts, as well as install them either lo- While these initially sound like gimmicks, they’ve ac-
cally or globally with a single click – and you can tually saved me serious hassle when mouse batteries
even do this from Dolphin’s right-click menu when have failed – on more than one occasion! The KDE
more than one font is selected. But there isn’t al- team have done a great job of implementing this in
ways an easy way of browsing fonts you may poten- such a way that actually works and is useful.
tially want to install. Font Finder (Figure 8) could be Since the early versions of KDE Connect, there has
the solution; it acts as a kind of application store for been a constant stream of updates with new features,
the free fonts hosted on Google’s font archive. It’s and there’s now even a version that decouples itself
also a lovely GTK3 application, with a sleek minimal from the majority of KDE Plasma bindings that make
interface. There’s a category browser, so you can it work so well with KDE. The developers are working
easily search for monospace, for instance, and a on a version that uses Bluetooth rather than a com-
search field that lets you filter fonts according to mon network connection for those times you need
your criteria. Results are delivered in the main view, KDE Connect synergy without wanting to connect to
but critically, the results are previews of the fonts a network.
themselves, which is exactly what you need. You You need not run a KDE desktop to use KDE Con-
can change the size of the fonts and even render nect: For example, there is also Nautilus support.
them onto a dark background. It’s a brilliant way for Gnome users can also try the alternative GSConnect
looking through fonts you may wish to install. When tool, which reimplements KDE Connect for Gnome.
you decide to install one, simply click on Install.
By Graham Morrison
By Graham Morrison
https://community.kde.org/KDEConnect
https://github.com/mmstick/fontfinder https://extensions.gnome.org/extension/1319/gsconnect/

Figure 8 Figure 9

101 LINUX HACKS – 2020 EDITION 23


GETTING STUFF DONE

H AC K
Self-Organization with GTG
18
Getting Things Gnome implements the Getting Things tasks for easy searching by category and record notes
Done (GTD) approach, which is so close to this catego- describing the state of the task.
ry’s name that we have to discuss it. GTD is a self-orga-
nization method developed by David Allen. It is based Creating Tasks
on the theory that trying to maintain a complex task list GTG focuses on the tasks, which the program displays
in your head takes a toll on concentration. The idea is to in the main window. You can create a new task by click-
move task tracking out of the mind and put it in the ing on the New Task button or using the keyboard short-
hands of a trusted external system. The system tracks cut Ctrl+N. A new dialog appears. In this example, I want
the tasks, so the user can focus on solving and com- to save a yoga class offer for a later date. In the first line
pleting the problems. of the new dialog, the title of the task, Yoga Class, ap-
GTD works both with physical objects (such as pen pears in a larger, underlined font followed by a task de-
and paper, an inbox that is a real box, and a physical fil- scription, Penguin Yoga, [email protected].
ing system) and with digital objects. Various applica- To make it easier to find tasks, you can assign tags,
tions let you implement the GTD system. One handy which consist of an @ sign, followed by the keyword. In
and free program that helps with GTD is Getting Things our example, you could assign the following tags:
Gnome (GTG). Even though development of the pro- @maybe/sometime @leisure @yoga. The tags are
gram ceased in 2014 and the former website (gtgnome. highlighted with yellow background making them im-
net) no longer works, it can easily be installed, and it mediately stand out in the text (Figure 10).
works well on modern systems. Figure 10 Finally, you will need to define
The first step is to collect all the a start and a due date. For the
documents that represent your yoga course, you might not
various responsibilities: letters, have time until August, and the
newspaper clippings, articles, offer is only good until October
photographs, and handwritten 31. There are two fields avail-
notes, as well as digital items able at the bottom of the dialog:
such as email messages or notes You can enter the data or select
in a digital notebook. You work from a calendar. If there is no
through the items that require ac- specific due date, just choose
tion and complete them according sometime. When you close the
to predefined criteria. The basic dialog, the new task will appear
rule is: Anything that you have in the main window.
taken from the inbox must not be Using View | Tags sidebar or
put back, but must be assigned to the F9 key, you can now open a
a suitable place in the system. sidebar in which all the tags you
Select items one by one. The first question for each have assigned are displayed. Right-clicking on a key-
item is: Throw it away, archive it, save it for later, or word opens a dialog, in which you can change the color
complete it? You can archive a newspaper article that the program uses to highlight a keyword for a task. Ad-
you have read but want to keep. The archive can also ditionally, you can assign an icon to the keyword; this
include things you might want to do later but don’t icon then appears in the main window next to the task
have time for now. Some of the tasks that you won’t ar- title. A search function is available in the sidebar.
chive or throw away are easier to do immediately, with-
out cluttering up the system. As a rule of thumb: Do Wait
things that take less than two minutes immediately; re- You can also enter tasks delegated to others in GTG.
serve the rest for later completion. Tracking delegation helps you keep an overview of
After separating out the things you will throw away, what you assigned, for when, and to whom, and it can
archive, or do immediately, you get down to something also remind you that completion is overdue.
resembling a to-do list with items that require intermit- For instance, you could forward a letter from the IRS,
tent or ongoing attention. GTG is a convenient, simple reminding you that you need to complete your income
tool for tracking tasks and maintaining that to-do list. tax return, to your tax consultant. At the same time,
You can quickly record the task, the start date, the de- you would use Ctrl+N to create a new task. Give the
sired completion date, and any sub-tasks that might be task a new title, such as Income Tax Return: Reminder
required as part of the process. GTG also lets you tag from Internal Revenue Service forwarded to tax consul-

24 101 LINUX HACKS – 2020 EDITION


GETTING STUFF DONE

tant February 1, 2019. Meaningful tags Figure 11


would be: @wait @income tax.
Your tax consultant promised to have
your income tax return ready by April 1,
so that sufficient time remains for you to
check the results from the consultant, sign
the forms, and file your return with the IRS
by April 18. You enter this data as the start
date and completion date of the new task.

Projects
Tasks that you want to complete yourself,
and that take more than two minutes, are
referred to as projects in GTD. Such proj-
ects are typically broken down into individual tasks. so that you are sure to complete all your pending pur-
First, create a new task for the overall project in the chases in a single visit. You can then highlight any com-
usual way by pressing Ctrl+N. If you want to write an pleted tasks, and they will disappear from the list.
article for a professional journal, the title could be Write
article. The following tags seem appropriate: @projects Simplification
@article. The completion date is the editorial deadline. Various plugins add functionality to GTG. To view and
Enter the individual sub-tasks line by line with a leading enable any available plugins, select Edit | Plugins. You’ll
dash; for example: -1. Literature research, -2. Get litera- find plugins that let you automatically delete high-
ture, -3. Read literature, -4. Write draft, -5. Send draft to lighted tasks, color-code tasks for easy recognition of
editor, -6. Incorporate editorial changes, and -7. Send urgency, and more.
finished article to editor. Use the Tomboy/Gnote plugin to sync tasks between
GTG creates its own subentry for each part of the task GTG and the Gnote or Tomboy note-taking tools (Fig-
and also creates a corresponding link in the main en- ure 12). First enable the plugin, and then enable syn-
try’s window. In the main window, the entries are chronization using Edit | Synchronization Services. A
shown indented under the main entry. To keep track, it new menu appears bottom left; now click on Add.
is useful to enumerate the tasks in the order in which Then select the program with which you want to sync
you want to complete them. tasks and notes (for example, Tomboy) and confirm by
In the sub-tasks, you can save notes for each step, for pressing OK. You just need to decide whether GTG
example, a list of the literature you want to read. As should synchronize all tasks and notes or just those
usual, you also have the option of entering tags. After that contain a specific keyword, say @GTG-Tomboy.
sending a draft of your article to the editor, you can to Click on Enable Synchronization and Close to complete
add the @wait tag to the corresponding sub-item. the synchronization.
You need to assign start dates and due dates for sub- By Daniel Tibi
http://gettingthingsdone.com
tasks. In this way, you can keep track of which sub-task
https://wiki.gnome.org/Apps/GTG
is pending; of course, the due date for a sub-task must
be no later than the due date for the project. After you Figure 12
complete a sub-task, right click on it and select Mark as
Done from the menu.

Regular Processing
To see which tasks are currently pending, you need to
work through GTG regularly as part of your daily rou-
tine. In the main window, click the Work View button.
Pressing the button changes the view of the main win-
dow (Figure 11). GTG only displays those tasks that you
need to complete. They include tasks that either have
no start date or whose start date has already been
reached. If required, you can filter this list for keywords.
For example, if you are heading into town for shopping,
you can display all items with the keyword @shopping,

101 LINUX HACKS – 2020 EDITION 25


Linux admins no longer say “Never change a running
system,” but they do say definitely keep it running!
We’ll show you hacks that help with everyday system
administration. Repair stuff that’s broken, keep an eye
on it so you notice when it breaks, and be prepared.

HACK 19
Clone It with Clonezilla 27
HACK 20
Write ISOs to USB Sticks 28
HACK 21
Get a Quick Machine Configuration 29
Overview with inxi
HACK 22
Repair Your Bootloader 30
HACK 23
Cleaning House with Stacer 32
HACK 24
Image © bowie15, 123RF.com Reset Your Debian-Based Distribution 33
HACK 25
Repair the Debian Package Database 34
HACK 26
Learn the vi Editor Basics 36
HACK 27
Follow File Updates 37
HACK 28
Monitor Your System in a Terminal 37
HACK 29
Visualize ddrescue’s Progress 38
HACK 30
Run dmesg with the Right Options 38
HACK 31
Where Did You Mount That Disk? 39

26 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

Clone It with Clonezilla


Experts agree that you should keep a copy
of your data, but restoring from incremental
backups takes time and sometimes doesn’t work as
expected. Alternatively, you can clone the partition.
HACK 19 After booting and selecting the
language, Clonezilla prompts you
for the operating mode. This is
where you say whether you want to
Several tools help with that job; we suggest you use clone a single partition or a complete
Clonezilla even though its text-mode interface looks disk. The software either copies the source to an identi-
outdated. cal target or creates an image of the source disk or par-
Cloning software differs greatly from backup soft- tition. As a further alternative, there are also possibili-
ware. Partition copies cannot be supplemented and ties for cloning on the network. The network option is
accelerated by differential or incremental partial back- intended for large-scale software deployment, while
ups. Admins can thus reconstruct the complete sys- the local option is more suitable for individual cloning
tem in a single pass in case of a crash. Unlike a backup operations or for creating snapshots for data backup.
solution, a clone does not require importing several In just a few steps, Clonezilla guides you to the fin-
backup layers. ished image. It first queries the target for saving. You
The best known example of cloning software is the can choose from a list, whether this is a local medium;
free live system Clonezilla. Developed in Taiwan, it is an SSH, Samba, or NFS server; or whether Clonezilla
based on Debian Sid or alternatively on Ubuntu 18.04.1 should park the image in the Amazon cloud.
LTS and has no graphical user interface (GUI). You con- If you go for the local option, you can specify the tar-
trol the complete system via ncurses menus. get disk in the next step. For external options, you can
Clonezilla is available in several variants. While the then connect to the source computer. The system auto-
alternative Ubuntu version exclusively serves 64-bit matically detects a plugged-in USB data carrier. Next,
systems, the Debian-based version also offers two choose the source; Clonezilla lists the different parti-
32-bit versions, one of which supports processors tions on the local disk.
with the Physical Address Extension (PAE). The ISO The following ncurses screen allows a filesystem
images each are less than 300MB and fit easily on check to ensure the filesystem’s integrity. Clonezilla
CD-ROMs, allowing the tool to run on very old com- also offers to check an image’s recoverability after it
puter systems. The software is available as a hybrid has been created. In the last dialog, the user can also
image, which can therefore be launched from a USB- select encryption according to the AES-256 standard.
based storage medium. The developers stipulate a After a final confirmation prompt, Clonezilla creates
minimum of 196MB RAM for operation. the image. When the software is finished, you can shut
Clonezilla is also suitable for heterogeneous infra- down the system, restart it, or create another image
structures: In addition to the filesystems commonly used (Figure 1).
under Linux, it also supports Windows, Apple’s HFS+, If you use Clonezilla, you must make sure that the tar-
and several BSD-derivative filesystems. Also, Clonezilla get partition is at least as big as the source partition
can reinstall the current 2.x ver-
sion of the GRUB bootloader (or Figure 1
the legacy version if required)
and can cope with common
older and newer BIOS versions.
On booting the Clonezilla
disc, several startup options
await the user in the GRUB
boot manager. If there are
problems with the screen reso-
lution, Clonezilla also offers a
generic VGA driver along with
legacy SVGA and XGA resolu-
tions. It is also possible to load
the entire system into the com-
puter’s RAM, which noticeably
accelerates work on older sys-
tems in particular.

101 LINUX HACKS – 2020 EDITION 27


KEEP IT RUNNING

when functions and the corresponding dialogs are included


cloning a in the previous routine’s ncurses menus, so you don’t
disk. In addi- need any additional training.
tion, the source If you want to restore an image, just reverse the
drive must be un- steps. After starting the live system, again set the lo-
mounted while the cale. In Clonezilla, define the source medium where the
cloning process is run- images you want to transfer are located. In the next
ning or creating an image. step, select the image repository in which the desired
Cloning does not work with the image is stored. In the last dialog, Clonezilla uses the
drive mounted. restoredisk option to transfer the clone or image to the
Since version 2.5.2-31, the devel- target disk.
opers have also integrated a Clonezilla
Lite server into the live system. The server By Erik Bärwaldt
is used to enable the rapid deployment of op- http://www.clonezilla.org/downloads.php
erating systems in a network environment; it can
clone 40 plus computers simultaneously. The server

Write ISOs to USB Sticks


Many of us still resort to the humble dd command
HACK 20
against an ISO file with the -p argument, and it will tell
when writing a distro image to USB storage, because you whether your ISO is hybrid and capable of being
it’s simple and untainted by options or system interac- written to USB storage and whether enough USB stor-
tion. But it does have some profound problems. Worst age has been found.
of all is that if you get a single character wrong when In the background, it’s also running lots of integrity
declaring the device node, such as /dev/sda, dd will checks on the ISO, making sure it will boot and has the
start overwriting your internal storage without even correct MIME type, as well as that the potential desti-
asking politely whether you’re certain, and you’ll soon nation is correct and not a single partition. This is use-
be diving for Ctrl+C. ful in itself, and it will even let you know which device
A great alternative is bootiso, which doesn’t present node your storage is hanging off in case you still want
the same risk, adds loads of new features, and still runs to risk dd. But bootiso is even better when you want to
from the command line. At its simplest, you can run it write the image, and that’s because it still uses dd.
Give it your ISO as the single
Figure 2 argument, for example, and it
will join up its detection rou-
tine with its encapsulation of
dd to write the image automati-
cally, carefully asking whether
you’re certain, after presenting
the details on what’s going to
happen (Figure 2). It will even
erase the storage first. While
there’s no progress indicator –
just like dd – by choosing the
optimal block size in the back-
ground, the transfer is often
quicker than trying your luck
with dd.

By Graham Morrison
https://github.com/jsamr/bootiso

28 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

Get a Quick Machine Configuration Overview with inxi


H AC K
Every admin knows how to retrieve information about
the system on which they are working. How many
cores does the CPU have? cat /proc/cpuinfo! Is eth3 a
parameter shown in Figures 3
and 4 is responsible for the
color scheme. The default color
21
gigabit interface? ip l sh! But instead of many tools, scheme is not easily legible on ter-
you can just use one: inxi. It is available via many pack- minals with a light background, but thanks to the pleth-
age repositories (e.g., openSUSE, Debian, Ubuntu, and ora of options from -c1 to -c32, selectable sets are avail-
Linux Mint). able to suit your taste.
Suppose I need an overview of a machine with which I can even talk inxi into a spot of simple process
I don’t normally work. Then, I call inxi without any pa- monitoring. If I want to know which five (this is the de-
rameters and get some basic information about the fault value) processes are currently hogging the most
hardware (CPU, clock speed, RAM, and disk size) and RAM, inxi -t m will help me find out. If I want to see the
the system (kernel and shell processes). If I want to see top 10 processes, I enter -t m10. If I hear the CPU fan
a few details, the -F parameter provides information on humming, on the other hand, I just need to replace the
the video and audio hardware, partitioning, RAID, tem- m with a c to view the processor load. You can also com-
peratures, and fan speeds (Figure 3). bine the two: inxi -t cm 10 returns the top 10 RAM and
If I’m only interested in a particular component, I can CPU hogs.
target this with specific parameters, such as -C, -A, and At the end of this informative newscast on the com-
-G, which stand for information on the CPU, audio, and puter, I’ll take a quick look at the weather: inxi -w Ber-
graphics, respectively. Information on the RAM is re- lin, Germany tells me what the situation looks like out-
turned after a (lowercase!) -m, which takes some getting side the server room.
used to.
By Charly Kühnast
Memory Details https://github.com/smxi/inxi
Running with root
privileges, inxi Figure 3
tells me more
about the RAM:
Apparently four
2GB DDR modules
are plugged into
my test machine
and clocked at
1600MHz – yes,
this is a fairly an-
cient beast (see
Figure 4). The -c4

Figure 4

101 LINUX HACKS – 2020 EDITION 29


KEEP IT RUNNING

Repair Your Bootloader


HACK 22 various options in tab groups and grays
any inaccessible tabs. In the first tab Main options, you
Sometimes things go wrong when you are installing an op- can only configure a few basic settings for GRUB 2; you
erating system on a hard disk drive or SSD. A boot repair will see that the tool has already activated the option for
disk gets your boot configuration back on its feet, quickly. reinstalling the bootloader. In addition, you can trigger an
The boot process for computers has become massively automated filesystem repair; Boot Repair gives you the
more complicated in recent years. Unified Extensible option of reanimating a damaged MBR if necessary.
Firmware Interface (UEFI) has largely replaced the tradi- As an important additional option, the dialog offers the
tional BIOS, while increasingly Figure 5 possibility to make a backup of the
large storage devices require new partition table, boot sector, and all
types of partitioning. logfiles so that you can reconstruct
The configuration options of the old data later in case of prob-
bootloaders such as GRUB 2 have lems. If you enable the Restore
thus been massively extended; MBR option, Boot Repair grays the
even minor changes to the sys- following tabs GRUB location and
tem can cause startup problems. GRUB options and instead enables
In the worst case, you will be left the MBR options dialog. In the sec-
sitting in front of a black screen ond tab from the left, labeled GRUB
with a flashing cursor without the location, you can define where
operating system having booted. GRUB 2 is installed. You can either
In this situation, Boot Repair select all mass storage media or a
Disk provides invaluable assis- specific disk, which you choose in a
tance: The operating system, based on the lean Lubuntu selection box. You can also specify which operating system
17.04 with the LXDE desktop takes care of damaged boot the bootloader should load as the default.
configurations even in heterogeneous environments, re- In the following tab, GRUB options, you can choose to
pairing them automatically at the push of a button. completely delete an existing GRUB 2 installation before set-
ting up GRUB again, or enable GRUB Legacy as the default
Ready, Steady, Boot boot manager. You can also configure various parameters
Boot Repair Disk is available as an ISO image of approxi- that GRUB 2 needs to correctly start specific operating sys-
mately 642MB for 64-bit architectures, or as 627MB for the tems. If the configurations offered here do not meet your
32-bit variant. Thus, you can burn both versions of the op- needs, you can press Edit GRUB configuration file to tune
erating system on a CD, which you can use even on leg- the configuration file manually to your liking.
acy hardware without a DVD drive. Alternatively, you can In the Other options tab, you can define various options
use UNetbootin to transfer the image to a USB stick for for logging the individual tasks. If you also have a Win-
use on computers without an optical drive. In our lab, we dows version on your computer, you can enable the Re-
were unable to write a bootable image to a stick with the pair Windows boot files option to repair a Microsoft sys-
on-board tools. tem that fails to launch. Then enable the respective op-
After setting up the image, boot the computer from the tions by clicking on the Apply button. If you want to repair
corresponding media, and choose the bottom entry in the a mass storage device’s MBR, enable the Restore MBR op-
boot manager Boot Repair Disk session. Within a short tion in the Main options tab. Boot Repair then grays the
time, the system starts and immediately launches the settings dialogs for GRUB and instead enables the MBR
Boot Repair software on a very plain LXDE desktop before options tab. You can then select which tool to use to re-
proceeding with a system scan. Then the program’s con- construct the MBR. If there are multiple partitions on the
trol dialog appears. mass storage device, you can also define here which of
Under normal circumstances, you will just want to press them to boot by default.
the large button labeled Recommended repair to initiate
an automatic reconstruction of damaged system compo- BootInfo
nents such as the Master Boot Record (MBR) and boot The Boot Repair Disk also comes with another program
manager. If you first need accurate data on the mass stor- dubbed BootInfo, which helps you with problems at system
age media, but do not want to make any modifications for startup time. It can be found in the System Tools menu of
the time being, then click instead on Create a BootInfo the operating system and provides a clear-cut window
summary. In addition, the window also offers advanced where you can define with a single mouse click whether the
configuration options, which you can access by clicking tool should store the boot log online or locally.
on Advanced options. After another click on Local report (text file), the tool scans
The window then expands to include a configuration di- the computer and then opens the Leafpad text editor, which
alog for the GRUB bootloader (Figure 5), which groups opens up with the scan log. You will not only find detailed in-

30 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

Figure 6 Desktop
formation on the system config-
uration here, but – at the end of Besides the tools for repairing the sys-
the log – also some hints on tem, you will only find a few prein-
how the repair tool will ap- stalled applications. Office applica-
proach the task. You can thus tions, games, multimedia, and educa-
determine what modifications tional applications are missing com-
the tool will make on the com- pletely. The Accessories menu con-
puter (Figure 6). tains entries for the LX terminal, the
The tool lists all the partition Leafpad text editor, and the PCManFM
data of all mass media (includ- file manager.
ing USB flash drives) connected You can launch Firefox from the In-
to the computer system, as well ternet menu, and the usual LXDE
as the GRUB configuration files. configuration dialogs are found
In addition to the repair pro- below the Preferences menu. As a
gram’s log, you will also find the special feature, the System Tools
complete output from the parted menu offers the Synaptic package
-l, parted -lm, mount, df -Th, and manager, which provides access to
fdisk -l commands in the text file. You are thus given a the Lubuntu repositories, if you need additional software.
good overview of the mass storage device configuration.
Hands-On
OS Uninstaller In our practical tests, the system was totally convincing. It
The third in-house developed tool included with Boot Re- not only successfully restored damaged boot sectors in
pair Disk is found in the System Tools menu: OS Unin- Linux-only installations, but also repaired a mixed system
staller. This helps you delete an operating system without with one Windows and two Linux partitions.
leaving any remains on your mass storage device and Also the OS Uninstaller enormously simplifies adminis-
without painstaking manual work. trative tasks: In our lab, it always reliably completed the
After launching, the desired tasks on multiple machines with a variety of
Figure 7
application first lists all shared storage devices, thus removing the need for time-
the existing operating consuming manual deletion and modification of the parti-
systems after a brief tion table, as well as the GRUB configuration files.
scan of the system (Fig- Boot Repair Disk is a very useful tool that every admin-
ure 7). Select the operat- istrator in a heterogeneous environment should include in
ing system that you want their toolbox. Even less experienced users will be able to
to delete from this list quickly control the intuitive tools without any problems.
and press Apply. After a The system itself is extremely stable, and the special tools
safety prompt, the unin- for revitalizing the MBR and the GRUB 2 boot manager
staller first deletes the operating system, then reconfig- were impressive. Also the deletion tool for unneeded op-
ures the boot manager, and finally displays the results. erating system installations saves a huge amount of man-
After a reboot, you will find the remaining operating sys- ual configuration work.
tems in the GRUB startup menu, where the OS Uninstaller By Erik Bärwaldt
adds entries for the Plop Boot Manager and Smart Boot https://sourceforge.net/projects/boot-repair-cd/files/
Manager. They do not have a function without additional https://unetbootin.github.io/
configuration, so you can safely remove them from the
Start menu. Figure 8

More Tools
In the event that hardware problems cause difficulties
when booting a computer, Boot Repair Disk comes with
two graphical diagnostic tools in the form of HardInfo and
GParted. HardInfo, which you will find under the System
Tools menu labeled System Profiler and Benchmark,
clearly visualizes the hardware in a two-pane window and
also performs benchmarks. GParted, on the other hand,
helps to manage the storage devices. This is where you
can, for example, identify problems arising from incorrect
formatting or damaged filesystems (Figure 8).

101 LINUX HACKS – 2020 EDITION 31


KEEP IT RUNNING

Cleaning House with Stacer Caution is advisable before HACK 23


deleting Application Caches: This could slow down appli-
Classic command-line utilities are considered the go-to cation launch, and you should proceed with caution when
tools for system administration, Figure 9 it comes to the logs and keep at
but some powerful graphical least the current X.org log and
tools also are available for mon- the apt and dpkg logfiles. Num-
itoring and optimizing a Linux bered logs are always older and
system. One of those tools is can typically be disposed of
Stacer, which lives on GitHub. without any worries.
SourceForge also offers sources
for compiling, as well as deb Apps and Services
packages for 32- and 64-bit In the Startup Apps tab, you can
systems and an AppImage for view the applications the sys-
64-bit machines. In this hack, I’ll tem launches at boot time and
discuss the AppImage version. set up new startup apps. This is
Stacer was designed for especially handy if you work
Ubuntu but works with any distri- with different distributions: You
bution; on systems with a KDE do not always need to think
Plasma desktop, however, you need to launch the program about where you need to set up applications that run at
as root because of a kdesu authorization error. Before the first boot time on the respective systems, and you can also tell
start, you need to make the AppImage executable, which Stacer to lock an application for the next start as a test,
you can do as a user working in the directory where the without having to plumb the depths of the Control Panel.
package is located; then, run Stacer from the same directory: Starting and stopping system services is just as easy in
the Services tab. A search function facilitates finding a
$ chmod a+x Stacer-x86_64.AppImage particular service. A word of caution: If you shut down the
$ ./Stacer-x86_64.AppImage wrong service here, you can look forward to a reboot.
The Uninstaller tab lets you remove packages. You will
(Depending on what download link you follow the file find many of the applications installed on the system
could be named Stacer-x64.AppImage.) A message tells you here, and you can point and click to uninstall and remove.
that the setup routine is adding the application to the This worked perfectly with Ubuntu and Arch, but not with
menu and putting an icon on the desktop, which is the any other distribution tested in the lab.
only way in which the application changes your computer.
Alternatively, you can launch Stacer, like all AppImage ap- Colorful Plots
plications, by double-clicking on the executable file. The Resources tab displays the last 30 seconds of CPU,
Stacer welcomes you with a modern window featuring RAM, and network activity (Figure 10). If you have four,
six tabs reachable via icons on the left: Dashboard, Sys- eight, or more cores, Stacer shows them individually in con-
tem Cleaner, Startup Apps, Services, Processes, Unin- trasting colors. To view each plot separately, press the Cpu
staller, Resources, APT Repository Manager (when on History button, for example.
Debian-based machines), and Settings (see Figure 9). The All tasks handled by Stacer can be run at the command
window is static, which means you can neither increase line, with standard graphical tools, or even with the use
nor decrease its size. of competitor products such as BleachBit, but not with
The program always starts with the Dashboard, which the same graphical appeal. Delivery as an AppImage lets
only provides information and does not allow any interac- you distribute Stacer to multiple distributions in a single
tion. It gives you an animated view of CPU, memory, disk package; additionally, you save yourself the installation
space, and network interface utilization, as well as informa- overhead. However, the AppImage weighs in at more
tion about the installed processor and operating system. than 50MB, and the executable file is around 75MB after
Figure 10 unpacking; the complete pack-
System Cleaner age tips the scales at 130MB.
The System Cleaner tab helps Keep in mind when using Stacer
you ditch the ballast: This is that you could shoot yourself in
where you can remove unnec- the foot with poorly considered
essary log or cache files and actions.
empty the trash can on your
system. In the initial state, Sta- By Ferdinand Thommes
cer does not provide any data
https://github.com/oguzhaninan/
for trash disposal; you first need
Stacer
to enable the desired categories
and then launch a system scan. https://appimage.github.io/Stacer/

32 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

Reset Your Debian-Based Distribution


HACK 24
If you have totally messed up your Debian, Ubuntu, Linux Resetter gives you the choice between an automatic and
Mint, elementary OS, or Deepin OS, you can simply reset a user-controlled mode.
with Resetter. It is a Python script that supports resetting Automatic Reset mercilessly removes all user accounts
to the default installation for some distributions, much and their home directories. If you select this, a warning
like the reset procedure for smartphones. prompt appears,
Figure 12
Resetter’s way of working is quite simple: Using the ap- reminding you
propriate manifest list, the tool determines which pack- of what you are
ages were originally copied to disk (and in which ver- doing (Figure 12).
sions) when the operating system was installed. Resetter If you approve,
will remove anything that does not match and reinstall Resetter first
the default packages according to the manifest. Besides shows a window
two reset modes, the software offers another possibility with all pack-
to search Personal Package Archives (PPAs) directly in ages to be re-
Launchpad and install them without any further manual moved. Here you can exclude packages. If you don’t, the
work (via the Easy Install button). tool removes all applications installed by the users, in-
Resetter reads the flags in Ubuntu, Linux Mint, and cluding packages in Snap format. Then, it creates a de-
some other distributions to return to the state immedi- fault account and adds any packages removed from the
ately after installation. The list of officially supported dis- default installation again.
tributions gets updated with every new release. Custom Reset gives you more control over the cleanup:
If you want to use Resetter, simply install the resetter_*.deb You decide which user accounts, packages, and dependen-
package with apt; users of non-Ubuntu-based Debian-ish cies Resetter should keep. If you want to keep user ac-
systems need a second pacakage (add-apt-key) – the proj- counts and their home directories, use this mode. You can
ect website explains the details. enter a username and password for the automatically cre-
Before using the tool, make sure you back up, in case ated standard user. You can also instruct the tool to remove
something goes wrong. Additionally, depending on the old kernels.
operating mode, Resetter may remove all user accounts
and the corresponding home directories without asking. Slightly Inconsistent
Those usually contain data that are still needed, such as A system in our lab with the current Linux Mint 19.1 Cin-
browser profiles, manually customized package configu- namon had initial startup problems. At program launch
rations, and the like. time, Resetter output two warnings that neither a mani-
fest nor a user list could be found. I was told that I would
Automatic or Controlled? have to select them; otherwise, the tool would not work.
After starting Resetter, you will see a window with three After clicking on OK, the normal start window appeared.
buttons: Easy Install, Automatic Reset, and Custom Reset If you see one or both of these warnings, you will find a
(Figure 11). I briefly described the function of the first but- remedy in the File menu at the top of the application. Click-
ton at the beginning; here, I am only interested in the ing on File reveals two menu items that refer to the error
main function of the program: resetting an installation. messages. Select the appropriate entry for your distribution
Figure 11 from the two options, then click on one of the two reset vari-
ants, and the program starts. In a window, Resetter informs
you where it is in the reset process and the overall progress.
It can take several minutes to reset the system.
At the end, another window appears telling you that Re-
setter has created a new user with the name default and
reveals their password. The default username and pass-
word are always the same, no matter what distribution
you use. You will want to create a new username with
your own password immediately after a restart.
Resetter has the potential to save users of Debian,
Ubuntu, and their derivatives the trouble of reinstalling.

By Ferdinand Thommes
https://github.com/gaining/Resetter
https://github.com/gaining/Resetter/releases/tag/v2.1.0-stable

101 LINUX HACKS – 2020 EDITION 33


KEEP IT RUNNING

H AC K
Repair the Debian Package Database
25
Sometimes the package database on a Debian-based which builds packages that contain only dependency
Linux installation becomes inconsistent, and you can information.
no longer install or remove packages. There are ways In all these tools, you should gather whatever infor-
to fix the situation with apt, aptitude, and dpkg. mation you can about the state of the packages in-
Maybe you installed the wrong package from the volved. However, actually fixing the problem is likely to
Testing or Unstable repositories or gambled on Ex- take you far beyond the usual internal commands like
perimental. Maybe you installed a flawed third-party install and remove.
package or mixed packages from different Debian de-
rivatives. Or maybe the maintainer made a mistake, Making Repairs with apt
or a major technology change has happened, and When apt announces that you have broken dependen-
you are not to blame at all. But in all of these cases, cies and suggests solutions, very occasionally, remov-
you either receive an error message (Figure 13) or a ing problem packages with
ranked list of possible solutions, and suddenly you
are unable to install, remove, or update anything apt remove PACKAGES
until the problem completes its efforts and returns
you to a waiting command prompt. can solve the problem. On the principle of starting with
If you are patient, a new version of the problem pack- the simplest solution, try this command, but don’t be
age will be released that fixes the problem. The only surprised if it does not succeed.
trouble is, the new version might not be released for Another relative long shot is editing package sources
weeks, depending on where Debian, or your Debian de- to get newer versions of the problem package(s), using
rivative (like Linux Mint or Ubuntu), happens to be in its apt update to make them available. In particular, search
development cycle. Even after filing a bug, it can some- for a mirror site with more recent packages than your
times take time to resolve the problem. Probably, then, usual ones to add to the file /etc/apt/sources.list.
you want to take more active steps. A more promising approach is running
Fortunately, the tools you need are ones with which
you are likely already familiar: apt, the package manag- apt dist-upgrade --no-upgrade
er’s front end; aptitude, the popular command-line in-
terface; and dpkg, the basic package tool. All three have to upgrade all the packages installed on the system. Do
the structure not use apt upgrade, since the last thing you want to do
is complicate the problem by adding more packages to
COMMAND SUBCOMMAND PACKAGES the mix.
Another possibility is to force completion of an install
as well as many of the same features for installing and with:
removing packages.
apt and dpkg are installed by default on any Debian or apt dist-upgrade -f
Debian derivative system. However, if you have risky
habits, like constantly taking the latest package ver- Sometimes, specifying some or all of the packages
sions from Unstable, you should make sure that apti- mentioned in apt messages will work instead:
tude is installed, as well as other useful tools such as
script, which can log your recovery efforts, or equivs, apt install -f PACKAGES

Figure 13 Alternatively, try

apt remove -f PACKAGES

but read the summary of


what will happen carefully
before continuing the com-
mand. For some obscure
reason, all these com-
mands may work the sec-
ond, third, or even the

34 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

fourth time you run them, so Figure 14


run them several times before
giving up on them. You can
also try specifying the reposi-
tory and full package name by
adding the -t option to any of
these commands.
However, if you try all these
solutions and have no luck, you
have exhausted the capabilities
of apt and need to try another
command.

Aptitude Dancing
When run without options, ap-
titude opens an ncurses inter-
face to the Debian package manager (Figure 14). How- ing background information that can help you develop
ever, what many users do not know is that aptitude a solution. The --yet-to-unpack option can be especially
contains many of the same tools as apt, apt-get, and useful when you have been looking for solutions for
dpkg for fixing broken packages, as well as several some time and don’t care to scroll back in your history
extra of its own. for the names of the problem packages.
For example, you may be able to resolve problems by An especially powerful dpkg option is --purge (-P).
using the markauto command to mark packages as being --purge is a more powerful version of remove, deleting
automatically installed, or unmarkauto to mark them as not only the package, but all records of it, including the
manual installations. Another useful command is -t RE- configuration files. In addition to removing the pack-
LEASE, which specifies which release version to use, or its age, --purge also runs its postrm (post-removal script).
counterpart forbid-version to specify a version not to use. While you are troubleshooting, this thorough deletion
Another useful pair of tools is why and why-not. Both are can simplify the problem’s background and sometimes
followed by a dependency. The why command shows why even solve the problem itself. The dpkg man page will
a dependency would be required, whereas why-not shows give you more information.
why a dependency produces a conflict. The results of Another important option is:
both can indicate how a subset of broken packages in-
volving conflicts with another package can be resolved. dpkg install --ignore-depends=PACKAGE
However, the most popular feature of aptitude is the
Resolver menu. The menu lists the package manager’s Descent into Dependency Hell
suggested solutions to dependency problems (see the
“Descent into Dependency Hell” box) and allows you Manually finding a solution when the package system no
to approve and reject them. Often, this menu alone longer works can be hard. Resolving broken dependen-
solves problems that apt-get, dpkg, or other features of cies can take hours, and the complications are so numer-
aptitude cannot, although at the cost of hiding exactly ous that, when you do find a solution, it can feel like luck.
what it is doing. The real solution, though, is to work systematically
through the possibilities.
Escalating to dpkg If you regularly find yourself in dependency hell – as bro-
ken dependencies were once called – then maybe you
Because dpkg is a lower-level package than apt, it in-
should consider your computing habits. While everyone
cludes many features that apt and aptitude do not. As is tempted by the latest possible release and can make
its man page shows, dpkg is especially useful for read- mistakes out of enthusiasm, by stepping outside the
ing detailed information about packages, including the safety of the package management system, you are strik-
state it is in, and for filtering the information displayed. ing out on your own.
You might be able to run dpkg with the option --forget- An expert can do that, but to do so requires caution
old-unavailable, --clear-selections to remove problems, every step of the way. Otherwise, you may be reduced to
or --audit (-C) to receive advice on what actions to try. desperate efforts such as editing a package’s scripts or
However, more often, dpkg options or commands, such fiddling with /etc/apt/preferences in the faint hope of
changing results that have already failed.
as dpkg-query, will be most useful in filtering or gather-

101 LINUX HACKS – 2020 EDITION 35


KEEP IT RUNNING

This option is misnamed, since it does check for depen- well as the man pages, before using it.
dencies but only reports conflicts between packages. In fact, dpkg as a whole can be so deadly that you
Often, it can be the solution for which you are looking. should use
An equally powerful solution is:
--no-act [--dry-run, --simulate]
dpkg --configure -a
to do a dry run of any action, simply on the off-
which configures all partially installed packages. In my chance of unexpected effects. The simulation will not
experience, this command fixes more broken depen- tell you in so many words that your system or desk-
dencies than any other option mentioned in this article, top environment will crash, but studying the list of
although it is not infallible. affected files should warn you that you risk making
If not, then take a detailed look at --force-things your situation worse.
THING, as well as --no-force-things and --refuse-things. The Debian package manager has other front ends,
Just as --purge is an enhanced version of remove, so notably Synaptic, a desktop interface. However, if apt,
--force-things is a fine-tuned version of the apt-get dpkg, or aptitude cannot restore full functionality, then
--force option. You probably want to avoid completions the chances are high that neither Synaptic nor anything
of these commands such as bad-version, remove, or over- else can do so.
write unless you are absolutely confident of what you By Bruce Byfield
are doing. However other completions, such as down- https://wiki.debian.org/Synaptic
grade, configure-any, and remove-reinstreq may provide
https://en.wikipedia.org/wiki/Dependency_hell
solutions. But --force-things can bring your system
down when used carelessly, so consult --force-help, as

Learn the vi Editor Basics


Half of the Linux administrator’s life is entering com-
mands in the shell. The other half is editing files in the
that it teaches you something
conceptually new.
HACK 26
vi editor (or its improved version, Vim – which stands So have a look at vi if you’ve never done so
for vi improved). Rumor has it that there are alterna- before. Search the web for “learning” and “vi,” and
tives to vi, names like Emacs, MCEdit, Gedit, Kate, and you’ll find lots of introductory texts and videos. Or run
nano come to mind. the vimtutor command. Start with the GUI version gVim
Don’t worry, this hack won’t take part in the famous (Figure 15) if you prefer to see a menubar.
“Editor War.” If your preferred editor is not vi, then let By Hans-Georg Eßer
me only suggest that you learn the basics of this an- https://www.vim.org/
cient tool that – according to its Wikipedia page – has https://en.wikipedia.org/wiki/Editor_war
been around since 1976. Why should you do that? Here https://en.wikipedia.org/wiki/Vi
are my favorite reasons.
• vi is not only available for every operating system, it Figure 15
is also preinstalled on every Unix-like system. So if
you happen to find an ancient Unix workstation run-
ning SunOS, HP-UX, or AIX, you’ll be able to edit its
configuration files.
• vi is a text-mode program, so it doesn’t require an X
session. It also does not require a mouse or a full key-
board with cursor keys. So even in the unlikely event
that you’re remotely logged in to a strange Unix ma-
chine and the keyboard settings are broken, you can
still use vi.
• vi uses two modes, one for text input (the Insert
Mode) and one for executing commands (called
Normal Mode). It starts in Normal Mode, so you can-
not enter text when you’ve just launched the editor.
That is so different from almost every other editor

36 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

HACK
27
Figure 16

Follow File Updates


Some Linux administration staff think that
the magic of a 27-inch display is that you
can open 25 xterm windows and use them
for various monitoring activities (Figure 16).
For example, if you need to keep an eye on
some critical infrastructure, it might make
sense to log in to those machines via SSH
and watch activities in logfiles. The stan-
dard command for doing that is:

tail -f <logfile>
In the case of one daemon process that handles logging of some ser-
But there is a tiny problem: Every now vice, telling it to switch to a new version of the logfile is easy. But there’s
and then a server will rotate some or all no way to keep track of various other processes that read the logfile and
of its logfiles. Rotating such a file means contact the processes in a meaningful way. Using a tool like lsof will
renaming it, possibly compressing it, and show all processes that have opened a specific file:
creating a new logfile with the old name.
When a helper program like logrotate has esser@xps13:~$ lsof /var/log/syslog
finished the rotation of some daemon’s COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
logfile, it will then inform the daemon by tail 16841 esser 3r REG 259,3 612458 12058755 /var/log/syslog
sending it a hang-up (HUP) signal (killall
-HUP daemon), and the daemon in turn will But how would you try to inform those processes of a change? The so-
close the (old) logfile and reopen it. If that lution must lie on the clients’s side. For tail, it is simple: Use the -F op-
last step does not happen, the daemon tion instead of -f, and you’re set. It will also make tail wait indefinitely
will continue to write to the old logfile for the watched file to reappear.
(even when that file completely disap- By Hans-Georg Eßer
pears after a compression step).

Monitor Your System in a Terminal


There are many command-line system monitor tools,
HACK 28
is running, and how well your fan or cooling is respond-
but there’s always room for another monitor that at- ing to this load. It presents this information as a scrolling
tempts to do something differently. Hegemon succeeds, and colorful histogram that can be expanded or con-
mostly, because it presents only the information you re- tracted with a simple press of the space bar (Figure 17).
ally care about in a way that’s useful. That information As system load increases, you can see exactly which
is how much of your CPU capacity is being used (bro- cores are carrying the burden, along with how it’s heat-
ken down into cores, if required), how hot your system ing up your system. The fan speed will typically ramp up
Figure 17 to compensate, creating a scrolling chart of your system
load and on-going performance.
The folding and unfolding of each element is a quick
and easy way to limit the details you see, as well as to
get more information when you need it. To Hegemon,
each of these sources is a data stream, effectively mak-
ing the application modular, and further data streams
can potentially be plugged into the same system. The
excellent documentation covers the details and also
promises incoming network, disk I/O, and GPU usage
streams to augment the already busy display.

By Graham Morrison
https://github.com/p-e-w/hegemon

101 LINUX HACKS – 2020 EDITION 37


KEEP IT RUNNING

Visualize ddrescue’s Progress


HACK 29 Run dmesg with the Right Options
HACK 30
Many users use GNU ddrescue to save data from The dmesg tool shows messages that appear in the “kernel
a faulty disk. However, the resulting logs are dif- ring buffer,” and it can do so in many ways as the im-
ficult to read. ddrescueview graphically processes pressive list of options in its man page shows. But most
logfiles. In order to use it, run ddrescue with a of those options aren’t interesting, you just need a few.
third file name argument so that it creates a Run dmesg -HTwx to see both user-level and kernel-level
“map file.” Then open that file in the viewer pro- messages with a human-readable, absolute time format
gram (File | Open mapfile). (-HT) as well as priority prefixes (-x) in continuous/follow
First, the tool determines the disk size and dis- mode (-w). That leads to the situation shown in Figure 19.
plays an overview of all sectors in the main win- The color highlighting appears automatically unless
dow (Figure 18). The different colors represent you’re remotely logged in from a terminal with strange
how a section is doing and whether the tool was settings; in that case, you can try to force colored output
able to save it (green), it is defective (red), not with --color=always.
trimmed (yellow), or not yet recognized (gray). While this lets you look at the screen and wait for fu-
You can receive detailed information about a ture messages, you cannot scroll back. If you want to see
sector via a mouse click: This will open the Block the past, lose the -w option: You will then see the ring buf-
inspector dialog, which reveals where the faulty fer’s entire contents (starting with the first lines), and you
section starts, how big it is, and how much data can scroll down to later entries. Note that the ring buffer
ddrescue could ultimately reconstruct. is in fact a ring: Its size is finite, and it will lose the boot
The tool assumes a block size of 512 bytes by messages after the machine has been up for a while. If
default. You can define other dimensions in the you want to restrict dmesg output to user-mode or kernel-
program settings, as well as set up a refresh in- mode messages, add an -u or -k option.
terval that keeps the display up to date while Note also that some pieces of information never make
ddrescue is running. it into this message buffer: Check files in /var/log as
On Ubuntu and Linux Mint, you can simply apt well; your syslog service might log some information
install the tool; for other distributions, you may to messages, syslog, or other files in that folder.
have to search for a repository that has a package.
By Hans-Georg Eßer
By Uwe Vollbracht Figure 19
http://www.gnu.org/software/ddrescue/
http://sourceforge.net/projects/ddrescueview
Figure 18

Image Copyright – Martin Bittermann

38 101 LINUX HACKS – 2020 EDITION


KEEP IT RUNNING

Where Did You Mount That Disk?


If you like to extend your computer’s life by adding hard So far, however, you do not know
HACK 31
disks (instead of replacing the old ones), you can which options were set when mounting the
quickly lose track of what partition holds what kind of partition. The mount command shows these kinds of
data. Some elementary tools help you get an overview. details. Its output is quite extensive and often includes nu-
lsblk (“list block devices”) shows all block devices. merous virtual devices managed by the kernel and sys-
The default view in lsblk includes tem programs. In many cases,
only the devices or partitions cur- Figure 20 however, only the physical mass
rently in use (Figure 20). In the storage devices and their
right-hand column, you will find mounted partitions are of interest.
the mount point (i.e., the position Since Linux partitions mostly
at which the device is mounted in use one of the “extended” file-
the directory tree). systems (today, typically ext4) or
The output of lsblk has six col- Btrfs, you could filter the output
umns. Table 1 gives an overview of mount with grep accordingly.
of the information you get from However, this detour is not actu-
this list. ally necessary. Mount offers a -t
Using the -a switch helps lsblk (short for --type) switch that out-
reveal more devices by including puts filesystems of a certain type
devices that are not currently in or several types only. So, type
use. For example, it will show a mount -t ext4,btrfs to restrict the
series of loop devices (virtual list to ext4 and Btrfs filesystems.
block devices that do not map to If you search for NTFS filesys-
a physical device, but use a file tems, remember that those are
for underlying storage) from / typically mounted via FUSE and
dev/loop0 to /dev/loop7. show up as fuseblk, not ntfs. If
you have installed a commercial NTFS driver, the file-
Filesystems system type will be ufsd (universal file system driver).
The -a switch tells you which partitions exist and under mount provides so much information that clarity can suf-
which mount points they are mounted in the filesystem. fer. The output of findmnt from the same software package
offers a better structure. When called without further op-
Table 1: lsblk
tions, findmnt shows a tree view from which you can see
NAME Device name how the individual filesystems are mounted. The findmnt
MAJ:MIN Main and extension numbers of the device command also identifies the origin and type of the file-
RM 0 for fixed and 1 for removable devices system and the options with which it was mounted.
(like CD/DVD drives) findmnt also supports the -t option, with which you can
SIZE Storage capacity of the device in human-
filter for ext4 filesystems, for example (Figure 21). Unlike
readable form mount, findmnt isn’t limited to a single filesystem parame-
ter but lets you enter several if required.
TYPE Type of device (see Table 2)
MOUNTPOINT Directory in which the device is mounted By Frank Hofmann

Table 2: lsblk Device Types Figure 21

disk Hard disk


loop Loop device
lvm Logic Volume Manager (LVM)
partition
part Partition on a storage medium
rom CD/DVD drive

101 LINUX HACKS – 2020 EDITION 39


When you expose your machines to the Internet, a
little paranoia makes sense. Hide your tracks in the
WWW, encrypt your files and mails, disable your
webcam and mic, and make sudo secure. And if you
sell your disk on ebay, wipe it – wipe it for real.

Image © lightwise, 123RF.com

HACK 32 HACK 37
Install and Run Tor Browser 41 Enhancing Security with sudo Options 44
HACK 33 HACK 38
Permanently Wipe Files from Your Hard Disk 41 Use Two-Factor Authentication with Authenticator 46
HACK 34 HACK 39
Disable Webcam and Microphone 42 Start Your Training with Web Security Dojo 46
HACK 35 HACK 40
Disable Password-Based SSH Logins 43 Lock Your Screen, Always 47
HACK 36 HACK 41
Encrypt Your Emails with GPG 43 Use ccrypt for Quick Encryption 47

40 101 LINUX HACKS – 2020 EDITION


SECURITY & PRIVACY

HACK
Permanently Wipe
33
Files from Your Hard Disk
H AC K When you delete a file in the file manager, it
does not exactly disappear. In most cases, it

Install and Run Tor


Browser
32 falls into the recycle bin, from which it can be
quickly restored. But even if you empty the
trash, the supposedly deleted data can still be
Do you like how searching for a new TV online will fill web reconstructed.
pages with ads for just that product over the next few Linux supports two console tools, shred and
months? If not, you may want to stop Google and other ad wipe, both of which securely delete files, direc-
sellers from tracking you. That’s not so simple: You need to tories, or entire partitions. In practice, how-
switch to a fresh IP address for every new website you visit, ever, you will rarely want to launch a terminal
block cookies, and more. Tor and Tor Browser combine all the to wipe a file off your hard disk. File managers
tricks currently available for secure and private web brows- can be equipped with similar functions. For
ing. The actual browser is a Firefox version that’s been pre- example, Gnome Files (formerly Nautilus) has
configured to use the Tor network. Nautilus Wipe, an extension that lets you se-
Install the torbrowser-launcher package (via apt install on curely delete files with a single mouse click.
Debian/Ubuntu/Mint, dnf install on Fedora, and zypper in on (For KDE, the Secure delete service menu
openSUSE) and then run the torbrowser-launcher command adds the same functionality to Dolphin.) After
from inside a terminal. If used for the first time, the tool will installing the package, restart the file man-
download the latest version of the Tor-enabled Firefox for ager by running the nautilus -q command.
your platform and language, check its signature, and then Two new options are then available in the
launch it. If you already have a current version, it’ll just start context menus of files and folders. The first
immediately. one (Secure Delete) overwrites the objects se-
In the window that appears after a few seconds, click on lected in the file manager with random data.
Connect; then Firefox – branded as Tor Browser (Figure 1) – The application lets you choose the number
will launch. Use the browser as usual, click on the onion of deletion passes (Figure 2). Two passes are
icon, and select New Identity to receive a fresh IP address. typically sufficient.
Figure 1 also shows proof in the server logs of a visited site: After that you should also use the second
three accesses to a non-existent page registered with three option, Wipe available disk space, which over-
IP addresses. writes the space marked as free on the parti-
tion and ensures that backup copies and
By Hans-Georg Eßer shadow files of the previously edited docu-
https://www.torproject.org/ ments are securely overwritten and thus per-
manently deleted.
Figure 1 If you use an SSD instead of a classical hard
disk, things are more complicated and wiping
will not protect you against an attack by a pro-
fessional forensic expert.

By Christoph Langner
http://wipetools.tuxfamily.org/

Figure 2

101 LINUX HACKS – 2020 EDITION 41


SECURITY & PRIVACY

Disable Webcam and Microphone


A computer’s webcam and microphone are often
abused for attacks and privacy violations. Thanks to
modern web technology, such as HTML5 with WebRTC,
Applications such as
Cheese will then no longer find a
webcam on the system. If necessary,
HACK 34
a browser is all it takes to transfer the image and sound you can reload the module later (with sudo mod-
from your living room to the web. For example, video probe uvcvideo). You will need administrative privileges
chats with AppRTC can be handled directly in the on the system for all of these commands.
browser (Figure 3); a web page could also tap the web- To prevent the Linux system from loading the kernel
cam for other purposes. module responsible for the webcam, add a blacklist
Figure 3 entry to a configuration file in the /etc/modprobe.d/ di-
rectory. On some distributions, you will find a file called
blacklist.conf in that folder; others may prefix the
name with a number (for example, 50-blacklist.conf on
openSUSE). If no such file exists, simply create it. The-
oretically, you are free to choose any name, such as
disable-webcam.conf, as long as it ends with .conf. Add
the following lines to that file:

# Webcam Disabled
blacklist uvcvideo

After a restart, the webcam should not work, which


you can test with Cheese or Skype. If you do want to
use the cam, load the kernel module manually (sudo
modprobe).

Although a browser requires the user to confirm that Cams with Microphone
the website is allowed to enable the webcam and micro- Things are a little different with a microphone built into the
phone, errors (on the part of developers, as well as device. Theoretically, as with a webcam, you need to dis-
users) occur time and time again. And with a locally in- able the necessary kernel module, which you can deter-
stalled application – installed voluntarily or by a trojan – mine quite easily by looking at a file in the /proc hierarchy:
you might not even be prompted to confirm. Apart
from a small light in the bezel, there is usually nothing $ cat /proc/asound/modules
to indicate that the device is recording. With some de- 0 snd_hda_intel
vices, the program can even turn off the webcam LED. 1 snd_usb_audio
With a classic desktop PC, you can usually simply un- 2 snd_usb_audio
plug the camera and microphone to stop the possibility 3 snd_usb_audio
of video spying. This solution is not available for porta-
ble systems with integrated input devices. Users with However, switching off snd_hda_intel not only takes
laptops and smartphones therefore often apply stickers down the microphone, but also the entire internal
to the webcam and the internal microphones. sound card. In this case, therefore, you need to com-
Alternatively, you can disable the webcam in the sys- promise between sound and perfect privacy. If neces-
tem settings so that programs can no longer call it. On sary, you could still connect a USB headset (even with
Linux, you have to disable the uvcvideo kernel module, an integrated microphone) – those devices use the snd_
which is normally loaded automatically at boot time. In usb_audio kernel module.
principle, you can do this manually via You can also scan the BIOS or UEFI settings for an
option to disable the integrated devices. Working at the
sudo modprobe -r uvcvideo BIOS or UEFI level gives you even more security: Theo-
retically, a blacklist created through the operating sys-
but very often, some program will have claimed the tem can be reversed by software running with adminis-
module so that modprobe fails with a “module in use” trative privileges.
error message that forces you to go for the heavy artil-
By Christoph Langner
lery and try:
https://webrtc.org
sudo rmmod -f uvcvideo https://github.com/webrtc/apprtc

42 101 LINUX HACKS – 2020 EDITION


SECURITY & PRIVACY

Disable Password-Based SSH Logins HACK 35


Do you run an SSH server that is accessible from the In- Authentication yes must be enabled. Don’t confuse this
ternet? That is extremely likely if you rent a root or vir- config file with ssh_config, which is for clients. Restart
tual server from some hosting company or if you have the SSH server (for example, with service ssh restart).
configured your home Internet router to forward some Copy the public key into the server user’s .ssh/au-
port to a local machine’s SSH port. In order to be safe thorized_keys file by running the ssh-copy-id TARGET
from dictionary-based brute-force attacks against your command. This will ask for your password one last
SSH server, you should enable public-key-based access time. Check that you can then log into the server
and then disable logins that are based on username without being asked for your password. Repeat this
and password entry. step on all client computers from which you want to
Before you start, note that if you do this and then log into the server.
later lose the key required for login, you will not be able Finally disable password-based login: Back in the
to login at all. So you need some way to reset the prop- sshd_config file, search for the line PasswordAuthentica-
erties, for example via a management console that tion yes and change the “yes” to “no.” If the whole line
your web hoster provides. (With your personal home is disabled via a “#” symbol, remove that symbol. Then
server, there’s no problem, because you can log in lo- restart the SSH server again.
cally to fix things.) Now logging in should only be possible from the
On a client computer, create an SSH key pair with computers whose keys you copied to the server; on
ssh-keygen -t rsa and protect it with a passphrase. (You every other machine a login attempt should be de-
can work without a passphrase but should only do that nied with the error message “Permission denied
when the client computer cannot be lost or stolen.) This (publickey).”
will generate a public key (id_rsa.pub) and a private key
(id_rsa) in your ~/.ssh directory. By Hans-Georg Eßer
Log into the server and make sure that key-based log-
ins are allowed: In /etc/ssh/sshd_config, the line Pubkey-

Encrypt Your Emails with GPG


HACK 36
GNU PrivacyGuard (GPG or GnuPG) is a cryptography As a starting point, read an older GPG article in Linux
tool that lets you send and receive encrypted mail. It Magazine (issue 90, May 2008) that describes how to
also comes preinstalled with every Linux system – but configure Enigmail, the GPG add-on for Thunderbird.
not preconfigured for email. Other mail clients can work with GPG, too: KDE’s KMail
Sadly, there is no out-of-the-box tool that you can in- has built-in GPG support.
stall and automatically have all your communication
secured. Sender and receiver have to use the same en- By Hans-Georg Eßer
cryption protocol, and in the case of GPG, some further https://www.gnupg.org/
preparation (key exchange) is needed before they can http://www.linux-magazine.com/Issues/2008/90/Enigmail
open an encrypted mail channel be-
tween the two. Figure 4
Using a keyserver (such as keyserver.
ubuntu.com or pgp.mit.edu), you can
search for public keys (Figure 4) and
import them into your GPG client. GPG
users often put a fingerprint in their
mail signature so that it’s easier to find
their key. Once you’ve imported a per-
son’s public key, you can use it to en-
crypt a message and send it. Only that
person can then decrypt the message,
because it requires possession of the
private key.

101 LINUX HACKS – 2020 EDITION 43


SECURITY & PRIVACY

Enhancing Security with sudo Options


The sudo command has been around since the 1980s, but
it has gained popularity in recent years as the default tool
Starting visudo opens a com-
HACK
mand-line text editor. Usually, this edi-
37
for running commands as root in Ubuntu. However, tor is Vim, but you can also set another editor.
there’s far more to sudo than Ubuntu’s policy. In fact, sudo‘s For instance, if you want visudo to open nano instead,
man page is over 2,400 lines long, covering a staggering you can change the environmental variable with the
number of situations – some of which, like many powerful command:
Linux commands, can get you in a lot of trouble if you are
careless. sudo also offers options that can greatly enhance export VISUAL=nano; visudo
security, especially if you take the time to be creative.
Why would you want to enhance your security? The The sudoers file itself is divided into three sections. The
answer is that, from a security viewpoint, Ubuntu’s use first section is the default behaviors. It lists one option
of sudo can be viewed as a problem (although opinions per line. For example, if you want to use the insults op-
do differ). As you may know, when sudo is configured tion – a genuine option, which insults users who make
the way it is in Ubuntu, you can use the password for mistakes trying to log in to sudo – the entry is:
your everyday account to log in to sudo and run root
commands. The trouble is that any password for an ev- Defaults insult
eryday account is exposed in a way that the root ac-
count is not, especially on the Internet. That means that Default settings can be overridden by specific users’ or
if the everyday account is compromised, the intruder groups’ settings. However, above the specific settings
gains root access, too, if sudo is set up on the system. is a section that defines aliases for hosts (hostnames, IP
The traditional separate root password is more secure, addresses, network numbers, or netgroups), for users
although less convenient. Fortunately, though, you can (account names, UIDs, groups, or netgroups), for users
manage both convenience and security by taking the to run as (account names, UIDs, groups, or netgroups),
time to learn the details of sudo. and for commands (usually with full path names). All
aliases consist of uppercase characters or underscores.
Editing sudoers These aliases exist to make defining specific settings
sudo has a unique configuration system. You can con- less cumbersome. For example, if you want user ac-
figure the behavior of the sudo command using the su- counts bab, plw, and vaf to all have the same privileges,
doers file in the /etc directory (Figure 5). sudoers lists you could create the alias
default behaviors and the privileges granted to individ-
ual users. As the top of the sudoers file warns, it should User_Alias ADMIN = bab,plw,vaf
only be edited using the visudo command. visudo is de-
signed to prevent you from editing sudoers in a way With this user alias defined, you can simply define privi-
that would cripple or disable sudo by doing all editing leges for ADMIN, instead of for bab, plw, and vaf separately.
in a temporary file and replacing the original file only In the same way, you create an alias for a list of network
when all editing is done.
Should you make an Figure 5
error while editing sudo-
ers, as you try to save,
visudo will give you the
option to reopen its tem-
porary copy of sudoers to
correct the errors (e) or
discard your edits (X) –
choices that you obvi-
ously should not ignore.
Depending on the distri-
bution, visudo may or
may not display these
choices, but they will be
available whether dis-
played or not.

44 101 LINUX HACKS – 2020 EDITION


SECURITY & PRIVACY

terminals from which a sudo user can log in or a set of password,


commands that a group can or cannot use. Although just as in
aliases can take time to set up, they make creating a new Ubuntu. How-
set of privileges or editing an old set much easier. ever, you can do
In the third section, individual privileges are defined better than that simply by specifying
one per line, using this structure: rootpw, which requires the root password.
Subtler still, targetpw USER can require another account’s
[USER or ALIAS] [TERMINALS]=[USER RUN AS] U password, so that you set up a user with root privileges
[OPTIONS:] [PERMISSIONS] that is used only with sudo. With targetpw, an intruder
will need to be able to read the list of users in order to
Permissions are generally those that a user or alias can find the password for sudo.
use, but adding an exclamation mark (!) in front of Still another basic piece of security is noexec. noexec is
them turns the list into those that cannot be used. designed to limit the running of applications from
For instance, the basic entry for the root user with all which other commands can be run. Without noexec, the
privileges is: running of one application could easily give intruders
access to other applications in the system.
root ALL=(ALL) ALL However, individual privileges are where ingenuity
reigns. With a little planning, you could set up separate
Individual terminals or commands can be entered in- accounts with limited root privileges. For example, one
stead of ALL. USER RUN AS and OPTIONS are optional, so that account could only be permitted to run tools for install-
the line ing package managers, such as Debian’s apt-get, apt,
and dpkg, while another would be limited to running
bab ALL= (ROOT) passwd, chown, chgrp, chmod commands for changing file attributes. With such an
arrangement, Linux can be made to mimic other so-
is enough to give user bab the ability to change pass- phisticated Unix descendants. While each limited ac-
words and permissions from all terminals on the sys- count can do specific functions, an intruder who gains
tem, while running as root. More simply still, defining access to one account via sudo would not have com-
an alias called ATTRIBUTES that included all four com- plete control over the system.
mands would reduce the line to:
Getting More from sudo
bb ALL=(ROOT) PERMISSIONS sudo is a sophisticated command. However, as you can
see, it is also a seriously underused one. Even if you
Remember, though, that specific permissions override have no interest in such options as setting the com-
those set as defaults. mand prompt, there are still a number of options that
can make switching into sudo to temporarily gain root
Defaults, Privileges, and Options privileges safer – and all without sacrificing any conve-
Defaults and privileges are defined using the options nience once everything is configured.
listed in the sudoers man page. The available options It is easy to think of sudo as a magical word that some
range from requiring no login whatsoever to specific distributions require at the front of administrative com-
settings for greater security. mands. And for many people, that may be enough. But
Many security options affect how to log in to sudo. For sudo can also be much more. You might even investi-
example, password_tries=NUMBER sets how many times a gate sudo plugins like Privilege Manager for Sudo,
user can try to log in before being denied. It is accom- which allows you to set policies for sudo graphically, or
panied by passwd_timeout= MINUTES, which sets how long sudo_pair, which requires an admin to approve any use
sudo runs before logging out a user – an especially use- of sudo. But, one way or the other, if you are concerned
ful feature when using root privileges, since basic secu- about security, you owe yourself the time to learn what
rity decrees that the root account should be used for as else sudo can do.
short a time as possible. With passwd_timeout, you no
long have to rely on your own memory to close root as By Bruce Byfield
soon as possible. Less drastically, timestamp_
https://www.sudo.ws/
timeout=MINUTES sets the time before sudo prompts for
another login. https://www.oneidentity.com/products/privilege-manager-
for-sudo/
Other options set which password sudo requires from
https://github.com/square/sudo_pair/tree/master/sudo_pair
you. The option runaspw requires the current account’s

101 LINUX HACKS – 2020 EDITION 45


SECURITY & PRIVACY

HACK
Use Two-Factor
Authentication 38
with Authenticator
Many of us now use two-factor authentication
(2FA) when connecting to vital online services
such as Google Mail and GitHub. 2FA adds a
Start Your Training
with Web Security Dojo
HACK 39
second factor alongside your password, with Protecting your own websites from attack either costs a
the most common second factor being a time- lot of money or requires a lot of expertise. A specialized
limited, one-time pin generated by an app on Linux environment called Web Security Dojo offers an easy
your smartphone. way for everyday users and beginning professionals to
But using a smartphone app isn’t always learn about web security. Dojo is designed to provide prac-
ideal. This is where Authenticator helps. It is a tical, hands-on exercises on web security and intrusion
GTK3+ application that fits perfectly into a techniques. On SourceForge, you can find a virtual ma-
Gnome or Cinnamon desktop (Figure 6). It has chine (VM) image of around 2.7GB in OVA format that
both a light and a dark theme and looks lovely, works with both VirtualBox and VMware; it is based on
oriented in a portrait style much like the app. Its Ubuntu 16.04 LTS with an Xfce desktop.
best feature is that when you press the Add but- When you launch a browser in the VM, you learn about
ton to add a credential, almost every service the software you’re going to attack: Damn Vulnerable Web
you can imagine that supports 2FA is listed, Application (DVWA). From a menu, you can pick various at-
rather than leaving you randomly entering en- tack technique options, such as Cross Site Scripting (XSS),
cryption keys in the hope it works. With that SQL Injection, CSRF, or Brute Force (Figure 7). For the vari-
done and with the necessary keys exchanged ous scenarios, you will receive background information in
with the online service, you can then use Au- the form of links to related websites and wikis.
thenticator just like the app – selecting the pre- Web Security Dojo provides an excellent training oppor-
configured service to get a time-limited code tunity for budding security professionals who want to be-
and entering this into your login prompt. come familiar with the basic mechanisms for protecting
Having this facility on the machine where web applications. The OVA image is easy to install, and
you’re likely to use the codes is very convenient. the Xfce desktop is easy to configure. The developers
However, it does add a security risk, as anyone have carefully adapted the tools and test environments for
with access to your machine will be able to con- their intended use, so you can get started with the practi-
nect to your services (if they know the other fac- cal exercises right away.
tor – the password), so be careful!
By Erik Bärwaldt
By Graham Morrison
https://www.mavensecurity.com/resources/web-security-dojo
https://github.com/bilelmoussaoui/Authenticator/
https://sourceforge.net/projects/websecuritydojo/
Figure 6

Figure 7

46 101 LINUX HACKS – 2020 EDITION


SECURITY & PRIVACY

Lock Your Screen, Always


There are obvious situations when you should lock
HACK 40
for unlocked computers, and open a website like lock-
your screen, like when you work in an open-plan office, yourscreen.com (Figure 8). Of course, that’s better than
you have opened four terminal windows with remote quickly installing a backdoor SSH server.
root shells on various machines, and you’re just about But even in less dangerous situations, it makes sense
to leave for an extended lunch break – you don’t want to lock the machine; if you’re at home and have a cat
the computer to stay accessible. who likes to walk across the keyboard, it may provide
During my time as a postgrad, it was a running joke just the right input for the vi editor to do bad things to
that co-workers would enter each other’s offices, search your configuration file. (However, it is unlikely that the
cat will enter :w and save her changes.)
Figure 8 Every desktop has some menu entry for locking the
screen, but it’s faster to just press some keys. In addi-
tion to Linux, I’ve also looked up the hotkeys for two
non-Linux operating systems, because this is important
on every computer:
• Gnome: Win+L
• KDE, Cinnamon, and Xfce: Ctrl+Alt+L
• macOS: Ctrl+Command+Q
• Windows: Win+L
Of course, the Linux desktops will let you change those
hotkeys to something else.

By Hans-Georg Eßer
https://lockyourscreen.com/

H AC K
Use ccrypt for Quick Encryption
There are many ways you could encrypt a file before
sending it as an email attachment or uploading it to
The command-line tool ccrypt
provides a quick solution. It is avail-
41
some cloud storage service, and most of them will able from most distributions’ repos, so apt install, zyp-
need some amount of preparation or contain needless per in, or dnf install the package on your machine.
steps. For example, sending the attachment in a GPG- For encrypting a single file, type ccrypt filename. The
encrypted email requires you and the recipient to have tool will ask for an encryption key twice (because it
GPG installed and configured, and you need the other does not echo the characters you type), and that’s it.
party’s public key. Putting the file in a password-pro- Look into the directory, and you find that filename is
tected ZIP archive is an option, but why create an ar- gone and a new file filename.cpt has taken its place.
chive when you have just one file to send? The program has actually overwritten the data blocks
of the original file.
Figure 9
If you’ve chosen a good password (something that
won’t easily be found in a dictionary attack), you can
now transfer the file over insecure channels. The re-
cipient runs the ccdecrypt command on the file and
enters the password – after that the original file is re-
stored (Figure 9). For even better security, drop the
.cpt extension before sending the file making it
harder to guess what kind of encryption algorithm
was used.

By Hans-Georg Eßer
http://ccrypt.sourceforge.net/

101 LINUX HACKS – 2020 EDITION 47


Networking
Hopefully, your network is doing fine, both Ethernet and WLAN.
If not, look at our hacks that help with network configuration
and analysis. We also talk about clever tools like NitroShare
and Magic Wormhole that let you do hassle-free file transfers.

Image © lightwise, 123RF.com

HACK 42 HACK 48
Monitor Your Network with Nutty 49 Run a Simple HTTP Server: weborf 53
HACK 43 HACK 49
DIY Network Configuration 49 Use screen in SSH Sessions 53
HACK 44 HACK 50
Share Files on the Local Network 50 Access a Remote PC’s Shell Session 54
HACK 45 HACK 51
Dig a Tunnel with sshuttle 51 Slow sudo? Check Your Hostname 56
Configuration
HACK 46
Send Files Without Knowing the Target’s IP 51 HACK 52
Use Network Manager’s CLI 56
HACK 47
Check Your DNS Server with DNSDiag 52

48 101 LINUX HACKS – 2020 EDITION


NETWORKING
H AC K
42 Monitor Your Network with Nutty
Network monitoring isn’t easy for everyday
users. If you’ve ever taken a look at the packets
Nutty isn’t quite that application, but it gets close. It’s a
network monitoring tool that will attach itself to one of
captured by Wireshark, they’re incredibly complex and your interfaces, such as your wireless connection, and
difficult to understand. And so too are the associated then perform a series of monitoring processes or tests.
command-line tools. It’s easier for distros to keep users The main view is a tabbed interface with the first pane
at arm’s length, letting them worry about wireless net- showing general details about your hardware, such as
work strength and streaming quality rather than present- your hostname, network driver, IP address, and firm-
ing users with a stream of data that’s difficult to inter- ware. The second tab, Usage, attempts to detect which
pret. But much like with a task manager or memory processes are using your bandwidth. This can be very
monitor, there are many good reasons for ordinary users revealing, especially if you’ve forgotten about that Next-
to be better informed about their network consumption. cloud daemon quietly syncing your files to the cloud in
We just need a decent application to turn that burden of the background. The third tab uses speedtest-cli to test
knowledge into something easy to use, yet powerful. the speed of your connection, while the fourth lists all
Figure 1 your local ports being used on the network and the pro-
cesses attached to those ports (Figure 1). This is likely
the most useful if you want to see which processes are
accessing your network. The final tab will perform a net-
work scan, much like netstat -sP on the command line.
And that’s really what this great little tool is all about, en-
capsulating some of the most useful output from dispa-
rate and sometimes difficult to use command-line tools
to help you monitor your network.

By Graham Morrison
https://github.com/babluboy/nutty

DIY Network
Configuration HACK 43
Just because you can have your IP address assigned
# ip route add default via 192.168.1.1

You need to substitute 192.168.1.1 with your gateway ad-


dress, which needs to be on the same subnet as you are.
automatically doesn’t mean it’s required. Good old Your final step is to tell Linux the DNS server to use:
static network configuration still works in 2019.
# echo 'nameserver 8.8.8.8' > /etc/resolv.conf
Linux provides several approaches to manual net-
work configuration. The traditional one is via ifconfig This will set up a Google DNS server. Replace that with
and route, but modern distributions don’t automatically your true local DNS server if you know its address.
install these tools anymore. Today’s alternative is using Things are trickier if your system uses resolvconf or a
the single ip tool, which is more versatile. similar management framework. In that case, refer to
The roadmap is as follows. You bring the device on- the man pages.
line, assign it an IP address, then add some routes (at If you don’t want to configure the network card but
least, the default one), and configure DNS. Imagine you only look up the current settings, use the show options
want to configure the network on the eth0 device. Then to get the output in Figure 2.
start with the following (as root): By Valentine Sinitsyn
# ip link set up dev eth0 https://en.wikipedia.org/wiki/Resolvconf

This brings the device “up,” that is, to the active state. Figure 2
Now, assign it an IPv4 address with:

# ip addr add 192.168.1.5/24 dev eth0

It is up to you to ensure that the IP address you’ve cho-


sen doesn’t conflict with anything else on your net-
work. You may also notice that I used a CIDR notation,
addr/mask: It allows you to assign both the IP address
and the netmask in a single command. mask is the num-
ber of bits set in the network mask, so /24 is equivalent
to 255.255.255.0 in ifconfig‘s parlance.
The next step is adding the default route:

101 LINUX HACKS – 2020 EDITION 49


NETWORKING

Share Files on the Local Network


HACK
for downloads in the General tab. Under Advanced,
44
you can change the port, buffer, time out, and interval,
There are many tools for exchanging data on heteroge- if necessary.
neous local networks. NitroShare promises to make
this task as easy as possible. In addition to Linux, Nitro- Encryption Is Possible
Share also supports macOS, Windows, and Android Although not normally necessary on your own net-
(see the “Installation” box). work, the software also offers the possibility to encrypt
After installation and an initial start via its menu entry, transfers via Transport Layer Security (TLS) in the Secu-
the program shows up in the system tray (Figure 3). rity tab. A description of the required configuration can
From there, it offers the exchange of files and directories be found on the NitroShare wiki.
with computers on the LAN on which the software is The menu also offers the Send Files and Send Direc-
also installed. NitroShare automatically detects the cor- tory options, which allow you to select files or directories
responding devices and offers them as transfer targets. and then specify the desired transfer destination. During
The software is ready for use immediately after installa- data transfer, the software displays its history. You can
tion; no further configuration is required. In the Settings close this dialog without affecting the transfer. After the
menu, you have the option of configuring a number of transfer has completed, you see a message to this effect.
settings depending on your distribution and desktop (Fig- NitroShare does not limit the size of the data sent and
ure 4). Checking Auto start after login causes the program can thus theoretically achieve high speeds on networks
to launch when you log designed for this purpose. Behind the scenes, the pro-
Figure 3
in. Automatic detection of gram uses IPv4 broadcasts to send User Datagram Pro-
other devices can take up tocol (UDP) packets to other devices on the LAN. These
to five minutes the first contain information that allows other devices to initiate
time. At the end of this transmissions without having to worry about broadcast
phase, the tool displays a addresses and the like.
message with the results.
In the Settings dialog, Conclusions
you can also specify a Despite a few problems that occurred on some of our test
name other than the machines, NitroShare does what it promises: After the in-
hostname for each de- stall, you can get started immediately without having to
vice and a default folder configure any settings. Transfers take place quickly.
Android devices are limited to the sender role; the
Installation software does not offer mobile devices as targets on
other clients. To do this, some Android apps offer the
I tested the tool with a network of computers running option of sending files directly via NitroShare. During
Debian Sid, Windows 10, Ubuntu, Ubuntu MATE, Fe- the test, I found corresponding entries in Google Docs,
dora, and the Android app. Users with Debian Sid, Gallery, and Photos, but there may be more of them.
Ubuntu, Fedora, Gentoo, and openSUSE will find the Nevertheless, as far as Android is concerned, KDE Con-
software in their respective repositories; for Arch Linux nect – which is even available on GTK desktops – seems
users, check out the AUR user archive. If necessary,
to be the more mature solution.
compile the program for other distributions from the
source code.
By Ferdinand Thommes
Packages for Nautilus (Gnome), Nemo (Cinnamon), and
https://nitroshare.net
Caja (MATE) are available for integration in file manag-
ers. Although NitroShare is based on the Qt5 frame- https://github.com/nitroshare/nitroshare-desktop/wiki/TLS
work, not all distributions have appropriate extensions. https://community.kde.org/KDEConnect
Packages for integration into Dolphin and Konqueror
were only available for openSUSE as dolphin-plu- Figure 4
gin-nitroshare and for Fedora as nitroshare-kservice.
Fedora also offers a plugin for use in the terminal.
The installation on Debian and Fedora is quickly done
by installing the nitroshare package via APT or DNF.
Ubuntu and Mint users can add a special repository – in-
structions and the URL for the Personal Package Archive
(PPA) can be found on the project website.
The package itself is only 150KB with just one depen-
dency on a small 40KB Qt web server. If you do not want
to use the PPA in Ubuntu, use the same command as in
Debian. For Windows, executables for 32- and 64-bit
systems are available on the website, and a DMG pack-
age is available for macOS.

50 101 LINUX HACKS – 2020 EDITION


Dig a Tunnel with sshuttle
HACK 45 NETWORKING

keep the local LAN still visible, I define it as an excep-


tion using the -x parameter:
In untrustworthy networks, I let OpenVPN tunnel my lap-
sudo sshuttle -r --dns <User>@<Server> 0/0 -x 192.168.2.0/24
top. There are certainly alternatives, and I would like to
present a particularly simple one: sshuttle. As the name --dns is included here. This means that DNS queries also
suggests, the tool relies on SSH. The tunnel’s endpoint is run through the tunnel, which does not happen auto-
a leased root server, just like with OpenVPN. Sshuttle is matically. This is sshuttle’s Achilles heel: It only trans-
very frugal. It only needs SSH access with user privileges ports TCP; ICMP and UDP do not pass through the tun-
on the server; root privileges are not necessary. Addition- nel, apart from DNS.
ally, Python must be installed on the server – that’s it.
This is because sshuttle loads and executes the re- Congestion Alert
quired Python code on the server after the SSH connec- Whereas other VPN technologies work at packet level
tion is established. It also avoids version conflicts be- and rely on TUN/TAP devices, sshuttle works at session
tween server and client software. The following com- level. It assembles the TCP stream locally, multiplexes
mand is all it takes to set up the tunnel: it over the SSH connection, while keeping the status,
and splits it into packets again on the destination side.
sudo sshuttle -r <User>@<Server>:<Port> 0/0
This avoids the TCP-over-TCP problem that plagues
You can leave out the port number if it is the SSH stan- other tools such as OpenVPN: TCP has an overload
dard port 22. The 0/0 means that Linux should direct all control (congestion control). The protocol defines a
connections into the tunnel. However, this means that I performance limit on the basis of dropped packets. If
cannot reach other devices in the local network. To you tunnel TCP over TCP, you lose congestion control
for the inner connection, which can lead to bizarre error
Figure 5
patterns. Sshuttle is immune to the problem.
Verbose parameters can help if you do need to trouble-
shoot. Figure 5 shows a connection setup with -v. With the
verbose option, sshuttle is very long-winded, so I recom-
mend redirecting the output to a file that can be evaluated
in peace. My conclusions: Sshuttle is an excellent and sim-
ple VPN for people who can do without UDP and ICMP.

By Charly Kühnast
https://github.com/apenwarr/sshuttle

H AC K
Send Files Without Knowing the Target’s IP
Magic Wormhole really is one of those tools that, once recipient then simply types wormhole
46
you’ve used it, you’ll wonder how on earth you man- receive followed by the same phrase, and
aged to get by without it. This is because Magic Worm- the transfer will start immediately downloading the file
hole solves one of those ancient problems that have from one machine to the other, no server required. It’s
been around on Linux since we first started to connect perfect for transferring between virtual machines or
computers together with null modem connectors and a people sitting next to each other, when every other solu-
couple of lengths of wire. Back in those olden days, you tion requires more than a set of Python scripts.
might have used ZMODEM to transfer a file across the By Graham Morrison
serial connection. If you then upgraded to a connection https://github.com/warner/magic-wormhole
capable of TCP/IP, you could use FTP to transfer files
and then came HTTP. In the modern age, scp is often the Figure 6
best choice if you’re accessing an SSH server and want
to securely transfer files, or rsync if you want to copy
folders or perform incremental backups. But all of these
solutions suffered and suffer from the same problem:
The remote machine needs to be running a server of
some kind, and you need to know the remote address
of the machine you want to access.
This is why Magic Wormhole is so brilliant. If you want
to send a file, simply type wormhole send followed by the
file name (Figure 6). In the output, you’ll see a secret
phrase that you’ll need to share with whomever you want
to receive the file. The phrase is constructed from a few
words, so it’s easy to say or copy without mistakes. Your

101 LINUX HACKS – 2020 EDITION 51


NETWORKING

Check Your DNS Server with DNSDiag HACK 47


If some transactions take an inexplicably long time, you The result from running it shows a remarkable dis-
don’t have to blame yourself for the delayed transmis- crepancy between minimum and maximum response
sion of user data – name resolution issues might be to times (Figure 7).
blame. Three tools help in studying the DNS server:
dnsping, dnstraceroute, and dnseval. Highwayman?
The tool collection for name resolution is entitled dnstraceroute determines the path my DNS query takes
DNSDiag. You need Python 3 and pip3 to install and run to reach the target. By comparing this with a classic
the trio and sudo to let it create ICMP sockets. ICMP traceroute, I can identify an attacker trying to kid-
dnsping lives up to its name, repeatedly querying a nap my DNS queries. My test call is:
DNS server and displaying the response times. The host-
name to be resolved is a mandatory parameter. dnsping sudo dnstraceroute.py --expert --asn -C -s 8.8.4.4 U
prompts you for the system’s default name server, which linux-magazine.com
can be changed using -s <nameserver>. After typing
The result is shown in Figure 8. The --expert parameter
sudo dnsping.py -v -s 8.8.8.8 linux-magazine.com provides tips if something seems to be suspicious in
the output – for example, if the target server is only a
I queried a public DNS server from Google. Its re- hop away from a private IP address (RFC 1918). False
sponses took 20 milliseconds to reach me, four times alarms also occur if you are not working on a cloud
more than my provider’s DNS. server, but locally, and a DNS cache such as Dnsmasq
dnseval queries several servers in parallel. As a com- is running on the router.
petition judge, it presents the results so that you can For each hop, the --asn parameter shows you the au-
immediately see which server responds fastest or tonomous system providing the network for the ad-
slowest. I redirected the list of servers to be checked dress. I can thus quickly see where the process crosses
into a text file, with one server in each line. Lists of my provider’s boundaries.
public DNS servers are easy to find; I took the first five By Charly Kühnast
servers from the list in an article on lifewire.com. The
call looks like this: https://dnsdiag.org
https://www.lifewire.com/free-and-public-dns-servers-2626062
sudo dnseval.py -f ./liste.txt -c 5 linux-magazine.com http://www.thekelleys.org.uk/dnsmasq/doc.html

Figure 7

Figure 8

52 101 LINUX HACKS – 2020 EDITION


NETWORKING

HACK 48
Run a Simple HTTP Server: weborf mously, even adding the ability to do NAT traversal to
share files outside of the local network and sending di-
The wormhole tool mentioned in hack 46 is perfect for rectories as .tar.gz files.
one-off transfers, but it’s not all that great if you want to
share more than a single file or leave the tunnel open By Graham Morrison
so that you don’t have to keep renegotiating keywords. https://github.com/ltworf/weborf
There are of course lots of ways this can be done, but
weborf attempts the task while remaining almost as sim- Figure 9
ple to use as wormhole.
weborf (Figure 9) sets up a simple HTTP server, just
like running Apache in the olden days to share the con-
tents of /home. This makes it supremely flexible, not just
from a web browser, but from almost anything with ac-
cess to the network using WebDAV, with caching, even
from virtual hosts or running CGI scripts. You simply
run the server command with an argument for the port
to use and a folder to share, and all the client has to do
is access your IP address with the correct port. It can be
run in the background as a daemon, use certificates,
handle authentication with your own tools, and listen
only for connections from specific IP addresses. If the
command line offers too many options, a convenient
Qt-based GUI can be run to handle all this semiautono-

H AC K Use screen in SSH Sessions


49 Imagine that you work on a remote machine via
SSH and launch a command that will take some
Maybe you’re not sure whether there is already a screen
session: The tool offers a -ls option for that situation:
time to complete (such as a build process). If the computer
esser@hp-z420:~$ screen -ls
you’re working on goes to sleep or temporarily loses its
There is a screen on:
network connection, the SSH connection will die. The re-
16712.pts-6.hp-z420 (05/06/2019 11:24:49 AM) (Detached)
mote machine will notice and terminate all processes
1 Socket in /run/screen/S-esser.
started in that login session, so your build process is gone.
There are several things you can do to avoid such a You can even automate the procedure of logging into
scenario. An old trick is to prepare for network outages the remote machine and checking for screen sessions.
and connection loss by explicitly running processes in Create a file ~/.bash_login (if you don’t have one al-
the background and using the nohup command so that ready), and add the following line to it:
they will ignore the hangup signals that are sent after
screen -ls > /dev/null && screen -ls
connection loss. For the build process, that might be
That way, when you log in and there is a detached
nohup make &
screen session, you will be notified about it (Figure 10).
instead of a simple make command. A side effect is that (Bash only executes ~/.bash_login in login shells.)
all output will be written to a nohup.out file in the current If you want do manually detach from a running ses-
directory. sion, press Ctrl+A, D. There are many more interesting
My preferred solution does not require nohup or simi- things you can do with screen – check the man page and
lar tricks. Instead, I use screen in remote sessions. (If try a few of them.
your machines don’t have it installed, the package is By Hans-Georg Eßer
typically called screen, too.)
Figure 10
When screen starts, it displays a longish message about
copyrights and licenses; press Return to get rid of it. You
can then continue to use the shell as usual. If you lose the
connection, simply reconnect to the remote machine and
run screen -r to reattach to the existing session.

101 LINUX HACKS – 2020 EDITION 53


NETWORKING

Access a Remote PC’s Shell Session


Your parents or friends have replaced an unmanage-
HACK 50
able Windows installation with an easy-to-use Linux Creating Keys
variant, and they have designated you as their remote After the installation, which occupies only a few kilo-
admin. To access the system from afar, you resort to a bytes on your disk, you need to create a key pair for SSH
GUI-based tool such as TeamViewer or AnyDesk. All is (if it does not already exist). As shown in Figure 11, this
well until the GUI for the computer you need to access is done by typing:
breaks. Suddenly, your GUI-based remote access tool
won’t work anymore, because you don’t have access to ssh-keygen -t rsa -b 4096
an X server.
Tmate, which stands for “team mate,” offers a con- Answer the individual prompts by pressing the Return
venient solution for remote access in situations when key. You can assign a password for the key, but in our
you can’t depend on the graphical tools. This fork of example this is not necessary for home use.
the popular tmux terminal multiplexer has signifi- Tmate uses the RSA cryptographic procedure with
cantly more functions than the original. Tmux is func- a secure key length of 4096 bits. By default, the keys
tionally similar to screen (see hack 49) in some ways. are located in the hidden .ssh/ directory in your
You can use tmate to create and manage remote vir- home directory; tmate will find them automatically
tual sessions within a terminal. when you use the tool. You need to generate these
keys on each computer involved if a key pair does
Self Hosted not already exist there.
Tmate is not only suitable for external help, but also When you open a connection, the program creates a
lets developers collaborate on software. Technically, 150-bit session token. It then launches a tmux server in
the tool uses SSH to establish a secure connection to a sandbox without a filesystem and user rights, but
the tmate.io server, which is under the control of the with its own namespace to isolate the server from
tmate developers. other processes. To make this possible, the software
Alternatively, you can run tmate on your own server. opens all the files it needs for running the tmux server
The resource requirements are kept within such narrow before they end up in the sandbox.
limits that a recent Raspberry Pi or a similarly equipped
single-board computer is sufficient. The machine only SSH ID or URL
generates an SSH ID and some URLs for different types After starting tmate as a user for the first time, a bar at
of sessions – SSH or web sessions in read-only mode the bottom of the terminal window shows a random
or with full access. SSH ID for establishing an SSH session in read/write
Installing tmate is easy, because the program is avail- mode (Figure 12). It is best to copy this line to the clip-
able from the package sources of all common distribu- board, because it disappears after a short time.
tions. For Arch Linux, use the AUR user archive; for If you did not react fast enough, you can get the data
Debian, Fedora, and their derivatives including Ubuntu, including the other IDs or URLs back on the screen with
just install the tmate package with APT or DNF. the tmate show-messages command. This command is
also used for logging during a session (Figure 13).
Figure 11
Figure 12

54 101 LINUX HACKS – 2020 EDITION


NETWORKING

Figure 13

While the SSH ID displayed at the start of tmate The tmate software itself is only required on the ma-
creates a read-write session in a terminal, tmate chine where you want to start sharing a session – that
show-messages also shows an ID for a terminal session can run Linux, macOS, or one of the BSDs.
that supports read-only access. In addition, the pro- When the other party now enters the transmitted
gram provides two URLs that support the same pro- SSH ID/URL, the terminal shows what the person
cedure in the browser. seeking help is seeing in the terminal themselves.
Commands and their results run back and forth be-
Sending Keys tween the computers in fractions of a second. In the
Now you only have to send the SSH ID or URL to the session with read and write access, all parties can
other participants of the session. The best way to do enter data themselves. In the restricted session, only
this is to use chat and mail or share via a service such the host can write; the participants remain restricted
as Nextcloud. The final option you could consider is an to reading.
error-prone transmission by telephone. If you no longer need the split terminal, it is advisable
The participants you invited and entrusted with an to end the session by entering exit. If you just close the
ID or URL do not need to install tmate or tmux. You terminal, the other side will still have access to the
don’t need to use the same distribution or even use computer – a potential security risk.
Linux: Any operating system is fine as long as it has
an SSH client (Figure 14). And even the SSH client re- By Ferdinand Thommes
quirement can be lifted, because tmate also supports https://tmate.io
web-based connections: Just create a URL https:// https://github.com/tmux/tmux/wiki
tmate.io/t/SESSION where SESSION is replaced with https://www.gnu.org/software/screen/
the username part.

Figure 14

101 LINUX HACKS – 2020 EDITION 55


NETWORKING

H AC K
51 Slow sudo? Check Your Hostname Configuration
While playing with a virtual machine, I the machine on which it is running, and that operation
came across a strange problem: Each time I ran a may cause a delay when you have mismatching infor-
command via sudo, there was an artificial delay of mation in your network configuration files.
about two or three seconds. After some Internet When you notice inexplicable delays, check the con-
searches and a confirming look at the source code, I tents of some of your configuration files – even if you
found out that the sudo tool queries the hostname of don’t think the problem is network-related:
• /etc/hosts should have an entry for your hostname –
Figure 15 either pointing to 127.0.0.1 (in that case it will be
placed on the same line as the localhost name) or on
a separate line with some other 127.* address.
• /etc/hostname should contain the hostname.
• Running the hostname command should also return
the hostname.
Figure 15 shows a situation (taken from a Linux Mint
18.3 desktop) in which settings are as expected: All
three use mint183cin. If you get different results (which
may happen after you have manually changed the
hostname instead of using your distribution’s “official”
configuration tool), fix the situation so that the new
hostname replaces the old one everywhere.

By Hans-Georg Eßer

HACK 52 Use Network


Manager’s CLI
Network Manager, the configuration tool used by most
nmcli dev wifi connect SSID password PASS

(with SSID and PASS replaced, of course). You need not


add sudo, because the tool connects to a daemon that has
current distributions, comes with a command line pro- the necessary privileges. Actually anyone who is logged
gram called nmcli (which is short for the obvious: Net- in to the machine can use nmcli or any other program that
work Manager command line interface). It lets you talks to Network Manager – for some tasks it will explicitly
query some interesting data, including available WLAN request root privileges or fail when run as a regular user.
access points (Figure 16). There is a long list of sub-commands (such as dev and
When you’re in a new place like a hotel and you’ve connection) that you can use with nmcli. Check its man
received a paper slip with an SSID and password, you page or the separate nmcli-examples(5) man page for
can use nmcli to connect without going through a further useful examples.
graphical dialog. Just type
By Hans-Georg Eßer
Figure 16 https://wiki.gnome.org/Projects/NetworkManager

56 101 LINUX HACKS – 2020 EDITION


This is Linux, so you’ve expected us to talk
about the shell, haven’t you? There are so
many interesting things that you can do in a
terminal window that we
could have filled a whole HACK 53
issue. Replace some of the Configure Your Shell History 59
classic commands with HACK 54
enormously improved Progress Bars for Standard Tools 60
versions. HACK 55
Replace top with htop 60
HACK 56
Super-Fast Terminal Emulator 61
HACK 57
Universal Unpacker 61
HACK 58
Listing Files with Style 62
HACK 59
ASCII Browsing with Browsh 63
HACK 60
Command References at cheat.sh 64
HACK 61
Highlighting Instead of Grepping 65
HACK 62
Add File Type Icons to Your File Listing 66
HACK 63
Upterm, a Terminal with Built-In Shell 67
HACK 64
The fish Shell 67
HACK 65
Using a Sub-Shell 68
HACK 66
Start the Right App 68

Image © lightwise, 123RF.com

58 101 LINUX HACKS – 2020 EDITION


SHELL HACKS

Configure Your Shell History $ tail -1 /var/log/messages


HACK 53
tail: cannot open '/var/log/messages' for reading: U
Most tasks are repetitive: You enter the same directory, Permission denied
open the same file in your favorite editor, and query $ sudo !!
your dynamic IP address once again. Typing the re- sudo tail -1 /var/log/messages
quired commands again and again would be annoying, 2019-04-26T17:39:16.973359+02:00 suse151b sudo: U
but often you won’t have to do that: The shell remem- pam_unix(sudo:session): U
bers your commands and stores them in its history – session opened for user root by esser(uid=0)
both in memory and (when you exit the shell) in a file in
your home directory. How old can a command be and still show up in the his-
Accessing the history is as simple as pressing the up tory? That depends on how often you use the shell – and
arrow key: Each keystroke will travel back further in time on the HISTSIZE variable that is typically set to 1000:
and show the last command, the second to last one, and
so on. When you have found the right command, either $ echo $HISTSIZE
press Return to run it again or edit it (like you would edit a 1000
freshly entered command with a typo).
Walking through the history with the up arrow key By default, a shell process will remember the last 1,000
might take too long if the command you want is rather command lines. If you want to improve the shell’s
old. You can skip the manual search if you know some memory, just add an
part of the command or its argument(s): Just press Ctrl+R
and type the letters you remember from your command. export HISTSIZE=20000
If you cannot make your search expression longer and the
current match is not what you’re searching for, press line in your ~/.bashrc file (and replace 20000 with the
Ctrl+R again to let the shell find an older match – repeat value you want).
until you’ve found the right command. When you increase HISTSIZE, you should also change
Another way to search for and reuse an old command a second variable: HISTFILESIZE controls how many lines
is running the history command: This Bash built-in the shell writes to the ~/.bash_history file when it termi-
shows the whole history and prefixes every entry with a nates – that is where the shell stores the history so that
number (Figure 1). You can rerun a command from the it can read it back when you start the next shell session.
list by typing ! and the number, and you can add text be- There are more variables that influence the shell his-
fore and after the !-number expression that lets you tory: For a full list, search for “hist” in the bash(1) man
modify the command. In the example in Figure 1, I’ve page. One last variable that I’d like to mention is HISTTIME-
built a pipeline out of history entry 73 (modinfo uio) and a FORMAT, which normally is undeclared. If you set it to %F %T
fresh head -2. (plus a trailing blank), history entries gain a timestamp:
The Bash shell also provides a shortcut (!!) for the
last command – why would you need that when the up $ HISTTIMEFORMAT="%F %T "; history | tail -5
arrow key will bring back the last command? One ex- 974 2019-04-26 17:50:48 ps
ample use is combining the last command with sudo so 975 2019-04-26 18:24:42 echo $HISTCMD
that it runs with root privileges. In the following lines, 976 2019-04-26 18:26:00 history
you can see a failed attempt to read the system logfile 977 2019-04-26 18:26:39 echo $HISTTIMEFORMAT
and then a successful one via sudo !!. 978 2019-04-26 18:32:09 HISTTIMEFORMAT="%F %T "; U
Figure 1 history | tail -10

The shell will also store those timestamps in the history


file (see Figure 2).
By Hans-Georg Eßer
Figure 2

101 LINUX HACKS – 2020 EDITION 59


SHELL HACKS

Progress Bars for


Standard Tools
HACK
54 Replace top with htop
There are few tools more effective
than the venerable top command for
HACK
process monitoring and control. This is because it
55
Almost since the beginning of Unix time, Unix com- can be run from almost anywhere that has a Linux
mand-line tools have done very little to show their command line and gives you an immediate and real-
output. The bearded wisdom is that if you can’t see time overview of what processes are running; how
any output from a command, then nothing has gone much memory, swap, and CPU they’re consuming;
wrong, and you can assume whatever task you and the ability to kill a process by simply selecting it
started has completed successfully. If you need error and issuing a command. Except that top is perhaps a
and success states, look for exit values and act ac- little too venerable at times, with its minimalist and
cordingly. This principle actually works very well, austere user interface written for 1984-era Unix rather
and it would be lovely if it was adopted by modern than the cloud droplets of the 21st century. htop is a
technology, such as REST websites and convergent drop-in replacement for this fabulous command.
mobile apps. But it does leave you with some ambi- htop adds lots of features lacking in the original,
guity over whether something is still running, and if and its developers occasionally release new versions
it is, how long until you can go to bed and turn off to make it adapt to new features of the various ker-
your PC. nels it supports: Besides Linux, htop runs on macOS,
This is where progress can help. It works with FreeBSD, OpenBSD, and Solaris.
many of the GNU core utilities that most of us take Launched just like top, and with shortcuts and argu-
for granted. These core utilities include cp, mv, dd, tar, ments that offer the same kind of functions, most
gzip/gunzip, cat, and many more. You simply run new users can simply continue with this new tool just
them alongside the progress command, either after as they would with the old one. They’ll get a much
the watch command to present a general overview of more reactive curses-based interface and overview of
core utility commands or forked after the execution what’s running (Figure 4). Unlike top, there’s a func-
of one specific command. tion key-driven menu system that can be used to
For example, you can monitor the progress of com- change the sort mode, kill processes, and get help.
pressing a tar archive with There are lots of other shortcuts, too – such as press-
ing C to see threaded processes or S for integrated
gzip /root/usr.tar & progress -mp $! strace. It’s quick, powerful, and takes very few re-
sources. If you haven’t already switched from top to
and let progress display the gzip percentages as the htop, now is the perfect time to do so.
compressor completes its task (Figure 3). This is use-
ful if you’re working with slow USB storage devices By Graham Morrison
on the command line. The tool simply monitors / http://hisham.hm/htop
proc/PID/fd and /proc/PID/fdinfo folders for processes
it watches: fdinfo shows the current read/write posi- Figure 4
tion. It’s a simple solution, but it works well and can
be used in a huge variety of tasks, from monitoring a
file or web server to the download status of some-
thing you’ve clicked in Firefox.

By Graham Morrison
https://github.com/Xfennec/progress

Figure 3

60 101 LINUX HACKS – 2020 EDITION


SHELL HACKS
HACK 56 is that this speed comes from a part of your system
Super-Fast Terminal Emulator you’re unlikely to be fully utilizing while typing on the
We don’t often consider the speed of terminal emulators command line, and that’s your GPU. Alacritty uses
like Konsole or xterm. The commands we run in them OpenGL directly to harness the power of your graphics
are nearly always the culprits when it comes to clogging hardware and is capable of rendering around 500 frames
up the system, but that doesn’t stop developers from try- per second with a high-resolution screen full of text, ac-
ing to create the fastest terminal in the land. This is what cording to the developer (Figure 5).
Alacritty is – a terminal emulator that promises “blazing The motivation behind developing Alacritty is to give
fast” speed. It’s even brave enough to claim to be the terminal-intensive applications, like Vim or tmux, a much
fastest terminal emulator available, and I certainly found needed performance boost, especially when running on
it faster than anything I had installed. But the clever part high-resolution, high-pixel density displays. It draws a
Figure 5 new frame whenever anything changes within the termi-
nal (and not when the terminal is sitting idle), and you no-
tice this whenever you deal with screenfuls of scrolling
text. What’s even more remarkable is that Alacritty is writ-
ten in Rust, a project under the auspices of Mozilla Re-
search. That a new and modern language can perform as
well as traditionally speedy but dangerous languages like
C or C++ is a sure sign of the future in terms of both code
security and validity. It also helps that the terminal looks
so good, thanks to the sub-pixel anti-aliasing, which is
presumably coming for free with OpenGL. If you use a
terminal all the time, as many of us are now doing, it’s
only when you use a super-fast terminal like Alacritty that
you realize you were using an old one.
By Graham Morrison
http://blog.jwilm.io/announcing-alacritty/

The tar command, in particular,


H AC K
Universal Unpacker
Linux users often have to deal with file archives in ZIP,
RAR, or compressed tarball (tar.gz, tar.bz2, tar.xz) for-
regularly challenges users with its
cryptic syntax. Especially annoying
is an archive full of files that are not
57
mat. The commands for unpacking these archives are stored in a separate subdirectory when unpacked but
as different as the formats: unzip, unrar e, and tar xf. instead clutter the current folder. Such shortcomings
Figure 6 are addressed by the dtrx unpacker – the abbreviation
stands for “do the right extraction.”
All the popular distributions have dtrx in their pack-
age sources; under Ubuntu, the installation includes a
number of tools for unpacking. On the other hand, with
Arch, you have to install the necessary tools yourself.
As a wrapper, dtrx is not able to unpack archives itself,
but it composes the required syntax for you. You only
have to remember dtrx file_name to unpack an archive.
Figure 6 shows dtrx in action: The badly built
bad-tarball.tar.xz tarball does not contain a base sub-
directory, so all the data will end up in the current
folder when unpacking with tar xf. Additionally, tar
adopts the file permissions without changes – in the
example, you are not allowed to edit the files without
first modifying the permissions using chmod or a file
manager. dtrx corrects all of this in one command.

By Christoph Langner
https://directory.fsf.org/wiki/Dtrx

101 LINUX HACKS – 2020 EDITION 61


SHELL HACKS

Listing Files with Style Figure 7


HACK 58
The ls command, which lists the content of a directory,
is one of the most common Bash commands. If you’re
looking for more information, try extending the simple
ls command with ls -alh. The -alh options turn the
simple list into a complete overview, including file per-
missions, ownership, file size, and more. The com-
mand also outputs hidden files and directories.
The ls command also supports colors, but usually
only color highlighting for directories and links. The al-
ternative exa, which is written in Rust, offers signifi-
cantly more convenience. Only a few distributions
offer exa in their package sources: openSUSE does,
and you will also find exa in the Arch User Repository
(AUR). If your distro doesn’t provide a package, check
out the project homepage, where the developer pro-
vides a zipped binary for 64-bit systems, which you Figure 8
can simply copy as exa to /usr/local/bin/ or ~/bin/.
The output of the exa command roughly corresponds to
the output of ls, except that exa adds more colors to the
overview (see Figure 7). For example, it highlights ar-
chives in red, images in light purple, videos in dark pur-
ple, backup copies in gray, and directories in light blue.
exa gets even more interesting when you enable further
options (see Table 1). For example, with exa -bghHlS, exa
displays the file permissions, including file sizes, affilia-
tions, and the last modification data, in a neatly sorted list.
Other colors help you keep track of things such as
file permissions. If you decide on a version of the com-
mand you like best, create an alias for it with

alias ll='exa -bghHlS'

and place the alias in the ~/.bashrc file. The above alias exa‘s ability to display entire folder structures recursively
would let you call the command by simply typing ll. in a tree view is also practical. To display a tree, supple-
ment the call with exa --tree or exa -T.
Table 1: Important exa Options You can limit the tree depth with the
Switch Long Form Comment --level=<n> option, where <n> is the
Display Options number of levels you want to display.
-1 --oneline Shows each file and folder in a new line Alternatively, switch off the tree view
-l --long Displays metadata, such as file permissions
and let exa list just the subfolders with
and size the -R or --recurse switch. Again, you
-R --recurse Lists the contents of subdirectories recursively can restrict the depth with the --level
(depth option: --level=<level_number>) option (Figure 8).
-T --tree Displays a tree structure (depth option: The shortcuts for sorting the output
--level=<level_number>) also make everyday life in the shell
--color <when> Color output is never, always, or automatic easier. For example, to find the largest
--color-scale Highlights the display of the file size in color files in a folder quickly, call exa like this:
for large files
Filtering and Sorting Options exa -lr --sort=size
-a --all Shows hidden files and folders
The -r switch reverses the order of
-d --list-dirs Treats directories like files
the output so that the largest files
-r --reverse Reverses the sort order
are at the beginning of the list.
-s <option> --sort=<option> Sorts by name, size, created, newest, or oldest,
By Christoph Langner
among other options
--group-directories-first Lists directories before files
https://the.exa.website

62 101 LINUX HACKS – 2020 EDITION


SHELL HACKS

ASCII Browsing with Browsh HACK


Despite the World Wide Web demanding more and with Firefox – a huge advan-
more of our systems, many of us just want a simple tage! The rendering is obvi-
browsing experience that neither detracts from the in- ously blocky and pixelated, but it’s also clear enough
formation we want nor diverts system resources. For to be navigable (Figure 9). Vitally, the text is still raw
this reason, a web browser running in a terminal win- text, which means that reading a page of content on
dow is very attractive. Ideally, it would focus on the text the terminal is often clearer than reading the same
and ignore both the images and wider site design, let- content on a design-heavy site.
ting you read and download only the parts that matter.
This would be brilliant if you’re also on a low-band- By Graham Morrison
width connection or connecting via SSH to a headless https://www.brow.sh
low-powered server such as a Raspberry
Pi. But the absolute minimalism of console Figure 9
browsers like Links, Lynx, ELinks, and
w3m is often too much for a modern site,
both in the way their limited rendering
makes a complex site difficult to navigate,
and in their compatibility with modern
web technologies like HTML5, CSS3, Ja-
vaScript, video, and even WebGL.
It’s these problems that Browsh at-
tempts to solve, albeit in an unconven-
tional way. The unconventional way is
that while it does run from the command
line, it still requires you to have Firefox
57 or later installed. This is because
Browsh uses Firefox to render the pages
you request before rendering them as
ASCII for use within your terminal. It
may sound like a cheat, but it works per-
fectly and means that Browsh is compat-
ible with every site that’s compatible
SHELL HACKS

Command References at cheat.sh


I love using the command line. It’s where I spend
most of my time, and I’ll go out of my way to find a
scriptions. You can even list the cheat
HACK
sheets that may be available for your favor-
60
command-line solution, even when there’s a poten- ite programming language: Typing
tially easier-to-use desktop application that does
the same thing. But my memory is rubbish, and un- curl cht.sh/cpp/:list
less I use something every day, I soon forget com-
mands and shortcuts for doing things in the utili- for instance, lists 30 documents on C++, from arrays
ties and languages I don’t use that often. What I to logical and bitwise operators. If you don’t like this
need is universal access to a cheat sheet system remote curl approach, you can easily download a
that enables me to quickly see the most commonly local command-line client, cht.sh. You can then run
used command shortcuts and arguments for the that client locally to access the same information,
tools I want to use. and you can even integrate the search and results
cheat.sh is that tool (Figure 10), developed to hit into editors like Vim and Emacs, which is perfect for
seven noble targets: people like me who can never even remember a sim-
ple for loop’s syntax or even have the Vim com-
1 It’s concise, only containing the details you want.
mands from within Vim itself!
It’s fast, delivering results on the command line
2 almost instantly. By Graham Morrison
It’s comprehensive, with access to plenty of tools https://github.com/chubin/cheat.sh
3 and information.
It’s universal, avail- Figure 10
4 able everywhere.
It’s unobtrusive when
5 you’re working.

6 It helps you learn.

7 It is inconspicuous.
cheat.sh is a GitHub repos-
itory that delivers on all of
these promises by allow-
ing you to grab pre-pre-
pared text documents that
help you to work with the
tools you commonly use.
Type

curl https://cheat.sh/ssh

for example, and you’ll


see quick examples of the
SSH command complete
with single-sentence de-

64 101 LINUX HACKS – 2020 EDITION


SHELL HACKS

Highlighting Instead of Grepping


HACK
61 Save it as /usr/local/bin/hl and make it executable
(chmod a+x /usr/local/bin/hl). For both actions, you will
The grep command can highlight the parts of output need root privileges.
lines that match your search expression. To make that You can only use the new tool as a filter, unlike grep,
happen, you only need to provide the --color option. In which also accepts filename arguments. So replacing
fact, some Linux distributions set up an alias
sometool | grep keyword
alias grep='grep --color=auto'
with
(and further ones to upgrade egrep and fgrep to colored
output, too). Seeing the search term emphasized in the sometool | hl keyword
output is so useful that it started to bother me that the sim-
ple cat tool cannot highlight expressions. So I searched for is fine, but if you want to output a file and highlight a
a tool that combines cat and grep --color: It should output keyword, either create a “useless use of cat” expres-
all lines but highlight a given keyword. Figure 11 shows a sion, such as
terminal running dmesg -w continuously – and highlighting
all occurrences of usb or eth?, ignoring case. cat filename | hl keyword

Figure 11
or redirect standard input via the < oper-
ator (hl keyword < filename).
Since hl simply forwards all options to
grep, you can use advanced features such
as searching for regular expressions or
asking the tool to ignore case. Highlight-
ing USB and network events took a simple

dmesg -w | hl -iwE "usb|eth."

where -i asks grep (and thus hl) to ignore


lower/uppercase differences, -w restricts
matches to full words (so that eth0 will
Performing this task boils down to inspecting every match, but ethernet will not), -E enables extended regular
single line, attempting to grep the search term in it expressions, and the "usb|eth." defines that we’re looking
(with coloring) – and, in case of failure, echoing it. Put for either usb or eth? (with exactly one character after eth).
the following code lines in a Bash script: If you want to build a (very) simple syntax highlighter
for a programming language, just provide hl with a list
#!/bin/bash of all reserved keywords. In Figure 12, you can see parts
while read -r; do of the output of:
line="$REPLY"
( echo "$line" | grep --color $@ ) || echo "$line" hl -wE "for|do|done|exit|case|esac|while|if|fi|elseU
done |elif|echo|export|alias|function|test|unset" < /etc/U
bash.bashrc
Figure 12
Actually it doesn’t take a script like hl at
all, if you can live with the restriction that
every search term must be a regular ex-
pression. In that case, grep can already
do the job on its own. Just add ^| before
your search patterns, for example:

dmesg -w | grep --color -iwE "^|usb|eth."

Every line is matched by ^, so grep discards


nothing, but coloring only happens to
lines with “proper” search results.

By Hans-Georg Eßer

101 LINUX HACKS – 2020 EDITION 65


SHELL HACKS
H AC K

Add File Type Icons to Your File Listing


62
The first time in our lives we got to a black-and-white optimized for dark terminals by specifying --dark. Fig-
Linux or Unix shell, most of us probably typed ls first. A ure 15 shows what a tree view (with --tree) looks like.
new implementation of this standard tool adds icons to
the output. colorls is written in Ruby. If you don’t have No Blind Faith in Color
this language on your system yet, install it quickly. For Speaking of the downside: colorls is a new implementa-
example, Ubuntu and Mint users would type tion of ls, which does not support all options identically
and others not at all. My big favorites -l and --sort=size
sudo apt install ruby ruby-dev ruby-colorize fortunately work. If you type -f, colorls only displays
files; -d only displays directories. If I want to see both, I
or, for openSUSE: have the choice between --sd (directories first – note the
two dashes!) and --sf (files first).
sudo zypper in -t pattern devel_C_C++ devel_basis If you would like a brightly colored ls, but have prob-
sudo zypper in ruby ruby-devel lems with colorls because of missing parameters,
schedule a test run with exa (see Hack 58).
Then you download a TrueType font that you like from
Nerd Fonts – say, Roboto Mono Nerd Font Regular. After By Charly Kühnast
unpacking the ZIP file, I moved the .ttf files to the /usr/ https://github.com/athityakumar/colorls
share/fonts/truetype/roboto/ directory on my Ubuntu https://nerdfonts.com/#downloads
desktop; users of other distributions may need to https://nerdfonts.com/#cheat-sheet
change this path.
Why do I even get this font when there are a few Figure 14
dozen others preinstalled? Because Nerd Font’s char-
acter sets are more extensive, containing more sym-
bols, special characters, glyphs, and emojis than
usual (Figure 13). Now I select the new font in my ter-
Figure 13

minal’s preferences. This fulfills the preconditions, Figure 15


and I can proceed to install colorls by typing:

sudo gem install colorls

Ubuntu and Mint users can at once use colorls. If you


run openSUSE, the tool’s file name is colorls.ruby2.5.
The developers know that nobody types colorls 50
times a day. I recommend that you create an lc alias in
your ~/.bashrc:

alias lc='colorls' # or colorls.ruby2.5

If you use a light terminal background, you should al-


ways specify --light or, preferably, make it permanent
by appending it to the .bashrc alias. The output then re-
sembles that in Figure 14 – note the cute icons and
bright colors. Light-shy workers can choose a variant

66 101 LINUX HACKS – 2020 EDITION


SHELL HACKS

Upterm, a Terminal with


HACK
63 Zsh is a very popular option, for example, because it
expands on Bash’s capabilities and can use a very help-
Built-In Shell ful plugin system, often used by developers to stream-
The next level up from debating which is the best Linux line their workflow. Upterm (Figure 16) is another and
desktop is debating which is the best shell. Bash is newer alternative, but one that’s on the cutting edge of
often the default. For most of us, there’s seldom a rea- features whilst possibly being of most use to command-
son to try anything else. It’s perfect for all kinds of line beginners. Running atop Electron from the Node.js
tasks, from editing text to manipulating files. But there package manager, Upterm is never going to replace the
are many other shells that can be more helpful when standard Linux default running on your freshly booted
you have more specific or technical requirements. server’s framebuffer, but it may help if you spend a lot of
time on the command line from the desktop.
Figure 16
Upterm describes itself as both a terminal emulator
and an interactive shell. The interactive part is the first
thing you notice. As you type cd to change directory, all
the possibilities magically appear like a Google autosug-
gestion. This is really useful and potentially more user-
friendly than having to press the Tab key to get the same
list, but it also lists command arguments, and, most im-
pressively, the single sentence includes text hints for
most arguments. This means that Upterm helps with
both navigation and how you use the tools you want to
type, which is ideal for beginners or for those of us who
forget the command-line nuances of git or Vim.

By Graham Morrison
https://github.com/railsware/upterm

The fish Shell HACK 64


After presenting Upterm in the previous hack, let’s
Figure 17

look at yet another alternative to Bash: The fish shell


uses your regular terminal program, so it’s also avail-
able in pure text mode and via remote connections.
fish offers some very helpful command-line com-
pletion features. For example, when you start typing
a command, you get suggestions for old commands
from your history – the shell shows how the letters
you typed so far could be completed to form your
old command, with the missing bits in light gray, so
you know what’s already there and what’s a sugges-
tion. Press the Up arrow key to go back to older his-
tory entries. mentation in a browser. Looking at the list of internal
Or press Tab to see what binaries in your $PATH start commands reveals interesting additions; for exam-
with the letters you entered so far. If there are many ple, the cdh (cd history) command shows all folders
possible completions, fish will only display a few of you’ve cd‘ed to in the past and lets you pick one with
them – to get the full list, press Tab again. If you think the cursor keys.
that Bash does that, too, that’s only half correct, be- There’s a lot more to discover, and if you like to write
cause fish will add information from the programs’ shell scripts, Fish has its own scripting language. Many
man pages (Figure 17) so that you can quickly check distributions have fish packages in their repositories –
whether you really want one of the commands from the for example, try sudo apt install fish for Ubuntu and
list. Autocompletion will also suggest options when Mint or sudo zypper in fish for openSUSE.
you press Tab after entering one or two minus signs.
Ask the fish shell for information about an internal By Hans-Georg Eßer
command via help command, and it will open the docu- https://fishshell.com/

101 LINUX HACKS – 2020 EDITION 67


SHELL HACKS

Using a Sub-Shell
HACK 65 or modify variables that might influence the shell’s be-
havior. For example, deleting the PATH variable (via unset
When you’re working in the shell and you type bash, PATH) renders the shell almost useless since it cannot
nothing happens – apparently. The shell prompt returns find binaries (unless you specify the full path).
at once, and you can continue to enter commands. But You may also start a sub-shell if you want to “hide”
when you try to run a command from the history, you commands: The shell writes the command history from
will notice that it is somehow outdated (and especially memory to disk when it terminates. The commands saved
the bash command that you’ve just typed will not appear by the sub-shell will be lost when the parent shell exits.
in the history). Another reason for a sub-shell is staying in a directory
That’s because you’re no longer working in the shell with a long path that you don’t want to memorize: Just
that was active when you typed the bash command: start a sub-shell, cd out of the current directory to do
You have started a sub-shell in a child process, and the some other work, and when you want to return, simply
original shell has been suspended until you leave the exit the sub-shell with Ctrl+D. Of course, there’s also the
new shell with exit. Running ps --forest will reveal the directory stack that was invented for exactly this pur-
parent-child pose – so if you know the pushd, popd, and dirs com-
Figure 18
relationship of mands, it is better to use those. If you don’t, a sub-shell
the two shells does the job, too.
(Figure 18). You can even temporarily suspend a sub-shell (using
But what is the suspend command) so that you can use the parent
that good for? shell; entering fg brings you back to the sub-shell.
Working in a When you use a desktop environment, you’re more
sub-shell is a likely to simply open more terminals instead of starting
good idea sub-shells. But if you often log into remote machines via
when you SSH, knowledge of sub-shells can come in handy.
want to test
shell options By Hans-Georg Eßer

Start the Right App


When I’m navigating through my folder hierarchy, I
HACK 66
Gnome users type gio open filename. On older
tend to find files that I cannot remember creating – and Gnome versions and on Linux Mint’s Cinnamon desk-
the file name does not help either. I want to look at the top, you can try gvfs-open. Xfce has its own tool, too:
contents (and possibly decide to delete the file or move It is called exo-open. There’s also a generic tool called
it elsewhere). xdg-open. You may have several *open tools installed;
If this sounds familiar, then how do you open such Figure 19 shows what happens when you run gio
files from the command line? You could launch the open and kde-open on the same text document. Nor-
right application from the desktop’s program menu, mally, it’s best to stick with the tool tailored to your
use a command like echo $PWD/filename to find the full favorite desktop.
path of the file, and paste that into the opening dialog.
Or, if you happen to know the file name for the program By Hans-Georg Eßer
binary (like soffice for LibreOffice), you Figure 19
can type soffice filename.
In most cases, your desktop knows
which applications could open your files:
Double-clicking them in the file manager
will normally start some program. You
can use that knowledge in the terminal,
too. For example, as a KDE user you can
run the kde-open filename command to
have KDE’s MIME type configuration sort
it out for you. Depending on your distri-
bution and KDE version, the tool may be
called kde-open5.

68 101 LINUX HACKS – 2020 EDITION


You run a website or you produce content for it:
The best hacks mean knowing the right tools. For
example, you don’t need to do a regular install of
a CMS for testing: Just run a Docker container and
get rid of it when you’re done.

HACK 67
Install the draw.io Diagram Editor Locally 71
HACK 68
Record Screencasts with Peek 72
HACK 69
Edit Your Videos with VidCutter 73
HACK 70
Creating Long Documents with Styles 73
HACK 71
Editing HTML with Live Preview in Brackets 74
HACK 72
Free Wildcard X.509 Certificates for
Your Domain 75
HACK 73
Test Alternative CMSs with Docker 76
HACK 74
How Fast Is Your Server? 76

Image © lightwise, 123RF.com

70 101 LINUX HACKS – 2020 EDITION


PUBLISH OR PERISH

Install the draw.io Diagram Editor Locally


HACK 67
Draw.io has been around for years as a website. If you generate diagrams of this quality so quickly, and
ever need to draw a diagram or flow chart, or even a none with the same powerful design and symbol library
circuit, it’s indispensable. Many users now rely on it for that can output in so many different formats.
their last-minute presentations, their documentation Diagrams aren’t necessarily the most exciting thing
projects, and project planning, because draw.io’s great to spend your time creating, which is precisely why
strength is that it offers many different symbols in draw.io is so good. It handles the tedious parts, so you
many different categories, making it capable of draw- can spend less time drawing and more time moving on
ing many different kinds of charts. You don’t have to to the implementation (or at least arguing over the im-
create your own icons, steal images from the Internet, plementation shown in your awesome diagrams).
or rely on Inkscape for your arrowheads. Instead, you Figure 1 shows what draw.io offers:
simply drag and drop symbols, join them together, and Symbol library: Drag and drop from a huge variety
then use draw.io’s wonderful styling, arrangement, and 1 of diagram icons.
node options to create a final file that you can save on- Connections: Components can be dragged, and
line or locally. The only downside is that the website 2 their connections remain intact.
version requires running it online. Edit: Scale, drag, rotate, and group elements on
But draw.io is an open source project, and there’s now a 3 your canvas.
desktop version you can run on your local computer, Style: Use a color palette for fill styles, plus control
which is perfect for last-minute cramming on that train 4 fonts and labels separately.
with abysmal connectivity. There’s an executable AppIm- Properties: If the mouse isn’t good enough, type in
age, as well as DEB, RPM, and even Chrome OS files. 5 exact values.
With the application installed, you can create diagrams Transparency: Make and mix your colors with opac-
without the online tether. There’s a huge number of sym- 6 ity values.
bols to choose from, with additional custom symbols em- Shape categories: Symbols cover a huge range of
bedded within many of the template New projects you 7 diagram types.
can open. The Google Cloud Platform (GCP) templates, Custom libraries: Include your own symbols, such
for example, include many GCP-centric symbols and lay- 8 as GCP or AWS shapes, to accompany your own
ers for use within your charts. Further examples include diagrams.
Venn diagrams, mind maps, Unified Modeling Language
(UML) diagrams, flow charts, and isometric network dia- By Graham Morrison
grams – with 3D symbols for Amazon Web Services https://github.com/jgraph/drawio
(AWS) infrastructure.
Although not as powerful Figure 1
as Inkscape for drawing, text,
or arrangement options, you
have more than enough tools
here to create the perfect dia-
gram, as well as group, layer,
and align, for instance. If you
need more control, you can
save as an SVG file and edit
within Inkscape. I tested this,
and it worked perfectly.
There’s even beta support for
VSDX output if you need to
import your file into Micro-
soft’s Visio. The UI still feels
web-based, and native desk-
top widgets would be prefer-
able, but this is an important
application with few desktop
rivals – I can’t think of any
Linux tools that are able to

101 LINUX HACKS – 2020 EDITION 71


PUBLISH OR PERISH

H AC K Record Screencasts with Peek

68 A screencast shows what happens


on the desktop. Peek lets you create
screencasts in the blink of an eye and
What is currently causing Peek difficulties is the
change to the new display server, Wayland. For secu-
rity reasons, Wayland isolates individual applications
export them to popular formats. Com- on the desktop from each other. Software is not al-
pared with other screencast tools, Peek has a very lowed to read the content of another program’s win-
small feature set, but the program is not trying to com- dow. Thus, screenshots of the entire desktop are no
pete with the more established applications. Originally, longer easily achievable. But that’s a problem for
it simply recorded the desktop as a GIF, thus producing many other tools, too.
videos that were easy to embed into web pages. How- Unless you launch Gnome under the classic X server
ever, Peek now also supports more traditional video via the display manager using Gnome on Xorg, Peek
formats such as WebM and MP4. therefore needs to revert to the XWayland compatibility
In terms of the interface, Peek is deliberately oriented layer, which happens automatically when you start
on the LICEcap screencast tool for Windows. The pro- Wayland; however, if needed, you can call the applica-
gram shows a scalable transparent window that is al- tion directly under XWayland with:
ways in the foreground on top of all your other applica-
GDK_BACKEND=x11 peek
tions. Everything inside the window frame is grabbed
as a video by the software when you click Record. After After fulfilling this condition, working with the program
pressing Stop, Peek immediately saves the results on is very easy: Launch, align the frame, and record the
the hard disk. screencast.
To set the output format, click on the Peek icon in the
upper left corner and choose Preferences. Adjust addi- By Christoph Langner
tional parameters, if necessary, such as the Delay in https://github.com/phw/peek
seconds before the recording starts and the Framerate, http://www.cockos.com/licecap
or influence the size of the recording with Resolution
downsampling.
For the GIF format, especially, you should keep in Listing 1: Add Ubuntu PPA
mind that it was not designed for recording: Capturing
the entire desktop in Full HD at 30fps will result in huge sudo apt-add-repository ppa:peek-developers/stable
files. Therefore, choose only the snippet that you actu- sudo apt update
ally want to view later. When scaling the window, the
sudo apt install peek
size display can help you align the
frame precisely (Figure 2). Addi- Figure 2
tionally, you can reduce the fram-
erate to about 10fps, and, if neces-
sary, use the Resolution down-
sampling option to scale the re-
cording by an integer factor.

Installation
For Ubuntu and Linux Mint,
there’s a Personal Package Ar-
chive (PPA) repository (Listing 1).
On Arch Linux, you can simply
install the application from the
Arch User Repository (AUR), and
if you use openSUSE, you can
find a package on software.open-
suse.org. For other distributions,
such as Fedora, Debian, or
Solus, the developer provides in-
structions for installation on the
project page.

72 101 LINUX HACKS – 2020 EDITION


PUBLISH OR PERISH

HACK 69 HACK 70
Edit Your Videos with VidCutter Creating Long Documents
If you create lots of videos, such as screencasts of with Styles
your desktop (see hack 68 featuring Peek), you often Some documents that I create in LibreOffice Writer
need to cut bits out of them. That’s an essential part are “throw-away” documents: I type a few lines, add
of both the recording and editing process. Editing is some quick formatting, print the document, and then
obviously the central process in all video editors, but save the file or just close the program and discard it
often, all you want is the video equivalent of selecting all. That’s the one case where it doesn’t make sense
a piece of text and pressing delete. to work with styles.
VidCutter aims to do just that: Make practical edits But most documents are different: You create them
quick and easy. The UI is refreshingly simple. Most one day, and weeks, months, or years later you edit
videos load without conversion, thanks to the libmpv them again or create a copy to which you add new
back end, and you can immediately start playing with text. The longer those documents become and the
the edits. The basic process mimics that of fully more they are structured (into chapters, sections,
fledged editors while restricting options to a mini- etc.), the more it becomes vital to format them with
mum. You go through a file creating clips by selecting proper styles. For example, automatic generation of
the start point and end point of each clip. The mouse a table of contents is only possible when you’ve as-
wheel or left/right cursor keys can be used to step a signed a headline style to every headline.
single frame forward or backward, which is excellent, Using styles also guarantees that you can keep a
while up and down keys will skip five seconds. The uniform look throughout the whole document:
Return key can be used to quickly create a start or Headlines on page two will look the same as those
end marker for a clip, and a thumbnail of each clip ap- on page 102 – same font, same size, same color (if
pears on the right after being defined. that’s something you do to your headlines). Also,
You can reorder the clips by dragging and dropping exporting the document into a format that can be
them within the panel, and when you’ve finished used for web publishing is much easier when you
making clips and dragging them into a sequence for stick to using styles.
your final video, clicking Save will output the final Figure 4 shows two headlines in a document – but
version to a new file (Figure 3). only the second one is a real headline. When you
modify the headline style settings, only the real one
By Graham Morrison will show the changes, while the faulty first headline
https://github.com/ozmartian/vidcutter will remain unchanged.

By Hans-Georg Eßer
Figure 3
Figure 4

101 LINUX HACKS – 2020 EDITION 73


PUBLISH OR PERISH

Editing HTML with Live Preview in Brackets


HACK 71
Brackets is an open source text editor targeted at web Brackets is available under an MIT license, and you
design. There’s obviously a Linux version, but the part can find Linux, Windows, and macOS versions in the
that makes Brackets unusual is that it’s developed by project’s GitHub repository.
Adobe, and it has been in development since 2014. This Figure 5 highlights some important features of
may lead you to think the project is some kind of failed Brackets:
trial by Adobe, but Brackets is far from being a failure. Live preview: Changes made to the raw text
In fact, it’s rather brilliant. The first thing you notice 1 source of a site update the preview.
when you launch the application is that it looks nothing Multiple files: Tab between open files or create
like a typical Adobe application. It actually looks good, 2 both vertical and horizontal splits.
and its user interface doesn’t impinge on usability, with Code highlighting: Text looks crisp and easy on
a large text pane holding the editor itself, complete 3 the eye, and HTML colors are previewed in small
with beautifully rendered text using whatever font you swatches.
prefer. Start typing into an HTML document, and the Distraction free: A simple mode can be enabled to
autocomplete helps you start and finish elements, fixes 4 remove all the window furniture so you can focus
indentation, and subtly highlights the tags from the on the code.
text. This helpful functionality extends to colors, where Extensions: Augment your editing environment with
you add the hex value for a specific hue and the editor 5 dozens of easily installed plugins and extensions.
will show you the color you’ve selected. Code completion: Use the drop-down menu to
But its smartest feature is the live preview. Select- 6 help with element memory.
ing this will open a simple web browser window con- Multiple languages: Designed for the web, but you
taining the rendered output of the HTML and CSS 7 can use Brackets with many different languages.
files you are editing. The clever part is that as you edit Refactor and linting: Change names easily, and
the source text files, the live preview updates in- 8 check and even fix common errors automatically.
stantly to reflect those changes. It feels like the devel-
oper modes you find in popular web browsers, where By Graham Morrison
you can temporarily change how a page is rendered, http://brackets.io/
but the difference here is that your changes are saved https://github.com/adobe/brackets/releases
to the files used to build your eventual site. Thanks to
its age and provenance,
there are also dozens of add- Figure 5
ons that can be installed, al-
lowing you to add themes,
watch videos, make notes,
and even turn the editor into
a fully fledged IDE. While the
emphasis is obviously on
CSS and HTML, Brackets
also supports a huge variety
of formats and programming
languages, from Bash to
YAML, where you can take
advantage of its excellent
font rendering, refactoring,
and split panes. It’s a clean
and effective editor. Al-
though it’s never going to re-
place something like Dream-
weaver when it comes to de-
signing a website without
touching the source, it’s per-
fectly suited to the modern
role of web developer.

74 101 LINUX HACKS – 2020 EDITION


PUBLISH OR PERISH

H AC K Free Wildcard X.509 Certificates for Your Domain

72 The pleasure of owning a nice domain


like sensorenresidenz.de is clouded by
the requirement of an X.509 certificate
Please deploy a DNS TXT record under the name U
_acme-challenge.sensorenresidenz.de with the U
following value:
for every subdomain that the admin wants ST8ehm-bKS6wRAxZk5vYDzUO9-OEr3NeIpClFyaq-kA
or has. The Let’s Encrypt project can help: Their certifi- Before continuing, verify the record is deployed.
cates are free and popular – supposedly half of all digi-
tal certificates used on the web originate there. After I have created the entry and checked that it is
However, anyone running web servers with many available, I press Enter. Let’s Encrypt retrieves my re-
subdomains has had the displeasure of having to man- quest and verifies it. Then it creates a certificate (full-
age a bag full of certificates. A single wildcard certifi- chain.pem ) and key (privkey.pem) and stores both under
cate from one of the usual certification bodies solves /etc/letsencrypt/live/Name – applause (Listing 1).
the problem in principle, but it is expensive. The situa- Integrating the certificate with the web server follows
tion changed in March 2018: All those who have access the usual steps. The browser is happy with the certifi-
to their domain name’s DNS server can now also obtain cate that my servers now deliver (Figure 6).
wildcard certificates from Let’s Encrypt. Wildcard certificates from Let’s Encrypt expire after
If you have not yet installed the Let’s Encrypt client, 90 days like all other certificates from that issuer. To
you can load it as follows: renew them, you simply call letsencrypt-auto renew.

git clone https://github.com/letsencrypt/letsencrypt By Charly Kühnast


https://letsencrypt.org
After that, web admins usually have to add some Py-
thon packages. That’s easily taken care of with Figure 6

letsencrypt/letsencrypt-auto --help

which will ask you for your sudo password when


launched. Let’s Encrypt uses a DNS-based challenge-re-
sponse method for authentication.

Let’s Dance
The following command lets me request a certificate
for the namespace *.sensorenresidenz.de (just don’t ask
why I registered the domain):

sudo /usr/local/letsencrypt/letsencrypt-auto certonly U


--manual --preferred-challenges dns --server https://U
acme-v02.api.letsencrypt.org/directory --email charly@U
kuehnast.com --domains *.sensorenresidenz.de

After a second of recollection, the Let’s Encrypt client


answers. It wants me to create a TXT record named
_acme-challenge.sensorenresidenz.de in the DNS with a
given random string as content:

Listing 1: I did it!

Congratulations! Your certificate and chain have been saved at:


/etc/letsencrypt/live/sensorenresidenz.de/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/sensorenresidenz.de/privkey.pem
Your cert will expire on 2018-08-07. To obtain a new or tweaked version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your certificates, run "letsencrypt-auto renew".

101 LINUX HACKS – 2020 EDITION 75


PUBLISH OR PERISH

HACK 73 Test Alternative


CMSs with Docker
Docker is the tool for what experts call container vir-
docker-compose -f stack.yml up

you get two images (one with Apache and WordPress,


the other with a MySQL database) and a working sys-
tualization. While the technical details are interest- tem – just login via http://localhost:8080, choose a user-
ing, what’s more important is that you can download name/password combination, and you’re ready to use
and run working instances of complex software pack- the fresh WordPress (Figure 7).
ages in seconds.
This makes Docker interesting for all sorts of appli- By Hans-Georg Eßer
cations, and it is especially helpful for website owners https://www.docker.com/
who think about switching to a different content man- https://hub.docker.com/_/wordpress
agement system (CMS). If that’s you, you’re likely to
read about the alternatives, but nothing helps you Figure 7
more with the decision than actually running the sys-
tems. Now setting up a CMS plus all the database ta-
bles, new virtual host, and more takes time. Once you
decide to remove it again, it is likely that bits and
pieces will remain and waste space on your server.
Instead, use Docker to install a test system on your
computer at home: Just visit the Docker Hub and
search for “content management”; the first hits give
you official Docker images of WordPress, Drupal,
Joomla, Plone, and Nuxeo. For example, when you
write 20 lines into a docker-compose file stack.yml as de-
scribed on Docker’s WordPress page and run

How Fast Is Your Server?


httpstat is a special stopwatch you can use to discover
how long web servers take to serve a page: Visible per-
for web pages secured
with TLS.
HACK 74
formance lags indicate optimization potential. The Py- Figure 8 shows httpstat measuring an
thon script wraps itself around curl. Apart from Python unencrypted call. Four milliseconds for a DNS reply
2 or 3 and curl, it has no other dependencies. You can is a really good value, but I cheated: The name is in my
retrieve it from the GitHub repository and call it using local cache. As soon as my computer has to turn to my
python httpstat.py <URL>. If the Python installer pip is provider’s DNS, the value rises to 80-200ms. The TCP
present on your system, you can use that to install handshake is 22ms, which is about par for the course.
httpstat and call it directly by its name. While you can The time the server needs to create the page (Server
leave http:// out of the URL, you must specify https:// Processing) shows whether it has some tuning poten-
tial that I have not tapped. My example is not represen-
Figure 8 tative, because instead of HTML the server simply out-
puts 301 Moved Permanently, which means I should
have called the page using HTTPS.
When requesting the same page using HTTPS, the
lookup and TCP values remain the same, but the
TLS Handshake takes forever for this static page.
The value can go up to several seconds for a big site
with a large volume of dynamic content and adver-
tising banners.

By Charly Kühnast
https://github.com/reorx/httpstat

76 101 LINUX HACKS – 2020 EDITION


Here, we get technical. Reboot a non-responding system,
loop-mount zip and tar.gz archives, make files append-only
or fully immutable, and copy program binaries from one
distribution to another – but don’t forget to bring their
libraries, too.

HACK 75
Safely Powering Off Your Machine 78
HACK 76
Make Files Immutable or Append-Only 79
HACK 77
Mount tar.gz and zip Archives 79
HACK 78
Recover Deleted Files with PhotoRec 80
and TestDisk
HACK 79
Change Root into Second Distro 81
HACK 80
Bind Mount a Directory with New Permissions 81
HACK 81
Run Binaries from a Different Linux Installation 82
HACK 82
Don’t Be So Case-Sensitive 83

Image © lightwise, 123RF.com

101 LINUX HACKS – 2020 EDITION 77


DEEP HACKS

HACK
75 Safely Powering Off Your Machine
Turning off your Linux com-
puter by cutting off the power is a bad idea: The system
magic-sysrq.conf file explain what each number means;
for this hack, we only show three lines:
wants to shut down properly so that it terminates all • 16 – enable sync command
running processes and unmounts the filesystems. If • 32 – enable remount read-only
you simply pull the plug, you risk losing data or cor- • 128 – allow reboot/poweroff
rupting the filesystem. Thanks to journaling, the repair To check whether these values are set you can either
procedure is quick, but you still should avoid a hard manually perform bitwise AND operations or use the shell.
shut down. For example, the following commands check whether 32
However, sometimes a regular shutdown procedure is part of the sum – the result is either 0 or 32:
takes too long. If you need to leave a room very sud-
denly and take your machine with you, waiting that one $ a=$(cat /proc/sys/kernel/sysrq)
minute until shutdown -h now completes may not be an $ echo $((a&32))
option. Maybe you have already heard of the magic 32
SysRq key – it can help you.
If enabled, Linux responds to key combinations Ubuntu sets the value to 176 which is 16+32+128 and
that begin with pressing Alt+SysRq. (The SysRq key precisely what you need to safely reboot the system. If
might be called PrintScreen on your keyboard or you need to change the value, either echo it into /proc/
have both names. On a keyboard with a standard lay- sys/kernel/sysrq or use the sysctl tool (Figure 2). In both
out, you will find it next to F12, see Figure 1.) Keep cases you will need root privileges.
Alt (or both keys) pressed, and then press a com- There are more magic SysRq features besides pre-
mand key to make the kernel perform some action. paring and performing the reboot, but most of them are
How should you use it? Just remember the following rarely needed. Check the official kernel documentation
sequence: (see link below) for a full description.
• Sync (S) – write buffered data to open files
By Hans-Georg Eßer
• Unmount (U) – remount filesystems in read-only
mode https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html
• Boot (B) – force reboot
Thus, press Alt+SysRq+S, Alt+SysRq+U, and
Alt+SysRq+B for fast and safe rebooting. Figure 2

Will It Work?
Check the contents of /proc/sys/kernel/sysrq – if it’s 0,
then the magic key combinations are turned off com-
pletely, and pressing Alt+SysRq will have no effect at
all. If the answer is 1, all hotkeys are enabled.
It is more likely that specific functions are enabled
and others are not: In that case, the result will be a sum
of some powers of 2 (namely, 2, 4, 8, 16, 32, 64, 128,
and 256). Comments in Ubuntu’s /etc/sysctl.d/10-

Figure 1

78 101 LINUX HACKS – 2020 EDITION


DEEP HACKS

Make Files Immutable


HACK 76 H AC K
or Append-Only
If you have some files that you want to stay on the
disk, no matter what erroneous commands you
Mount tar.gz and
zip Archives
77
might issue, make them immutable: All it takes is the I often find that a file I search for has been tucked
chattr tool, which changes file attributes. away in some .zip, .tar.gz, or similar archive file. Con-
The normal way to protect a file is to make it read- sequently, a typical sequence before accessing the
only (i.e., remove its “writeable” bits for all users). In needed file would be the following:
the terminal, chmod a-w file does this job. But some • tar tzf file.tar.gz (to check whether unpacking
programs override the read-only flag. For example, will dump everything in the current folder or – as is
the Vim editor will perform the following steps when proper – in a sub-directory)
you use the w! command with a read-only file: • mkdir tmp; cd tmp (to avoid the dump action)
1 Rename file to file~. • tar xzf ../file.tar.gz (to actually unpack the archive)
Create an empty new file and write the memory • Access the file
2
contents to it. • cd out of the temporary folder and remove it
3 Set access rights to 0444 (read-only). However, this is not the best way. A tool called ar-
4 Close it. chivemount improves the situation by allowing you to
5 Delete the renamed read-only file. loop-mount archive files (like you would loop-mount
Renaming and deleting do not require changing the an ISO image). Mounting does not require root privi-
access rights, which are still set to read-only! To leges. As archivemount is not a standard tool, you’ll
make a long story short, sometimes Linux (tools) have to install it. For openSUSE or Ubuntu/Mint, the
will delete files even though you think you’ve pro- zypper in archivemount or apt install archivemount
tected them. commands will do that.
Now mounting an archive is as simple as
Please Don’t Delete Me
There is a solution, namely, the “immutable” attri- archivemount file.tar.gz mnt/
bute. Set it via:
where mnt/ is some path that must exist. Figure 4
sudo chattr +i file
shows how this simplifies access; for the screenshot I
The command requires root privileges even when chose a .zip archive.
you make this change to your own file. From now on, For unmounting, use fusermount -u mnt/. The man
whatever you try to do to the file will fail; you cannot page suggests using the regular umount command in-
even move it to a different folder (Figure 3). You can stead. While that is not wrong, it is not helpful, either, as
later get rid of the attribute via sudo chattr -i file. umount requires root privileges and fusermount does not.
If the file you want to protect is a logfile, there’s an While the tool is helpful, here’s a warning: In my
even better change you can make. Set the “append- test with version 0.8.7 (which is what you get on
only” attribute via: openSUSE), changes in the mount folder of a .zip file
led to creation of a new version of the .zip file – but
sudo chattr +a file
in .tar format!
Then programs with otherwise sufficient write access
can still append new log entries to the file, but it can- By Hans-Georg Eßer
not be modified in the middle, shortened, or deleted. https://www.cybernoia.de/software/archivemount.html
To find out whether a file has some of the attributes
that you can set with chattr, use the lsattr command. Figure 4

By Hans-Georg Eßer

Figure 3

101 LINUX HACKS – 2020 EDITION 79


DEEP HACKS

Recover Deleted Files with PhotoRec and TestDisk


Undeleting files – that is, undoing a remove (rm) or un-
link command – is a very filesystem-specific task. Its
metastructure quickly, so it is
HACK
quite difficult to find old entries once
78
chances of success depend on the structure and fea- the filesystem tree is automatically opti-
tures of the filesystem. We’ll look at one of the most mized. Only very recent changes, which are kept in
simple filesystems first – FAT32 – which stores filesys- the journal, can be replayed or reversed with special,
tem information in a simple table. The earliest FAT file- filesystem-specific software. Unfortunately, any ref-
systems were only able to handle file names with eight erences to file names and file metadata – like time
uppercase letters, and an additional three-letter exten- stamps – disappear very quickly in modern filesys-
sion. This scheme is still used in the modern FAT, but it tems after the file has been deleted, so you might
also supports long filenames with fewer limitations. still be able to recover the file data, but you won’t get
An example disk contains just three files, with file- back its name.
names lecture{1,2,3}.pdf. After deleting lecture2.pdf If you care more about the data of a single file than
(with rm -f lecture2.pdf) and releasing the filesystem about retrieving the complete filesystem and directory
with umount, thus writing back all changes, the raw view structure, you can try PhotoRec instead of TestDisk to
of the directory entries looks like Figure 5. get your data back. PhotoRec scans raw data and finds
The most obvious change is the replacement of the file contents based on header signatures (Figure 6). In
filename’s first letter, L, by character hex 0xE5. This is some cases, the file content also reveals the original
how FAT first “hides” deleted files, before they are file name, even if the file no longer appears in the file-
eventually overwritten by newly created files. In this system organizational structure, so you can get back
stage, recovering the file is easily done by replacing the file with its (almost) original name. However, in
the 0xE5 character with a letter (for example, back to most cases, such as pictures or videos, the file name is
the original L). After doing this, the deleted file is no longer associated with the data after file removal, so
back when the filesystem is mounted again. Recov- you have to search or guess from the recovered files’
ery programs for DOS or Windows do exactly that. A sizes and block positions on disk, which are used by
good recovery tool for Linux is TestDisk: It knows PhotoRec to assign new names to files recovered and
about the specifics of file deletion and recovery for saved to a new partition or medium.
many filesystems. PhotoRec scans files regardless of which filesystem is
Although recovering files in a FAT filesystem is used on the source partition, but it honors filesystem-
comparably easy, the file’s data and metadata will specific data links and file fragments if the filesystem is
only stay intact as long as no new file claims the known or specified in the initial configuration options.
same directory entry or overwrites the file’s data
blocks. If this happens, the file and its contents are By Klaus Knopper
really gone for good.
https://www.cgsecurity.org/wiki/TestDisk
Native Linux filesystems, such as ext4 or Btrfs, use
https://www.cgsecurity.org/wiki/PhotoRec
more complex data structures than FAT; they support
more file attributes and mechanisms like journaling
(which allows for quick repair of the filesystem in the
case of unfinished file operations or a crash before Figure 6
the filesystem is unmounted properly) or “copy on
write.” The advantages of modern journaling filesys-
tems are a trade-off against the “undoing” of valid
transactions. A deleted file is unlinked from the data

Figure 5

80 101 LINUX HACKS – 2020 EDITION


DEEP HACKS

Change Root into Second Distro HACK 79


On my test machines, I tend to have several Linux dis- lsb_release first responding with Linux Mint 19.1 and
tributions installed on the same disk, with a GRUB then, after chroot, with openSUSE Leap 42.3. In the win-
menu that lets me decide which one I want to boot. dow below, you can also see the root directory property
However, I regularly find that the “wrong” distribution of the “chrooted” shell and a normal shell.
is running – not the one that I need just then.
Instead of rebooting, it is often enough to chroot By Hans-Georg Eßer
(change root) into the other distribution, which works
as follows: Figure 7
• Mount the other distribution’s root partition (e.g.,
under /mnt/dist)

cd /mnt/dist
mount -o bind /proc proc
mount -o bind /sys sys
mount -o bind /dev dev
mount -t devpts pts dev/pts

And if you need networking, also copy /etc/resolv.


conf to /mnt/dist/etc/
• Finally run chroot /mnt/dist bash -l to start a login
shell in the other distribution
The chroot tool starts the Bash shell with its root direc-
tory property set to /mnt/dist (whereas normal pro-
cesses have / as their root directory). Figure 7 shows

Bind Mount a Directory with New Permissions H AC K


You may know the mount option --bind (or -o bind) that
lets you mount some directory to a second place in the
filesystem hierarchy. We’ve also used it in the previous
worked on Ubuntu, Linux Mint,
and openSUSE.
As a first example, the following
80
hack (which used chroot to jump into a different distri- will give you an alternative folder to access your docu-
bution). While classical bind mounts are often useful, ments – but all set to read-only:
the feature is not very flexible and also requires root
cd ~
privileges. bindfs which is a FUSE-based filesystem
mkdir ro_Documents
driver, lets you perform bind mounts in much more
bindfs -r -p a-w --no-allow-other Documents ro_Documents
flexible ways.
You can grab and unpack the source code and then The second example is even more useful: When you
run make to build the binary, or – if you’re lucky – install mount a /home partition from an old Linux installation,
a package from the repositories. In my tests, this user IDs likely don’t match your current settings, so you
Figure 8 cannot access the files without becoming root. bindfs
lets you switch UIDs:

sudo bindfs --map=500/1000 /mnt/olddisk /mnt/bindfs

This will create a new view in /mnt/olddisk where UID


500 is globally replaced with UID 1000 (Figure 8). This
assumes that 500 was your UID on the old system and
1000 is your current UID.

By Hans-Georg Eßer
https://bindfs.org/
https://github.com/libfuse/libfuse

101 LINUX HACKS – 2020 EDITION 81


DEEP HACKS

H AC K
Run Binaries from a
Different Linux Installation
81 able. Try to launch the program copy in the cur-
rent directory and check the error message. In the
example, xv was missing the file libpng.so.16. But it’s
Software installation has become so simple with the ad- right here: You copied it from the source system. So
vent of repositories and management tools like APT, Zyp- add that (with its absolute path) to LD_PRELOAD by typing
per, Yum/DNF, and so on: Just pick a package you want
LD_PRELOAD="$PWD/libpng.so.16" ./xv
and let the package manager figure out what other pack-
ages (dependencies) it needs and install all of it in one go. This might lead to another (new) error message com-
Sadly, not every application appears in every Linux plaining about some other file. For the xv example, the
distribution’s repositories. For example, the image next command was
viewer xv is one of my favorite tools, because it is incred-
LD_PRELOAD="$PWD/libpng.so.16 $PWD/libz.so.1" ./xv
ibly fast. But it’s also very old and has not seen any up-
dates since 1994. Its source code is available, but it has a and that finally started the tool (Figure 10). Note how the
habit of not compiling. Some distributors (for example file names have to be separated by a blank character, and
openSUSE) have managed to get it running and provide the whole argument is put inside double quotation marks.
packages in their repositories. Other, like Ubuntu, do not. For other programs, constructing the command line
So how about just copying the binary file from a open- might take longer, and there are going to be problems
SUSE system to an Ubuntu installation? if you want to use this method to make a 32-bit binary
Well, that is likely to cause problems; in most cases, run on a 64-bit system. At least, those can normally be
attempting to run the binary will produce an error mes- solved by installing the 32-bit compatibility layer. The
sage that complains about missing libraries. In that other way (moving a 64-bit binary to a 32-bit machine)
case, don’t start searching for them on the target sys- is technically impossible.
tem. They are either missing, or they have been in-
stalled with the wrong version numbers. Or Use LD_LIBRARY_PATH?
Instead, go back to the source system, and search for If you’re aware of the LD_LIBRARY_PATH variable in which
the library files there. You can use ldd to get a list by you can add directories with libraries that the program
typing ldd /usr/bin/xv (for the xv example; Figure 9). loader will search before the default folders, you might be
Now quickly build a ZIP archive by supplying the whole tempted to simply set it to the local directory that has all
output of ldd as argument list for zip: the needed libraries in them: I tried that, too. Turns out, it
did not work with the example files, because the GNU C
zip xv.zip $(ldd /usr/bin/xv)
Library was incompatible with the running kernel. But
This will generate many error messages, because there’s you can start with that approach, too: Run the command
more than file names in the output, but it does not matter:
LD_LIBRARY_PATH=$PWD ./xv
zip will happily put everything it recognizes as a file into
the new ZIP file. Copy that one over to the target machine. and then, step by step, delete all libraries that the
On the target machine, create a new folder for your loader complains about until those error messages
program, move the program binary itself into that stop and your program starts. Read the ld.so man page
folder, and unzip the file with if you want to find out more about how loading a pro-
gram works on Linux.
unzip -j xv.zip

The -j option makes unzip lose the path information, so By Hans-Georg Eßer
it simply drops all files in the current directory. http://www.trilon.com/xv/
Now you’re almost there: Start an incremental pro-
cess in which you add library files to the LD_PRELOAD vari- Figure 10
Figure 9

82 101 LINUX HACKS – 2020 EDITION


DEEP HACKS

HACK 82 Don’t Be So
Case-Sensitive
We all know the eternal Unix rule: File names are case-
Now the ciopfs driver follows these rules:
• Files that already exist in folder1 and whose file
names are all lowercase appear in folder2 with the
same spelling – other files are left out completely.
sensitive. That means, Makefile is not makefile. While it • Files that you create in folder2 – with whatever mix of
would be bad practice to have both in the same folder, upper/lowercase letters – will show up in folder1 in all
you could do it, and it has been done. lowercase (Figure 11).
When you leave the Linux world, the situation be-
comes complicated: Windows stores file names the Adjusting a Folder
way you typed them at file creation, and that’s the way So how can you convert a folder’s file names to all
you will see them in the file manager or in the shell – lowercase? If you don’t, files with upper-case letters
but the system does not care what spelling you use for will not show up under the ciopfs mount point. Sev-
accessing them. This means that you can not have eral solutions have been discussed on Stack Overflow
Makefile and makefile in the same directory on an NTFS – the overall idea is to let find run a breadth-first
or FAT partition. Then again, Windows is not a Unix-like search over the directory tree with
system. But turn to macOS, which is partly based on
find . -depth -exec mksmall {} \;
BSD, yet also ignores upper/lowercase differences in
file names – the old Unix rule is not valid for macOS. where mksmall is a shell script that will update the file
This is relevant to Linux users who use Wine to run name. Listing 1 shows an example implementation; it
Windows applications on their Linux machine. Some uses a collection of classic command-line tools:
Windows apps have problems with the Linux way of • dirname extracts the directory part from a file’s path
expecting the precise spelling of a file name and may • basename extracts the file name
break. If you’ve encountered this problem, then there • tr exchanges one set of letters with another one, in
is a solution: ciopfs (which stands for case insensitive this case uppercase (A-Z) with lowercase letters (a-z)
on purpose file system), a FUSE filesystem driver. It al- • test compares two strings
lows you to perform an action similar to a bind-mount Ciopfs, like archivemount and bindfs, is one of many
(see hack 80), but with an important difference: when FUSE-based filesystems. The FUSE project page lists
you try to access files under the new mount point, several more – maybe you’ll find some other interest-
case doesn’t matter. ing drivers (e.g., ClamFS, which integrates antivirus
Of course, you’re not restricted to using ciopfs with a scanning).
Wine installation. If you’re also a macOS user and often By Hans-Georg Eßer
switch between the two Unix-like environments, you
may want to unify the experience and make Linux be- http://www.brain-dump.org/projects/ciopfs/
have like macOS. https://wiki.winehq.org/Case_Insensitive_Filenames
https://stackoverflow.com/questions/152514/
Installation
https://github.com/libfuse/libfuse/wiki/Filesystems
If you’re running Ubuntu or Linux Mint, you can just
type sudo apt install ciopfs. I could not find any RPM
packages, and I tested source code compilation on an Listing 1: mksmall
openSUSE Leap 15 machine. If you have an openSUSE
system, get the source archive from the project web- #!/bin/bash
site, unzip it, and run the following commands (which FILE="$@"
DIR=$(dirname "$FILE")
assume that you’re still in the directory that holds the BASE=$(basename "$FILE")
ZIP archive): NEWBASE=$(echo "$BASE" | tr A-Z a-z)
if test "$BASE" != "$NEWBASE"; then
sudo zypper in gcc fuse-devel glib2-devel libattr-devel mv "$DIR/$BASE" "$DIR/$NEWBASE"
cd ciopfs-0.4; make && sudo U fi
make install Figure 11
Type ciopfs --version to check
whether you’ve been successful.
Using the new tool is ex-
tremely simple: Just type ciopfs
folder1 folder2 where folder1 is
the original directory that holds
files, and folder2 is an empty
directory that’s used as the
mount point.

101 LINUX HACKS – 2020 EDITION 83


What’s your favorite language? We can’t help you there, but
we can suggest some editors to try. If your code eventually
compiles to a binary, you might want to attack it with a hex
editor, a disassembler, or perhaps a line count tool.

HACK 83 HACK 89
Run C Code from the Command Line 85 Reverse Engineering with Cutter 88
HACK 84 HACK 90
Edit Binary Files with hx 85 Textadept Works in Graphics 89
and Text Mode
HACK 85
Edit Your Text Files with Textosaurus 86 HACK 91
Perform Dynamic Code Analysis with 89
HACK 86
SystemTap
Manage Your git Repository with GRV 86
HACK 92
HACK 87
Count Lines of Code in Your Project 90
Make git Interactive 87
HACK 93
HACK 88
Fix Disturbing Indentation and More 90
Replace hexdump with a Colorful Tool 87

Image © lightwise, 123RF.com

84 101 LINUX HACKS – 2020 EDITION


WORKING WITH CODE
H AC K Save this file as ~/bin/runc and
Run C Code from the Command Line
Sometimes you want to use functionality that is famil-
iar from C programming, but you cannot find a corre-
83 make it executable. (Use a dif-
ferent filename if you use the
opencontainers.org runc tool for
sponding tool that you could call from a shell script. For starting containers.) Also check that the C compiler
example, the getsid() and getpgid() functions (which gcc is installed.
return the running task’s session and process group You can now run C commands via runc. For example,
IDs) have no shell programming equivalent. in order to identify the session ID of process 1292, type:
Of course, you could write a small C program that
runc 'printf("%d\n",getsid(1292))'
calls the function of interest for you, but then you’d
have to include parameter evaluation. A better way is Figure 1 shows this (and a getpgid() call) in action for the
to use a simple wrapper script that takes a string of C mc process with PID 26815. Of course, I could have asked
code, puts it into a C source code file, and then com- ps to show the information via ps -h -o sess -p 26815 (-h
piles and runs it. The following five lines do just that: equals no header, -o sess equals only the session ID col-
umn, and -p equals only the process with this PID) and
#!/bin/bash
via ps -h -o pgid -p 26815, but I knew neither the -h op-
msg="$@"
tion nor the session ID column code by heart.
fname=/tmp/_run_
printf "int main () { %s ; };" "$msg" > ${fname}.c
More Examples
gcc -o ${fname} ${fname}.c 2> /dev/null && ${fname}
You can use more complex code blocks including
rm -f ${fname} ${fname}.c
loops, so for example
Figure 1 runc 'for(int i=0;i<1000;i++)printf("%d ",i);printf("\n")'

will print a sequence of numbers (just like seq -s " " 0 999
does). It is also possible to use code that stretches across
several lines: When you start the C string with the ' charac-
ter (like in the examples), you can press Return as often as
you like – until you end the string with another ' symbol.
As a fun example, in order to generate a segmenta-
tion fault, just try an assembler instruction that would
disable interrupts: runc 'asm("cli")' cannot be run suc-
cessfully, because cli is a privileged instruction that
can only run in kernel mode.
By Hans-Georg Eßer

HACK 84 Edit Binary Files with hx


Even if you’re not a developer or a
“hacker,” sooner or later you’ll need to use a hex editor.
gladly ignore the context of a file and happily display its
contents. Because the context has been lost, that display
usually defaults to hexadecimal values, or base 16, repre-
This is primarily because a hex editor is a bridge between senting the raw binary contents of a file. Thanks to your
the worlds of code, binary, and content, allowing you to computers’ binary logic, this single hex value is a “nib-
open and view a file regardless of its format or whether ble” of data, usually grouped into pairs to form a “byte.”
the file is corrupt or complete or not. The file could be an Bytes are also turned into ASCII text, so you can read raw
executable binary, or it could be a LibreOffice document data if necessary, and those bytes in turn are grouped into
corrupted while saving. Either way, a hex editor will columns containing 64 bits per column. This makes find-
Figure 2 ing a specific location or offset much easier, whether
that’s in your computer’s raw memory or within a file.
You can download, unpack, and compile the source
with make – that should work on any system with basic de-
velopment packages installed. The editor is Vim-like, so
you can switch between insert and command modes.
Navigation and search keys are also the same as in Vim,
and you should be able to start editing without referring
to the excellent man page if you’re already familiar with
Vim. This means you can search, update, edit, insert, and
replace right from the command line, working with binary
files just as you can with text. Figure 2 shows the success-
ful detection of a hidden password in a binary file.
By Graham Morrison
https://github.com/krpors/hx

101 LINUX HACKS – 2020 EDITION 85


WORKING WITH CODE

Edit Your Text Files with Textosaurus


Yes, there are many, many text editors for Linux. But there
HACK 85
cursor editing. The editor is available as an AppImage, so
isn’t a Linux equivalent to the venerable Notepad++ on you can take it for a test ride without installing a pack-
Windows. Textosaurus attempts to fill the void. Thanks to age. Just download the textosaurus-*-linux64.AppImage
being built on both Qt and the Scintilla text editing frame- file, make it executable, and run it.
work, it’s completely cross-platform, so it can replace By Graham Morrison
Notepad++ on Windows as well as it does on Linux. It https://notepad-plus-plus.org/
even features a very similar layout and design.
https://github.com/martinrotter/textosaurus
To help with its cross-platform credentials, it uses
UTF-8 internally, so your text should remain legible Figure 3
whatever platform or locale you’re using, and many
input encodings are supported. It also features menu
options to convert end-of-line characters into some-
thing that works, which is often still a problem when
working with text files generated in Windows. The syn-
tax highlighting looks fantastic and will even print,
while the UI remains very easy to use. (On our test ma-
chine, syntax highlighting did not work until we
changed the editor color theme; Figure 3 shows the So-
larized Dark theme.) You can move parts of the UI
around, as you can with many KDE apps, but Qt and the
bundled Scintilla are the only dependencies.
There are lots of small utility functions too, such as a
menu full of MIME tools, JSON beautifying, and Mark-
down preview. You’ll also find advanced features, such as
being able to record and play back macros and multiple

H AC K
Manage Your git
Repository with GRV 86 the repository are captured by monitoring the filesys-
tem and updating the UI automatically. This means it
works well in a separate tmux panel, for example, cap-
So many of us now use git on the command line that it’s turing changes as you make them, or as they’re pulled
difficult to contemplate using a GUI tool to make access- from upstream. git is complicated; while GRV is never
ing git repositories easier. The context shift from com- going to make the learning curve any easier, it’s a fan-
mand line to desktop is often enough to break your train tastic tool for confirming what you think is happening is
of thought or concentration, but here is a helpful tool actually happening, as well as helping you to visualize
that’s worth using: Git Repository Viewer (GRV) effec- how your projects are developing.
tively builds an entire GUI on the command line around By Graham Morrison
your local git repositories. The main view, called the His- https://github.com/rgburke/grv
tory View, consists of three panels (Figure 4). On the left is
a pane for branches, tags, and remote references, and in Figure 4
the middle is a list of commits for your chosen branch.
The pane on the right side lists the changes, as a diff, for
that specific commit. The other view is Status View, which
gives an overview of what’s changed in your local branch
since the last push. If you’re already familiar with git, this
makes complete sense. It’s wonderful to see your work
presented in such a clear way, just as you likely imagine
the whole repository hanging somewhere.
GRV is also deep. It defaults to using Vim key bind-
ings and makes a wonderful partner to programming in
the editor. There’s even theme support and the ability
to filter through the data using a query language rather
than simple searches. In the background, changes to

86 101 LINUX HACKS – 2020 EDITION


WORKING WITH CODE

HACK
87
Make git Interactive
Tools for navigating git repositories, their history, and
their branches are becoming almost as common as
Replace
hexdump with
a Colorful Tool
HACK 88
CPU monitors. And like CPU monitors, they also hap-
pen to be very useful. Having more options to choose When viewing binary files in hexadecimal,
from means you’re more likely to find a tool to fit your you’re able to decode more than the literal value for
exact requirements. One fully fledged option is GRV any one location; you’re actually able to see pat-
(see previous hack), which runs from the command terns and even raw data emerge, because the view
line and features several different view modes. can represent the way data is physically passing
At the other end of the feature spectrum, gitin is through memory. This is why a hexadecimal viewer
another great option. Unlike GRV, gitin isn’t an appli- is an essential utility to have at hand, especially if
cation in which you spend time; instead, it works best you’re interested in how files are stored or how an
as a form of wrapper around the git commands you executable may work.
might use anyway. Its principle functions allow you One such tool, hexyl, couldn’t be any simpler (Fig-
to work with branches, check the status of a reposi- ure 6). It’s driven from the command line and takes a
tory, and view the logs, which are all you need in a filename as an argument. The only other potential ar-
tool, since you don’t want to duplicate what git al- gument is a value to adjust the number of bytes read
ready does so well. from the input. This is useful if you’re trying to view
Type gitin log within a repository, for instance, and something large, such as a swap file or even a virtual
you see a list of the most recent commits. Using the device such as memory. But the best thing about
cursor keys to scroll through them updates details on hexyl is how clean the output is. There’s no superflu-
each commit, and pressing return dives into which ous detail, with the same three columns you tradi-
files have changed. Select a file, and you get the tradi- tionally see in hexadecimal viewers and editors – the
tional diff view of what’s changed within that file starting location for a row, the hexadecimal value for
(Figure 5). It’s minimal and effective and, most impor- each location for the input shown on the left, and an
tantly, doesn’t detract from the work you’re likely try- ASCII rendering of those locations on the right. Loca-
ing to do in the same terminal. It just lets you see the tions are colored according to their type: NULL bytes,
same details git can provide in a more convenient and printable ASCII characters, ASCII white-space charac-
interactive way that’s also quicker than trying to work ters, other ASCII characters, and non-ASCII. This
out the commands using git alone. The use of color is makes it very easy to correlate the ASCII output with
also very effective and helps especially in the diff view that data’s location within the main view.
where the red of a remove line and the green of a new
line is far easier to see than the - and +. By Graham Morrison
https://github.com/sharkdp/hexyl
By Graham Morrison
https://github.com/isacikgoz/gitin
Figure 6
Figure 5

101 LINUX HACKS – 2020 EDITION 87


WORKING WITH CODE

HACK 89
Reverse Engineering with Cutter
The reverse engineering tool radare2 takes your Linux ence, Cutter still helps you get some usable knowledge
binaries and turns them into a stream of assembler, let- out of almost any binary, even if it’s just a greater un-
ting you work out exactly what happens when a pro- derstanding of how many subsystems there are in
gram is executed. You need to understand assembler, Linux and how many CPU cycles are potentially wasted
your hardware, and the various subsystems a binary drawing drop shadows.
takes to produce output, but
that’s where radare2 will try and Figure 7
help, splitting the assembler
into functions, libraries, and
symbols to help you make bet-
ter sense of what’s happening.
The only problem with
radare2 is that it’s pretty com-
plex. Fortunately, there’s a
shiny GUI called Cutter that
makes its functions better ac-
cessible. After launch, you sim-
ply select a binary and you’re
done. Something locally com-
piled will reveal the most use-
ful information, but you can try
anything. Cutter will then
spend a little time analyzing
the binary, the results of which
are shown in a “sections” pie
chart that brilliantly shows the
components detected within
the binary. A similar graphical
representation of the binary is
also shown as a horizontal bar
chart at the top of the window,
with each slice showing the use of sequential memory. Figure 7 shows the following areas of the Cutter pro-
But the main part of the Qt-configurable window is gram window:
the disassembler itself, showing the assembly mne- Functions: Cutter can detect when a chunk of code
monics, usually helpfully annotated to show loops 1 is independent and therefore a function.
and jumps, the names of registers, and function Binary map: See where things are in the binary file
names. This is where you can follow the track of exe- 2 you’re viewing.
cution through your binary. Cutter helps with this, Search: Look through the binary for both flags and
too, as you can click on jump or branch instructions to 3 a specific address.
see where the code runs next. Overview: See the details for where you are and
The disassembler is augmented with various other 4 where you’re going next.
tabs to help you make sense of the code. There’s a hex Pie chart: Analysis breaks up the executable into
viewer, for instance, so you can see the raw binary con-
tents of the file. There’s also an excellent pseudocode
5 parts; this chart shows how each part is propor-
tionately used.
generator that will turn the assembler in a function into Details info: Create pseudocode, see a visual repre-
something where loops and structures are demarcated 6 sentation of the current function, and view the hex.
by curly brackets – it feels a lot like C, where the key-
words have been replaced with assembler. Strings, 7 Sections: The annotated version of the pie chart.
entry points, and imports can be investigated in the
same way, and you can search directly for either a flag 8 Disassembly: The code being run by your binary.

name or an address. It’s still complicated, of course, but By Graham Morrison


even if you have no prior reverse engineering experi- https://github.com/radareorg/cutter

88 101 LINUX HACKS – 2020 EDITION


WORKING WITH CODE

Textadept Works in Graphics and Text Mode


HACK 90
Not many other software categories offer such breadth within the command line or as an application within its
of choice as text editors. Textadept is another one, own window. The executable is around 5MB and can be
promising speed and distraction-free design without run off a USB stick, with a promise to consume a mere
sacrificing essential features. It is available for Linux, 15MB of RAM. Into this tiny space, it’s not only capable
macOS, BSD, and Windows. of cramming in lightning fast syntax highlighting (for
Textadept is certainly mature. It’s now over 10 years around 100 languages) but, more importantly, code
old, with a release every two months for more than completion, too. For those of us without photographic
eight years. It can be run either as a curses binary memories, this feature is essential. Autocompletion
works with symbols within the
Figure 8
files you’re editing, as well as
symbols for the language you’re
working with, complete with
links to the API documentation.
The editor is also almost en-
tirely keyboard driven, com-
pletely themable, and well docu-
mented. An interesting feature
that you don’t find in most edi-
tors is changing the font size in-
dividually for each open file
(Figure 8). When you use a ma-
chine remotely, you can also run
a terminal version of Textadept
called textadept-curses.

By Graham Morrison
https://foicica.com/textadept/

H AC K
Perform Dynamic Code Analysis with SystemTap
In multithreaded programs, the order of statements in One of the things that SystemTap
91
the sources is different from the order of execution. So, can do is help determine “contended
merely looking at the code doesn’t reveal the sequence locks.” SystemTap comes with many examples found in
of the program’s operations. You want some tools for /usr/share/doc/systemtap/examples. One of these is the
live introspection, also called process/futexes.stp script. It traces the futex system call
dynamic code analysis. and dumps how many times the thread had to sleep
SystemTap is one waiting for the lock and for how long:
such tool, and stap is
its main executable. $ sudo stap -c ./sampleprog futex.stp
SystemTap lets you ...
write small scripts sample[3006] lock 0x7ffcc7301fe0 contended 79 times,
called probes and attach them 26 avg us
to various events happening
in user space and in the ker- Lower values are generally better. Make sure you
nel. It was designed to be safe have the kernel debug info installed.
for use on production sys-
tems, so you can study non- By Valentine Sinitsyn
trivial situations such as dead- http://sourceware.org/systemtap/
locks in the wild.

101 LINUX HACKS – 2020 EDITION 89


WORKING WITH CODE

Count Lines of Code in Your Project


Are you interested in statistics about your coding pro- the result in an SQL database; the
HACK 92
jects? You could process your source files with wc -l to tool generates the necessary INSERT and
get a line count, but that simple tool does not know the DDL statements.
difference between comments and real code. We sug- By Uwe Vollbracht
gest that you use something better. http://github.com/AlDanial/cloc
cloc – which stands for Count Lines of Code – gives
you an overview of your projects. The practical helper Figure 9
counts blank lines and comments, as well as real lines of
code (Figure 9). It writes the statistical analysis to the
standard output. cloc requires Perl 5.6 or later and un-
derstands more than 200 different programming lan-
guages; you can obtain a full list using cloc --show-lang.
You specify individual files or entire directories to be
counted. CLOC also understands compressed TAR or ZIP
archives; the tool can receive optional help when un-
packing via --extract-with. The Perl program also tracks
changes between two files, directories, or archives. Its
man page lists various diff parameters with examples.
You can save the results from cloc‘s evaluation in
CSV, XML, or YAML format. Entering --sql processes

H AC K
Fix Disturbing Indentation and More 93
A classic developer tool is the “indenter”: a program Once it’s installed, grab a suitable source file and run:
that takes a source code file and creates a new version
uncrustify -c linux.cfg testfile.c
where indentation and brace placement follow some
fixed rules. To see an old example program from this This will create a new file testfile.c.uncrustify. I tried it
category, look at indent. The Unix original is from the on auto-generated (and poorly formatted) code with
late 70s, and on Linux you can install GNU indent, some success, as you can see in Figure 10.
which was last updated in 2008.
By Hans-Georg Eßer
For a more flexible approach have a look at the
uncrustify tool: It can do a bit more than indent, and https://en.wikipedia.org/wiki/Indent_(Unix)
you can configure it by writing a complex config file. http://uncrustify.sourceforge.net/
The program supports the C, C++, C#, Objective C, D, http://uncrustify.sourceforge.net/linux.cfg.txt
Java, Pawn, and Vala languages. To get you
Figure 10
started, there’s a sample file (search for linux.
cfg.txt in the package contents or on the pro-
ject web page).
I found packages for Ubuntu/Mint and open-
SUSE in the regular repositories. If it isn’t avail-
able for your distribution, compile it from the
sources; you can check out the current version via

git clone git://github.com/uncrustify/uncrustify.git

In my test, I was able to build the project with

mkdir build; cd build


cmake .. && make && sudo make install

run in the uncrustify folder.

90 101 LINUX HACKS – 2020 EDITION


Is it a hack to bring back the old DOS editor EDIT for
editing your config files? Or to run an old BBS like they
did in the 80s? Yes, those are hacks. Also, did you like
the old “3D Pipes” screensaver? Read on…

HACK 94
Interesting Terminal Output for Your Visitors 93
HACK 95
Scan the Network Like a Script Kiddie 94
HACK 96
Image © lightwise, 123RF.com
Drive Your Moon Lander – in ASCII! 94
HACK 97
Run Your Own BBS 95
HACK 98
Explore, Expand, Exterminate: Play Star Ruler 2 95
HACK 99
Edit Files Like It’s the 90s Again 96
HACK 100
Read Old Unix Books 96
HACK 101
Pick a Retro Screensaver 97

92 101 LINUX HACKS – 2020 EDITION


HAVING FUN

H AC K
94 Interesting Terminal Output for Your Visitors
Are your friends or colleagues coming over Other than producing interesting output and (when
and you want to impress them with interesting programming) seeing stuff in octal, knowing od is help-
and mysterious stuff happening on your computer ful when you prepare for the LPI tests: It’s listed in the
screens when they take a tour of the house? Then you curriculum, and that is why I know it.
have lots of options.
Create Slowly Scrolling Output
Glitch in the Matrix You know those movies where hackers sit in a dark
Let me introduce to you the od program. The name is room behind a big screen, and occasionally you get a
short for “octal dump,” and the tool works like hexdump glimpse of what’s happening on the screen. In some
but uses octal numbers (0-7) instead of hexadecimal window, there’s always some scrolling output. That
ones. Let it output /dev/urandom to get a (very cheap) could be C compiler messages, dmesg lines, or just some
text mode version of the scrolling symbols from The random JavaScript and HTML from a webpage – it
Matrix movies. If you want to get rid of the space signs, doesn’t really matter (to the film directors) as long as it
remove them with tr: looks geeky.
Now you want to have this on your screen, but sim-
od /dev/urandom | tr "\n" " " | tr -d " " ply running dmesg or cat source.c in a terminal window
lets the text scroll far too fast. The following code re-
Run the od output through lolcat to get the colorful places cat as a filter and introduces a short sleep after
version shown in Figure 1. Some distributions carry a every line:
lolcat package; if yours does not, you can install it
manually. while read -r; do echo "$REPLY$"; sleep .02; done

Figure 1 Combine it with something that produces colors for


an even nicer effect, my favorite is a colored dmesg
(Figure 2):

dmesg -L=always | while read -r; do echo "$REPLY$"; U


sleep .02; done

(Even if your dmesg is colored by default, you will


need the -L option, because dmesg notices that output
is not going to a terminal but into a pipe, which will
turn colors off.)
If that’s not enough, install the aalib or libaa-bin
package, which contains a library for rendering graph-
ics (and videos) as ASCII output. There’s an example
program called aafire that turns your terminal window
into a fireplace.

Figure 2 By Hans-Georg Eßer


https://github.com/busyloop/lolcat

101 LINUX HACKS – 2020 EDITION 93


HAVING FUN

HACK
95 Scan the Network Like a Script Kiddie
The nmap (network mapper) tool searches for open security focus – as well as hackers gone bad who at-
ports in a computer network or on some specific ma- tack machines for evil purposes. The second group in-
chine, and it can even find out what operating system cludes script kiddies.
and version is running on a remote computer. That True script kiddies like to see their texts in leetspeak
makes it an important tool for IT professionals with a (or l3375p34k). Since they are likely to (mis-)use the
nmap tool, it provides a leetspeak option to
Figure 3 make them comfortable. You can feel like
a script kiddie, too: Just run the mapper
with the option -oS to change the output
format to “Script kiddie” – the result will
look like Figure 3.
The -o option has further, more legiti-
mate parameters as well: -oX switches to
XML output, and -oG to a format that can
easily be filtered with grep. But where’s the
fun in that?

By Hans-Georg Eßer
https://nmap.org/
https://en.wikipedia.org/wiki/Leet

Drive Your Moon Lander – in ASCII!


When you think of games made with ASCII characters,
HACK 96
engine than the original. However, in this case, it makes
you typically start with Snake and end with NetHack, the graphics utterly enchanting. You’ll be hooked from
both of which are brilliant in their own way. You don’t the moment you launch the game and the moon buggy
typically think of something with refined arcade quality scrolls smoothly from one side of the screen to the
playability and design. And yet that exactly describes other (Figure 4).
Ascii Patrol, an interpretation of the arcade classic, Pressing any key from the title screen takes you to an
Moon Patrol, recreated in text characters to run on a overview screen, which includes a player profile and
console. Outside of 80s-era home computer arcade editable avatar, a campaign overview complete with a
conversions, it’s one of the few modern recreations of map, and an online leaderboard, plus there’s a handy
an old game built atop an even more limited graphics controls overview. It’s like an AAA game on Xbox Live.
Press the spacebar, and you’re playing the game. As
Figure 4 with the original, the action comes from a side-scrolling
obstacle course where you accelerate and decelerate
your buggy, jump, and shoot at things while avoiding
rocks and crevasses. Your score is based on how far
you get. It’s deceptively tricky and very addictive, and
you soon forget the entire game is being rendered
using only ASCII characters. It plays brilliantly, and it’s
just as addictive and as playable as the original, with
the new/old graphics being part of the charm, rather
than a compromise.

By Graham Morrison
http://ascii-patrol.com/

94 101 LINUX HACKS – 2020 EDITION


HAVING FUN

HACK 98 Explore, Expand, Exterminate:


Play Star Ruler 2
While it’s sad when a company gives up on gam-
ing, if they choose to do the right thing with the

HACK 97
Run Your Own BBS
code, it benefits a much wider audience. Star Ruler 2
is a great example (Figure 6). This is a popular real-
time strategy (RTS) game from 2015, and it’s still
available on Steam, but the studio behind it, Blind
Mind Studios, has been inactive for a few years.
Those of a certain age will remember a time before Rather than drop off the radar completely and alien-
the Internet: If you wanted your computer to commu- ate its players, the studio decided to open source
nicate with other computers, you did it on a 1:1 basis both its game and the expansion pack.
across a telephone line. You dialed a number, and an- You can install the game with just a single com-
other computer answered before proceeding to ne- mand, sudo snap install starruler2, if your system
gotiate a stream of ASCII going back and forth down supports snaps. You get a major multiplayer RTS title
the line. There were many popular “online” services on Linux for free, and that’s never a bad thing.
that you could pay to access, but perhaps the best
Figure 6
use of this technology was the humble bulletin board
system (BBS). These were portals for files, messages,
and games that were usually run from a home com-
puter with nothing more than one or two phone lines
that became available in the middle of the night.
The Internet killed the BBS scene, but it’s now hav-
ing a slight renaissance, partly for nostalgia, but also
because the web has become hugely distracting.
These new BBSs, and even the old ones restored
from backups, are accessible over a simple Telnet
connection (and sometimes SSH); you can even be-
come the sysop of your domain. MBSE BBS is a mod-
ern BBS (Figure 5) you can install that’s still being up-
dated, although it takes some setting up. If you try it,
your users will be able to create accounts and log in,
leave messages, download and upload curated files,
and chat with each other. You can even allow as
many concurrent users as you need. Set in space, the game itself is known as a “4x”
RTS game, meaning your role as the ruler of the gal-
By Graham Morrison axy is to explore, expand, exploit, and exterminate.
https://sourceforge.net/projects/mbsebbs/ There are seven different races with different attri-
butes, or you can create your own. With that done,
Figure 5 you’re dumped into space where you need to man-
age your finances and local resources to build an em-
pire. You map resources between systems by drag-
ging lines between them, creating networks for im-
ports and exports. You use your fleet of ships to ex-
plore and colonize just as you do with other RTS
games. As you play, you acquire influence points that
can be used in diplomacy, and you can even design
your own ships. The game is a lot of fun, and it’s pol-
ished, just as you’d expect with a commercial game.

By Graham Morrison
http://starruler2.com

101 LINUX HACKS – 2020 EDITION 95


HAVING FUN

Edit Files Like It’s the 90s Again


Look at Figure 7, which shows good old EDIT from DOS
HACK
EDIT.ZIP file into /tmp. You will find a new BIN
99
editing a file. If you want to replace all other editors subfolder that contains EDIT.EXE and EDIT.HLP – copy
with this classic tool, there’s a way. To prepare, first in- those to ~/dos.
stall DOSBox (the dosbox package) and create a dos Also copy some text file you want to edit to ~/dos/
folder in your home directory. Then grab an image of file.txt. Now you can boot DOS and let it execute EDIT.
some MS-DOS clone, for example FreeDOS 1.2. Mount EXE by typing
the CD-ROM image (FD12CD.iso) and unpack the BASE/
dosbox -c "mount c ~/dos" -c "c:" -c "edit file.txt" -c exit
Figure 7
which brings back the glorious old look and edits your
file. You can access the menu entries by pressing Alt
and then using the cursor keys, or via Alt+F (File), Alt+E
(Edit), etc., or even using the mouse. (If your mouse
gets stuck inside DOSBox, press Ctrl+F10 to release it.)
When you’re done, press Alt+X to exit both the editor
and the DOS emulator.
It’s only a little more work to create a script that will
copy a text file to the ~/dos folder, launch the emulation,
and then copy it back.

By Hans-Georg Eßer
https://www.dosbox.com/
https://www.freedos.org/

H AC K
Read Old Unix Books
Reading a good book is always worth-
100 pen to all documentation. While a KDE book
from the late 90s has lost all usefulness (except
as a picture album to see what desktops looked
while, but computing literature tends to age in like in the old days), you can always grab a good intro-
a way that makes most practical descriptions obsolete duction to the shell – for example the article “The UNIX
within a decade. In the case of Linux, that doesn’t hap- Shell” from Stephen R. Bourne, who developed the
Bourne shell (sh), an ancestor of the Bash
Figure 8 shell. After all, Bash is an acronym, meaning
“Bourne-again shell.”
The article was published in 1978. Grab a
copy from archive.org and read the introduc-
tion to using the shell, working with variables,
writing shell scripts with for and while loops,
pipelines, and much more (Figure 8). Travel-
ing back in time 40 years does not change
shell usage a lot. Of course, a modern Bash
has more features and offers more comfort,
but the basics have remained the same.

By Hans-Georg Eßer
https://archive.org/details/bstj57-6-1971

96 101 LINUX HACKS – 2020 EDITION


HAVING FUN

Pick a Retro Screensaver HACK


There are two things you might miss from working with
101Click on Pipes to start a good approximation of the 3D
Windows, especially the older versions: the 3D Pipes Pipes from Windows (Figure 10). You can configure this
screensaver and the occasional display of the famous screensaver via the Settings button; there are a few in-
Blue Screen of Death (BSoD). Sadly, modern Windows teresting options. Figure 10
versions do not suffer from the same stability issues. If If you want to
you remember Windows 95 and 98, you know what I’m see a screen-
talking about. saver in full-
Everything you need to celebrate your nostalgia is screen mode,
available in XScreenSaver and add-on packages. You you’ll have to
only need to install and configure them. The names of start the previ-
the required packages differ from distro to distro, so, ously mentioned
for example, on Ubuntu or Mint, type daemon. Click on
Preview, and the
sudo apt install xscreensaver xscreensaver-gl U
warning dialog
xscreensaver-screensaver-bsod
will reappear –
to get the packages. OpenSUSE users run the command this time, start the daemon. In my tests, I had to click on
Preview again.
sudo zypper in xscreensaver xscreensaver-data U
When you’ve found your favorite, check the process
xscreensaver-data-extra
list (or tree, with pstree) to find its program name: You
and Fedora wants you to type: can find individual screensaver binaries in various fold-
ers, depending on your distribution, for example:
sudo dnf install xscreensaver
• /usr/libexec/xscreensaver/ (Fedora)
Once the files are on the disk, you can decide whether • /usr/lib64/xscreensaver/ (openSUSE)
you just want to play with the screensavers or really in- • /usr/lib/xscreensaver/ (Linux Mint)
stall one (so that it will start up after a few minutes of
inactivity). Make It Permanent
To install a screensaver from this collection as your regu-
Just Playing lar screensaver (instead of the desktop’s standard tool),
If you just want to have a look at the screensavers, run make sure that the desktop’s screensaver and/or locking
the xscreensaver-demo program. You’ll get a warning dia- mechanism is off, that XScreenSaver starts automati-
log from which you can launch the XScreenSaver dae- cally when you log in, and that it’s properly configured.
mon – for testing purposes, click Cancel. On KDE, disable the built-in Screen Locking in the
From the list on the left side (Figure 9), select a KDE system settings. Then make sure that the
screensaver and see what it looks like on the right side. XScreenSaver daemon starts when you login: open
The BSOD entry holds more than just a blue screen KDE’s Autostart settings, click Add Program, type
from Windows: There are all kinds of operating system xscreensaver in the input field, and click OK. Check that
crashes from various architectures, some of them ani- it works by logging out and in again, and then search
mated. The Windows 10 bluescreen even displays a QR the process table for xscreensaver.
code (Figure 9); if you encounter it, quickly start the QR When using Gnome, uninstall the gnome-screensaver
code viewer app on your phone: I don’t want to ruin the package (if installed). Make xscreensaver an autostart
experience for you by telling what you’ll get. program (open Startup Applications, click Add, enter
xscreensaver in both the Name and Command fields,
Figure 9
and click Add). Then log out and in again; you should
see a splash screen.
For Mint’s Cinnamon desktop, disable the Cinnamon
screensaver and then create a hard link like this:

sudo ln -sf /usr/bin/xscreensaver-command U


/usr/bin/cinnamon-screensaver-command

When you want to both auto-activate the screensaver and


make it lock the screen, use XScreenSaver’s settings dia-
log (that you start with xscreensaver-demo) and set two
timeouts in the Blank After and Lock Screen After fields.

By Hans-Georg Eßer
https://www.jwz.org/xscreensaver/

101 LINUX HACKS – 2020 EDITION 97


MASTHEAD / AUTHORS

We are always looking for good articles on Linux and the The technical level of the article should be consistent
tools of the Linux environment. Although we will consider with what you normally read in Linux Magazine. Re-
any topic, the following themes are of special interest: member that Linux Magazine is read in many countries,
• System administration so it is best to avoid using slang and idioms that might
not be understood by all readers.
• Useful tips and tools
Be careful when referring to dates or events in the future.
• Security, both news and techniques
Many weeks could pass between your manuscript sub-
• Product reviews, especially from real-world experience mission and the final copy reaching the reader’s hands.
• Community news and projects When submitting proposals or manuscripts, please use a
If you have an idea, send a proposal subject line in your email message that helps us identify
with an outline, an estimate of the your message as an article proposal. Screenshots and
length, a description of your back- other supporting materials are always welcome.
ground, and contact information to Additional information is available at:
[email protected]. http://www.linux-magazine.com/contact/write_for_us.

Contact Info

Editor-in-Chief While every care has been taken in the content of the
Hans-Georg Eßer magazine, the publishers cannot be held responsible for
Senior Editor the accuracy of the information contained within it or any
Joe Casad consequences arising from the use of it. The use of the
Managing Editor DVD provided with the magazine or any material provided
Lori White on it is at your own risk.
Localization & Translation
Ian Travis Copyright and Trademarks © 2020 Linux New Media
Authors USA, LLC
Copy Editors
Amy Pettle, Megan Phelps No material may be reproduced in any form whatso-
Erik Bärwaldt Layout ever in whole or in part without the written permis-
Dena Friesen, Lori White sion of the publishers. It is assumed that all corre-
Paul Brown
Cover Design spondence sent, for example, letters, email, faxes,
Bruce Byfield Dena Friesen, Illustration based on graphics photographs, articles, drawings, are supplied for
by bowie15,123RF.com publication or license to third parties on a non-exclu-
Hans-Georg Eßer
Advertising sive worldwide basis by Linux New Media unless
Frank Hofmann Brian Osborn, [email protected] otherwise stated in writing.
phone +49 89 3090 5128
Klaus Knopper
Marketing Communications Linux Magazine Special (ISSN 1757-6369) is published
Charly Kühnast Gwen Clark, [email protected] by Linux New Media USA, LLC, 2721 W 6th St, Ste D,
Christoph Langner Publisher Lawrence, KS, 66049, USA.
Brian Osborn
Graham Morrison All brand or product names are trademarks of their re-
Customer Service / Subscription spective owners. Contact us if we haven’t credited your
Tim Schürmann For USA and Canada:
Email: [email protected] copyright; we will always correct any oversight.
Valentine Sinitsyn Phone: 1-866-247-2802
(toll-free from the US and Canada) Printed in Germany on FSC and PEFC certified paper.
Ferdinand Thommes
For all other countries: Distributed by Seymour Distribution Ltd, United
Daniel Tibi Email: [email protected] Kingdom
Alexander Tolstoy Linux New Media USA, LLC
2721 W 6th St, Ste D, Lawrence, KS, 66049 USA Published in Europe by: Sparkhaus Media GmbH,
Uwe Vollbracht www.linux-magazine.com Zieblandstr. 1, 80799 Munich, Germany

98 101 LINUX HACKS – 2020 EDITION

You might also like