Table des matières

Exercices Chapitre 6


1. Log

Le fichier dmesg.log contient un log des messages système d’une machine Linux. Le champ entre [ ] indique le temps en secondes depuis le démarrage du système. Par exemple [ 2.878733] signifie 2 secondes 878,733 ms.

#!/bin/bash
# shows the lines containing 'eth0' between the start of the machine and the number of seconds entered as parameter

grep eth0 dmesg.log | while IFS= read -r line; do
    seconds="$(sed 's/^\[ *\([0-9]*\)\..*$/\1/' <<< "$line")"
    [ $seconds -gt $1 ] && break || echo $line
done
exit

2. Créations des users, setgid

Créer un script qui réalisera les opérations suivantes :

#!/bin/bash

set -eu # if one of the commands have an non 0 return the script stops
file='/home/sivananda/ex-ch6/users.txt'
groups=$(cat "$file" | cut -d " " -f2 | sort -u)

for group in $groups
do
        echo "création du groupe $group"
        groupadd "$group"

        echo "création du répertoire /home/$group"
        mkdir "/home/$group"
        chgrp "$group" "/home/$group"
        chmod 2770 "/home/$group"

users=$(cat "$file" | grep "$group" | cut -d " " -f1)

        for user in $users
        do
                echo "création de l'utilisateur utilisateur $user dans le groupe $group"
                password=$(cat "$file" | grep "$user" | cut -d " " -f3)
                newpassword="$user:$password"
                useradd "$user" -U -G "$group" -s /bin/bash
                echo "$newpassword" | chpasswd
        done
done

SUPPRESSION DES UTILISATEURS

#!/bin/bash

set -e # if one of the commands have an non 0 return the script stops
file='/home/sivananda/ex-ch6/users.txt'
groups=$(cat "$file" | cut -d " " -f2 | sort -u)

for group in $groups
do
        echo "suppression du répertoire /home/$group"
        rmdir "/home/$group"

        users=$(cat "$file" | grep "$group" | cut -d " " -f1)

        for user in $users
        do
                echo "suppression de l'utilisateur utilisateur $user"
                userdel "$user"
        done

        echo "suppression du groupe $group"
        groupdel "$group"

done

3. Gestion des Users

Créer un script qui réalisera les opérations suivantes :

sudo timedatectl set-ntp false
sudo date --set "Jan 1 11:57:03 UTC 2019"
ssh olduser3@127.0.0.1
exit
sudo timedatectl set-ntp true
#!/bin/bash

set -eu
users=$(cat /etc/passwd | cut -d : -f1)

for user in $users
do
        lst=$(last $user | head -1)
        lst=${lst:43:12}

        if [ -n "$lst" ]; then
                echo "$user: $lst"
        fi
done

https://www.tldp.org/LDP/abs/html/filearchiv.html
–after-date only process files with a date stamp after specified date -z gzip the archive

tar -zcf archive-name.tar.gz directory-name

Where,

tar -zxf old-directory.tar.gz
tar -zxf old-directory.tar.gz -C /tmp

https://www.cyberciti.biz/faq/how-do-i-compress-a-whole-linux-or-unix-directory/


Ubuntu Serveur