# Commandes General



# Lister les services

Sur CentOS/RedHat Tous :

```bash
systemctl list-units --type service --all
```

En cours :

```bash
systemctl list-units --type service --all | grep running
```

Sur Debian :

```bash
service --status-all
```

# Quelques commandes

Commandes simples :

\\ : utilisation des apostrophes

Lire les fichiers : <span style="color: rgb(230, 126, 35);">cat, more, less</span>

<span style="color: rgb(230, 126, 35);">nmtui</span> : modifier connexion via interface graphique  
<span style="color: rgb(230, 126, 35);">ctrl + alt + f1 ou f2 </span>: changer de terminal  
<span style="color: rgb(230, 126, 35);">\#</span> : utilisation compte root  
<span style="color: rgb(230, 126, 35);">$</span> : utilisation autre utilisateur  
<span style="color: rgb(230, 126, 35);">~</span> : répertoire de connexion  
<span style="color: rgb(230, 126, 35);">ctrl + s </span>: Pause du terminal  
<span style="color: rgb(230, 126, 35);">ctrl + q </span>: Reprendre le terminal  
<span style="color: rgb(230, 126, 35);">ctrl + w </span>: Effacer le mot  
<span style="color: rgb(230, 126, 35);">ctrl + i </span>: efface l'affichage  
<span style="color: rgb(230, 126, 35);">ctrl + u </span>: efface ligne entière  
<span style="color: rgb(230, 126, 35);">man -k </span>: recherche pages du manuel associés à un mot

<span style="color: rgb(230, 126, 35);">who</span> : liste utilisateurs connectés  
<span style="color: rgb(230, 126, 35);">whoami </span>: affiche utilisateur actuel connecté  
<span style="color: rgb(230, 126, 35);">finger </span>: affiche informations plus précises sur l'utilisateur connecté  
<span style="color: rgb(230, 126, 35);">pwd</span> : affiche répertoire courant  
<span style="color: rgb(230, 126, 35);">Cd ~</span> =&gt; renvoie dans le répertoire /home personnel.

  
<span style="color: rgb(230, 126, 35);">/bin/bash :</span> Shell par défaut | Boîte de l'invite de commande.  
<span style="color: rgb(230, 126, 35);">passwd</span> : modifier mot de passe | commandes liés au mot de passe  
<span style="color: rgb(230, 126, 35);">passwd -l :</span> verrouiller compte  
<span style="color: rgb(230, 126, 35);">passwd -u</span> : déverrouiller compte  
<span style="color: rgb(230, 126, 35);">passwd -d</span> : désactiver saisie du mot de passe pour un compte

<span style="color: rgb(230, 126, 35);">wc </span>: compter le nombre de lignes, mots, caractères contenus dans un fichier.  
<span style="color: rgb(230, 126, 35);">wc -l -w -c </span>(line, word, caracter)

<span style="color: rgb(230, 126, 35);">env</span> : affiche varaibles d'environnement  
<span style="color: rgb(230, 126, 35);">echo</span> : afficher valeur d'environnement  
<span style="color: rgb(230, 126, 35);">date</span> : affiche date  
<span style="color: rgb(230, 126, 35);">exit</span> or <span style="color: rgb(230, 126, 35);">logout </span>: déconnecter utilisateur  
<span style="color: rgb(230, 126, 35);">which \[commande\]</span> : affiche le chemin complet d'une commande

Changement de valeur d'une variable d'environnement  
<span style="color: rgb(230, 126, 35);">export PS1=’\[\\u@\\h \\W \\t\]$’</span>  
Export valide la nouvelle variable.  
<span style="color: rgb(230, 126, 35);">$TMOUT</span> : Variable qui déconnectee automatiquement au bout d'un temps défini.  
La commande export ne marche que pour la session en cours. (temporaire)

==Recherche==  
<span style="color: rgb(230, 126, 35);">\* </span>: substitue le caractères générique ? par n'impoorte quel caractère.  
<span style="color: rgb(230, 126, 35);">\[\] </span>: remplace le caractère par un des caractères donnés. Exemple  
<span style="color: rgb(230, 126, 35);">\[abc\] </span>remplace le caractère par a ou b ou c  
<span style="color: rgb(230, 126, 35);">\[f-z\] :</span> caractère entre f et z. Respect de la casse.  
exemple : <span style="color: rgb(230, 126, 35);">ls \[fr\]?tab</span>  
<span style="color: rgb(230, 126, 35);">! </span>: cherche les caractères ne se trouvant pas entre les  
<span style="color: rgb(230, 126, 35);">{} </span>: Cherche tous les noms de fichiers commençant par une chaîne de caractères au lieu  
d'un seul caractère.  
Avec des doubles apostrophes, les caractères spéciaux sont interprétés mais pas le \* .

<span style="color: rgb(230, 126, 35);">alias </span>: remplace une ligne de commandes complexe par son alias.  
pour l'avoir de manière permanente pour tous les utilisateurs, l'ajouter dans .bashrc  
qui se trouve dans le dossier de connexion de l'utilisateur.

<span style="color: rgb(230, 126, 35);">unalias -a nom\_alias</span> : supprimer alias  
<span style="color: rgb(230, 126, 35);">type</span> : affiche les informations à propos d'une commande et son interprétation.  
<span style="color: rgb(230, 126, 35);">cat </span>: sert à lire un fichier  
<span style="color: rgb(230, 126, 35);">whereis </span>: localiser rapidement une commande externe ainsi que les fichiers  
aide et sources associées.

<span style="color: rgb(230, 126, 35);">source .bash\_profile </span>: recharger le fichier profile sans se déconnecter.  
<span style="color: rgb(230, 126, 35);">set -o </span>: affiche l'état (actif ou inactif) des options  
<span style="color: rgb(230, 126, 35);">set -o \[nom\_option\]</span> : activer  
<span style="color: rgb(230, 126, 35);">set +o \[nom\_option\] </span>: désactiver

Tous les fichiers qui commencent par p ou se terminent par d :  
<span style="color: rgb(230, 126, 35);">ls -d {p\*,\*.d}</span>

==Avancé==  
processus associé à des descripteurs de fichiers.  
point d'entrée = paramètre rentré sur le clavier  
sortie = résultat affiché à l'écran  
Entrée (0) =&gt; Processus =&gt; Sortie (1)  
||  
Erreur (2)

<span style="color: rgb(230, 126, 35);">&lt; </span>: rediriger l'entrée  
<span style="color: rgb(230, 126, 35);">&gt;</span> : rediriger la sortie vers un fichier  
<span style="color: rgb(230, 126, 35);">&gt;&gt;</span> : permet d'ajouter dans un fichier existant  
<span style="color: rgb(230, 126, 35);">2&gt;</span> : redirection de l'erreur standard  
<span style="color: rgb(230, 126, 35);">n&gt;&amp;m</span> : avoir une sortie + erreur dans un seul fichierr  
où n est numéro du descripteur à rediriger et m : numéro de celui vers  
lequel il faut renvoyer les données . exemple : Ou &gt; ficsortie 1&gt;&amp;2

<span style="color: rgb(230, 126, 35);">/dev/null </span>: puit sans fond. Tout ce qui est envoyé est supprimé du système  
exemple : ps -ef &gt; /tmp/test  
<span style="color: rgb(230, 126, 35);">ls bin dev bleu &gt; ficout 2&gt;&gt; ficerr2</span>

<span style="color: rgb(230, 126, 35);">/dev </span>: fichiers qui sont des pointeurs vers des périphériques  
<span style="color: rgb(230, 126, 35);">touch </span>: permet de changer l'heure du fichier et créé un fichier vide.

tubes (pipe) : <span style="color: rgb(230, 126, 35);">|</span>  
<span style="color: rgb(230, 126, 35);">tee </span>: lis l'entrée, génèree le fichier et effectue une copie sur la sortie intermédiaire  
<span style="color: rgb(230, 126, 35);">grep</span> : filtre et garde que ce qui contient après ce paramètre

<span style="color: rgb(230, 126, 35);">su</span> : switch user | changer d'utilisateur  
<span style="color: rgb(230, 126, 35);">su - \[nom\_user\] </span>: tirer permet de garder l'environnement de l'utilisateur actuel  
Lancer une commande en tant qu'utilisateur test :  
<span style="color: rgb(230, 126, 35);">su - test -c "ps -ef"</span>  
chaque commande lancé via su crée une exécution en shell.

<span style="color: rgb(230, 126, 35);">sudo</span> : exécution de commandes en tant que sans élévation de compte /etc/sudoers

  
\[Commandes Avancées\]  
<span style="color: rgb(230, 126, 35);">Dmesg </span>: verbosité du noyau. Les informations sur des périphériques attachés ou détachés apparaissent dedans.  
<span style="color: rgb(230, 126, 35);">Fdisk –l </span>: commandes de table de partition  
<span style="color: rgb(230, 126, 35);">mount</span> : monter le périphérique à un répertoire. | affiche la liste des montages  
<span style="color: rgb(230, 126, 35);">df</span> : fourni la quantité d'espace occupé des systèmes de fichiers.  
<span style="color: rgb(230, 126, 35);">mkfs -t ext4 /dev/sdx</span> : formater au format ext4

editeurs de texte : <span style="color: rgb(230, 126, 35);">vi, nano, emacs</span>  
<span style="color: rgb(230, 126, 35);">cd -</span> =&gt; renvoie dans le répertoire précédent  
<span style="color: rgb(230, 126, 35);">file</span> : permet de déterminer le type d'un fichier  
<span style="color: rgb(230, 126, 35);">stat</span> : indique les informations du bloc où est stocké le fichier  
<span style="color: rgb(230, 126, 35);">mkdir -p</span> : crée arborescence du répertoire

<span style="color: rgb(230, 126, 35);">cp -R</span> : Copier contenu d'un répertoire avec les droits associés.  
<span style="color: rgb(230, 126, 35);">mv</span> : renommer ou déplacer fichiers  
<span style="color: rgb(230, 126, 35);">rename</span> : renommer fichier

<span style="color: rgb(45, 194, 107);">find : rechercher des fichiers</span>  
<span style="color: rgb(45, 194, 107);">exemple : find / -name</span>  
<span style="color: rgb(230, 126, 35);">find -size +1M</span> : fichiers de plus d'un megaoctet  
<span style="color: rgb(230, 126, 35);">find -mtime</span> : heure de modification

<span style="color: rgb(45, 194, 107);">Grep permet de filtrer des lignes.</span>  
<span style="color: rgb(230, 126, 35);">Grep –i </span>=&gt; prend en compte les minuscules et majuscules  
<span style="color: rgb(230, 126, 35);">Grep –l </span>=&gt; affiche uniquement les noms des fichiers possédant des lignes qui correspondent à la valeur donnée.

<span style="color: rgb(230, 126, 35);">Cut </span>=&gt; supprimer une partie de chaque ligne d’un fichier  
<span style="color: rgb(230, 126, 35);">Sort </span>=&gt; trier les lignes d’un fichier.  
<span style="color: rgb(230, 126, 35);">Head</span> =&gt; consulter le début d’un fichier  
<span style="color: rgb(230, 126, 35);">Tail </span>=&gt; consulter la fin d’un fichier.

La commande watch permet d’exécuter un programme périodiquement en affichant le résultat à l’écran

<span style="color: rgb(230, 126, 35);">ln </span>: lien dur (hard link) -&gt; seconde entrée pointant vers le même inode  
<span style="color: rgb(230, 126, 35);">ln -s</span> : lien symbolique -&gt; raccourci

# MAIL

Pour voir la file d'attente :

```bash
mailq
```

Pour le journal des logs :

```bash
journalctl -u postfix
```

Pour forcer le renvoi des mails bloqués dans la file d'attente :

```bash
postfix flush
```

Pour tester l'envoi :

```bash
Mail email@addresse.com
```

et suivre les étape

Pour reload le service

```bash
postfix reload
```

# Lancer une commande active même après la déconnexion

Précéder la commande par la commande <span style="color: rgb(230, 126, 35);">nohup</span>  
S'il est nécessaire de récupérer la console, terminer la ligne avec une esperluette <span style="color: rgb(230, 126, 35);">&amp;</span> exemple :

```bash
nohup  wget ftp://...../Fedora-8-i386-rescuecd.iso &
```

La sortie de la commande est loggué dans un fichier nohup.out  
il est possible de consulter soit en direct en lançant immédiatement

```bash
tail -f nohup.out
```

Soit après coup :

```bash
less nohup.out
```

# NTP

Pour voir l'heure et le status du service

```bash
/etc/init.d/hwclock.sh show
```

Pour checker l'heure du matériel :

```bash
date; hwclock
```

Pour synchro :

```bash
hwclock --systohc
```

Pour debugguer :

```bash
hwclock --systohc --debug
```

# Drop RAM SWAP CPU & MEM

<span style="color: rgb(126, 140, 141);">lister les process qui occupe le swap : </span>

```bash
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(45, 194, 107);">DROP mémoire cache + voir résultat </span></span>

```bash
free -h && sudo sysctl vm.drop_caches=3 && free –h
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(45, 194, 107);">Relancer le swap</span></span>

```bash
sudo swapoff –a && sudo swapon -a
```

#### **Problème lié au swap**

Déplacer le cache de Swap vers la mémoire si elle est pas trop sollicitée en exécutant la commande :

```bash
swapoff -a
```

changer le swappiness à 10 au lieu de 40 (valeur recommandée sur postgre) ce qui a permis de soulager le swap  
il ne sera sollicité que la mémoire passe en full (ce qui n'était pas le cas avant ce changement, le swap se remplit fréquemment même si la RAM est vide )

  
Pour modifier la valeur sans redémarrer le système d'exploitation, exécutez la commande suivante :

```bash
sysctl -w vm.swappiness=10
```

Pour modifier la valeur temporairement (après le redémarrage la valeur sera sautée)

```bash
htop
```

Pour reloader la config :

```bash
sysctl --system
```

une fois le swap vidé passer la commande suivante pour réactiver le swap :

```bash
swapon -a
```

La config swappiness se trouve :   
<span style="color: rgb(230, 126, 35);">/etc/sysctl.conf  
  
</span>

<span style="color: rgb(149, 165, 166);">droper cache mem et swap autrement : </span>

```bash
sync; echo 3 > /proc/sys/vm/drop_caches
```

[![image.png](https://cavallone.fr/uploads/images/gallery/2024-11/scaled-1680-/lFBYlh5gaCvAGJHl-image.png)](https://cavallone.fr/uploads/images/gallery/2024-11/lFBYlh5gaCvAGJHl-image.png)

trier le CPU ou la mémoire sur la machine :   
<span style="color: rgb(230, 126, 35);">top ou htop</span>

commande avancée pour trier la mémoire:

```bash
ps -eo size,pid,user,command --sort -size |    awk '{ hr=$1/1024 u; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |    cut -d "" -f2 | cut -d "-" -f1
```

[![image.png](https://cavallone.fr/uploads/images/gallery/2024-11/scaled-1680-/p73behMJXXpG1apj-image.png)](https://cavallone.fr/uploads/images/gallery/2024-11/p73behMJXXpG1apj-image.png)

autres commande pour CPU et mem triez par top:

```bash
ps -eo pid,user,comm,%mem --sort=-%mem | head -n 6
```

```bash
ps -eo pid,user,comm,%mem --sort=-%cpu | head -n 6
```

<span style="color: rgb(126, 140, 141);">Vous pouvez faire des teste de stress sur une machine pour voir comment elle réagit via :</span>

```bash
Yum install stress
```

<span style="color: rgb(230, 126, 35);">Commande stress mémoire : </span>

```bash
stress --vm 1 --vm-bytes 1G --timeout 30s
```

# Astuces Cron

Vérifier la crontab système :

```bash
cat /etc/crontab
```

Lister les crontab de tous les utilisateurs

```bash
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
```

Lister les crontab d'un user spécifique

```bash
crontab -l -u "user"
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(45, 194, 107);">CRONTAB</span>  
<span style="color: rgb(45, 194, 107);">mm hh jj MMM JJJ tâche</span>  
</span>\# Example of job definition:  
\# .---------------- minute (0 - 59)  
\# | .------------- hour (0 - 23)  
\# | | .---------- day of month (1 - 31)  
\# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...  
\# | | | | .---- day of week (0 - 6) (Sunday=0, Monday=1, and so on)  
\# | | | | |  
\# \* \* \* \* \* user-name command-to-be-executed<span style="color: rgb(230, 126, 35);">  
</span>

crontab -l  
ls -al /etc/cron.\*  
cat /etc/cron.d/\* | grep "votre recherche"

Valider la planification avec crontab guru :   
[https://crontab.guru](https://crontab.guru)

Aide à la création de la cron:  
[https://crontab-generator.org/](https://crontab-generator.org/)

# Extraction link web page

[![image.png](https://cavallone.fr/uploads/images/gallery/2024-10/scaled-1680-/OEDBmIykIQV84pw2-image.png)](https://cavallone.fr/uploads/images/gallery/2024-10/OEDBmIykIQV84pw2-image.png)

[![image.png](https://cavallone.fr/uploads/images/gallery/2024-10/scaled-1680-/bEsgI7iJWSmi2d7f-image.png)](https://cavallone.fr/uploads/images/gallery/2024-10/bEsgI7iJWSmi2d7f-image.png)

# Rechercher des fichier : find/locate/size

<span style="color: rgb(126, 140, 141);">• Rechercher tous les fichiers avec l'extension .txt dans le répertoire courant :</span>

```bash
find . -name "*.txt"
```

<span style="color: rgb(126, 140, 141);">• Rechercher tous les fichiers avec l'extension .txt dans tous les sous-répertoires du répertoire courant :</span>

```bash
find . -name "*.txt" -type f -exec ls -lh {} \;
```

<span style="color: rgb(126, 140, 141);">• Rechercher tous les fichiers dont la taille est supérieure à 100 Mo :</span>

```bash
find . -size +100M
```

<span style="color: rgb(126, 140, 141);">• Rechercher tous les fichiers modifiés au cours des 7 derniers jours :</span>

```bash
find . -mtime -7
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(126, 140, 141);">Pour utiliser la commande locate, vous devez d'abord vous assurer que la base de données locate est à jour. Pour ce faire, exécutez la commande suivante :</span>  
</span>

```bash
sudo updatedb
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(126, 140, 141);">Une fois la base de données locate à jour, vous pouvez commencer à rechercher des fichiers. La syntaxe de base de la commande locate est la suivante :</span>  
</span>

```bash
locate [options] nom_de_fichier
```

<span style="color: rgb(230, 126, 35);"><span style="color: rgb(126, 140, 141);">Par exemple, pour rechercher tous les fichiers avec l'extension .txt, vous pouvez exécuter la commande suivante :</span>  
</span>

```bash
locate *.txt
```

<span style="color: rgb(126, 140, 141);">Recherche dans l'historique des commande taper sur le shell:</span>

```bash
CRTL + R : 
```