Module GNU/Linux CEFIPA : TP n 2
Transcription
Module GNU/Linux CEFIPA : TP n 2
Module GNU/Linux CEFIPA : TP n◦2 - Droits et processus Nicolas Burrus 26 Avril 2005 Question 1 Ajouter un utilisateur nommé tp2. Vérifier qu’il est présent dans /etc/passwd. Quel est son UID ? Ajouter un groupe tp2-group2. Vérifier qu’il est présent dans /etc/group. Quel est son GID ? Question 2 En utilisant usermod, faire en sorte que l’utilisateur tp2 fasse partie du groupe tp2 et du groupe tp2-group2. Vérifiez le résultat. Question 3 Placez vous sous l’identité de l’utilisateur tp2. Pouvez-vous créer un fichier vide /usr/test1 ? Pourquoi ? Pouvez-vous créer un fichier vide /tmp/test1 ? Pourquoi ? Question 4 Faites en sorte que l’utilisateur knoppix ne puisse pas lire le fichier /tmp/test1. Faites en sorte que l’utilisateur knoppix ne puisse pas lister le contenu de /home/tp2. Question 5 Donnez les droits du fichier /bin/su sous forme numérique (ex : 0644). Qu’est ce que ça signifie ? Question 6 A quel utilisateur et à quel groupe appartiennent les fichiers dans /sbin ? Question 7 Vérifier que init est bien le processus parent de tous les autres. Quel est son PID ? Question 8 En utilisant top, donner les PID et les commandes associées aux 2 processus qui utilisent le plus de mémoire (RES). 1 Question 9 top consomme des ressources. Faites en sorte que sa priorité soit de 18. Est-ce une forte priorité ? Quel est la priorité du processus associé à la commande /usr/X11R6/bin/X ? Question 10 Consulter le man de kill. Placez le processus en arrière plan sans le terminer. Consulter le man de ps. Placez le également en arrière plan sans le terminer. Ré-affichez le man de kill. Ouvrez un autre terminal. Envoyez le signal SIGTERM au processus affichant le man de kill. Retournez dans le premier terminal, réactivez le man de ps, et quittez le normalement. Question 11 Executez la commande less /etc/mime.types. Déplacez vous dans le fichier. Ouvrez un autre terminal. Envoyez le signal SIGSTOP au processus associé à less. Retournez dans le premier terminal. Pouvez vous continuer à naviguer dans le fichier ? Faites en sorte que le processus réponde à nouveau. Question 12 Que fait la commande kill -9 -1, exécutée sous l’identité de l’utilisateur tp2 ? 2 Module GNU/Linux CEFIPA : TP n◦3 - Shell avancé Nicolas Burrus 26 Avril 2005 Question 1 Créer un fichier $HOME/programs contenant la liste des fichiers de /bin. Question 2 Entrez la commande ls /bin /nowhere. Complétez la commande pour faire disparaître le message d’erreur. Quel est le code de sortie du programme ? Question 3 Donnez une commande qui appelle mkdir pour créer le répertoire $HOME/dir1 uniquement s’il n’existe pas. Question 4 Donnez la liste des fichiers dont le nom contient la chaîne "syslog" dans /var. Question 5 Cherchez le PID du processus init en combinant ps et grep. Question 6 Parcourez la liste des fichiers présents dans /dev, sans scroller dans le terminal, en utilisant less. Question 7 Dans $HOME, créez les fichiers fa.c, fb.c et fc.c. En une seule ligne de commande, créez une copie de chacun de ces fichiers en rajoutant l’extension .backup. A la fin, vous devez avoir 6 fichiers : fa.c, fa.c.backup, fb.c, . . . Question 8 En une seule ligne de commande, changez l’extension des fichiers $HOME/f*.c en .txt. Question 9 Cherchez la chaîne "networking" sans tenir compte de la casse dans tous les fichiers présents dans /etc et ses sous-répertoires. 1 Question 10 Donner une commande qui affiche les 3 premiers répertoires de /dev, en excluant /dev lui même de la sortie. Exemple : /dev/rd /dev/ida /dev/ataraid Question 11 Copiez le fichier /etc/passwd en $HOME/mypasswd. Donnez une commande qui permet de ne garder que le nom de l’utilisateur sur chaque ligne, préfixé par "user : ". Exemple : $ cat mypasswd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh $ commande magique $ cat mypasswd user: root user: daemon user: bin Question 12 (bonus) Donnez une commande permettant de compter le nombre de lignes de vrai code dans un fichier source C++. Vous devez donc ignorer les lignes vides et les lignes commençant par ’//’. Exemple : $ cat > foo.cpp #include <iostream> // I love useless comments // to make meaningless lines of code int main() { // With two space before, it is more difficult. std::cout << "Hello World" << std::endl; } $ commande-magique foo.cpp 5 2 Module GNU/Linux CEFIPA : TP n◦4 - Scripting Nicolas Burrus 26 Avril 2005 But du TP Le but de ce TP est de créer un script trash.sh permettant d’émuler une "corbeille" sous Linux. Au lieu d’utiliser la commande rm, on pourra ensuite utiliser notre script trash.sh qui ne supprimera pas vraiment les fichiers ni les répertoires mais les compressera et les stockera dans un répertoire spécial. De cette façon, ils resteront récupérables. Question 1 Créer le script $HOME/trash.sh, qui se contente d’afficher "Vide" pour l’instant. Exemple : $ cd $ ./trash.sh Vide Question 2 Faire en sorte que le script puisse être appelé de n’importe où : $ cd /tmp $ trash.sh Vide Question 3 La variable spéciale "$@" regroupe tous les arguments du script (man sh). Écrire "$@" est équivalent à "$1" "$2" .... Faire en sorte que la sortie du script soit : $ trash.sh arg1 arg2 toto Argument: arg1 Argument: arg2 Argument: toto Question 4 Pour chaque argument, le script doit maintenant créer une archive contenant le fichier ou répertoire en argument, nommée argument.tar.gz, dans $HOME/trash. Exemple : $ cd $ touch file1 file2 $ mkdir dir1 $ touch dir1/file5 $ trash.sh file1 file2 dir1 $ ls ~/trash/ file1.tar.gz 1 file2.tar.gz dir1.tar.gz $ cd /tmp $ tar xfz ~/trash/dir1.tar.gz $ ls dir1 file5 S’il n’y a pas d’argument, le script doit afficher un message d’erreur et une aide d’utilisation : $ trash.sh Usage: trash.sh file [file] ... Question 5 Ajouter la date dans le nom de l’archive (on s’aidera de la commande date), exemple : $ cd $ trash.sh file1 file2 dir1 $ ls ~/trash/ dir1-25_Apr_05-19h55.tar.gz file1-25_Apr_05-19h55.tar.gz file2-25_Apr_05-19h55.tar.gz Question 6 Supprimez effectivement les arguments de trash.sh : $ cd $ trash.sh file1 file2 dir1 $ ls [vide] $ ls ~/trash/ dir1-25_Apr_05-19h55.tar.gz file1-25_Apr_05-19h55.tar.gz file2-25_Apr_05-19h55.tar.gz Question 7 (bonus) Faire en sorte que si aucun argument n’est donnée à trash.sh, il envoie le répertoire courant dans la corbeille : $ cd $ mkdir dir1 && touch dir1/file5 $ cd dir1 $ trash.sh $ cd $ ls [vide] $ ls ~/trash/ dir1-25_Apr_05-19h55.tar.gz 2 GNU Emacs Reference Card (for version 20) Starting Emacs To enter GNU Emacs 20, just type its name: emacs To read in a file to edit, see Files, below. Leaving Emacs suspend Emacs (or iconify it under X) exit Emacs permanently C-z C-x C-c Files read a file into Emacs save a file back to disk save all files insert contents of another file into this buffer replace this file with the file you really want write buffer to a specified file version control checkin/checkout C-x C-x C-x C-x C-x C-x C-x C-f C-s s i C-v C-w C-q Getting Help The help system is simple. Type C-h (or F1) and follow the directions. If you are a first-time user, type C-h t for a tutorial. remove help window scroll help window C-x 1 C-M-v apropos: show commands matching a string show the function a key runs describe a function get mode-specific information C-h C-h C-h C-h a c f m Motion Multiple Windows entity to move over character word line go to line beginning (or end) sentence paragraph page sexp function go to buffer beginning (or end) scroll scroll scroll scroll scroll backward C-b M-b C-p C-a M-a M-{ C-x [ C-M-b C-M-a M-< to next screen to previous screen left right current line to center of screen forward C-f M-f C-n C-e M-e M-} C-x ] C-M-f C-M-e M-> C-v M-v C-x < C-x > C-u C-l Killing and Deleting entity to kill character (delete, not kill) word line (to end of) sentence sexp When two commands are shown, the second is for “other frame.” delete all other windows C-x 1 split window, above and below C-x 2 C-x 5 2 delete this window C-x 0 C-x 5 0 split window, side by side C-x 3 scroll other window C-M-v switch cursor to another window C-x o C-x 5 o select buffer in other window C-x 4 b C-x 5 b display buffer in other window C-x 4 C-o C-x 5 C-o find file in other window C-x 4 f C-x 5 f find file read-only in other window C-x 4 r C-x 5 r run Dired in other window C-x 4 d C-x 5 d find tag in other window C-x 4 . C-x 5 . grow window taller C-x ^ shrink window narrower C-x { grow window wider C-x } Formatting backward DEL M-DEL M-0 C-k C-x DEL M-- C-M-k forward C-d M-d C-k M-k C-M-k kill region copy region to kill ring kill through next occurrence of char C-w M-w M-z char yank back last thing killed replace last yank with previous kill C-y M-y Marking indent current line (mode-dependent) indent region (mode-dependent) indent sexp (mode-dependent) indent region rigidly arg columns insert newline after point move rest of line vertically down delete blank lines around point join line with previous (with arg, next) delete all white space around point put exactly one space at point fill paragraph set fill column set prefix each line starts with set face Error Recovery set mark here exchange point and mark C-@ or C-SPC C-x C-x Case Change abort partially typed or executing command recover a file lost by a system crash M-x undo an unwanted change restore a buffer to its original contents M-x redraw garbaged screen set mark arg words away mark paragraph mark page mark sexp mark function mark entire buffer M-@ M-h C-x C-p C-M-@ C-M-h C-x h uppercase word lowercase word capitalize word uppercase region lowercase region C-g recover-file C-x u or C-_ revert-buffer C-l Incremental Search C-s C-r C-M-s C-M-r select previous search string select next later search string exit incremental search undo effect of last character abort current search M-p M-n RET DEL C-g Use C-s or C-r again to repeat the search in either direction. If Emacs is still searching, C-g cancels only the part not done. interactively replace a text string M-% using regular expressions M-x query-replace-regexp Valid responses in query-replace mode are replace this one, go on to next replace this one, don’t move skip to next without replacing replace all remaining matches back up to the previous match exit query-replace enter recursive edit (C-M-c to exit) c 1997 Free Software Foundation, Inc. Permissions on back. v2.2 1 M-u M-l M-c C-x C-u C-x C-l The Minibuffer Query Replace search forward search backward regular expression search reverse regular expression search TAB C-M-\ C-M-q C-x TAB C-o C-M-o C-x C-o M-^ M-\ M-SPC M-q C-x f C-x . M-g 2 SPC , DEL ! ^ RET C-r The following keys are defined in the minibuffer. complete as much as possible TAB complete up to one word SPC complete and execute RET show possible completions ? fetch previous minibuffer input M-p fetch later minibuffer input or default M-n regexp search backward through history M-r regexp search forward through history M-s abort command C-g Type C-x ESC ESC to edit and repeat the last command that used the minibuffer. Type F10 to activate the menu bar using the minibuffer. 3 GNU Emacs Reference Card Buffers select another buffer list all buffers kill a buffer C-x b C-x C-b C-x k Transposing transpose transpose transpose transpose characters words lines sexps C-t M-t C-x C-t C-M-t Spelling Check check spelling of current word check spelling of all words in region check spelling of entire buffer M-$ M-x ispell-region M-x ispell-buffer Tags find a tag (a definition) find next occurrence of tag specify a new tags file M-. C-u M-. M-x visit-tags-table regexp search on all files in tags table M-x tags-search run query-replace on all the files M-x tags-query-replace continue last tags search or query-replace M-, Regular Expressions any single character except a newline zero or more repeats one or more repeats zero or one repeat quote regular expression special character c alternative (“or”) grouping same text as nth group at word break not at word break entity line word buffer class of characters explicit set word-syntax character character with syntax c match start ^ \< \‘ match these [ ... ] \w \sc execute a shell command run a shell command on the region filter region through a shell command start a shell in window *shell* M-! M-| C-u M-| M-x shell . (dot) * + ? \c \| \( . . . \) \n \b \B match end $ \> \’ match others [^ . . . ] \W \Sc International Character Sets specify principal language M-x set-language-environment show all input methods M-x list-input-methods enable or disable input method C-\ set coding system for next command C-x RET c show all coding systems M-x list-coding-systems choose preferred coding system M-x prefer-coding-system copy rectangle to register kill rectangle yank rectangle open rectangle, shifting text right blank out rectangle prefix each line with a string C-x C-x C-x C-x C-x C-x r r r r r r r k y o c t Abbrevs C-x r s C-x r i save value of point in register jump to point saved in register C-x r SPC C-x r j Keyboard Macros start defining a keyboard macro C-x ( end keyboard macro definition C-x ) execute last-defined keyboard macro C-x e append to last keyboard macro C-u C-x ( name last keyboard macro M-x name-last-kbd-macro insert Lisp definition in buffer M-x insert-kbd-macro Commands Dealing with Emacs Lisp eval sexp before point eval current defun eval region read and eval minibuffer load from standard system directory C-x C-e C-M-x M-x eval-region M-: M-x load-library Simple Customization customize variables and faces M-x customize (global-set-key "\C-cg" ’goto-line) (global-set-key "\M-#" ’query-replace-regexp) C-h i C-h C-i Writing Commands Moving within a node: scroll forward scroll reverse beginning of node SPC DEL . (dot) (defun command-name (args) "documentation" (interactive "template") body) An example: Moving between nodes: Rectangles save region in register insert register contents into buffer Making global key bindings in Emacs Lisp (examples): Info enter the Info documentation reader find specified function or variable in Info Shells Registers next node previous node move up select menu item by name select nth menu item by number (1–9) follow cross reference (return with l) return to last node you saw return to directory node go to any node by name n p u m n f l d g (defun this-line-to-top-of-window (line) "Reposition line point is on to top of window. With ARG, put point on line ARG." (interactive "P") (recenter (if (null line) 0 (prefix-numeric-value line)))) The interactive spec says how to read arguments interactively. Type C-h f interactive for more details. Other: add global abbrev add mode-local abbrev add global expansion for this abbrev add mode-local expansion for this abbrev explicitly expand abbrev C-x C-x C-x C-x C-x expand previous word dynamically M-/ 4 a a a a a g l i g i l e run Info tutorial quit Info search nodes for regexp h q M-s c 1997 Free Software Foundation, Inc. Copyright v2.2 for GNU Emacs version 20, June 1997 designed by Stephen Gildea Permission is granted to make and distribute copies of this card provided the copyright notice and this permission notice are preserved on all copies. For copies of the GNU Emacs manual, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 5 6