Chapter 9. System tips (original) (raw)
Here, I describe basic tips to configure and manage systems, mostly from the console.
There are some utility programs to help your console activities.
Table 9.1. List of programs to support console activities
package | popcon | size | description |
---|---|---|---|
See Section 1.3, “Midnight Commander (MC)”
script
(1) command to make a record of terminal session
terminal multiplexer with VT100/ANSI terminal emulation
terminal multiplexer alternative (Use "Control-B" instead)
fuzzy text finder
fuzzy text finder
readline feature command line wrapper
readline feature command line wrapper
readline feature command line wrapper
fast recursive string search in the source code tree with automatic filtering
The simple use of script
(1) (see Section 1.4.9, “Recording the shell activities”) to record shell activity produces a file with control characters. This can be avoided by using col
(1) as the following.
$ script Script started, file is typescript
Do whatever … and press Ctrl-D
to exit script
.
$ col -bx < typescript > cleanedfile $ vim cleanedfile
There are alternative methods to record the shell activities:
- Use
tee
(usable during the boot process in the initramfs):
$ sh -i 2>&1 | tee typescript - Use
gnome-terminal
with the extend line buffer for scrollback. - Use
screen
with "^A H
" (see Section 9.1.2, “The screen program”) to perform recording of console. - Use
vim
with ":terminal
" to enter the terminal mode. Use "Ctrl-W N
" to exit from terminal mode to normal mode. Use ":w typescript
" to write the buffer to a file. - Use
emacs
with "M-x shell
", "M-x eshell
", or "M-x term
" to enter recording console. Use "C-x C-w
" to write the buffer to a file.
In Section 1.4.2, “Customizing bash”, 2 tips to allow quick navigation around directories are described: $CDPATH
and mc
.
If you use fuzzy text filter program, you can do without typing the exact path. For fzf
, include following in ~/.bashrc
.
FZF_KEYBINDINGS_PATH=/usr/share/doc/fzf/examples/key-bindings.bash if [ -f $FZF_KEYBINDINGS_PATH ]; then . $FZF_KEYBINDINGS_PATH fi
For example:
- You can jump to a very deep subdirectory with minimal efforts. You first type "
cd **
" and pressTab
. Then you will be prompted with candidate paths. Typing in partial path strings, e.g.,s/d/b foo
, will narrow down candidate paths. You select the path to be used bycd
with cursor and return keys. - You can select a command from the command history more efficiently with minimal efforts. You press
Ctrl-R
at the command prompt. Then you will be prompted with candidate commands. Typing in partial command strings, e.g.,vim d
, will narrow down candidates. You select the one to be used with cursor and return keys.
After you learn basics of vim
(1) through Section 1.4.8, “Using vim”, please read Bram Moolenaar's "Seven habits of effective text editing (2000)" to understand how vim
should be used.
The behavior of vim
can be changed significantly by enabling its internal features through the Ex
-mode commands such as "set ...
" to set vim options.
These Ex
-mode commands can be included in user's vimrc file, traditional "~/.vimrc
" or git-friendly "~/.vim/vimrc
". Here is a very simple example[2]:
""" Generic baseline Vim and Neovim configuration (~/.vimrc) """ - For NeoVim, use "nvim -u ~/.vimrc [filename]" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" let mapleader = ' ' " :h mapleader """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" set nocompatible " :h 'cp -- sensible (n)vim mode syntax on " :h :syn-on filetype plugin indent on " :h :filetype-overview set encoding=utf-8 " :h 'enc (default: latin1) -- sensible encoding """ current vim option value can be verified by :set encoding? set backspace=indent,eol,start " :h 'bs (default: nobs) -- sensible BS set statusline=%<%f%m%r%h%w%=%y[U+%04B]%2l/%2L=%P,%2c%V set listchars=eol:¶,tab:⇄\ ,extends:↦,precedes:↤,nbsp:␣ set viminfo=!,'100,<5000,s100,h " :h 'vi -- bigger copy buffer etc. """ Pick "colorscheme" from blue darkblue default delek desert elflord evening """ habamax industry koehler lunaperche morning murphy pablo peachpuff quiet ron """ shine slate torte zellner colorscheme industry """ don't pick "colorscheme" as "default" which may kill SpellUnderline settings set scrolloff=5 " :h 'scr -- show 5 lines around cursor set laststatus=2 " :h 'ls (default 1) k """ boolean options can be unset by prefixing "no" set ignorecase " :h 'ic set smartcase " :h 'scs set autoindent " :h 'ai set smartindent " :h 'si set nowrap " :h 'wrap "set list " :h 'list (default nolist) set noerrorbells " :h 'eb set novisualbell " :h 'vb set t_vb= " :h 't_vb -- termcap visual bell set spell " :h 'spell set spelllang=en_us,cjk " :h 'spl -- english spell, ignore CJK set clipboard=unnamedplus " :h 'cb -- cut/copy/paste with other app set hidden " :h 'hid set autowrite " :h 'aw set timeoutlen=300 " :h 'tm
The keymap of vim
can be changed in user's vimrc file. E.g.:
![]() |
Caution |
---|---|
Don't try to change the default key bindings without very good reasons. |
""" Popular mappings (imitating LazyVim etc.) """ Window moves without using CTRL-W which is dangerous in INSERT mode nnoremap h nnoremap j nnoremap k silent! nnoremap l """ Window resize nnoremap vertical resize -2 nnoremap resize -2 nnoremap resize +2 nnoremap vertical resize +2 """ Clear hlsearch with ( is mapped as above) nnoremap noh inoremap noh """ center after jump next nnoremap n nzz nnoremap N Nzz """ fast "jk" to get out of INSERT mode () inoremap jk noh """ fast "" to get out of TERM mode (CTRL-\ CTRL-N) tnoremap <C-> """ fast "jk" to get out of TERM mode (CTRL-\ CTRL-N) tnoremap jk <C-> """ previous/next trouble/quickfix item nnoremap [q cprevious nnoremap ]q cnext """ buffers nnoremap bprevious nnoremap bnext nnoremap [b bprevious nnoremap ]b bnext """ Add undo break-points inoremap , ,u inoremap . .u inoremap ; ;u """ save file inoremap w xnoremap w nnoremap w snoremap w """ better indenting vnoremap < >gv """ terminal (Somehow under Linux, becomes in Vim) nnoremap terminal "nnoremap terminal """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" if ! has('nvim') """ Toggle paste mode with p for Vim (no need for Nvim) set pastetoggle=p """ nvim default mappings for Vim. See :h default-mappings in nvim """ copy to EOL (no delete) like D for d noremap Y y$ """ sets a new undo point before deleting inoremap u inoremap u """ is re-purposed as above """ execute the previous macro recorded with Q nnoremap Q @@ """ repeat last substitute and KEEP flags nnoremap & :&& """ search visual selected string for visual mode xnoremap * y/\V" xnoremap # y?\V" endif
In order for the above keybindings to function properly, the terminal program needs to be configured to generate "ASCII DEL" for Backspace
-key and "Escape sequence" for Delete
-key.
Other miscellaneous configuration can be changed in user's vimrc file. E.g.:
""" Use faster 'rg' (ripgrep package) for :grep if executable("rg") set grepprg=rg\ --vimgrep\ --smart-case set grepformat=%f:%l:%c:%m endif """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """ Retain last cursor position :h '" augroup RetainLastCursorPosition autocmd! autocmd BufReadPost * \ if line("'"") > 0 && line ("'"") <= line("$") | \ exe "normal! g'"" | \ endif augroup END """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """ Force to use underline for spell check results augroup SpellUnderline autocmd! autocmd ColorScheme * highlight SpellBad term=Underline gui=Undercurl autocmd ColorScheme * highlight SpellCap term=Underline gui=Undercurl autocmd ColorScheme * highlight SpellLocal term=Underline gui=Undercurl autocmd ColorScheme * highlight SpellRare term=Underline gui=Undercurl augroup END """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """ highlight tailing spaces except when typing as red (set after colorscheme) highlight TailingWhitespaces ctermbg=red guibg=red """ \s+ 1 or more whitespace character: and """ %#@<! Matches with zero width if the cursor position does NOT match. match TailingWhitespaces /\s+%#@<!$/
Interesting external plugin packages can be found:
- Vim - the ubiquitous text editor -- The official upstream site of Vim and vim scripts
- VimAwsome -- The listing of Vim plugins
- vim-scripts -- Debian package: a collection of vim scripts
Plugin packages in the vim-scripts package can be enabled using user's vimrc file. E.g.:
packadd! secure-modelines packadd! winmanager " IDE-like UI for files and buffers with w nnoremap w :WMToggle
The new native Vim package system works nicely with "git
" and "git submodule
". One such example configuration can be found at my git repository: dot-vim. This does essentially:
- By using "
git
" and "git submodule
", latest external packages, such as "_name_
", are placed into~/.vim/pack/*/opt/_name_
and similar. - By adding
:packadd! _name_
line to user's vimrc file, these packages are placed onruntimepath
. - Vim loads these packages on
runtimepath
during its initialization. - At the end of its initialization, tags for the installed documents are updated with "
helptags ALL
".
For more, please start vim
with "vim --startuptime vimstart.log
" to check actual execution sequence and time spent for each step.
It is quite confusing to see too many ways[3] to manage and load these external packages to vim
. Checking the original information is the best cure.
Here are notable log analyzers ("~Gsecurity::log-analyzer
" in aptitude
(8)).
Table 9.4. List of system log analyzers
package | popcon | size | description |
---|---|---|---|
log analyzer with nice output written in Perl
ban IPs that cause multiple authentication errors
web server log analyzer
powerful and featureful web server log analyzer
squid analysis report generator
Postfix log entry summarizer
firewall log analyzer
monitor and analyze squid access.log files
log file viewer with regexp matching, highlighting, and hooks
Controllable Regex Mutilator and Spam Filter (CRM114)
interpret ICMP messages
![]() |
Note |
---|---|
CRM114 provides language infrastructure to write fuzzy filters with the TRE regex library. Its popular use is spam mail filter but it can be used as log analyzer.
You can record the editor activities for complex repeats.
For Vim, as follows.
- "
qa
": start recording typed characters into named register "a
". - … editor activities
- "
q
": end recording typed characters. - "
@a
": execute the contents of register "a
".
For Emacs, as follows.
- "
C-x (
": start defining a keyboard macro. - … editor activities
- "
C-x )
": end defining a keyboard macro. - "
C-x e
": execute a keyboard macro.
Program activities can be monitored and controlled using specialized tools.
Table 9.8. List of tools for monitoring and controlling program activities
package | popcon | size | description |
---|---|---|---|
nice
(1): run a program with modified scheduling priority
renice
(1): modify the scheduling priority of a running process
"/proc
" filesystem utilities: ps
(1), top
(1), kill
(1), watch
(1), …
"/proc
" filesystem utilities: killall
(1), fuser
(1), peekfd
(1), pstree
(1)
time
(1): run a program to report system resource usages with respect to time
sar
(1), iostat
(1), mpstat
(1), …: system performance tools for Linux
Interactive System Activity Grapher for sysstat
lsof
(8): list files opened by a running process using "-p
" option
strace
(1): trace system calls and signals
ltrace
(1): trace library calls
xtrace
(1): trace communication between X11 client and server
powertop
(1): information about system power use
run processes according to a schedule in background from cron
(8) daemon
cron-like command scheduler for systems that don't run 24 hours a day
at
(1) or batch
(1): run a job at a specified time or below certain load level
![]() |
Tip |
---|---|
The procps packages provide very basics of monitoring, controlling, and starting program activities. You should learn all of them. |
For the command-line interface (CLI), the first program with the matching name found in the directories specified in the $PATH
environment variable is executed. See [Section 1.5.3, “The "$PATH" variable”](ch01.en.html#%5Fthe%5Fliteral%5Fpath%5Fliteral%5Fvariable "1.5.3. The "$PATH" variable").
For the graphical user interface (GUI) compliant to the freedesktop.org standards, the *.desktop
files in the /usr/share/applications/
directory provide necessary attributes for the GUI menu display of each program. Each package which is compliant to Freedesktop.org's xdg menu system installs its menu data provided by "*.desktop" under "/usr/share/applications/". Modern desktop environments which are compliant to Freedesktop.org standard use these data to generate their menu using the xdg-utils package. See "/usr/share/doc/xdg-utils/README".
For example, the chromium.desktop
file defines attributes for the "Chromium Web Browser" such as "Name" for the program name, "Exec" for the program execution path and arguments, "Icon" for the icon used, etc. (see the Desktop Entry Specification) as follows:
[Desktop Entry] Version=1.0 Name=Chromium Web Browser GenericName=Web Browser Comment=Access the Internet Comment[fr]=Explorer le Web Exec=/usr/bin/chromium %U Terminal=false X-MultipleArgs=false Type=Application Icon=chromium Categories=Network;WebBrowser; MimeType=text/html;text/xml;application/xhtml_xml;x-scheme-handler/http;x-scheme-handler/https; StartupWMClass=Chromium StartupNotify=true
This is an oversimplified description. The *.desktop
files are scanned as follows.
The desktop environment sets $XDG_DATA_HOME
and $XDG_DATA_DIR
environment variables. For example, under the GNOME 3:
$XDG_DATA_HOME
is unset. (The default value of$HOME/.local/share
is used.)$XDG_DATA_DIRS
is set to/usr/share/gnome:/usr/local/share/:/usr/share/
.
So the base directories (see XDG Base Directory Specification) and the applications
directories are as follows.
$HOME/.local/share/
→$HOME/.local/share/applications/
/usr/share/gnome/
→/usr/share/gnome/applications/
/usr/local/share/
→/usr/local/share/applications/
/usr/share/
→/usr/share/applications/
The *.desktop
files are scanned in these applications
directories in this order.
![]() |
Tip |
---|---|
A user custom GUI menu entry can be created by adding a *.desktop file in the $HOME/.local/share/applications/ directory. |
![]() |
Tip |
---|---|
The "Exec=..." line isn't parsed by the shell. Use the env(1) command if environment variables need to be set. |
![]() |
Tip |
---|---|
Similarly, if a *.desktop file is created in the autostart directory under these base directories, the specified program in the *.desktop file is executed automatically when the desktop environment is started. See Desktop Application Autostart Specification. |
![]() |
Tip |
---|---|
Similarly, if a *.desktop file is created in the HOME/DesktopdirectoryandtheDesktopenvironmentisconfiguredtosupportthedesktopiconlauncherfeature,thespecifiedprograminitisexecuteduponclickingtheicon.PleasenotethattheactualnameoftheHOME/Desktop directory and the Desktop environment is configured to support the desktop icon launcher feature, the specified program in it is executed upon clicking the icon. Please note that the actual name of the HOME/DesktopdirectoryandtheDesktopenvironmentisconfiguredtosupportthedesktopiconlauncherfeature,thespecifiedprograminitisexecuteduponclickingtheicon.PleasenotethattheactualnameoftheHOME/Desktop directory is locale dependent. See xdg-user-dirs-update(1). |
Some programs start another program automatically. Here are check points for customizing this process.
- Application configuration menu:
- GNOME3 desktop: "Settings" → "System" → "Details" → "Default Applications"
- KDE desktop: "K" → "Control Center" → "KDE Components" → "Component Chooser"
- Iceweasel browser: "Edit" → "Preferences" → "Applications"
mc
(1): "/etc/mc/mc.ext
"
- Environment variables such as "
$BROWSER
", "$EDITOR
", "$VISUAL
", and "$PAGER
" (seeenviron
(7)) - The
update-alternatives
(1) system for programs such as "editor
", "view
", "x-www-browser
", "gnome-www-browser
", and "www-browser
" (see Section 1.4.7, “Setting a default text editor”) - the "
~/.mailcap
" and "/etc/mailcap
" file contents which associate MIME type with program (seemailcap
(5)) - The "
~/.mime.types
" and "/etc/mime.types
" file contents which associate file name extension with MIME type (seerun-mailcap
(1))
![]() |
Tip |
---|---|
update-mime(8) updates the "/etc/mailcap" file using "/etc/mailcap.order" file (see mailcap.order(5)). |
![]() |
Tip |
---|---|
The debianutils package provides sensible-browser(1), sensible-editor(1), and sensible-pager(1) which make sensible decisions on which editor, pager, and web browser to call, respectively. I recommend you to read these shell scripts. |
![]() |
Tip |
---|---|
In order to run a console application such as mutt under GUI as your preferred application, you should create an GUI application as following and set "/usr/local/bin/mutt-term" as your preferred application to be started as described. # cat /usr/local/bin/mutt-term <<EOF #!/bin/sh gnome-terminal -e "mutt \$@" EOF # chmod 755 /usr/local/bin/mutt-term |
Use cron
(8) to schedule tasks regularly. See crontab
(1) and crontab
(5).
You can schedule to run processes as a normal user, e.g. foo
by creating a crontab
(5) file as "/var/spool/cron/crontabs/foo
" with "crontab -e
" command.
Here is an example of a crontab
(5) file.
use /usr/bin/sh to run commands, no matter what /etc/passwd says
SHELL=/bin/sh
mail any output to paul, no matter whose crontab this is
MAILTO=paul
Min Hour DayOfMonth Month DayOfWeek command (Day... are OR'ed)
run at 00:05, every day
5 0 * * * HOME/bin/daily.job>>HOME/bin/daily.job >> HOME/bin/daily.job>>HOME/tmp/out 2>&1
run at 14:15 on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
run at 22:00 on weekdays(1-5), annoy Joe. % for newline, last % for cc:
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%.%% 23 */2 1 2 * echo "run 23 minutes after 0am, 2am, 4am ..., on Feb 1" 5 4 * * sun echo "run at 04:05 every Sunday"
run at 03:40 on the first Monday of each month
40 3 1-7 * * [ "$(date +%a)" == "Mon" ] && command -args
![]() |
Tip |
---|---|
For the system not running continuously, install the anacron package to schedule periodic commands at the specified intervals as closely as machine-uptime permits. See anacron(8) and anacrontab(5). |
![]() |
Tip |
---|---|
For scheduled system maintenance scripts, you can run them periodically from root account by placing such scripts in "/etc/cron.hourly/", "/etc/cron.daily/", "/etc/cron.weekly/", or "/etc/cron.monthly/". Execution timings of these scripts can be customized by "/etc/crontab" and "/etc/anacrontab". |
Systemd has low level capability to schedule programs to run without cron
daemon. For example, /lib/systemd/system/apt-daily.timer
and /lib/systemd/system/apt-daily.service
set up daily apt download activities. See systemd.timer
(5) .
Although most of the hardware configuration on modern GUI desktop systems such as GNOME and KDE can be managed through accompanying GUI configuration tools, it is a good idea to know some basics methods to configure them.
Table 9.14. List of hardware configuration tools
package | popcon | size | description |
---|---|---|---|
Linux console font and keytable utilities
X server utilities: xset
(1), xmodmap
(1)
daemon to manage events delivered by the Advanced Configuration and Power Interface (ACPI)
utility to display information on ACPI devices
daemon to put a laptop to sleep during inactivity
hard disk access optimization (see Section 9.6.9, “Optimization of hard disk”)
control and monitor storage systems using S.M.A.R.T.
collection of tools for serial port management
collection of tools for memory hardware management
collection of tools for SCSI hardware management
compact disc drive access optimization
larger mouse cursors for X
Here, ACPI is a newer framework for the power management system than APM.
![]() |
Tip |
---|---|
CPU frequency scaling on modern system is governed by kernel modules such as acpi_cpufreq. |
Device drivers for sound cards for current Linux are provided by Advanced Linux Sound Architecture (ALSA). ALSA provides emulation mode for previous Open Sound System (OSS) for compatibility.
Application softwares may be configured not only to access sound devices directly but also to access them via some standardized sound server system. Currently, PulseAudio, JACK, and PipeWire are used as sound server system. See Debian wiki page on Sound for the latest situation.
There is usually a common sound engine for each popular desktop environment. Each sound engine used by the application can choose to connect to different sound servers.
![]() |
Tip |
---|---|
Use "cat /dev/urandom > /dev/audio" or speaker-test(1) to test speaker (^C to stop). |
![]() |
Tip |
---|---|
If you can not get sound, your speaker may be connected to a muted output. Modern sound system has many outputs. alsamixer(1) in the alsa-utils package is useful to configure volume and mute settings. |
Table 9.15. List of sound packages
package | popcon | size | description |
---|---|---|---|
utilities for configuring and using ALSA
OSS compatibility under ALSA preventing "/dev/dsp not found
" errors
audio and video processing engine multimedia server - metapackage
audio and video processing engine multimedia server - audio server and CLI programs
audio and video processing engine multimedia server - audio server to replace ALSA
audio and video processing engine multimedia server - audio server to replace PulseAudio
PulseAudio server
PulseAudio client library
JACK Audio Connection Kit. (JACK) server (low latency)
JACK Audio Connection Kit. (JACK) library (low latency)
GStreamer: GNOME sound engine
Phonon: KDE sound engine
Poor system maintenance may expose your system to external exploitation.
For system security and integrity check, you should start with the following.
- The
debsums
package, seedebsums
(1) and [Section 2.5.2, “Top level "Release" file and authenticity”](ch02.en.html#%5Ftop%5Flevel%5Frelease%5Ffile%5Fand%5Fauthenticity "2.5.2. Top level "Release" file and authenticity"). - The
chkrootkit
package, seechkrootkit
(1). - The
clamav
package family, seeclamscan
(1) andfreshclam
(1). - Debian security FAQ.
- Securing Debian Manual.
Table 9.18. List of tools for system security and integrity check
package | popcon | size | description |
---|---|---|---|
daemon to mail anomalies in the system logfiles to the administrator
utility to verify installed package files against MD5 checksums
rootkit detector
anti-virus utility for Unix - command-line interface
report system security vulnerabilities
file and directory integrity checker
active password cracking tool
Advanced Intrusion Detection Environment - static binary
file integrity verification program
password guessing program
Here is a simple script to check for typical world writable incorrect file permissions.
find / -perm 777 -a ! -type s -a ! -type l -a ! ( -type d -a -perm 1777 )
![]() |
Caution |
---|---|
Since the debsums package uses |
MD5 checksums stored locally, it can not be fully trusted as the system security audit tool against malicious attacks.
Booting your system with Linux live CDs or debian-installer CDs in rescue mode makes it easy for you to reconfigure data storage on your boot device.
You may need to umount
(8) some devices manually from the command line before operating on them if they are automatically mounted by the GUI desktop system.
For disk partition configuration, although fdisk
(8) has been considered standard, parted
(8) deserves some attention. "Disk partitioning data", "partition table", "partition map", and "disk label" are all synonyms.
Older PCs use the classic Master Boot Record (MBR) scheme to hold disk partitioning data in the first sector, i.e., LBA sector 0 (512 bytes).
Recent PCs with Unified Extensible Firmware Interface (UEFI), including Intel-based Macs, use GUID Partition Table (GPT) scheme to hold disk partitioning data not in the first sector.
Although fdisk
(8) has been standard for the disk partitioning tool, parted
(8) is replacing it.
![]() |
Caution |
---|---|
Although parted(8) claims to create and to resize filesystem too, it is safer to do such things using best maintained specialized tools such as mkfs(8) (mkfs.msdos(8), mkfs.ext2(8), mkfs.ext3(8), mkfs.ext4(8), …) and resize2fs(8). |
![]() |
Note |
---|---|
In order to switch between |
GPT and MBR, you need to erase first few blocks of disk contents directly (see Section 9.8.6, “Clearing file contents”) and use "parted /dev/sdx mklabel gpt
" or "parted /dev/sdx mklabel msdos
" to set it. Please note "msdos
" is use here for MBR.
LVM2 is a logical volume manager for the Linux kernel. With LVM2, disk partitions can be created on logical volumes instead of the physical harddisks.
LVM requires the following.
- device-mapper support in the Linux kernel (default for Debian kernels)
- the userspace device-mapper support library (
libdevmapper*
package) - the userspace LVM2 tools (
lvm2
package)
Please start learning LVM2 from the following manpages.
lvm
(8): Basics of LVM2 mechanism (list of all LVM2 commands)lvm.conf
(5): Configuration file for LVM2lvs
(8): Report information about logical volumesvgs
(8): Report information about volume groupspvs
(8): Report information about physical volumes
For ext4 filesystem, the e2fsprogs
package provides the following.
mkfs.ext4
(8) to create new ext4 filesystemfsck.ext4
(8) to check and to repair existing ext4 filesystemtune2fs
(8) to configure superblock of ext4 filesystemdebugfs
(8) to debug ext4 filesystem interactively. (It hasundel
command to recover deleted files.)
The mkfs
(8) and fsck
(8) commands are provided by the e2fsprogs
package as front-ends to various filesystem dependent programs (mkfs.fstype
and fsck.fstype
). For ext4 filesystem, they are mkfs.ext4
(8) and fsck.ext4
(8) (they are symlinked to mke2fs
(8) and e2fsck
(8)).
Similar commands are available for each filesystem supported by Linux.
Table 9.20. List of filesystem management packages
package | popcon | size | description |
---|---|---|---|
utilities for the ext2/ext3/ext4 filesystems
utilities for the Btrfs filesystem
utilities for the Reiserfs filesystem
utilities for the OpenZFS filesystem
utilities for the FAT filesystem. (Microsoft: MS-DOS, Windows)
utilities for the exFAT filesystem maintained by Samsung.
read/write exFAT filesystem (Microsoft) driver for FUSE.
utilities for the exFAT filesystem maintained by the exfat-fuse author.
utilities for the XFS filesystem. (SGI: IRIX)
read/write NTFS filesystem (Microsoft: Windows NT, …) driver for FUSE.
utilities for the JFS filesystem. (IBM: AIX, OS/2)
utilities for the Reiser4 filesystem
utilities for HFS and HFS Plus filesystem. (Apple: Mac OS)
program to zero free blocks from ext2/3/4 filesystems
Solid state drive (SSD) is auto detected now.
Reduce unnecessary disk accesses to prevent disk wear out by mounting "tmpfs
" on volatile data path in /etc/fstab
.
You can monitor and log your hard disk which is compliant to SMART with the smartd
(8) daemon.
- Enable SMART feature in BIOS.
- Install the
smartmontools
package. - Identify your hard disk drives by listing them with
df
(1).- Let's assume a hard disk drive to be monitored as "
/dev/hda
".
- Let's assume a hard disk drive to be monitored as "
- Check the output of "
smartctl -a /dev/hda
" to see if SMART feature is actually enabled.- If not, enable it by "
smartctl -s on -a /dev/hda
".
- If not, enable it by "
- Enable
smartd
(8) daemon to run by the following.- uncomment "
start_smartd=yes
" in the "/etc/default/smartmontools
" file. - restart the
smartd
(8) daemon by "sudo systemctl restart smartmontools
".
- uncomment "
![]() |
Tip |
---|---|
The smartd(8) daemon can be customized with the /etc/smartd.conf file including how to be notified of warnings. |
Here, we discuss manipulations of the disk image.
The disk image "partition.img
" containing a single partition image can be mounted and unmounted by using the loop device as follows.
losetup --show -f partition.img
/dev/loop0
mkdir -p /mnt/loop0
mount -t auto /dev/loop0 /mnt/loop0
...hack...hack...hack
umount /dev/loop0
losetup -d /dev/loop0
This can be simplified as follows.
mkdir -p /mnt/loop0
mount -t auto -o loop partition.img /mnt/loop0
...hack...hack...hack
umount partition.img
Each partition of the disk image "disk.img
" containing multiple partitions can be mounted by using the loop device.
losetup --show -f -P disk.img
/dev/loop0
ls -l /dev/loop0*
brw-rw---- 1 root disk 7, 0 Apr 2 22:51 /dev/loop0 brw-rw---- 1 root disk 259, 12 Apr 2 22:51 /dev/loop0p1 brw-rw---- 1 root disk 259, 13 Apr 2 22:51 /dev/loop0p14 brw-rw---- 1 root disk 259, 14 Apr 2 22:51 /dev/loop0p15
fdisk -l /dev/loop0
Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: 6A1D9E28-C48C-2144-91F7-968B3CBC9BD1
Device Start End Sectors Size Type /dev/loop0p1 262144 4192255 3930112 1.9G Linux root (x86-64) /dev/loop0p14 2048 8191 6144 3M BIOS boot /dev/loop0p15 8192 262143 253952 124M EFI System
Partition table entries are not in disk order.
mkdir -p /mnt/loop0p1
mkdir -p /mnt/loop0p15
mount -t auto /dev/loop0p1 /mnt/loop0p1
mount -t auto /dev/loop0p15 /mnt/loop0p15
mount |grep loop
/dev/loop0p1 on /mnt/loop0p1 type ext4 (rw,relatime) /dev/loop0p15 on /mnt/loop0p15 type vfat (rw,relatime,fmask=0002,dmask=0002,allow_utime=0020,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro) ...hack...hack...hack
umount /dev/loop0p1
umount /dev/loop0p15
losetup -d /dev/loop0
Alternatively, similar effects can be done by using the device mapper devices created by kpartx
(8) from the kpartx
package as follows.
kpartx -a -v disk.img
add map loop0p1 (253:0): 0 3930112 linear 7:0 262144 add map loop0p14 (253:1): 0 6144 linear 7:0 2048 add map loop0p15 (253:2): 0 253952 linear 7:0 8192
fdisk -l /dev/loop0
Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: 6A1D9E28-C48C-2144-91F7-968B3CBC9BD1
Device Start End Sectors Size Type /dev/loop0p1 262144 4192255 3930112 1.9G Linux root (x86-64) /dev/loop0p14 2048 8191 6144 3M BIOS boot /dev/loop0p15 8192 262143 253952 124M EFI System
Partition table entries are not in disk order.
ls -l /dev/mapper/
total 0 crw------- 1 root root 10, 236 Apr 2 22:45 control lrwxrwxrwx 1 root root 7 Apr 2 23:19 loop0p1 -> ../dm-0 lrwxrwxrwx 1 root root 7 Apr 2 23:19 loop0p14 -> ../dm-1 lrwxrwxrwx 1 root root 7 Apr 2 23:19 loop0p15 -> ../dm-2
mkdir -p /mnt/loop0p1
mkdir -p /mnt/loop0p15
mount -t auto /dev/mapper/loop0p1 /mnt/loop0p1
mount -t auto /dev/mapper/loop0p15 /mnt/loop0p15
mount |grep loop
/dev/loop0p1 on /mnt/loop0p1 type ext4 (rw,relatime) /dev/loop0p15 on /mnt/loop0p15 type vfat (rw,relatime,fmask=0002,dmask=0002,allow_utime=0020,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro) ...hack...hack...hack
umount /dev/mapper/loop0p1
umount /dev/mapper/loop0p15
kpartx -d disk.img
The ISO9660 image file, "cd.iso
", from the source directory tree at "source_directory
" can be made using genisoimage
(1) provided by cdrkit by the following.
genisoimage -r -J -T -V volume_id -o cd.iso source_directory
Similarly, the bootable ISO9660 image file, "cdboot.iso
", can be made from debian-installer
like directory tree at "source_directory
" by the following.
genisoimage -r -o cdboot.iso -V volume_id \
-b isolinux/isolinux.bin -c isolinux/boot.cat
-no-emul-boot -boot-load-size 4 -boot-info-table source_directory
Here Isolinux boot loader (see Section 3.1.2, “Stage 2: the boot loader”) is used for booting.
You can calculate the md5sum value and make the ISO9660 image directly from the CD-ROM device as follows.
$ isoinfo -d -i /dev/cdrom CD-ROM is in ISO 9660 format ... Logical block size is: 2048 Volume size is: 23150592 ...
dd if=/dev/cdrom bs=2048 count=23150592 conv=notrunc,noerror | md5sum
dd if=/dev/cdrom bs=2048 count=23150592 conv=notrunc,noerror > cd.iso
![]() |
Warning |
---|---|
You must carefully avoid ISO9660 filesystem read ahead bug of Linux as above to get the right result. |
Here, we discuss direct manipulations of the binary data on storage media.
The most basic viewing method of binary data is to use "od -t x1
" command.
Table 9.21. List of packages which view and edit binary data
package | popcon | size | description |
---|---|---|---|
basic package which has od
(1) to dump files (HEX, ASCII, OCTAL, …)
utility package which has hd
(1) to dump files (HEX, ASCII, OCTAL, …)
binary editor and viewer (HEX, ASCII)
full featured hexadecimal editor (GNOME)
full featured hexadecimal editor (KDE4)
binary editor and viewer (HEX, ASCII, EBCDIC)
binary editor and viewer (HEX, ASCII, EBCDIC, OCTAL, …)
Software RAID systems offered by the Linux kernel provide data redundancy in the kernel filesystem level to achieve high levels of storage reliability.
There are tools to add data redundancy to files in application program level to achieve high levels of storage reliability, too.
There are tools for data file recovery and forensic analysis.
Table 9.24. List of packages for data file recovery and forensic analysis
package | popcon | size | description |
---|---|---|---|
utilities for partition scan and disk recovery
utility to recover files by looking for magic bytes
frugal, high performance file carver
rescue data from damaged harddisks
utility to undelete files on the ext3/4 filesystem
utility to undelete files on the ext3/4 filesystem
tool to help recover deleted files on the ext3 filesystem
data recovery program for NTFS filesystems
gzip recovery toolkit
tools for forensics analysis. (Sleuthkit)
graphical interface to SleuthKit
forensics application to recover data
forensic imaging tool based on Qt
enhanced version of dd
for forensics and security
![]() |
Tip |
---|---|
You can undelete files on the ext2 filesystem using list_deleted_inodes and undel commands of debugfs(8) in the e2fsprogs package. |
With physical access to your PC, anyone can easily gain root privilege and access all the files on your PC (see Section 4.6.4, “Securing the root password”). This means that login password system can not secure your private and sensitive data against possible theft of your PC. You must deploy data encryption technology to do it. Although GNU privacy guard (see Section 10.3, “Data security infrastructure”) can encrypt files, it takes some user efforts.
Dm-crypt facilitates automatic data encryption via native Linux kernel modules with minimal user efforts using device-mapper.
![]() |
Caution |
---|---|
Data encryption costs CPU time etc. Encrypted data becomes inaccessible if its password is lost. Please weigh its benefits and costs. |
You can encrypt contents of removable mass devices, e.g. USB memory stick on "/dev/sdx
", using dm-crypt/LUKS. You simply format it as the following.
fdisk /dev/sdx
... "n" "p" "1" "return" "return" "w"
cryptsetup luksFormat /dev/sdx1
...
cryptsetup open /dev/sdx1 secret
...
ls -l /dev/mapper/
total 0 crw-rw---- 1 root root 10, 60 2021-10-04 18:44 control lrwxrwxrwx 1 root root 7 2021-10-04 23:55 secret -> ../dm-0
mkfs.vfat /dev/mapper/secret
...
cryptsetup close secret
Then, it can be mounted just like normal one on to "/media/_username/disklabel_
", except for asking password (see Section 10.1.7, “Removable storage device”) under modern desktop environment using the udisks2
package. The difference is that every data written to it is encrypted. The password entry may be automated using keyring (see Section 10.3.6, “Password keyring”).
You may alternatively format media in different filesystem, e.g., ext4 with "mkfs.ext4 /dev/mapper/sdx1
". If btrfs is used instead, the udisks2-btrfs
package needs to be installed. For these filesystems, the file ownership and permissions may need to be configured.
Debian distributes modularized Linux kernel as packages for supported architectures.
If you are reading this documentation, you probably don't need to compile Linux kernel by yourself.
Debian has its own method of compiling the kernel and related modules.
If you use initrd
in Section 3.1.2, “Stage 2: the boot loader”, make sure to read the related information in initramfs-tools
(8), update-initramfs
(8), mkinitramfs
(8) and initramfs.conf
(5).
![]() |
Warning |
---|---|
Do not put symlinks to the directories in the source tree (e.g. "/usr/src/linux*") from "/usr/include/linux" and "/usr/include/asm" when compiling the Linux kernel source. (Some outdated documents suggest this.) |
![]() |
Note |
---|---|
When compiling the latest Linux kernel on the Debian stable system, the use of backported latest tools from the Debian unstable may be needed. module-assistant(8) (or its short form m-a) helps users to build and install module package(s) easily for one or more custom kernels. The dynamic kernel module support (DKMS) is a new distribution independent framework designed to allow individual kernel modules to be upgraded without changing the whole kernel. This is used for the maintenance of out-of-tree modules. This also makes it very easy to rebuild modules as you upgrade kernels. |
The hardware driver is the code running on the main CPUs of the target system. Most hardware drivers are available as free software now and are included in the normal Debian kernel packages in the main
area.
- GPU driver
- Intel GPU driver (
main
) - AMD/ATI GPU driver (
main
) - NVIDIA GPU driver (
main
for nouveau driver, andnon-free
for binary-only drivers supported by the vendor.)
- Intel GPU driver (
The firmware is the code or data loaded on the device attach to the target system (e.g., CPU microcode, rendering code running on GPU, or FPGA / CPLD data, …). Some firmware packages are available as free software but many firmware packages are not available as free software since they contain sourceless binary data. Installing these firmware data is essential for the device to function as expected.
- The firmware data packages containing data loaded to the volatile memory on the target device.
- firmware-linux-free (
main
) - firmware-linux-nonfree (
non-free-firmware
) - firmware-linux-* (
non-free-firmware
) - *-firmware (
non-free-firmware
) - intel-microcode (
non-free-firmware
) - amd64-microcode (
non-free-firmware
)
- firmware-linux-free (
- The firmware update program packages which update data on the non-volatile memory on the target device.
- fwupd (
main
): Firmware update daemon which downloads firmware data from Linux Vendor Firmware Service. - gnome-firmware (
main
): GTK front end for fwupd - plasma-discover-backend-fwupd (
main
): Qt front end for fwupd
- fwupd (
Please note that access to non-free-firmware
packages are provided by the official installation media to offer functional installation experience to the user since Debian 12 Bookworm. The non-free-firmware
area is described in Section 2.1.5, “Debian archive basics”.
Please also note that the firmware data downloaded by fwupd from Linux Vendor Firmware Service and loaded to the running Linux kernel may be non-free
.
Use of virtualized system enables us to run multiple instances of system simultaneously on a single hardware.
There are several virtualization and emulation tool platforms.
- Complete hardware emulation packages such as ones installed by the games-emulator metapackage
- Mostly CPU level emulation with some I/O device emulations such as QEMU
- Mostly CPU level virtualization with some I/O device emulations such as Kernel-based Virtual Machine (KVM)
- OS level container virtualization with the kernel level support such as LXC (Linux Containers), Docker,
systemd-nspawn
(1), ... - OS level filesystem access virtualization with the system library call override on the file path such as chroot
- OS level filesystem access virtualization with the system library call override on the file ownership such as fakeroot
- OS API emulation such as Wine
- Interpreter level virtualization with its executable selection and run-time library overrides such as virtualenv and venv for Python
The container virtualization uses Section 4.7.5, “Linux security features” and is the backend technology of Section 7.7, “Sandbox”.
Here are some packages to help you to setup the virtualized system.
Table 9.27. List of virtualization tools
package | popcon | size | description |
---|---|---|---|
GNU core utilities which contain chroot
(8)
systemd container/nspawn tools which contain systemd-nspawn
(1)
specialized tool for executing Debian binary packages in chroot
tool for building Debian binary packages from Debian sources
bootstrap a basic Debian system (written in sh)
bootstrap a Debian system (written in C)
cloud image management utilities
cloud guest utilities
Virtual Machine Manager: desktop application for managing virtual machines
programs for the libvirt library
Incus: system container and virtual machine manager (for Debian 13 "Trixie")
LXD: system container and virtual machine manager (for Debian 12 "Bookworm")
podman: engine to run OCI-based containers in Pods
engine to run OCI-based containers in Pods - wrapper for docker
docker: Linux container runtime
games-emulator: Debian's emulators for games
Bochs: IA-32 PC emulator
QEMU: fast generic processor emulator
QEMU: full system emulation binaries
QEMU: user mode emulation binaries
QEMU: utilities
KVM: full virtualization on x86 hardware with the hardware-assisted virtualization
VirtualBox: x86 virtualization solution on i386 and amd64
Boxes: Simple GNOME app to access virtual systems
tools to manage debian XEN virtual server
Wine: Windows API Implementation (standard suite)
DOSBox: x86 emulator with Tandy/Herc/CGA/EGA/VGA/SVGA graphics, sound and DOS
Linux containers user space tools
venv for creating virtual python environments (system library)
virtualenv for creating isolated virtual python environments
pipx for installing python applications in isolated environments
See Wikipedia article Comparison of platform virtual machines for detail comparison of different platform virtualization solutions.
![]() |
Note |
---|---|
Default Debian kernels support |
KVM since lenny
.
Typical work flow for virtualization involves several steps.
- Create an empty filesystem (a file tree or a disk image).
- The file tree can be created by "
mkdir -p /path/to/chroot
". - The raw disk image file can be created with
dd
(1) (see Section 9.7.1, “Making the disk image file” and Section 9.7.5, “Making the empty disk image file”). qemu-img
(1) can be used to create and convert disk image files supported by QEMU.- The raw and VMDK file format can be used as common format among virtualization tools.
- The file tree can be created by "
- Mount the disk image with
mount
(8) to the filesystem (optional).- For the raw disk image file, mount it as loop device or device mapper devices (see Section 9.7.3, “Mounting the disk image file”).
- For disk images supported by QEMU, mount them as network block device (see Section 9.11.3, “Mounting the virtual disk image file”).
- Populate the target filesystem with required system data.
- The use of programs such as
debootstrap
andcdebootstrap
helps with this process (see Section 9.11.4, “Chroot system”). - Use installers of OSs under the full system emulation.
- The use of programs such as
- Run a program under a virtualized environment.
- chroot provides basic virtualized environment enough to compile programs, run console applications, and run daemons in it.
- QEMU provides cross-platform CPU emulation.
- QEMU with KVM provides full system emulation by the hardware-assisted virtualization.
- VirtualBox provides full system emulation on i386 and amd64 with or without the hardware-assisted virtualization.
For the raw disk image file, see Section 9.7, “The disk image”.
For other virtual disk image files, you can use qemu-nbd
(8) to export them using network block device protocol and mount them using the nbd
kernel module.
qemu-nbd
(8) supports disk formats supported by QEMU: raw, qcow2, qcow, vmdk, vdi, bochs, cow (user-mode Linux copy-on-write), parallels, dmg, cloop, vpc, vvfat (virtual VFAT), and host_device.
The network block device can support partitions in the same way as the loop device (see Section 9.7.3, “Mounting the disk image file”). You can mount the first partition of "disk.img
" as follows.
modprobe nbd max_part=16
qemu-nbd -v -c /dev/nbd0 disk.img
...
mkdir /mnt/part1
mount /dev/nbd0p1 /mnt/part1
![]() |
Tip |
---|---|
You may export only the first partition of "disk.img" using "-P 1" option to qemu-nbd(8). |
If you wish to try a new Debian environment from a terminal console, I recommend you to use chroot. This enables you to run console applications of Debian unstable
and testing
without usual risks associated and without rebooting. chroot
(8) is the most basic way.
![]() |
Caution |
---|---|
Examples below assumes both parent system and chroot system share the same amd64 CPU architecture. |
Although you can manually create a chroot
(8) environment using debootstrap
(1), this requires non-trivial efforts.
The sbuild package to build Debian packages from source uses the chroot environment managed by the schroot package. It comes with helper script sbuild-createchroot
(1). Let's learn how it works by running it as follows.
$ sudo mkdir -p /srv/chroot $ sudo sbuild-createchroot -v --include=eatmydata,ccache unstable /srv/chroot/unstable-amd64-sbuild http://deb.debian.org/debian ...
You see how debootstrap
(8) populates system data for unstable
environment under "/srv/chroot/unstable-amd64-sbuild
" for a minimal build system.
You can login to this environment using schroot
(1).
$ sudo schroot -v -c chroot:unstable-amd64-sbuild
You see how a system shell running under unstable
environment is created.
![]() |
Note |
---|---|
The "/usr/sbin/policy-rc.d" file which always exits with 101 prevents daemon programs to be started automatically on the Debian system. See "/usr/share/doc/init-system-helpers/README.policy-rc.d.gz". |
![]() |
Note |
---|---|
Some programs under chroot may require access to more files from the parent system to function than sbuild-createchroot provides as above. For example, "/sys", "/etc/passwd", "/etc/group", "/var/run/utmp", "/var/log/wtmp", etc. may need to be bind-mounted or copied. |
![]() |
Tip |
---|---|
The systemd-nspawn(1) command helps to run a command or OS in a light-weight container in similar ways to chroot. It is more powerful since it uses namespaces to fully virtualize the the process tree, IPC, hostname, domain name and, optionally, networking and user databases. See systemd-nspawn. |