Knowledge about Linux

linuxfundamentalspart1 linuxfundamentals


Linux Logo

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...

🎠 Random distros: Gentoo, QubesOS.

Linux in its simplest form is a command-line interface (CLI), commonly referred to as terminal/shell (or console/command prompt).

Linux: Terminal

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

linuxfundamentalspart1 linuxfundamentalspart2 linux1 linuxfundamentals

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

linuxfundamentalspart1 linuxfundamentalspart2 linux1 linuxfundamentals

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 the pwd 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

linuxfundamentalspart2 linuxfundamentals

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

Permissions Linux

  • The first character is the type of the file
  • The 3 following letters rw- are the permissions of u: read+write.
  • The 3 following letters r-- are the permissions of g: read.
  • The 3 following letters r-- are the permissions of o: read.
  • Ignore the 1
  • The following string listro is the name of the user u
  • The following string listro is the name of the group g
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 😎.

WildcardsDescription
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._]
GPDescriptionExamples
*

Anything

  • <nothing>
  • folder
  • myFile.txt
???

match a 3-characters string

abc

toto*

A word starting with "toto"

  • toto
  • toto1
[0-9]*
[[:digit:]]*

a word starting with a digit

0ac

[^ab]*
[!ab]*

match a string not starting with "a", nor "b"

downloads


Cron jobs

linuxfundamentalspart3 linuxfundamentals

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.

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