Knowledge about Linux
Linux is a family of Unix-like operating systems, often called distros, such as Debian, Ubuntu, Fedora, or Arch Linux... They were designed to be free, open-source, light, secure, and performant.
β‘οΈ Each distro has its pro and cons according to what specific usage they were designed for (ex: gaming, security, performance...).
π¦ Common distros: Ubuntu, Debian, Arch Linux, Red Hat, CentOS, Kali Linux, Parrot Linux, Linux Mint, Fedora...
Linux in its simplest form is a command-line interface (CLI), commonly referred to as terminal/shell (or console/command prompt).
Inside the CLI, we can run commands interpreted by a shell π. There are multiple shell languages (sh, csh, tcsh, ksh, bash), however most Linux distributions are using bash (bourne again shell) π₯.
π Customize your prompt: Bash Prompt Generator/powerline.
π Note that shell, the CLI, and a terminal are not the same thing, although they are closely related, hence the common misuse.
π You can use CTRL+ALT+F1 to swap to "console mode" (from F1, up to F6), while you can use ALT+F7 to swap back to the "GUI mode".
Commands
A command has a name, which may be followed by arguments, including some usually called flag/switches (options
). The difference between both is that a flag starts with a -
while operands do not.
Below are some examples with the command ls
:
No arguments.
$ ls
One operand.
$ ls toto.txt
$ ls "toto.txt"
Two flags β‘οΈ One flag.
$ ls -l -a
$ ls -la
One flag and one operand.
$ ls -la toto/
π Browse the manual (man
) to learn more about some command
$ man ls
π€ Use tab key to autocomplete commands/paths
$ ls /<TAB>
# will display every path starting with "/"
A few takeaways:
- π£οΈ you are writing commands right after the
$
- π press ENTER to execute a command
- π₯ press CTRL-C to cancel/kill a command
- β press CTRL-D to end the input
- π§ press CTRL-R to search in the command history
- ποΈ press CTRL-E to move the cursor to the end
- π§Ό press CTRL-U to remove everything before the cursor
- π£οΈ press ALT+B/CTRL+arrow to move between words
- π Usually, flags can be merged (ex:
-l -a
is the same as-la
). - πΊοΈ Most commands have an option "help":
-h
,-help
, or--help
File system
A path π£οΈ is a suite of one or more folders π that may lead to a regular file π. They are separated by a separator which is: /
(slash).
$ pwd
/usr/home/toto/Documents/
β‘οΈ The root folder (similar to C: on Windows) contains every other folder. It's the leading /
of every path.
β‘οΈ Each user has a home folder with their documents...
π¦ See also: Linux Filesystem and Files.
Absolute and relative paths
A path starting by the root is called absolute path. Otherwise, they are called relative path. There are 3 shortcuts used in relative paths:
-
.
(dot): will be replaced with the absolute path to the working directory. See thepwd
command. -
..
(dot dot): will be replaced with the parent folder of.
-
~
(tilde): path to user home, same as$HOME
$ pwd
/home/example
$ ./toto.txt
$ /home/example/toto.txt # same
β‘οΈ root
is its own parent (/../
is the same as /
).
π§ͺ Following slashes are merged (///
becomes /
).
Users and permissions
Permissions are assigned on a file/folder, and are applied according to the target π― of the permissions (ownership)
- u: user, applied to the user (owner)
- g: group, applied to the main group of the user (ex: students_2022)
- o: others, applied to everyone else
There are 3 well-known levels of permissions π
- r (4): can read
- w (2): can write (=can edit+save, can create)
- x (1): can execute a script, can move through a folder
Giving us something like: u=r+w, g=r, o=r
. Instead of letters, we usually use numbers, as it's shorter. We could write u=6, g=4, o=4
which can be shortened again to 644
.
groups
Each user is part of a group called after their name (e.g. the user toto
is in the group toto
). Use /etc/group
to see members of each group.
Sudo
There is a super-user π¦Έ, usually called root, that has absolute control over the machine π. They can delegate their privileges to users called sudoers. The command to execute something "as root" is:
# execute "cat [...]" as administrator
$ sudo cat /etc/shadow
β‘οΈ See man sudo_root
, and sudo
/wheel
groups.
See permissions using ls
- The first character is the type of the file
- The 3 following letters
rw-
are the permissions ofu
: read+write. - The 3 following letters
r--
are the permissions ofg
: read. - The 3 following letters
r--
are the permissions ofo
: read. - Ignore the
1
- The following string
listro
is the name of the useru
- The following string
listro
is the name of the groupg
The 3 not well-known permissions
SUID bit (on user) | SGID bit (on group) | Sticky bit (on others) | |
---|---|---|---|
File | This file will be executed using the permissions of its owner. | This file will be executed using the permissions of its group owner. | |
Folder | The group of newly created sub-folders will be the same as the folder with the SGID bit. | User can't delete files belonging to another user. | |
Add: u+s Remove: u-s Ex: -rwsr--r-- |
Add: g+s Remove: g-s Ex: -rwxr-sr-- |
Add: o+t Remove: o-t Ex: -rwxrw-rwt |
If you are giving one of these, in a context where you couldn't (such as giving s
to u
, while u
don't have x
), then the permission would be displayed in uppercase, indicating an error.
Environment variables
Environment variables (Variables dβenvironnement
) are global variables, mostly used by commands/applications to access information about the system, save configurations...
- HOME: path to the current user home
- USER: username of the current user
- LANG: language of the current user
- SHELL: path to the shell
- PWD: path to the current folder
- RANDOM: return a random value
- DISPLAY: identify display
And, there is PATH. This variable is used to store a list of folders. When you write a command on Linux, then the shell will look for the command's file in the PATH, starting from the first folder inside.
β‘οΈ On Linux, folders in the PATH are separated with :
.
Print all environment variables
$ env
$ printenv
Print the value of one environment variable
$ echo $PATH
$ printenv PATH
$ env | grep "^PATH="
Set an environment variable
$ export VAR_NAME=value
Add /home/toto/bin
to the PATH
$ export PATH=/home/toto/bin:$PATH
Glob-patterns
A glob-pattern is an expression using wildcards (motifs
), that when evaluated by the shell, will be replaced with a list of files.
For instance, *.h
will be replaced with every file -- and directory --, ending with .h
. They are mostly used on commands taking too many filenames, in which you don't want to manually write all of them π.
Wildcards | Description |
---|---|
x (a character) | the character 'x' |
* (asterisk) | a possibly empty suite of characters |
? (question mark) | one character |
[abc] | one character which is either a, b, or c. |
[^abc] [!abc] | any character which is not a, nor b, nor c. |
If you write the glob-pattern a?c
, then it could be abc
... But if you write a\?c
, or a[?]c
, then it will only match a?c
. This is called "escaping".
π Glob-patterns are pretty similar but different to Regexes.
Everything defined in []
is called a charset. If you want every character between 'a', and 'z', then you could write the charset [a-z]
. There are pre-defined charsets if needed
-
[[:digit:]]
which is[0-9]
-
[[:upper:]]
which is[A-Z]
-
[[:lower:]]
which is[a-z]
-
[[:space:]]
which is[ \n\t]
-
[[:alnum:]]
which is[a-zA-Z0-9._]
GP | Description | Examples |
---|---|---|
* |
Anything |
|
??? |
match a 3-characters string |
abc |
toto* |
A word starting with "toto" |
|
[0-9]* [[:digit:]]* |
a word starting with a digit |
0ac |
[^ab]* [!ab]* |
match a string not starting with "a", nor "b" |
downloads |
Cron jobs
We can run scheduled tasks on Linux using cron jobs. They are an alternative to System Timers.
$ crontab -l # list current user jobs
$ sudo crontab -l -u xxx # list user 'xxx' jobs
To add a new scheduled task, use -e
$ crontab -e
β‘οΈ See also: /etc/cron*
, /var/spool/cron/crontabs/*
.
The format of the file is schedule /path/to/your/script
. Schedule is made of 5 numbers whitespace-separated.
- minute (0-59)
- hour (0-23)
- day (month, 1-31)
- month (1-12)
- day (week, 0-6)
You can generate a schedule using crontab.guru or crontab-generator.
π» To-do π»
Stuff that I found, but never read/used yet.
- ELF, polkit
- Solaris
- shift+insert, middle button of the mouse, primary clipboard, most recently selected text
- linuxhandbook
- linuxhint
- linuxize
- shell-tips
- howtouselinux
- linuxconfig
Memory Swapping
- Used for memory managment
- Free inactive memory pages and give it to others
-
mkswap
: set up a swap area -
swapon
: activate a swap area