From 3e221065aefd71dc6132c32bcd24e012e54dcaa3 Mon Sep 17 00:00:00 2001 From: Antoine Le Morvan Date: Fri, 17 Dec 2021 15:40:27 +0100 Subject: [PATCH] Import from crowdin --- docs/books/admin_guide/00-toc.fr.md | 26 + docs/books/admin_guide/01-presentation.fr.md | 303 ++++ docs/books/admin_guide/03-commands.fr.md | 1563 +++++++++++++++++ .../admin_guide/04-advanced-commands.fr.md | 356 ++++ 4 files changed, 2248 insertions(+) create mode 100644 docs/books/admin_guide/00-toc.fr.md create mode 100644 docs/books/admin_guide/01-presentation.fr.md create mode 100644 docs/books/admin_guide/03-commands.fr.md create mode 100644 docs/books/admin_guide/04-advanced-commands.fr.md diff --git a/docs/books/admin_guide/00-toc.fr.md b/docs/books/admin_guide/00-toc.fr.md new file mode 100644 index 0000000000..8cf8be6eb4 --- /dev/null +++ b/docs/books/admin_guide/00-toc.fr.md @@ -0,0 +1,26 @@ +--- +title: Apprendre Linux avec Rocky +--- + +# Apprendre Linux avec Rocky + +Le Guide d'administration est une collection de documents éducatifs destinés aux administrateurs systèmes. Ils peuvent être utilisés par les futurs administrateurs systèmes qui veulent se mettre à niveau, par les administrateurs systèmes actuels qui souhaitent se rafraîchir la mémoire, ou par tout utilisateur de Linux qui souhaite en savoir plus sur l'environnement, les commandes, les processus etc. de Linux. Comme tous les documents de ce type, il évoluera et sera mis à jour au fil du temps. + +Dans un premier temps, nous parlerons de Linux, des distributions, et de tout l'écosystème autour de notre système d'exploitation. + +Nous nous pencherons ensuite sur les commandes utilisateurs qui sont essentielles pour se familiariser avec Linux. Les utilisateurs les plus expérimentés pourront également consulter le chapitre consacré aux commandes "plus avancées". + +Vient ensuite le chapitre sur l'éditeur VI. Si Linux est livré avec de nombreux éditeurs, VI est l'un des plus puissants. D'autres commandes utilisent parfois des syntaxes identiques à celles de VI (on pensera notamment à `sed`). Il est donc très important de connaître VI, ou du moins de démystifier ses fonctions essentielles (comment ouvrir un fichier, enregistrer, quitter ou quitter sans enregistrer). L'utilisateur deviendra plus à l'aise avec les autres fonctions de VI au fur et à mesure qu'il utilisera l'éditeur. Une alternative serait d'utiliser nano qui est installé par défaut dans Rocky Linux. Bien qu'il ne soit pas aussi polyvalent, il est simple à utiliser, direct, et fait le travail. + +Nous pourrons ensuite entrer dans le fonctionnement profond de Linux pour découvrir comment le système gère : + +* les utilisateurs. +* les systèmes de fichiers. +* les processus. + +Ensuite, nous aborderons un sujet essentiel pour l'administrateur systèmes : Le processus de sauvegarde. De nombreuses solutions logicielles sont fournies avec Linux pour améliorer les sauvegardes (rsnapshot, lsyncd, etc..). Il est bon de connaître les composants essentiels de la sauvegarde qui se trouvent dans le système d'exploitation. Nous allons étudier deux outils : `tar` et le moins répandu `cpio` dans ce chapitre. + +La gestion du système pendant le processus de démarrage a beaucoup évolué ces dernières années depuis l'arrivée de systemd. + + +Nous terminerons en examinant la gestion des tâches, la mise en place du réseau et l'installation des logiciels. diff --git a/docs/books/admin_guide/01-presentation.fr.md b/docs/books/admin_guide/01-presentation.fr.md new file mode 100644 index 0000000000..0b3b456a2c --- /dev/null +++ b/docs/books/admin_guide/01-presentation.fr.md @@ -0,0 +1,303 @@ +--- +title: Introduction à Linux +--- + +# Introduction au système d'exploitation Linux + +Dans ce chapitre, vous allez en apprendre plus à propos des distributions GNU/Linux. + +**** + +**Objectifs : **Dans ce chapitre, vous apprendrez à : + +:heavy_check_mark: Décrire les caractéristiques et les architectures possibles d'un système d'exploitation. +:heavy_check_mark: Retracer l'histoire d'UNIX et de GNU/Linux +:heavy_check_mark: Choisir la distribution Linux adaptée à ses besoins +:heavy_check_mark: Expliquer la philosophie des logiciels libres et open source +:heavy_check_mark: Découvrir l'utilité de la SHELL. + +:checkered_flag: **généralités**, **linux**, **distributions** + +**Connaissances : ** :star: +**Complexité : ** :star: + +**Temps de lecture : **10 minutes + +**** + +## Qu'est-ce qu'un système d'exploitation ? + +Linux, UNIX, BSD, Windows et MacOS sont tous des **systèmes d'exploitation**. + +!!! abstract Un système d'exploitation est un ensemble de programmes qui gèrent les ressources disponibles d'un ordinateur. + +Parmi cette gestion des ressources, le système d'exploitation doit : + +* Gérer la mémoire physique ou virtuelle. + * La **mémoire physique** est constituée des barrettes de RAM et de la mémoire cache du processeur, utilisées pour l'exécution des programmes. + * La **mémoire virtuelle** est un emplacement sur le disque dur (la partition **swap**) qui permet de décharger la mémoire physique et de sauvegarder l'état actuel du système lors de l'arrêt électrique de l'ordinateur. +* Intercepter les **accès aux périphériques**. Les logiciels sont rarement autorisés à accéder directement au matériel (sauf les cartes graphiques pour des besoins très spécifiques). +* Fournir aux applications une **gestion correcte des tâches**. Le système d'exploitation est responsable de la planification des processus pour occuper le processeur. +* **Protéger les fichiers** contre les accès non autorisés. +* **Collecter des informations** sur les programmes utilisés ou en cours d'exécution. + +![Operation of an operating system](images/operating_system.png) + +## Généralités UNIX - GNU/Linux + +### Histoire + +#### UNIX + +* De **1964 à 1968** : MULTICS (MULTiplexed Information and Computing Service) est développé pour le MIT, Bell Labs (AT&T) et General Electric. + +* **1969** : Après le retrait de Bell (1969) puis de General Electric du projet, deux développeurs (Ken Thompson et Dennis Ritchie), rejoint plus tard par Brian Kernighan, jugeant MULTICS trop complexe, ont lancé le développement de UNIX (UNiplexed Information and Computing Service). Développé à l'origine en assembleur, les concepteurs d'UNIX développent le langage B puis le langage C (1971) et réécrivent complètement UNIX. Ayant été développé en 1970, la date de référence des systèmes UNIX/Linux est toujours fixée au 1er janvier 1970. + +Le langage C est encore l'un des langages de programmation les plus populaires aujourd'hui! Un langage de bas niveau, proche du matériel, il permet d'adapter le système d'exploitation à toute architecture de machine ayant un compilateur C. + +UNIX est un système d'exploitation ouvert et évolutif qui a joué un rôle majeur dans l'histoire de l'informatique. Il a été la base de beaucoup d'autres systèmes : Linux, BSD, MacOS, etc. + +UNIX est toujours d'actualité aujourd'hui (HP-UX, AIX, Solaris, etc.) + +#### Minix + +* **1987** : A.S. Tanenbaum développe MINIX, un UNIX simplifié, pour enseigner les systèmes d'exploitation de manière simple. M. Tanenbaum met à disposition le code source de son système d'exploitation. + +#### Linux + +* **1991** : Un étudiant finlandais, **Linus Torvalds**, crée un système d'exploitation dédié à son ordinateur personnel et le nomme Linux. Il publie sa première version 0.02, sur le forum de discussion Usenet et d'autres développeurs viennent l'aider à améliorer son système. Le terme Linux est un jeu de mots entre le prénom du fondateur, Linus, et UNIX. + +* **1993** : La distribution Debian est créée. Debian est une distribution non commerciale basée sur la communauté. Développé à l'origine pour être utilisée sur les serveurs, elle est particulièrement adaptée à ce rôle, mais elle est destinée à être un système universel et donc utilisable sur un ordinateur personnel également. Debian est utilisée comme base pour de nombreuses autres distributions, comme Mint ou Ubuntu. + +* **1994** : La distribution commerciale RedHat est créée par la société RedHat, qui est aujourd'hui le premier distributeur du système d'exploitation GNU/Linux. RedHat prend en charge la version communautaire Fedora et récemment la distribution gratuite CentOS. + +* **1997** : L'environnement de bureau KDE est créé. Il est basé sur la bibliothèque de composants Qt et le langage de développement C++. + +* **1999** : L'environnement de bureau Gnome est créé. Il est basé sur la bibliothèque de composants GTK+. + +* **2002** : La distribution Arch est créée. Sa particularité est d'être diffusée en Rolling Release (mise à jour continue). + +* **2004** : Ubuntu est créé par la compagnie Canonical (Mark Shuttleworth). Elle est basé sur Debian, mais inclut des logiciels libres et propriétaires. + +* **2021** : Naissance de Rocky Linux, basée sur la distribution RedHat. + +### Part de marché + + + +Linux is still not well known by the general public, even though they use it regularly. Indeed, Linux is hidden in **smartphones**, **televisions**, **internet boxes**, etc. Almost **70% of the web pages** served in the world are served by a Linux or UNIX server! + +Linux equips a little more than **3% of personal computers** but more than **82% of smartphones**. **Android** being an operating system whose kernel is a Linux. + + + +Linux equips 100% of the 500 supercomputers since 2018. A supercomputer is a computer designed to achieve the highest possible performance with the techniques known at the time of its design, especially with regard to computing speed. + +### Architectural design + +* The **kernel** is the first software component. + * It is the heart of the Linux system. + * It manages the hardware resources of the system. + * The other software components must go through it to access the hardware. +* The **shell** is a utility that interprets user commands and ensures their execution. + * Main shells: Bourne shell, C shell, Korn shell and Bourne-Again shell (bash). +* Applications are user programs such as : + * Internet browser ; + * the word processor ; + * ... + +#### Multitask + +Linux belongs to the family of time-sharing operating systems. It shares process time between several programs, switching from one to another in a transparent way for the user. This implies: + +* simultaneous execution of several programs; +* distribution of CPU time by the scheduler; +* reduction of problems due to a failed application; +* reduced performance when there are too many programs running. + +#### Multi user + +The purpose of Multics was to allow several users to work from several terminals (screen and keyboard) on a single computer (very expensive at the time). Linux, which is inspired by this operating system, has kept this ability to work with several users simultaneously and independently, each one having its own user account, memory space and access rights to files and software. + +#### Multiprocessor + +Linux is able to work with multi-processor computers or with multi-core processors. + +#### Multi platform + +Linux is written in a high-level language that can be adapted to different types of platforms during compilation. It therefore runs on : + +* home computers (PC or laptop); +* servers (data, applications,...); +* portable computers (smartphones or tablets) +* embedded systems (car computer); +* active network elements (routers, switches) +* household appliances (TVs, refrigerators,...). + +#### Open + +Linux is based on recognized standards [posix](http://fr.wikipedia.org/wiki/POSIX), TCP/IP, NFS, Samba ... allowing to share data and services with other application systems. + +### The UNIX/Linux philosophy + +* Everything is a file. +* Portability. +* Do only one thing and do it well. +* KISS: Keep It Simple Stupid. +* "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." (__Dennis Ritchie__) +* "Unix is user-friendly. It just isn't promiscuous about which users it's friendly with." (__Steven King__) + +## The GNU/LINUX distributions + +A Linux distribution is a **consistent set of software** assembled around the Linux kernel and ready to be installed along with the necessary components to manage this software (installation, removal, configuration). There are **associative or community** distributions (Debian, Rocky) or **commercial** (RedHat, Ubuntu). + +Each distribution offers one or more **desktop environments**, provides a set of pre-installed software and a library of additional software. Configuration options (kernel or services options for example) are specific to each one. + +This principle allows you to have distributions oriented to **beginners** (Ubuntu, Linux Mint...), to have a more complex approach (Gentoo, Arch), to be focused more towards **servers** (Debian, Redhat), or to be dedicated towards **workstations**. + +### Desktop environments + +There are many graphic environments: **Gnome**, **KDE**, **LXDE**, **XFCE**, etc. There is something for everyone, and their **ergonomics** have nothing to be ashamed of when compared to Microsoft or Apple systems! + +So why is there so little enthusiasm for Linux, when **there are no (or almost no) viruses for this system**? Maybe because all editors (Adobe) or manufacturers (Nvidia) do not play the free game and do not provide a version of their software or their __drivers__ for GNU/Linux? Fear of change? The difficulty to find where to buy a Linux computer? Too few games (but not for long) distributed under Linux? Will the situation change with the arrival of the steam-box game console that runs on Linux? + +![Gnome Desktop](images/01-presentation-gnome.png) + +The **Gnome 3** desktop environment no longer uses the concept of Desktop but that of Gnome Shell (not to be confused with the command line shell). It serves as a desktop, a dashboard, a notification area and a window selector. The Gnome desktop environment is based on the GTK+ component library. + +![KDE Desktop](images/01-presentation-kde.png) + +The **KDE** desktop environment is based on the **Qt** component library. + +It is traditionally more recommended for users coming from a Windows world. + +![Tux - The Linux mascot](images/tux.png) + +### Free / Open source + +A user of a Microsoft or Mac operating system must purchase a license to use the operating system. This license has a cost, although it is usually transparent (the price of the license is included in the price of the computer). + +In the **GNU/Linux** world, the Free Software movement provides mostly free distributions. + +**Free** does not mean free! + +**Open source**: the source codes are available, so it is possible to consult and modify them under certain conditions. + +A free software is necessarily Open Source, but the opposite is not true since an Open Source software is separated from the freedom proposed by the GPL license. + +#### GPL License (General Public License) + +The **GPL License** guarantees the author of a software its intellectual property, but allows modification, redistribution or resale of software by third parties, provided that the source codes are provided with the software. The GPL is the license that came out of the **GNU** (GNU is Not UNIX) project, which was instrumental in creating Linux. + +It implies : + +* the freedom to run the program, for any purpose; +* the freedom to study how the program works and adapt it to your needs +* the freedom to redistribute copies; +* the freedom to improve the program and publish your improvements, for the benefit of the whole community. + +On the other hand, even products licensed under the GPL can be paid for. This is not the product itself, but the guarantee that a team of developers will continue to work on it to make it evolve and troubleshoot errors, or even provide support to users. + +## Areas of use + +A Linux distribution excels for : + +* **A server**: HTTP, email, groupware, file sharing, etc. +* **Security**: Gateway, firewall, router, proxy, etc. +* **Central computer**: Banks, insurance, industry, etc. +* **Embedded system**: Routers, Internet boxes, SmartTV, etc. + +Linux is a suitable choice for hosting databases or websites, or as a mail server, DNS or firewall. In short, Linux can do just about anything, which explains the quantity of specific distributions. + +## Shell + +### Generalities + +The **shell**, known as _command interface_, allows users to send commands to the operating system. It is less visible today, since the implementation of graphical interfaces, but remains a privileged means on Linux systems which do not all have graphical interfaces and whose services do not always have a setting interface. + +It offers a real programming language including the classical structures: loops, alternatives, and the common constituents: variables, passing of parameters, and sub-programs. It allows the creation of scripts to automate certain actions (backups, creation of users, system monitoring, etc.). + +There are several types of shells available and configurable on a platform or according to the user's preference: + +* sh, the POSIX standard shell ; +* csh, command-oriented shell in C ; +* bash, Bourne-Again Shell, Linux shell. +* etc, ... + +## Functionalities + +* Command execution (checks the command given and executes it); +* Input/Output redirection (returns data to a file instead of writing it on the screen); +* Connection process (manages the user's connection); +* Interpreted programming language (allowing the creation of scripts); +* Environment variables (access to information specific to the system during operation). + +### Principle + +![Operating principle of the SHELL](images/shell-principle.png) + +## Check your Knowledge + +:heavy_check_mark: An operating system is a set of programs for managing the available resources of a computer: + +- [ ] True +- [ ] False + +:heavy_check_mark: The operating system is brought to: + +- [ ] Manage physical and virtual memory +- [ ] Allow direct access to peripherals +- [ ] Subcontract the management of tasks to the processor +- [ ] Collect information about the programs used or in use + +:heavy_check_mark: Among these personalities, which ones participated in the development of UNIX: + +- [ ] Linus Torvalds +- [ ] Ken Thompson +- [ ] Lionel Richie +- [ ] Brian Kernighan +- [ ] Andrew Stuart Tanenbaum + +:heavy_check_mark: The original nationality of Linus Torvalds, creator of the Linux kernel, is: + +- [ ] Swedish +- [ ] Finnish +- [ ] Norwegian +- [ ] Flemish +- [ ] French of course + +:heavy_check_mark: Which of the following distributions is the oldest: + +- [ ] Debian +- [ ] Slackware +- [ ] RedHat +- [ ] Arch + +:heavy_check_mark: Is the Linux kernel: + +- [ ] Multitasking +- [ ] Multi user +- [ ] Multiprocessor +- [ ] Multi-core +- [ ] Cross-platform +- [ ] Open + +:heavy_check_mark: Is free software necessarily open source? + +- [ ] True +- [ ] False + +:heavy_check_mark: Is Open Source software necessarily free? + +- [ ] True +- [ ] False + +:heavy_check_mark: Which of the following is not a shell: + +- [ ] Jason +- [ ] Jason-Bourne shell (jbsh) +- [ ] Bourne-Again shell (bash) +- [ ] C shell (csh) +- [ ] Korn shell (ksh) diff --git a/docs/books/admin_guide/03-commands.fr.md b/docs/books/admin_guide/03-commands.fr.md new file mode 100644 index 0000000000..487100e818 --- /dev/null +++ b/docs/books/admin_guide/03-commands.fr.md @@ -0,0 +1,1563 @@ +--- +title: Linux Commands +author: Antoine Le Morvan +contributors: Steven Spencer, Aditya Putta +update: 11-08-2021 +--- + +# Commands for Linux Users + +In this chapter you will learn how to work with Linux with the commands. + +**** + +**Objectives** : In this chapter, future Linux administrators will learn how to: + +:heavy_check_mark: **move** in the system tree; +:heavy_check_mark: **create** a text file, **display** its contents and **modify** it; +:heavy_check_mark: use the most useful Linux commands. + +:checkered_flag: **user commands**, **linux** + +**Connaissances : ** :star: +**Complexité : ** :star: + +**Temps de lecture : **40 minutes + +**** + +## Generalities + +Current Linux systems have graphical utilities dedicated to the work of an administrator. However, it is important to be able to use the interface in command line mode for several reasons: + +* The majority of system commands are common to all Linux distributions, which is not the case for graphical tools. +* It can happen that the system does not start correctly but that a backup command interpreter remains accessible. +* Remote administration is done on the command line with an SSH terminal. +* In order to preserve server resources, the graphical interface is either not installed or launched on demand. +* Administration is done by scripts. + +Learning these commands allows the administrator to connect to a Linux terminal, to manage its resources, its files, to identify the station, the terminal, and the connected users, etc. + +### The users + +The user of a Linux system is defined in the `/etc/passwd` file, by: + +* a **login name**, more commonly called "login", containing no spaces; +* a numeric identifier : **UID** (User Identifier); +* a group identifier : **GID** (Group Identifier); +* a **command interpreter**, a shell, which can be different from one user to another; +* a **connection directory**, the __home directory__. + +In other files by: + +* a **password**, which will be encrypted before being stored (`/etc/shadow`); +* a **command prompt**, or __prompt__ login, which will be symbolized by a `#` for administrators and a `$` for other users (`/etc/profile`). + +Depending on the security policy implemented on the system, the password will have to contain a certain number of characters and meet certain complexity requirements. + +Among the existing command interpreters, the **Bourne-Again Shell** (`/bin/bash`) is the one most frequently used. It is assigned by default to new users. For various reasons, advanced Linux users can choose alternative shells from among the Korn Shell (`ksh`), the C Shell (`csh`), etc. + +The user's login directory is by convention stored in the `/home` directory of the workstation. It will contain the user's personal data and the configuration files of his applications. By default, at login, the login directory is selected as the current directory. + +A workstation type installation (with graphical interface) starts this interface on terminal 1. Linux being multi-user, it is possible to connect several users several times, on different **physical terminals** (TTY) or **virtual terminals** (PTS). Virtual terminals are available within a graphical environment. A user switches from one physical terminal to another using Alt + Fx from the command line or using CTRL + Alt + Fx. + +### The shell + +Once the user is connected to a console, the shell displays the command **prompt**. It then behaves like an infinite loop, with each statement entered: + +* displaying the command prompt; +* reading the command ; +* syntax analysis ; +* substitution of special characters ; +* execute the command; +* display the command prompt; +* etc. + +The key sequence CTRL + C is used to interrupt a running command. + +The use of a command generally follows this sequence: + +```bash +command [option(s)] [arguments(s)] +``` + +The name of the command is **often in lower case**. + +A space separates each item. + +Short **options** begin with a dash (`-l`), while **long options** begin with two dashes (`--list`). A double dash (`--`) indicates the end of the option list. + +It is possible to group some short options together: + +```bash +$ ls -l -i -a +``` + +is equivalent to: + +```bash +$ ls -lia +``` + +There can of course be several arguments after an option: + +```bash +$ ls -lia /etc /home /var +``` + +In the literature, the term "option" is equivalent to the term "parameter," which is more commonly used in programming. The optional side of an option or argument is symbolized by enclosing it in square brackets `[` and `]`. When more than one option is possible, a vertical bar called a "pipe" separates them `[a|e|i]`. + +## General commands + +### `apropos`, `whatis` and `man` commands + +It is impossible for an administrator at any level to know all the commands and options in detail. A manual is usually available for all installed commands. + +#### `apropos` command + +The command `apropos` allows you to search by keyword within these manual pages: + +| Options | Observations | +| ------------------------------------------- | ---------------------------------------------------------- | +| `-s`, `--sections list` or `--section list` | Limited to manual sections. | +| `-a` or `--and` | Displays only the item matching all the provided keywords. | + +Example: + +```bash +$ apropos clear +clear (1) - clear the terminal screen +clear_console (1) - clear the console +clearenv (3) - clear the environment +clearerr (3) - check and reset stream status +clearerr_unlocked (3) - nonlocking stdio functions +feclearexcept (3) - floating-point rounding and exception handling +fwup_clear_status (3) - library to support management of system firmware updates +klogctl (3) - read and/or clear kernel message ring buffer; set console_loglevel +sgt-samegame (6) - Block-clearing puzzle +syslog (2) - read and/or clear kernel message ring buffer; set console_loglevel +timerclear (3) - timeval operations +XClearArea (3) - clear area or window +XClearWindow (3) - clear area or window +XSelectionClearEvent (3) - SelectionClear event structure +``` + +To find the command that will allow changing the password of an account: + +```bash +$ apropos --exact password -a change +chage (1) - change user password expiry information +passwd (1) - change user password +``` + +#### `whatis` command + +The `whatis` command displays the description of the command passed as argument: + +```bash +whatis clear +``` + +Example: + +```bash +$ whatis clear +clear (1) - clear the terminal screen +``` + +#### `man` command + +Once found by `apropos` or `whatis`, the manual is read by `man` ("Man is your friend"). This set of manuals is divided into 8 sections, grouping information by topic, the default section being 1: + +1. User Commands; +2. System Calls; +3. C library functions; +4. Peripherals and special files; +5. File formats ; +6. Games; +7. Miscellaneous; +8. System administration tools and daemons. Information about each section can be accessed by typing `man x intro`, where `x` is the section number. + +The command: + +```bash +man passwd +``` + +will tell the administrator about the passwd command, its options, etc. While a: + +```bash +$ man 5 passwd +``` + +will inform him about the files related to the command. + +Not all pages of the manual are translated from English. However, they are generally very accurate and provide all the information needed. The syntax used and the division may confuse the beginner administrator, but with practice, he will quickly find the information he is looking for. + +The navigation in the manual is done with the arrows and . The manual is exited by pressing the q key. + +### `shutdown` command + +The `shutdown` command allows you to **electrically shut down** a Linux server, either immediately or after a certain period of time. + +```bash +shutdown [-h] [-r] time [message] +``` + +The shutdown time should be specified in the format `hh:mm` for a precise time, or `+mm` for a delay in minutes. + +To force an immediate stop, the word `now` will replace the time. In this case, the optional message is not sent to other users of the system. + +Examples: + +```bash +[root]# shutdown -h 0:30 "Server shutdown at 0:30" +[root]# shutdown -r +5 +``` + +Options: + +| Options | Remarks | +| ------- | -------------------------------- | +| `-h` | Shutdown the system electrically | +| `-r` | Restarts the system | + +### `history` command + +The `history` command displays the history of commands that have been entered by the user. The commands are stored in the `.bash_history` file in the user's login directory. + +Example of a history command + +```bash +$ history +147 man ls +148 man history +``` + +| Options | Comments | +| ------- | ------------------------------------------------------------------------------------------------------------------ | +| `-w` | The `-w` option will copy the history of the current session to it. | +| `-c` | The `-c` option will delete the history of the current session (but not the contents of the `.bash_history` file). | + +* Manipulating history: + +To manipulate the history, the following commands entered from the command prompt will: + +| Keys | Function | +| ------------------ | --------------------------------------------------------- | +| !! | Recall the last command placed. | +| !n | Recall the command by its number in the list. | +| !string | Recall the most recent command beginning with the string. | +| | Recall the most recent command beginning with the string. | +| | Recall the most recent command beginning with the string. | + +### The auto-completion + +Auto-completion is also a great help. + +* It allows you to complete commands, entered paths, or file names. +* A press of the TAB key completes the entry in the case of a single solution. +* Otherwise, a second press will be required to obtain the list of possibilities. + +If a double press of the TAB key causes no reaction from the system, then there is no solution to the current completion. + +## Display and identification + +### `clear` command + +The `clear` command clears the contents of the terminal screen. In fact, to be more precise, it shifts the display so that the command prompt is at the top of the screen on the first line. + +In a terminal, the display will be permanently hidden, whereas in a graphical interface, a scrollbar will allow you to go back in the history of the virtual terminal. + +!!! Tip CTRL + L will have the same effect as the `clear` command + +### `echo` command + +The `echo` command is used to display a string of characters. + +This command is most commonly used in administration scripts to inform the user during execution. + +The `-n` option will not return to the line after displaying the text (which is the default behavior of the command). + +For various reasons, the script developer may need to use special sequences (starting with a `\` character). In this case, the `-e` option will be stipulated, allowing interpretation of the sequences. + +Among the frequently used sequences, we can mention: + +| Sequence | Result | +| -------- | --------------------- | +| `\a` | Send a sonor bip | +| `\b` | Back | +| `\n` | Adds a line break | +| `\t` | Adds a horizontal tab | +| `\v` | Adds vertical tab | + +### `date` command + +The `date` command displays the date and time. The command has the following syntax: + +```bash +date [-d AAAAMMJJ] [format] +``` + +Examples: + +```bash +$ date +Mon May 24 16:46:53 CEST 2021 +$ date -d 20210517 +%j +137 +``` + +In this last example, the `-d` option displays a given date. The `+%j` option formats this date to show only the day of the year. + +!!! Warning The format of a date can change depending on the value of the language defined in the environment variable `$LANG`. + +The date display can follow the following formats: + +| Option | Format | +| ------ | -------------------------------- | +| `+%A` | Full Name of Day | +| `+%B` | Full Name of Month | +| `+%c` | Full Date Display | +| `+%d` | Day number | +| `+%F` | Date in `YYYY-MM-DD` format | +| `+%G` | Year | +| `+%H` | Time of day | +| `+%j` | Day of the year | +| `+%m` | Month number | +| `+%M` | Minute | +| `+%R` | Time in `hh:mm` format | +| `+%s` | Seconds since January 1, 1970 | +| `+%T` | Time in `hh:mm:ss` format | +| `+%u` | Day of the week (`1` for Monday) | +| `+%V` | Week number (`+%V`) | +| `+%x` | Date in format `DD/MM/YYYY` | + +The `date` command also allows you to change the system date and time. In this case, the `-s` option will be used. + +```bash +[root]# date -s "2021-05-24 10:19" +``` + +The format to be used following the `-s` option is this: + +```bash +date -s "[AA]AA-MM-JJ hh:mm:[ss]" +``` + +### `id`, `who` and `whoami` commands + +The `id` command displays the name of the current user and its groups or those of a user, if the user's login is given as an argument. + +```bash +$ id rockstar +uid=1000(rockstar) gid=1000(rockstar) groups=1000(rockstar),10(wheel) +``` + +The `-g`, `-G`, `-n` and `-u` options display the main group GID, subgroup GIDs, names instead of numeric identifiers, and the user's UID respectively. + +The `whoami` command displays the login of the current user. + +The `who` command alone displays the names of logged in users: + +```bash +$ who +rockstar tty1 2021-05-24 10:30 +root pts/0 2021-05-24 10:31 +``` + +Since Linux is multi-user, it is likely that multiple sessions are open on the same station, either physically or over the network. It is interesting to know which users are logged in, if only to communicate with them by sending messages. + +* tty: represents a terminal. +* pts/: represents a virtual console in a graphical environment with the number after representing the instance of the virtual console (0, 1, 2...) + +The `-r` option also displays the execution level (see chapter "startup"). + +## File tree + +In Linux, the file tree is an inverted tree, called a **single hierarchical tree**, whose root is the directory `/`. + +The **current directory** is the directory where the user is located. + +The **connection directory** is the working directory associated with the user. The login directories are, by default, stored in the `/home` directory. + +When the user logs in, the current directory is the login directory. + +An **absolute path** references a file from the root by traversing the entire tree to the file level: + +* `/home/groupA/alice/file` + +The **relative path** references that same file by traversing the entire tree from the current directory: + +* `../alice/file` + +In the above example, the "`..`" refers to the parent directory of the current directory. + +A directory, even if it is empty, will necessarily contain at least **two references**: + +* `.`: reference to itself. +* `..`: reference to the parent directory of the current directory. + +A relative path can thus start with `./` or `../`. When the relative path refers to a subdirectory or file in the current directory, then the `./` is often omitted. Mentioning the first `./` in the tree will only really be required to run an executable file. + +Errors in paths can cause many problems: creating folders or files in the wrong places, unintentional deletions, etc. It is therefore strongly recommended to use auto-completion when entering paths. + +![our example tree](images/commands-pathabsolute.png) + +In the above example, we are looking to give the location of the file `myfile` from the directory of bob. + +* By an **absolute path**, the current directory does not matter. We start at the root, and work our way down to the directories `home`, `groupA`, `alice` and finally the file `myfile`: `/home/groupA/alice/myfile`. +* By a **relative path**, our starting point being the current directory `bob`, we go up one level through `..` (i.e., into the `groupA` directory), then down into the alice directory, and finally the `myfile` file: `../alice/myfile`. + +### `pwd` command + +The `pwd` (Print Working Directory) command displays the absolute path of the current directory. + +```bash +$ pwd +/home/rockstar +``` + +To move around using a relative path, you must know its position in the tree. + +Depending on the command interpreter, the command prompt may also display the name of the current directory. + +### `cd` command + +The `cd` (Change Directory) command allows you to change the current directory, in other words, to move through the tree. + +```bash +$ cd /tmp +$ pwd +/tmp +$ cd ../ +$ pwd +/ +$ cd +$ pwd +/home/rockstar +``` + +As you can see in the last example above, the command `cd` with no arguments moves the current directory to the `home directory`. + +### `ls` command + +The `ls` command displays the contents of a directory + +```bash +ls [-a] [-i] [-l] [directory1] [directory2] […] +``` + +Example: + +```bash +$ ls /home +. .. rockstar +``` + +The main options of the `ls` command are: + +| Option | Information | +| ------ | ---------------------------------------------------------------------------------------------------- | +| `-a` | Displays all files, even hidden ones. Hidden files in Linux are those beginning with `.`. | +| `-i` | Displays inode numbers. | +| `-l` | The `-l` command displays a vertical list of files with additional information formatted in columns. | + +The `ls` command, however, has a lot of options (see `man`): + +| Option | Information | +| ------ | ------------------------------------------------------------------------------------------------------------------------------------ | +| `-d` | Displays information about a directory instead of listing its contents. | +| `-g` | Displays UID and GID rather than owner names. | +| `-h` | Displays file sizes in the most appropriate format (byte, kilobyte, megabyte, gigabyte, ...). `h` stands for Human Readable. | +| `-s` | Displays the size in bytes (unless `k` option). | +| `-A` | Displays all files in the directory except `.` and `.`. | +| `-R` | Displays the contents of subdirectories recursively. | +| `-F` | Displays the type of files. Prints a `/` for a directory, `*` for executables, `@` for a symbolic link, and nothing for a text file. | +| `-X` | Sort files according to their extensions. | + +* Description of columns: + +```bash +$ ls -lia /home +78489 drwx------ 4 rockstar rockstar 4096 25 oct. 08:10 rockstar +``` + +| Value | Information | +| --------------- | ------------------------------------------------------------------------------------------------------------- | +| `78489` | Inode Number. | +| `drwx------` | File type (`d`) and rights (`rwx------`). | +| `4` | Number of subdirectories (`.` and `..` included). For a file of type physical link: number of physical links. | +| `rockstar` | For a physical link file: number of physical links. | +| `rockstar` | For a file of type physical link: number of physical links. | +| `4096` | For a physical link type file: number of physical links. | +| `25 oct. 08:10` | Last modified date. | +| `rockstar` | The name of the file (or directory). | + +!!! Note **Aliases** are frequently positioned in common distributions. + + This is the case of the alias `ll`: + + ``` + alias ll='ls -l --color=auto' + ``` + +The `ls` command has many options and here are some advanced examples of uses: + +* List the files in `/etc` in order of last modification: + +```bash +$ ls -ltr /etc +total 1332 +-rw-r--r--. 1 root root 662 29 may 2021 logrotate.conf +-rw-r--r--. 1 root root 272 17 may. 2021 mailcap +-rw-------. 1 root root 122 12 may. 2021 securetty +... +-rw-r--r--. 2 root root 85 18 may. 17:04 resolv.conf +-rw-r--r--. 1 root root 44 18 may. 17:04 adjtime +-rw-r--r--. 1 root root 283 18 may. 17:05 mtab +``` + +* List `/var` files larger than 1 megabyte but less than 1 gigabyte: + +```bash +$ ls -Rlh /var | grep [0-9]M +... +-rw-r--r--. 1 apache apache 1,2M 10 may. 13:02 XB RiyazBdIt.ttf +-rw-r--r--. 1 apache apache 1,2M 10 may. 13:02 XB RiyazBd.ttf +-rw-r--r--. 1 apache apache 1,1M 10 may. 13:02 XB RiyazIt.ttf +... +``` + +* Show the rights on a folder: + +To find out the rights to a folder, in our example `/etc`, the following command would not be appropriate: + +```bash +$ ls -l /etc +total 1332 +-rw-r--r--. 1 root root 44 18 nov. 17:04 adjtime +-rw-r--r--. 1 root root 1512 12 janv. 2010 aliases +-rw-r--r--. 1 root root 12288 17 nov. 17:41 aliases.db +drwxr-xr-x. 2 root root 4096 17 nov. 17:48 alternatives +... +``` + +since the command lists by default the contents of the folder and not the container. + +To do this, use the `-d` option: + +```bash +$ ls -ld /etc +drwxr-xr-x. 69 root root 4096 18 nov. 17:05 /etc +``` + +* List files by size: + +```bash +$ ls -lhS +``` + +* Display the modification date in "timestamp" format: + +```bash +$ ls -l --time-style="+%Y-%m-%d %m-%d %H:%M" / +total 12378 +dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin +dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot +``` + +* Add the _trailing slash_ to the end of folders: + +By default, the `ls` command does not display the last slash of a folder. In some cases, like for scripts for example, it is useful to display them: + +```bash +$ ls -dF /etc +/etc/ +``` + +* Hide some extensions: + +```bash +$ ls /etc --hide=*.conf +``` + +### `mkdir` command + +The `mkdir` command creates a directory or directory tree. + +```bash +mkdir [-p] directory [directory] [...] +``` + +Example: + +```bash +$ mkdir /home/rockstar/work +``` + +The "rockstar" directory must exist to create the "work" directory. Otherwise, the `-p` option should be used. The `-p` option creates the parent directories if they do not exist. + +!!! Danger It is not recommended to use Linux command names as directory or file names. + +### `touch` command + +The `touch` command changes the timestamp of a file or creates an empty file if the file does not exist. + +```bash +touch [-t date] file +``` + +Example: + +```bash +$ touch /home/rockstar/myfile +``` + +| Option | Information | +| --------- | -------------------------------------------------------------------------- | +| `-t date` | Changes the date of last modification of the file with the specified date. | + +Date format: `[AAAA]MMJJhhmm[ss]` + +!!! Tip The `touch` command is primarily used to create an empty file, but it can be useful for incremental or differential backups for example. Indeed, the only effect of executing a `touch` on a file will be to force it to be saved during the next backup. + +### `rmdir` command + +The `rmdir` command deletes an empty directory. + +Example: + +```bash +$ rmdir /home/rockstar/work +``` + +| Option | Information | +| ------ | ----------------------------------------------------------------------- | +| `-p` | Removes the parent directory or directories provided if they are empty. | + +!!! Tip To delete both a non-empty directory and its contents, use the `rm` command. + +### `rm` command + +The `rm` command deletes a file or directory. + +```bash +rm [-f] [-r] file [file] [...] +``` + +!!! Danger Any deletion of a file or directory is final. + +| Options | Information | +| ------- | ---------------------------------------- | +| `-f` | Do not ask for confirmation of deletion. | +| `-i` | Requires confirmation of deletion. | +| `-r` | Recursively deletes subdirectories. | + +!!! Note The `rm` command itself does not ask for confirmation when deleting files. However, with a RedHat/Rocky distribution, `rm` does ask for confirmation of deletion because the `rm` command is an `alias` of the `rm -i` command. Don't be surprised if on another distribution, like Debian for example, you don't get a confirmation request. + +Deleting a folder with the `rm` command, whether the folder is empty or not, will require the `-r` option to be added. + +The end of the options is signaled to the shell by a double dash `--`. + +In the example: + +```bash +$ >-hard-hard # To create an empty file called -hard-hard +hard-hard +[CTRL+C] To interrupt the creation of the file +$ rm -f -- -hard-hard +``` + +The hard-hard file name starts with a `-`. Without the use of the `--` the shell would have interpreted the `-d` in `-hard-hard` as an option. + +### `mv` command + +The `mv` command moves and renames a file. + +```bash +mv file [file ...] destination +``` + +Examples: + +```bash +$ mv /home/rockstar/file1 /home/rockstar/file2 +$ mv /home/rockstar/file1 /home/rockstar/file2 /tmp +``` + +| Options | Information | +| ------- | --------------------------------------------------------------- | +| `-f` | Don't ask for confirmation if overwriting the destination file. | +| `-i` | Request confirmation if overwriting destination file (default). | + +A few concrete cases will help you understand the difficulties that can arise: + +```bash +$ mv /home/rockstar/file1 /home/rockstar/file2 +``` + +Renames `file1` to `file2`, if `file2` already exists, it will be replaced by `file1`. + +```bash +$ mv /home/rockstar/file1 /home/rockstar/file2 /tmp +``` + +Moves `file1` and `file2` into the `/tmp` directory. + +```bash +$ mv file1 /repexist/file2 +``` + +Moves `file1` into `repexist` and renames it `file2`. + +```bash +$ mv file1 file2 +``` + +`file1` is renamed to `file2`. + +```bash +$ mv file1 /repexist +``` + +If the destination directory exists, `file1` is moved to `/repexist`. + +```bash +$ mv file1 /wrongrep +``` + +If the destination directory does not exist, `file1` is renamed to `wrongrep` in the root directory. + +### `cp` command + +The `cp` command copies a file. + +```bash +cp file [file ...] destination +``` + +Example: + +```bash +$ cp -r /home/rockstar /tmp +``` + +| Options | Information | +| ------- | ---------------------------------------------------------------- | +| `-i` | Request confirmation if overwriting (default). | +| `-f` | Do not ask for confirmation if overwriting the destination file. | +| `-p` | Keeps the owner, permissions and timestamp of the copied file. | +| `-r` | Copies a directory with its files and subdirectories. | +| `-s` | Creates a symbolik links rather than copying | + +```bash +cp file1 /repexist/file2 +``` + +`file1` is copied to `/repexist` under the name `file2`. + +```bash +$ cp file1 file2 +``` + +`file1` is copied as `file2` to this directory. + +```bash +$ cp file1 /repexist +``` + +If the destination directory exists, `file1` is copied to `/repexist`. + +```bash +$ cp file1 /wrongrep +``` + +If the destination directory does not exist, `file1` is copied under the name `wrongrep` to the root directory. + +## Visualization + +### `file` command + +The `file` command displays the type of a file. + +```bash +file file1 [files] +``` + +Example: + +```bash +$ file /etc/passwd /etc +/etc/passwd: ASCII text +/etc: directory +``` + +### `more` command + +The `more` command displays the contents of one or more files screen by screen. + +```bash +more file1 [files] +``` + +Example: + +```bash +$ more /etc/passwd +root:x:0:0:root:/root:/bin/bash +... +``` + +Using the ENTER key, the move is line by line. Using the SPACE key, the move is page by page. `/text` allows you to search for the occurrence in the file. + +### `less` command + +The `less` command displays the contents of one or more files. The `less` command is interactive and has its own commands for use. + +```bash +less file1 [files] +``` + +The commands specific to `less` are: + +| Command | Action | +| ----------------- | ----------------------------------------------- | +| `h` | Help. | +| `Arrows` | Move up, down a line, or to the right or left.. | +| `Enter` | Move down one line. | +| `Space` | Move down one page. | +| `PgUp` and `PgDn` | Move up or down one page. | +| `Begin` and `End` | Move to the beginning or end of a file. | +| `/texte` | Search for text. | +| `q` | Quit the `less` command. | + +### `cat` command + +The `cat` command concatenates the contents of multiple files and displays the result on the standard output. + +```bash +cat file1 [files] +``` + +Example 1 - Displaying the contents of a file to the standard output: + +```bash +$ cat /etc/passwd +``` + +Example 2 - Displaying the contents of multiple files to standard output: + +```bash +$ cat /etc/passwd /etc/group +``` + +Example 3 - Displaying the contents of several files in the file `usersAndGroups.txt`: + +```bash +$ cat /etc/passwd /etc/group > usersAndGroups.txt +``` + +Example 4 - Displaying the line numbering: + +```bash +$ cat -n /etc/profile + 1 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) + 2 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). + 3 + 4 if [ "`id -u`" -eq 0 ]; then + 5 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + 6 else +… +``` + +Example 5 - Shows the numbering of non-empty lines: + +```bash +$ cat -b /etc/profile + 1 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) + 2 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). + + 3 if [ "`id -u`" -eq 0 ]; then + 4 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + 5 else +… +``` + +### `tac` command + +The `tac` command does almost the opposite of the `cat` command. It displays the contents of a file starting from the end (which is particularly interesting for reading logs!). + +Example: Display a log file by displaying the last line first: + +```bash +[root]# tac /var/log/messages | less +``` + +### `head` command + +The `head` command displays the beginning of a file. + +```bash +head [-n x] file +``` + +| Option | Observation | +| ------ | --------------------------------------- | +| `-n x` | Display the first `x` lines of the file | + +By default (without the `-n` option), the `head` command will display the first 10 lines of the file. + +### `tail` command + +The `tail` command displays the end of a file. + +```bash +tail [-f] [-n x] file +``` + +| Option | Observation | +| ------ | ----------------------------------------- | +| `-n x` | Displays the last `x` lines of the file | +| `-f` | Displays changes to the file in real time | + +Example: + +```bash +tail -n 3 /etc/passwd +sshd:x:74:74:Privilege-separeted sshd:/var/empty /sshd:/sbin/nologin +tcpdump::x:72:72::/:/sbin/nologin +user1:x:500:500:grp1:/home/user1:/bin/bash +``` + +With the `-f` option, the `tail` command does not give back and runs until the user interrupts it with the sequence CTRL + C. This option is very frequently used to track log files (the logs) in real time. + +Without the `-n` option, the tail command displays the last 10 lines of the file. + +### `sort` command + +The `sort` command sorts the lines of a file. + +It allows you to order the result of a command or the content of a file in a given order, numerically, alphabetically, by size (KB, MB, GB) or in reverse order. + +```bash +sort [-kx] [-n] [-u] [-o file] [-ty] file +``` + +Example: + +```bash +$ sort -k3 -t: -n /etc/passwd +root:x:0:0:root:/root:/bin/bash +adm:x:3:4:adm:/var/adm/:/sbin/nologin +``` + +| Option | Observation | +| --------- | -------------------------------------------- | +| `-kx` | Specifies the `x` column to sort on | +| `-n` | Requests a numeric sort | +| `-o file` | Saves the sort to the specified file | +| `-ty` | Specifies the field separator character `y`. | +| `-r` | Reverse the order of the result | +| `- u` | unique | + +The `sort` command sorts the file only on the screen. The file is not modified by the sorting. To save the sort, use the `-o` option or an output redirection `>`. + +By default, the numbers are sorted according to their character. Thus, "110" will be before "20", which will itself be before "3". The `-n` option must be specified so that the numeric character blocks are sorted by their value. + +The `sort` command reverses the order of the results, with the `-r` option: + +```bash +$ sort -k3 -t: -n -r /etc/passwd +root:x:0:0:root:/root:/bin/bash +adm:x:3:4:adm:/var/adm/:/sbin/nologin +``` + +In this example, the `sort` command will sort the contents of the `/etc/passwd` file this time from largest uid to smallest. + +Some advanced examples of using the `sort` command: + +* Shuffling values + +The `sort` command also allows you to shuffle values with the `-R` option: + +```bash +$ sort -R /etc/passwd +``` + +* Sorting IP addresses + +A system administrator is quickly confronted with the processing of IP addresses from the logs of his services such as SMTP, VSFTP or Apache. These addresses are typically extracted with the `cut` command. + +Here is an example with the file `dns-client.txt`: + +``` +192.168.1.10 +192.168.1.200 +5.1.150.146 +208.128.150.98 +208.128.150.99 +``` + +```bash +$ sort -nr dns-client.txt +208.128.150.99 +208.128.150.98 +192.168.1.200 +192.168.1.10 +5.1.150.146 +``` + +* Sorting file by removing duplicates + +The `sort` command knows how to remove the duplicates from the file output using `-u` as option. + +Here is an example with the file `colours.txt`: + +``` +Red +Green +Blue +Red +Pink +``` +``` +$ sort -u colours.txt +Blue +Green +Pink +Red +``` + +* Sorting file by sizes + +The `sort` command knows how to recognize file sizes, from commands like `ls` with the `-h` option. + +Here is an example with the file `size.txt`: + +``` +1,7G +18M +69K +2,4M +1,2M +4,2G +6M +124M +12,4M +4G +``` + +```bash +$ sort -hr size.txt +4,2G +4G +1,7G +124M +18M +12,4M +6M +2,4M +1,2M +69K +``` + +### `wc` command + +The `wc` command counts the number of lines, words and/or bytes in a file. + +```bash +wc [-l] [-m] [-w] file [files] +``` + +| Option | Observation | +| ------ | ------------------------------- | +| `-c` | Count the number of bytes. | +| `-m` | Count the number of characters. | +| `-l` | Counts the number of lines. | +| `-w` | Counts the number of words. | + +## Search + +### `find` command + +The `find` command searches for files or directories location. + +```bash +find directory [-name name] [-type type] [-user login] [-date date] +``` + +Since there are so many options to the `find` command, it is best to refer to the `man`. + +If the search directory is not specified, the `find` command will search from the current directory. + +| Option | Observation | +| ------------------- | -------------------------------------- | +| `-perm permissions` | Search for files by their permissions. | +| `-size size` | Search for files by size. | + +### `-exec` option of the `find` command + +It is possible to use the `-exec` option of the `find` command to execute a command on each result line: + +```bash +$ find /tmp -name *.txt -exec rm -f {} \; +``` + +The previous command searches for all files in the `/tmp` directory named `*.txt` and deletes them. + + +!!! Tip "Understand the `-exec` option" In the example above, the `find` command will construct a string representing the command to be executed. + + If the `find` command finds three files named `log1.txt`, `log2.txt`, and `log3.txt`, then the `find` command will construct the string by replacing in the string `rm -f {} \;` the braces with one of the results of the search, and do this as many times as there are results. + + This will give us: + + ``` + rm -f /tmp/log1.txt ; rm -f /tmp/log2.txt ; rm -f /tmp/log3.txt ; + ``` + + + The `;` character is a special shell character that must be protected by a `\` to prevent it from being interpreted too early by the `find` command (and not in the `-exec`). + +!!! Tip `$ find /tmp -name *.txt -delete` does the same thing. + +### `whereis` command + +The `whereis` command searches for files related to a command. + +```bash +whereis [-b] [-m] [-s] command +``` + +Example: + +```bash +$ whereis -b ls +ls: /bin/ls +``` + +| Option | Observation | +| ------ | ------------------------------- | +| `-b` | Search only the binary file. | +| `-m` | Searches only for man pages. | +| `-s` | Searches only for source files. | + +### `grep` command + +The `grep` command searches for a string in a file. + +```bash +grep [-w] [-i] [-v] "string" file +``` + +Example: + +```bash +$ grep -w "root:" /etc/passwd +root:x:0:0:root:/root:/bin/bash +``` + +| Option | Observation | +| ------ | --------------------------------------- | +| `-i` | Ignore the case of the searched string. | +| `-v` | Excludes lines containing the string. | +| `-w` | Search for the exact word. | + +The `grep` command returns the complete line containing the string you are looking for. +* The `^` special character is used to search for a string at the beginning of a line. +* The special character `$` searches for a string at the end of a line. + +```bash +$ grep -w "^root" /etc/passwd +``` + +!!! Note This command is very powerful and it is highly recommended to consult its manual. It has many derivatives. + +It is possible to search for a string in a file tree with the `-R` option. + +```bash +grep -R "Virtual" /etc/httpd +``` + +### Meta-characters (wildcards) + +Meta-characters replace one or more characters (or even an absence of characters) during a search. These meta-characters are also known as wildcards. They can be combined. The `*` character replaces a string composed of any characters. The `*` character can also represent an absence of character. + +```bash +$ find /home -name "test*" +/home/rockstar/test +/home/rockstar/test1 +/home/rockstar/test11 +/home/rockstar/tests +/home/rockstar/test362 +``` + +Meta-characters allow more complex searches by replacing all or part of a word. Simply replace the unknowns with these special characters. + +The character `?` replaces a single character, whatever it is. + +```bash +$ find /home -name "test?" +/home/rockstar/test1 +/home/rockstar/tests +``` + +The square brackets `[` are used to specify the values that a single character can take. + +```bash +$ find /home -name "test[123]*" +/home/rockstar/test1 +/home/rockstar/test11 +/home/rockstar/test362 +``` + +!!! Note Always surround words containing meta-characters with `"` to prevent them from being replaced by the names of files that meet the criteria. + +!!! Warning Do not confuse shell meta-characters with regular expression meta-characters. The `grep` command uses regular expression meta-characters. + +## Redirects and pipes + +### Standard input and output + +On UNIX and Linux systems, there are three standard streams. They allow programs, via the `stdio.h` library, to input or output information. + +These streams are called X channel or X file descriptor. + +By default: +* the keyboard is the input device for channel 0, called **stdin** ; +* the screen is the output device for channels 1 and 2, called **stdout** and **stderr**. + +![standards channels](images/input-output.png) + +**stderr** receives the error streams returned by a command. The other streams are directed to **stdout**. + +These streams point to peripheral files, but since everything is a file in UNIX/Linux, I/O streams can easily be diverted to other files. This principle is the strength of the shell. + +### Input redirection + +It is possible to redirect the input stream from another file with the character `<` or `<<`. The command will read the file instead of the keyboard: + +```bash +$ ftp -in serverftp << ftp-commands.txt +``` + +!!! Note Only commands that require keyboard input will be able to handle input redirection. + +Input redirection can also be used to simulate user interactivity. The command will read the input stream until it encounters the defined keyword after the input redirection. + +This feature is used to script interactive commands: + +```bash +$ ftp -in serverftp << END +user alice password +put file +bye +END +``` + +The keyword `END` can be replaced by any word. + +```bash +$ ftp -in serverftp << STOP +user alice password +put file +bye +STOP +``` + +The shell exits the `ftp` command when it receives a line containing only the keyword. + +!!! Warning The ending keyword, here `END` or `STOP`, must be the only word on the line and must be at the beginning of the line. + +The standard input redirection is rarely used because most commands accept a filename as an argument. + +The command `wc` could be used like this: + +```bash +$ wc -l .bash_profile +27 .bash_profile # the number of lines is followed by the file name +$ wc -l < .bash_profile +27 # returns only the number of lines +``` + +### Output redirection + +Standard output can be redirected to other files using the `>` or `>>` characters. + +The simple `>` redirection overwrites the contents of the output file: + +```bash +$ date +%F > date_file +``` + +while the double redirection `>>` adds (concatenates) to the content of the output file. + +```bash +$ date +%F >> date_file +``` + +In both cases, the file is automatically created when it does not exist. + +The standard error output can also be redirected to another file. This time it will be necessary to specify the channel number (which can be omitted for channels 0 and 1): + +```bash +$ ls -R / 2> errors_file +$ ls -R / 2>> errors_file +``` + +### Examples of redirection + +Redirection of 2 outputs to 2 files: + +```bash +$ ls -R / >> ok_file 2>> nok_file +``` + +Redirection of the 2 outputs to a single file: + +```bash +$ ls -R / >> log_file 2>&1 +``` + +Redirection of *stderr* to a "bottomless pit" (`/dev/null`): + +```bash +$ ls -R / 2>> /dev/null +``` + +When both output streams are redirected, no information is displayed on the screen. To use both the output redirection and to keep the display, you will have to use the command `tee`. + +### Pipes + +A **pipe** is a mechanism allowing you to link the standard output of a first command to the standard input of a second command. + +This communication is uni directional and is done with the `|` symbol. The pipe symbol `|` is obtained by pressing the SHIFT + | simultaneously. + +![pipe](images/pipe.png) + +All data sent by the control on the left of the pipe through the standard output channel is sent to the standard input channel of the control on the right. + +The commands particularly used after a pipe are filters. + +* Examples: + +Display only the beginning: + +```bash +$ ls -lia / | head +``` + +Display only the end: + +```bash +$ ls -lia / | tail +``` + +Sort the result: + +```bash +$ ls -lia / | sort +``` + +Count the number of words / characters: + +```bash +$ ls -lia / | wc +``` + +Search for a string in the result: + +```bash +$ ls -lia / | grep fichier +``` + +## Special points + +### `tee` command + +The `tee` command is used to redirect the standard output of a command to a file while maintaining the screen display. + +It is combined with the `|` pipe to receive as input the output of the command to be redirected: + +```bash +$ ls -lia / | tee fic +$ cat fic +``` + +The `-a` option adds to the file instead of overwriting it. + +### `alias` and `unalias` commands + +Using **alias** is a way to ask the shell to remember a particular command with its options and give it a name. + +For example: + +```bash +$ ll +``` + +will replace the command: + +```bash +$ ls -l +``` + +The `alias` command lists the aliases for the current session. Aliases are set by default on Linux distributions. Here, the aliases for a Rocky server: + +```bash +$ alias +alias l.='ls -d .* --color=auto' +alias ll='ls -l --color=auto' +alias ls='ls --color=auto' +alias vi='vim' +alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' +``` + +The aliases are only defined temporarily, for the time of the user session. + +For permanent use, they must be created in the: + +* `.bashrc` file in the user's login directory; +* `/etc/profile.d/alias.sh` file for all users. + +!!! Warning Special care must be taken when using aliases which can be potentially dangerous! For example, an alias set up without the administrator's knowledge: + + ```bash + alias cd='rm -Rf' + ``` + +The `unalias` command allows you to delete aliases. + +To delete a single alias: + +```bash +$ unalias ll +``` + +To delete all aliases: + +```bash +$ unalias -a +``` + +To disable an alias temporarily, the combination is `\`. + +For example if we do: + +```bash +$ type ls +``` + +it might return the following: + +```bash +ls is an alias to « ls -rt » +``` + +Now that this is known, we can see the results of using the alias or disabling it one time with the `\` by executing the following: + +```bash +$ ls file* # order by time +file3.txt file2.txt file1.txt +$ \ls file* # order by name +file1.txt file2.txt file3.txt +``` + +### Aliases and useful functions + +* `grep` alias Colorize the result of the `grep` command: `alias grep='grep --color=auto'` + +* `mcd` function It is common to create a folder and then move around in it: `mcd() { mkdir -p "$1"; cd "$1"; }` + +* `cls` function Move to a folder and list its contents: `cls() { cd "$1"; ls; }` + +* `backup` function Create a backup copy of a file: `backup() { cp "$1"{,.bak}; }` + +* `extract` function Extract any type of archive: + +```bash +extract () { + if [ -f $1 ] ; then + case $1 in + *.tar.bz2) tar xjf $1 ;; + *.tar.gz) tar xzf $1 ;; + *.bz2) bunzip2 $1 ;; + *.rar) unrar e $1 ;; + *.gz) gunzip $1 ;; + *.tar) tar xf $1 ;; + *.tbz2) tar xjf $1 ;; + *.tgz) tar xzf $1 ;; + *.zip) unzip $1 ;; + *.Z) uncompress $1 ;; + *.7z) 7z x $1 ;; + *) + echo "'$1' cannot be extracted via extract()" ;; + esac + else + echo "'$1' is not a valid file" + fi +} + +``` + +* If `alias cmount` returns the following: `alias cmount="mount | column -t"` + +Then we can use cmount to show all of the system mounts in columns like this: + +`[root]# cmount` + +which would return our mounted filesystem in the following format: + +```bash +/dev/simfs on / type simfs (rw,relatime,usrquota,grpquota) +proc on /proc type proc (rw,relatime) +sysfs on /sys type sysfs (rw,relatime) +none on /dev type devtmpfs (rw,relatime,mode=755) +none on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000) +none on /dev/shm type tmpfs (rw,relatime) +none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime) + +``` + +### The character `;` + +The `;` character strings the commands. + +Les commandes s'exécuteront toutes séquentiellement dans l'ordre d'entrée une fois que l'utilisateur aura pressé ENTRÉE. + +```bash +$ ls /; cd /home; ls -lia; cd / +``` + +## Check your Knowledge + +:heavy_check_mark: What defines a user under Linux? (7 answers) + +:heavy_check_mark: What characterizes a long option for an order? + +:heavy_check_mark: Which commands allow you to search for help on a command: + +- [ ] `google` +- [ ] `chuck --norris` +- [ ] `info` +- [ ] `apropos` +- [ ] `whatis` + +:heavy_check_mark: Which command allows you to view a user's history? + +:heavy_check_mark: Which command allows you to search for text in a file? + +- [ ] `find` +- [ ] `grep` + +:heavy_check_mark: Which command allows you to search for a file? + +- [ ] `find` +- [ ] `grep` + +:heavy_check_mark: Which command redirects the error stream of a command to a new `errors.log` file: + +- [ ] `ls -R / 2> errors.log` +- [ ] `ls -R / 2>> errors.log` +- [ ] `ls -R / 2> errors.log 2>&1` diff --git a/docs/books/admin_guide/04-advanced-commands.fr.md b/docs/books/admin_guide/04-advanced-commands.fr.md new file mode 100644 index 0000000000..9955453f51 --- /dev/null +++ b/docs/books/admin_guide/04-advanced-commands.fr.md @@ -0,0 +1,356 @@ +--- +title: Advanced Linux Commands +--- + +# Advanced Commands for Linux users + +In this chapter you will learn some advanced commands for Linux. + +**** + +**Objectives** : In this chapter, future Linux administrators will learn: + +:heavy_check_mark: some useful commands not covered in the previous chapter; +:heavy_check_mark: some advanced commands. + +:checkered_flag: **user commands**, **Linux** + +**Knowledge**: :star: +**Complexity**: :star: :star: :star: + +**Temps de lecture : **20 minutes + +**** + +## `uniq` command + +The `uniq` command is a very powerful command, used with the `sort` command, especially for log file analysis. It allows you to sort and display entries by removing duplicates. + +To illustrate how the `uniq` command works, let's use a `firstnames.txt` file containing a list of first names: + +``` +antoine +xavier +steven +patrick +xavier +antoine +antoine +steven +``` + +!!! Note `uniq` requires the input file to be sorted because it only compares consecutive lines. + +Without an argument, the `uniq` command will not display identical lines that follow each other in the `firstnames.txt` file: + +``` +$ sort firstnames.txt | uniq +antoine +patrick +steven +xavier +``` + +To display only the rows that appear only once, use the `-u` option: + +``` +$ sort firstnames.txt | uniq -u +patrick +``` + +Conversely, to display only the lines that appear at least twice in the file, you must use the `-d` option: + +``` +$ sort firstnames.txt | uniq -d +antoine +steven +xavier +``` + +To simply delete lines that appear only once, use the `-D` option: + +``` +$ sort firstnames.txt | uniq -D +antoine +antoine +antoine +steven +steven +xavier +xavier +``` + +Finally, to count the number of occurrences of each line, use the `-c` option: + +``` +$ sort firstnames.txt | uniq -c + 3 antoine + 1 patrick + 2 steven + 2 xavier +``` + +``` +$ sort firstnames.txt | uniq -cd + 3 antoine + 2 steven + 2 xavier +``` + +## `xargs` commands + +The `xargs` command allows the construction and execution of command lines from standard input. + +The `xargs` command reads whitespace or linefeed delimited arguments from standard input, and executes the command (`/bin/echo` by default) one or more times using the initial arguments followed by the arguments read from standard input. + +A first and simplest example would be the following: + +``` +$ xargs +use +of +xargs + +use of xargs +``` + +The `xargs` command waits for an input from the standard **stdin** input. Three lines are entered. The end of the user input is specified to `xargs` by the keystroke sequence CTRL+D. `xargs` then executes the default command `echo` followed by the three arguments corresponding to the user input, namely : + +``` +$ echo "use" "of" "xargs" +use of xargs +``` + +It is possible to specify a command to be run by `xargs`. + +In the following example, `xargs` will run the command `ls -ld` on the set of folders specified in the standard input: + +``` +$ xargs ls -ld +/home +/tmp +/root + +drwxr-xr-x. 9 root root 4096 5 avril 11:10 /home +dr-xr-x---. 2 root root 4096 5 avril 15:52 /root +drwxrwxrwt. 3 root root 4096 6 avril 10:25 /tmp +``` + +In practice, the `xargs` command executed the `ls -ld /home /tmp /root` command. + +What happens if the command to be executed does not accept multiple arguments as is the case with the `find` command? + +``` +$ xargs find /var/log -name +*.old +*.log +find: paths must precede expression: *.log +``` + +The `xargs` command attempted to execute the `find` command with multiple arguments behind the `-name` option, which caused `find` to generate an error: + +``` +$ find /var/log -name "*.old" "*.log" +find: paths must precede expression: *.log +``` + +In this case, the `xargs` command must be forced to execute the `find` command several times (once per line entered as standard input). The `-L` option followed by an **integer** allows you to specify the maximum number of entries to be processed with the command at one time: + +``` +$ xargs -L 1 find /var/log -name +*.old +/var/log/dmesg.old +*.log +/var/log/boot.log +/var/log/anaconda.yum.log +/var/log/anaconda.storage.log +/var/log/anaconda.log +/var/log/yum.log +/var/log/audit/audit.log +/var/log/anaconda.ifcfg.log +/var/log/dracut.log +/var/log/anaconda.program.log + +``` + +If we wanted to be able to specify both arguments on the same line, we would have to use the `-n 1` option: + +``` +$ xargs -n 1 find /var/log -name +*.old *.log +/var/log/dmesg.old +/var/log/boot.log +/var/log/anaconda.yum.log +/var/log/anaconda.storage.log +/var/log/anaconda.log +/var/log/yum.log +/var/log/audit/audit.log +/var/log/anaconda.ifcfg.log +/var/log/dracut.log +/var/log/anaconda.program.log + +``` + +Case study of a backup with a `tar` based on a search: + +``` +$ find /var/log/ -name "*.log" -mtime -1 | xargs tar cvfP /root/log.tar +$ tar tvfP /root/log.tar +-rw-r--r-- root/root 1720 2017-04-05 15:43 /var/log/boot.log +-rw-r--r-- root/root 499270 2017-04-06 11:01 /var/log/audit/audit.log +``` + +The special feature of the `xargs` command is that it places the input argument at the end of the called command. This works very well with the above example since the files passed in will form the list of files to be added to the archive. + +Now, if we take the example of the `cp` command and want to copy a list of files in a directory, this list of files will be added at the end of the command... but what the `cp` command expects at the end of the command is the destination. To do this, we use the `-I` option to put the input arguments somewhere else than at the end of the line. + +``` +$ find /var/log -type f -name "*.log" | xargs -I % cp % /root/backup +``` + +The `-I` option allows you to specify a character (in our example the `%` character) where the input files to `xargs` will be placed. + +## `yum-utils` package + +The `yum-utils` package is a collection of utilities from different authors for `yum`, which make it easier and more powerful to use. + +!!! Note While `yum` has been replaced by `dnf` in Rocky Linux 8, the package name has remained `yum-utils` although it can be installed as `dnf-utils` as well. These are classic YUM utilities implemented as CLI shims on top of DNF to maintain backwards compatibility with `yum-3`. + +Here are some examples of usage: + +* `repoquery` command: + +The `repoquery` command queries the repositories. + +Examples of use: + + * Knowing the dependencies of an uninstalled package: + +``` +repoquery --requires +``` + + * Know the files provided by a non-installed package: + +``` +$ repoquery -l yum-utils +/etc/bash_completion.d +/etc/bash_completion.d/yum-utils.bash +/usr/bin/debuginfo-install +/usr/bin/find-repos-of-install +/usr/bin/needs-restarting +/usr/bin/package-cleanup +/usr/bin/repo-graph +/usr/bin/repo-rss +/usr/bin/repoclosure +/usr/bin/repodiff +/usr/bin/repomanage +/usr/bin/repoquery +/usr/bin/reposync +/usr/bin/repotrack +/usr/bin/show-changed-rco +/usr/bin/show-installed +/usr/bin/verifytree +/usr/bin/yum-builddep +/usr/bin/yum-config-manager +/usr/bin/yum-debug-dump +/usr/bin/yum-debug-restore +/usr/bin/yum-groups-manager +/usr/bin/yumdownloader +… +``` + +* `yumdownloader` command: + +The `yumdownloader` command downloads RPM packages from the repositories. + +!!! Note This command is very useful to quickly build a local repository of a few rpm! + +Example: `yumdownloader` will download the _repoquery_ rpm package and all its dependencies: + +``` +$ yumdownloader --destdir /var/tmp -- resolve repoquery +``` + +| Options | Comments | +| ----------- | --------------------------------------------------------------- | +| -`-destdir` | The downloaded packages will be stored in the specified folder. | +| `--resolve` | Also downloads the package dependencies. | + +## `psmisc` packages + +The `psmisc` package contains utilities for managing system processes: + +* `pstree`: the `pstree` command displays the current processes on the system in a tree-like structure. +* `killall`: the `killall` command sends a kill signal to all processes identified by name. +* `fuser`: the `fuser` command identifies the `PID` of processes that use the specified files or file systems. + +Examples: + +``` +$ pstree +systemd─┬─NetworkManager───2*[{NetworkManager}] + ├─agetty + ├─auditd───{auditd} + ├─crond + ├─dbus-daemon───{dbus-daemon} + ├─firewalld───{firewalld} + ├─lvmetad + ├─master─┬─pickup + │ └─qmgr + ├─polkitd───5*[{polkitd}] + ├─rsyslogd───2*[{rsyslogd}] + ├─sshd───sshd───bash───pstree + ├─systemd-journal + ├─systemd-logind + ├─systemd-udevd + └─tuned───4*[{tuned}] +``` + +``` +# killall httpd +``` + +Kill processes (option `-k`) that access the `/etc/httpd/conf/httpd.conf` file: + +``` +# fuser -k /etc/httpd/conf/httpd.conf +``` + +## `watch` command + +The `watch` command regularly executes a command and displays the result in the terminal in full screen. + +The `-n` option allows you to specify the number of seconds between each execution of the command. + +!!! Note To exit the `watch` command, you must type the keys: CTRL+C to kill the process. + +Examples: + +* Display the end of the `/etc/passwd` file every 5 seconds: + +``` +$ watch -n 5 tail -n 3 /etc/passwd +``` + +Result: + +``` +Every 5,0s: tail -n 3 /etc/passwd rockstar.rockylinux.lan: Thu Jul 1 15:43:59 2021 + +sssd:x:996:993:User for sssd:/:/sbin/nologin +chrony:x:995:992::/var/lib/chrony:/sbin/nologin +sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin +``` + +* Monitoring the number of files in a folder: + +``` +$ watch -n 1 'ls -l | wc -l' +``` + +* Display a clock: + +``` +$ watch -t -n 1 date +```