Linux System Administration. Paul Cobbaut

Transcription

1 Linux System Administration Paul Cobbaut

2 Linux System Administration Paul Cobbaut lt-1.9 Publication date Sat 25 Apr 215 1:1:25 AM CEST Abstract This book is meant to be used in an instructor-led training. For self-study, the intent is to read this book next to a working Linux computer so you can immediately do every subject, practicing each command. This book is aimed at novice Linux system administrators (and might be interesting and useful for home users that want to know a bit more about their Linux system). However, this book is not meant as an introduction to Linux desktop applications like text editors, browsers, mail clients, multimedia or office applications. More information and free.pdf available at Feel free to contact the author: Paul Cobbaut: Contributors to the Linux Training project are: Serge van Ginderachter: build scripts and infrastructure setup Ywein Van den Brande: license and legal sections Hendrik De Vloed: buildheader.pl script We'd also like to thank our reviewers: Wouter Verhelst: Geert Goossens: geertgoossens Elie De Brauwer: Christophe Vandeplas: Bert Desmet: Rich Yonts: Copyright Paul Cobbaut

3 Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled 'GNU Free Documentation License'.

13 introduction to processes 1.1. terminology process A process is compiled source code that is currently running on the system PID All processes have a process id or PID PPID Every process has a parent process (with a PPID). The child process is often started by the parent process init The init process always has process ID 1. The init process is started by the kernel itself so technically it does not have a parent process. init serves as a foster parent for orphaned processes kill When a process stops running, the process dies, when you want a process to die, you kill it daemon Processes that start at system startup and keep running forever are called daemon processes or daemons. These daemons never die zombie When a process is killed, but it still shows up on the system, then the process is referred to as zombie. You cannot kill zombies, because they are already dead. 4

14 introduction to processes 1.2. basic process management $$ and $PPID Some shell environment variables contain information about processes. The $$ variable will hold your current process ID, and $PPID contains the parent PID. Actually $$ is a shell parameter and not a variable, you cannot assign a value to it. Below we use echo to display the values of $$ and $PPID. ~]$ echo $$ $PPID pidof You can find all process id's by name using the pidof command. ~# pidof mingetty parent and child Processes have a parent-child relationship. Every process has a parent process. When starting a new bash you can use echo to verify that the pid from before is the ppid of the new shell. The child process from above is now the parent process. ~]$ bash ~]$ echo $$ $PPID Typing exit will end the current process and brings us back to our original values for $$ and $PPID exit ~]$ echo $$ $PPID ~]$ exit ~]$ echo $$ $PPID ~]$ 5

15 introduction to processes fork and exec A process starts another process in two phases. First the process creates a fork of itself, an identical copy. Then the forked process executes an exec to replace the forked process with the target child process ~]$ echo $$ ~]$ bash ~]$ echo $$ $PPID ~]$ exec With the exec command, you can execute a process without forking a new process. In the following screenshot a Korn shell (ksh) is started and is being replaced with a bash shell using the exec command. The pid of the bash shell is the same as the pid of the Korn shell. Exiting the child bash shell will get me back to the parent bash, not to the Korn shell (which does not exist anymore). ~]$ 4224 ~]$ $ echo $$ $PPID $ exec bash ~]$ ~]$ exit ~]$ 4224 echo $$ # PID of bash ksh # PID of ksh and bash echo $$ $PPID # PID of bash and bash exit echo $$ 6

17 introduction to processes pgrep Similar to the ps -C, you can also use pgrep to search for a process by its command name. ~]$ sleep 1 & [1] ~]$ pgrep sleep ~]$ ps -C sleep PID TTY TIME CMD pts/3 :: sleep You can also list the command name of the process with pgrep. pgrep -l sleep 9661 sleep top Another popular tool on Linux is top. The top tool can order processes according to cpu usage or other properties. You can also kill processes from within top. Press h inside top for help. In case of trouble, top is often the first tool to fire up, since it also provides you memory and swap space information. 8

19 introduction to processes kill -15 (SIGTERM) The SIGTERM signal is also called a standard kill. Whenever kill is executed without specifying the signal, a kill -15 is assumed. Both commands in the screenshot below are identical. kill 1942 kill kill -9 (SIGKILL) The SIGKILL is different from most other signals in that it is not being sent to the process, but to the Linux kernel. A kill -9 is also called a sure kill. The kernel will shoot down the process. As a developer you have no means to intercept a kill -9 signal. ~# kill SIGSTOP and SIGCONT A running process can be suspended when it receives a SIGSTOP signal. This is the same as kill -19 on Linux, but might have a different number in other Unix systems. A suspended process does not use any cpu cycles, but it stays in memory and can be reanimated with a SIGCONT signal (kill -18 on Linux). Both signals will be used in the section about background processes. 1

20 introduction to processes pkill You can use the pkill command to kill a process by its command name. ~]$ sleep 1 & [1] 323 ~]$ pkill sleep [1]+ Terminated ~]$ sleep killall The killall command will send a signal 15 to all processes with a certain name. sleep 8472 & [1] 1878 sleep 121 & [2] jobs [1]- Running [2]+ Running killall sleep [1]- Terminated [2]+ Terminated jobs sleep 8472 & sleep 121 & sleep 8472 sleep killall5 Its SysV counterpart killall5 can by used when shutting down the system. This screenshot shows how Red Hat Enterprise Linux 5.3 uses killall5 when halting the system. ~# grep killall /etc/init.d/halt action $"Sending all processes the TERM signal..." /sbin/killall5-15 action $"Sending all processes the KILL signal..." /sbin/killall top Inside top the k key allows you to select a signal and pid to kill. Below is a partial screenshot of the line just below the summary in top after pressing k. PID to kill: 1932 Kill PID 1932 with signal [15]: 9 11

21 introduction to processes 1.4. practice : basic process management 1. Use ps to search for the init process by name. 2. What is the process id of the init process? 3. Use the who am i command to determine your terminal name. 4. Using your terminal name from above, use ps to find all processes associated with your terminal. 5. What is the process id of your shell? 6. What is the parent process id of your shell? 7. Start two instances of the sleep 3342 in background. 8. Locate the process id of all sleep commands. 9. Display only those two sleep processes in top. Then quit top. 1. Use a standard kill to kill one of the sleep processes. 11. Use one command to kill all sleep processes. 12

23 introduction to processes sleep 3342 & sleep 3342 & 8. Locate the process id of all sleep commands. pidof sleep 9. Display only those two sleep processes in top. Then quit top. top -p pidx,pidy (replace pidx pidy with the actual numbers) 1. Use a standard kill to kill one of the sleep processes. kill pidx 11. Use one command to kill all sleep processes. pkill sleep 14

24 Chapter 2. process priorities 15

25 process priorities 2.1. priority and nice values introduction All processes have a priority and a nice value. Higher priority processes will get more cpu time than lower priority processes. You can influence this with the nice and renice commands pipes (mkfifo) Processes can communicate with each other via pipes. These pipes can be created with the mkfifo command. The screenshots shows the creation of four distinct pipes (in a new directory). mkdir procs cd procs/ mkfifo pipe33a pipe33b pipe42a pipe42b ls -l total prw-r--r-- 1 paul paul :21 pipe33a prw-r--r-- 1 paul paul :21 pipe33b prw-r--r-- 1 paul paul :21 pipe42a prw-r--r-- 1 paul paul :21 pipe42b some fun with cat To demonstrate the use of the top and renice commands we will make the cat command use the previously created pipes to generate a full load on the cpu. The cat is copied with a distinct name to the current directory. (This enables us to easily recognize the processes within top. You could do the same exercise without copying the cat command, but using different users. Or you could just look at the pid of each process.) [1] 167 [2] 1671 [3] 1673 [4] 1674 cp /bin/cat proj33 cp /bin/cat proj42 echo -n x./proj33 - pipe33a > pipe33b &./proj33 <pipe33b >pipe33a & echo -n z./proj42 - pipe42a > pipe42b &./proj42 <pipe42b >pipe42a & The commands you see above will create two proj33 processes that use cat to bounce the x character between pipe33a and pipe33b. And ditto for the z character and proj42. 16

26 process priorities top Just running top without options or arguments will display all processes and an overview of innformation. The top of the top screen might look something like this. top - 13:59:29 up 48 min, 4 users, load average: 1.6,.25,.14 Tasks: 139 total, 3 running, 136 sleeping, stopped, zombie Cpu(s):.3%us, 99.7%sy,.%ni,.%id,.%wa,.%hi,.%si,.%st Mem: 59352k total, 464k used, 49312k free, 66752k buffers Swap: 74698k total, k used, 74698k free, k cached Notice the cpu idle time (.%id) is zero. This is because our cat processes are consuming the whole cpu. Results can vary on systems with four or more cpu cores top -p The top -p 167,1671,1673,1674 screenshot below shows four processes, all of then using approximately 25 percent of the cpu. top -p 167,1671,1673,1674 PID USER paul paul paul paul PR NI VIRT RES SHR S S R S R %CPU %MEM TIME+ :11.92 :23.16 :23.7 :11.48 COMMAND proj42 proj33 proj33 proj42 All four processes have an equal priority (PR), and are battling for cpu time. On some systems the Linux kernel might attribute slightly varying priority values, but the result will still be four processes fighting for cpu time renice Since the processes are already running, we need to use the renice command to change their nice value (NI). The screenshot shows how to use renice on both the proj33 processes. renice : old priority, new priority 8 renice : old priority, new priority 8 Normal users can attribute a nice value from zero to 2 to processes they own. Only the root user can use negative nice values. Be very careful with negative nice values, since they can make it impossible to use the keyboard or ssh to a system. 17

27 process priorities impact of nice values The impact of a nice value on running processes can vary. The screenshot below shows the result of our renice +8 command. Look at the %CPU values. PID USER paul paul paul paul PR NI 8 8 VIRT RES SHR S %CPU %MEM S R S R TIME+ :22.37 :21.65 :29.65 :29.82 COMMAND proj42 proj42 proj33 proj33 Important to remember is to always make less important processes nice to more important processes. Using negative nice values can have a severe impact on a system's usability nice The nice works identical to the renice but it is used when starting a command. The screenshot shows how to start a script with a nice value of five. nice -5./backup.sh 18

28 process priorities 2.2. practice : process priorities 1. Create a new directory and create six pipes in that directory. 2. Bounce a character between two pipes. 3. Use top and ps to display information (pid, ppid, priority, nice value,...) about these two cat processes. 4. Bounce another character between two other pipes, but this time start the commands nice. Verify that all cat processes are battling for the cpu. (Feel free to fire up two more cats with the remaining pipes). 5. Use ps to verify that the two new cat processes have a nice value. Use the -o and -C options of ps for this. 6. Use renice te increase the nice value from 1 to 15. Notice the difference with the usual commands. 19

32 background jobs 3.1. background processes jobs Stuff that runs in background of your current shell can be displayed with the jobs command. By default you will not have any jobs running in background. ~# jobs ~# This jobs command will be used several times in this section control-z Some processes can be suspended with the Ctrl-Z key combination. This sends a SIGSTOP signal to the Linux kernel, effectively freezing the operation of the process. When doing this in vi(m), then vi(m) goes to the background. The background vi(m) can be seen with the jobs command. ~]$ vi procdemo.txt [5]+ Stopped ~]$ jobs [5]+ Stopped vim procdemo.txt vim procdemo.txt & ampersand Processes that are started in background using the & character at the end of the command line are also visible with the jobs command. ~]$ find / > allfiles.txt 2> /dev/null & [6] 523 ~]$ jobs [5]+ Stopped vim procdemo.txt [6]- Running find / >allfiles.txt 2>/dev/null & ~]$ jobs -p An interesting option is jobs -p to see the process id of background processes. [1] 492 [2] ~]$ sleep 5 & ~]$ sleep 4 & ~]$ jobs -p ~]$ ps `jobs -p` 23

Linux System Administration Paul Cobbaut Linux System Administration Paul Cobbaut Publication date 215-5-24 CEST Abstract This book is meant to be used in an instructor-led training. For self-study, the

Linux System Administration on Red Hat Kenneth Ingham September 29, 2009 1 Course overview This class is for people who are familiar with Linux or Unix systems as a user (i.e., they know file manipulation,

HARFORD COMMUNITY COLLEGE 401 Thomas Run Road Bel Air, MD 21015 Course Outline CIS 110 - INTRODUCTION TO UNIX Course Description: This is an introductory course designed for users of UNIX. It is taught

ENTERPRISE LINUX SYSTEM ADMINISTRATION The GL250 is an in-depth course that explores installation, configuration and maintenance of Linux systems. The course focuses on issues universal to every workstation

WES 9.2 DRIVE CONFIGURATION WORKSHEET This packet will provide you with a paper medium external to your WES box to write down the device names, partitions, and mount points within your machine. You may

Unit 24: Linux+ Learning Outcomes A candidate following a programme of learning leading to this unit will be able to: Demonstrate knowledge of planning the implementation Show knowledge of how to install

Red Hat Enterprise Linux 7- RH124 Red Hat System Administration I Red Hat System Administration 1(RH124) is Designed for IT Professionals who are new to Linux. This course will actively engage students

Chapter 8: Installing Linux The Complete Guide To Linux System Administration Modified by M. L. Malone, 11/05 At the end of this chapter the successful student will be able to Describe the main hardware

Recovering Data from Windows Systems by Using Linux Published by the Open Source Software Lab at Microsoft. November 2007. Special thanks to Chris Travers, Contributing Author to the Open Source Software

Advanced Linux System Administration on Red Hat Kenneth Ingham September 29, 2009 1 Course overview This class is for people who are familiar with basic Linux administration (i.e., they know users, packages,

Overview Last Lecture Scripting This Lecture Linux/Unix file system Next Lecture System installation Sources Installation and Getting Started Guide Linux System Administrators Guide Chapter 6 in Principles

Backtrack 4 Bootable USB Thumb Drive with Full Disk Encryption This is a step-by-step guide showing how to create an encrypted bootable Backtrack 4 USB thumb drive. I put quotes around full in the title

In today s IT environments, administrators need to learn and understand how to make all of the components that provide services to their users work together. These components include network devices, servers,

Introduction to Operating Systems It is important that you familiarize yourself with Windows and Linux in preparation for this course. The exercises in this book assume a basic knowledge of both of these

Cloning Complex Linux Servers Cloning A Linux Machine That Has A Complex Storage Setup Where I work we have Cent OS servers whose drives are in various software raid and LVM2 configurations. I was recently

GUIDE FOR Zentyal Server is a small business server based on Ubuntu s LTS server version 10.04 and the ebox platform. It also has the LXDE desktop installed with Firefox web browser and PCMAN File manager.

Replacing a Laptop Hard Disk On Linux Khalid Baheyeldin http://2bits.com KWLUG, September 2015 About Khalid 30 years in software development and software consulting First computer: Sinclair ZX Spectrum

Linux Software Raid Aug 2010 Mark A. Davis a What is RAID? Redundant Array of Inexpensive/Independent Drives It is a method of combining more than one hard drive into a logic unit for the purpose of: Increasing

Navigating the Rescue Mode for Linux SUPPORT GUIDE DEDICATED SERVERS ABOUT THIS GUIDE This document will take you through the process of booting your Linux server into rescue mode to identify and fix the

GLS250 "Enterprise Linux Systems Administration" Intended for students already comfortable with working in the Unix environment, this in-depth course helps students acquire the variety of skills needed

Ever want those pesky little Linux syslog messages (/var/log/messages) to forward to IBM Director? Well, it s not built in, but it s pretty easy to setup. You can forward syslog messages from an IBM Director

Setting up PostgreSQL 1 Introduction to PostgreSQL PostgreSQL is an object-relational database management system based on POSTGRES, which was developed at the University of California at Berkeley. PostgreSQL

This section describes how to install and configure the Dell small computer system interface (SCSI) device drivers included with your Dell PowerEdge 1400 computer system. These device drivers are designed

Embedded MegaRAID SATA User's Guide Areas Covered Before Reading This Manual This section explains the notes for your safety and conventions used in this manual. Chapter 1 Overview This chapter introduces

Red Hat Certifications: Red Hat Certified System Administrator (RHCSA) Overview Red Hat is pleased to announce a new addition to its line of performance-based certifications Red Hat Certified System Administrator

QWERTYUIOP{ Linux Troubleshooting 5 Days The class is designed to give Linux administrators experience with both common and uncommon system problems. The course is based on the idea that the best way to

Procedure to Create and Duplicate Master LiveUSB Stick A. Creating a Master LiveUSB stick using 64 GB USB Flash Drive 1. Formatting USB stick having Linux partition (skip this step if you are using a new

System Backup Strategies Objective At the conclusion of this module, the student will be able to: describe the necessity for creating a backup regimen describe the advantages and disadvantages of the most

MegaRAID SAS User's Guide Areas Covered Before Reading This Manual This section explains the notes for your safety and conventions used in this manual. Chapter 1 Overview This chapter explains an overview

Cristie Bare Machine Recovery Restoring a Suse Linux Enterprise Server 9 64 Bit on Dissimilar Hardware with CBMR for Linux 1.02 This documentation shows how to restore or migrate a Linux system on dissimilar

ThinkServer RD540 and RD640 Operating System Installation Guide Note: Before using this information and the product it supports, be sure to read and understand the Read Me First and Safety, Warranty, and

I. Introducing IO Monitoring Disk IO subsystems are the slowest part of any Linux system. This is due mainly to their distance from the CPU and the fact that disks require the physics to work (rotation