Linux Debian

Excellent References

  • Book - [REF-3]

Basic Concept

  • Application running in user space can access kernel resources by directly invoking Linux system calls or by invoking libc (or glibc)-provided API calls. In the latter case, libc (or glibc) invokes a Linux system call.

  • Drivers in user space can directly access hardware, can access a Linux system call, or can access libc (or glibc) APIs. libc (or glibc) is an example library running in user space; there are many more such user space libraries.

  • bash is a Unix shell, a user command line interface (CLI). It is the default shell on Debian OS.

Package Management

apt is debian package management.

Debian maintains a list of links to repository that apt- or apt-get can download from. The list(s) is stored in a file /etc/apt/sources.list, and in a directory containing the list files. These files are merged at runtime to form the complete list. Therefore, the content format of these files are exactly the same.

The benefits for allowing list of files in the sources.list.d folder are: ease of configuration and automation. For example, to disable a repository that is listed on a file, you can just remove the file instead of manipulating the main list (i.e. sources.list).

Also see:

/etc

/etc is where a Linux system's configuration files live.

  • apt Repository list -

    /etc/apt/sources.list
    /etc/apt/sources.list.d/*
  • File System Table - /etc/fstab;Note: Add entry in the fstab file to persist mount point across reboot.

  • Hosts - /etc/hosts

  • rc.local - /etc/rc.local; for setting start-up program;

  • Timezone - /etc/timezone;

Networking

  • The networking folder - /proc/net/

$ ls -al /proc/net/
total 0
dr-xr-xr-x  66 gabe gabe 0 Dec  7 17:28 .
dr-xr-xr-x   9 gabe gabe 0 Dec  7 17:28 ..
-r--r--r--   1 root root 0 Dec  7 17:28 anycast6
-r--r--r--   1 root root 0 Dec  7 17:28 arp
-r--r-----   1 root root 0 Dec  7 17:28 arp_tables_matches
-r--r-----   1 root root 0 Dec  7 17:28 arp_tables_names
-r--r-----   1 root root 0 Dec  7 17:28 arp_tables_targets
dr-xr-xr-x   3 root root 0 Dec  7 17:28 bonding
[...]
  • For example, the host IP can be found in the file fib_trie

How-to

Basic info

Processor Architecture
$ dpkg --print-architecture
OS Version

Source: Check what Debian version you are running on your Linux system

$ cat /etc/os-release
$ cat /etc/debian_version
$ cat /etc/issue

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

# Alternative - not for WSL
$ uname -r
Which SHELL
$ echo $SHELL
/bin/bash
$ ls -al ~
[...]
-rw------- 1 gabe gabe      184 Jun 19 15:36 .bash_history
-rw-r--r-- 1 gabe gabe      220 Jun  3 16:13 .bash_logout
-rw-r--r-- 1 gabe gabe     3771 Jun  3 16:13 .bashrc
[...]
$

Admin

User Info (UID, GID, Group)
# complete user info
$ id <username>

# user id
$ id -u <username>

# user gid
$ id -g <username>

# all the group ids the user belongs to
$ id -G <username>

# id vs name mapping
$ cat /etc/passwd
$ cat /etc/group
Manage user (add, add to group, delete)
# add user and create the home directory for the user
# group with the same name also created
$ useradd -m <username>
$ useradd -m <username> -p <password>

# add user with group; so that group of the same name NOT created
$ useradd -m <username> -g <groupname>

# add user BUT not create home directory, and not allowed to login
$ useradd -M -L <username>

# add user with a specific id; a group of SAME "name and id" is also created 
$ useradd -M <username> -u 1883

##################################
# DELETE
##################################
# delete user with no home directory, no login
$ userdel <username>

# delete regular user with password, home dir, login, files, etc. 
$ passwd -l <username>
$ tar -zcvf /nas/backup/account/deleted/v/<username>.$uid.$now.tar.gz /home/<username/

$ pgrep -u <username>
$ ps -fp $(pgrep -u <username>)
$ killall -KILL -u <username>
$ find /var/spool/at/ -name "[^.]*" -type f -user <username> -delete
$ crontab -r -u <username>
$ lprm <username>
$ find / -user <username> -print
$ find / -user <username> -exec chown newUserName:newGroupName {} \;
$ userdel -r <username>
Manage group
# add group
$ groupadd <groupname>
$ cat /etc/group

# delete user from a group
$ deluser <username> <groupname>

# delete group
$ groupdel <groupname>
chmod and chown
# Make file executable:
$ chmod +x <filename>

# change the owner of the file
$ chown <userName> <filename>

# change the group of the file
$ chown :<groupName> <filename>

# Change both owner and group of file
$ chown <userName>:<groupName> <filename>
$ chown -R <userName>:<groupName> <foldename>
Mountpoint - Persist across boot

Add entry in the fstab file to persist mount point across reboot

Setup Environment

Date, Time, Timezone
$ date "+%H:%M:%S"

# Timezone set in 
$ /etc/timezone
Add the executable path to your PATH variable

Typically, the application is installed at the executable path ~/.local/bin. To add this path to your PATH variable:

  • The shell profile script in stored in the user folder, ~

    • Bash – .bash_profile, .profile, or .bash_login.

      Zsh – .zshrc

      Tcsh – .tcshrc, .cshrc or .login.

  • Add an export command to your profile script. The following example adds the path represented by ~/.local/bin to the current PATH variable.

$ export PATH=~/.local/bin:$PATH
$
  • Load the profile script described in the first step into your current session. The following example loads the profile script represented by PROFILE_SCRIPT

// Example for bash shell
$ source ~/PROFILE_SCRIPT
  • Echo the PATH

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
[...]
$
Setting Environment vars
// setting
export K3S_URL="https://rpik3ssvr:6443"
echo $K3S_URL

// showing var HOME
echo $HOME

The Global environment variables of your system are stored in /etc/environment. Any changes here will get reflected throughout the system and will affect all users of the system. Also, you need to reboot or source, for any changes to take effect. Eg. source /etc/environment

User level Environment variables are mostly stored in .bashrc and .profile files in the user home directory. Changes only affect that particular user. Close and reopen the terminal for configuration changes to take effect. E.g., source .bashrc

Environment variables to configure the AWS CLI

Setup SSH for putty and winscp

Configure

Program to run at boot

Old school mechanism for starting python program, svc.py at boot:

  • add it to /etc/rc.local

  • make sure rc.local runs at boot time

  • must be setup as root.

setup commands:

$ sudo chmod 755 /etc/rc.local
$ sudo nano /etc/rc.local

# in the editor; scroll to the bottom & add one line
ifup eth0
 
$ /usr/bin/python /home/svc.py &
 
exit 0

# exit nano editor CTL-X

sudo /etc/rc.local

Alternative: see "A super-simple way to run scripts on boot - crontab"

Package Management

Update & Upgrade
$ sudo apt-get -y update
$ sudo apt-get -y upgrade --no-install-recommends
$ sudo apt-get -y autoremove
List of apt repositories
$ grep ^[^#] /etc/apt/sources.list /etc/apt/sources.list.d/*

# alternative
$ cat /etc/apt/sources.list
$ ls -al /etc/apt/sources.list.d/

For raspbian, see an example of the default content of /etc/apt/sources.list

$ deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#$ deb-src http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi

For debian, see /etc/apt/sources.list.d/debian.list

$ deb http://archive.raspberrypi.org/debian/ stretch main ui
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#$ deb-src http://archive.raspberrypi.org/debian/ stretch main ui

Also see:

  • man sources.list for details

  • [REF-5] How to Add Apt Repository in Ubuntu & Debian

List of installed packages
$ dpkg -l

$ dpkg --get-selections | grep python3-picamera

# list where package install files
$ dpkg -L <packageName>
Install & Uninstall package
# Install
$ apt-get install <packageName> --no-install-recommends

# Uninstall - errors should depedencies exist
$ dpkg -r <packageName>
$ apt-get remove <packageName>

Notes:

  • Do not consider recommended packages as a dependency for installing

  • during package search, gathers the list required packages, recommended packages, and suggested packages

  • The required packages are dependencies so their installation is mandatory

  • The recommended packages will be installed by default unless the --no-install-recommends is specified or turned off in the /etc/apt/apt.conf

  • The suggested packages is not installed by default

  • Also see https://www.howtoforge.com/linux-dpkg-command/ for additional dpkg features & how-to

Search apt packages using Web-based tool

Services

List al the services
$ systemctl list-unit-files --type service -all
UNIT FILE                              STATE    VENDOR PRESET
accounts-daemon.service                enabled  enabled
[...]
Manage a service
$ systemctl status (ServiceName)
$ systemctl start (ServiceName)
$ systemctl stop (ServiceName)
$ systemctl restart(ServiceName)

# ALTERNATIVE
$ service (ServiceName) status
$ service (ServiceName) start
$ service (ServiceName) stop
$ service (ServiceName) restart
Manager service at boot
$ systemctl enable (ServiceName)
$ systemctl disable (ServiceName)
  • TODO: journalctl ;

Build-tool

Source: https://www.youtube.com/watch?v=9vsu67uMcko

  • Buildroot

  • OpenWrt

  • Yocto

Books

References

Last updated