Debian Basics
About Debian
Debian is one of the most popular linux distributions and available in 75 languages. It’s the base of many other distributions, among others the well known Ubuntu. Debian can run on many different platforms from small single board computers like the Raspberry Pi up to massive supercomputers.
CLI
The Command Line Interface is an essential part of many linux distributions and describes a text based way for users to interact with the computer.
The actual application that provides such an interface is called a shell, on debian it’s usually /bin/bash
or /bin/sh
. Many alternatives such as zsh
or fish
are available.
About Files
Everything is a file
One of the most important, if not the most important concept of Unix are files. A file is more than just something that just stores bytes in Unix.
A file can be a directory, keyboard, mouse, network interface, SPI bus, framebuffer, disk and more. The advantage of this approach is that the same set of APIs or tools can be used in a wide variety of cases.
A disadvantage is that it can cause confusion amongst users not familiar with the directory structure of Unix and derivatives.
Directory Structure
Directory | Content |
---|---|
bin | Essential command binaries |
boot | Static files of the boot loader |
dev | Device files |
etc | Host-specific system configuration |
home | User home directories |
lib | Essential shared libraries and kernel modules |
media | Contains mount points for replaceable media |
mnt | Mount point for mounting a file system temporarily |
proc | Virtual directory for system information |
root | Home directory for the root user |
run | Run-time variable data |
sbin | Essential system binaries |
sys | Virtual directory for system information |
tmp | Temporary files |
usr | Contains all user programs (/usr/bin), libraries (/usr/lib) and documentation (/usr/share/doc) |
var | Variable data (e.g. /var/logs) |
srv | Data for services provided by the system |
opt | Add-on application software packages |
To be able to boot /etc, /bin, /sbin, /lib and /dev have to be available at the root partition /.
Commands
To view more information about a command use man command
or command --help
.
To cancel a command pressing Ctrl+C
is usually enough. Some commands might open a vi
style editor which might ignore Ctrl+C
. To quite out of those editors first hit ESC
to make sure you are in command mode then type :q!
and hit ENTER
.
File Commands
The directory .
always points to the current working directory (pwd
to view) while ..
points a level up. Useful for copying/moving files to the current directory without typing the full path.
ls
- List
# list files in current directory
ls
# list "a"ll files in /dev, in "l"ist format, "h"uman readable and color them
ls -alh --color=auto /dev
cd
- Change Directory
# invoking cd without arguments changes to the home directory (~)
cd
# change to /var/logs
cd /var/logs
# move up a level (e.g. from /var/logs to /var)
cd ..
cp
- Copy
# To copy a file pass the source and destination to cp
cp source.txt destination.txt
# To copy a directory the '-r' recursive flag is needed
# '/' indicates a directory but is not required
cp -r source/ destination/
mv
- Move
# Just like 'cd' pass the source and then the destination
mv source.txt destination.txt
# For directories the '-r' recursive flag is needed
mv -r source/ destination/
# move a file to a directory, keeping the name of the source file
mv source.txt destination/
rm
- Remove
# To remove a file pass the file name
rm file.txt
# To remove an empty directory use the '-r' recursive flag
rm -r empty_dir/
# If the directory isn't empty '-f' force needs to be passed aswell
rm -rf dir/ another_dir/
mkdir
- Make Directory
# Pass the name of the directory to create
mkdir new_directory/
# Using '-p' parent directories are created too
mkdir dir/subdir/
find
- Find a file or directory
# find for file.txt in the current directory
find -name file.txt
# to find a directory use '-type d'
find / -type d -name my_dir
cat
- Concatenate files to standard output
# use '>' operator to redirect stdout to a file
echo "hello" > file.txt
# using cat the content is written to stdout
cat file.txt
sudo
A very useful command which unofficially means ‘superuser do’ and is mostly used to run a command as root without having to su root
before which comes with it’s own pitfalls like different environment variables or .bashrc configuration. sudo
skips those problems by using setuid()
to impersonate the invoking user.
Since sudo
is not always bundled with debian it needs to be installed first.
# switch to root user
su root
# update available package list
apt update
# install sudo
apt install sudo
# add user 'alice' to the 'sudo' group
/sbin/usermod -a -G sudo alice
# exit root user
exit
# update sudo group
newgrp sudo
# now 'alice' should be able to use 'sudo' using the password for 'alice'
sudo echo $USER
# notice the user is still 'alice' but with the permissions of 'root'
Following command executes the last command using sudo. This is very useful if a command failed because of insufficient rights and you want to repeat it with sudo.
sudo !!
Users
adduser
- Add user
> sudo adduser bob
Adding user 'bob'
Adding new group 'bob' (1007)
Adding new user 'bob' (1005) with group 'bob'
Creating home directory '/home/bob'
Copying files from '/etc/skel'
New password: <type password>
Retype new password: <repeat password>
Changing the user information for bob
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] <press ENTER (selects capitalized option - Y)>
passwd
- Change the password of a user
> sudo passwd bob
New password: <type password>
Retype new password: <repeat password>
passwd: password updated successfully
deluser
- Delete user
# delete a user and the home directory
sudo deluser --remove-home bob
# delete a user and all associated files
sudo deluser --remove-all-files bob
Groups
Groups are often used to grant a set of permissions to a user while keeping things organized.
groupadd
- Create a group
sudo groupadd mygroup
groupmod
- Rename a group
sudo groupmod mygroup -n newgroup
groupdel
- Delete a group
sudo groupdel newgroup
Permissions
Following commands cover Unix file permissions which are limited to permissions for the owner, one group and “others”. If you need more sophisticated permissons have a look into ACL (Access Control List).
Permissions are usually displayed either as a 10 character string or a 4 digit number.
Digit Form
Displayed when using stat file.txt
0644
The digits are responsible for:
- Sticky bit, ignore that for now (0)
- Owner (7)
- Group (6)
- Other (4)
A digit consists of the sum of 3 bits which represent following permissions (subject refers to owner, group or “others”):
- Bit 0: decimal ‘1’
- Execute: Allows subject to execute the file. In case of a directory it allows traversal of the directory.
- Bit 1: decimal ‘2’
- Write: Allows subject to write to the file. In case of a directory it allows deleting, modifying and adding files.
- Bit 2: decimal ‘4’
- Read: Allows subject to read contents of the file. In case of a directory not only listing the files but also listing meta informations such as permissions is allowed.
So to break it down ‘0644’ means that:
- (0) ignored
- (7) The owner can read (4) and(+) write(2) and(+) execute(1) the file
- (6) The group can read and write the file
- (4) Others can read the file
String Form
Displayed when using ls -al
drwxr-xr-x
The first character ‘d’ describes the type of the file.
- d - directory
- c - character device
- l - symlink
- p - named pipe
- s - socket
- b - block device
- D - door (you’ll likely never encounter this)
The next 9 characters consist of blocks of 3 characters. The first block rwx
means that the owner of this file is allowed to read, write and execute the file. The following groups r-x
and r-x
mean that the assigned group and “others” are allowed to read and execute, but not write the file.
Modifying Permissions
To modify permissions of files use chmod
(change mode) and chown
(change owner). To apply the changes to subdirectories aswell add the -R
(recursive) flag.
# only the owner can read/write 'file.txt'
chmod 600 file.txt
# change the owner of the directory 'dir' and all it's subdirectories to 'bob' and the group to 'sync'
chown -R bob:sync dir/
Package Manager
As a last topic let’s quickly have a look at the most important commands to manage the applications on your system.
Pretty much all linux distros come with a package manager. This allows you to search, install, update, list and delete applications. Debian comes with 3 package managers dpkg
, apt-get
and apt
.
- dpkg: The lowest level and least user-friendly package manager provides basic functions like “install”, “list”, “remove” and “search” of local packages for the higher level package mangers
apt-get
andapt
. - apt-get: This package manager adds (remote) repository functions to install/update packages over the network.
- apt: A more user-friendly version of
apt-get
with a reduced but sufficient amount of commands, a progress bar and more.
dpkg
Essentials:
# List installed packages
sudo dpkg --list
# Install a local .deb file
sudo dpkg -i application.deb
# use this if you encounter 'dpkg: error processing package' to try and install it's dependencies
sudo apt -f install
# Remove a package
sudo dpkg -r appname
apt
Essentials:
# Update the list of available packages
sudo apt update
# Search for a package
sudo apt search htop
# Install a package
sudo apt install htop
# Remove a package
sudo apt remove htop
# Upgrade all packages
sudo apt upgrade
# Remove unused packages
sudo apt autoremove
Conclusion
Knowing the basic commands and concepts explained here you should be able to comfortably use debian on your personal computer and follow many other tutorials to customize debian to your needs.