CompTIA Linuxþ Guide to Linux Certification

CompTIA Linuxþ Guide to Linux Certification
Domain 105: Shells, Scripting, and Data Management
Objective
Chapter
105.1 Customize and use the shell environment
7
105.2 Customize or write simple scripts
7
105.3 SQL data management
13
Domain 106: User Interfaces and Desktops
Objective
Chapter
106.1 Install and configure X11
2, 6, 8, 14
106.2 Setup a display manager
8
106.3 Accessibility
14
Domain 107: Administrative Tasks
Objective
Chapter
107.1 Manage user and group accounts and related system files
10
107.2 Automate system administration tasks by scheduling jobs
9
107.3 Localization and internationalization
2, 13
Domain 108: Essential System Services
Objective
Chapter
108.1 Maintain system time
2, 13
108.2 System logging
10, 14
108.3 Mail Transfer Agent (MTA) basics
1, 13
108.4 Manage printers and printing
10
Domain 109: Networking Fundamentals
Objective
Chapter
109.1 Fundamentals of Internet protocols
12
109.2 Basic network configuration
12
109.3 Basic network troubleshooting
12
109.4 Configure client side DNS
12
Domain 110: Security
Objective
Chapter
110.1 Perform security administration tasks
4, 10, 12, 14
110.2 Set up host security
10, 12, 14
110.3 Securing data with encryption
12, 14
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
CompTIA Linux+ Guide to Linux
Certification
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
CompTIA Linux+ Guide to Linux
Certification
Fourth Edition
Jason W. Eckert
triOS College
Australia • Brazil • Mexico • Singapore • United Kingdom • United States
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial
review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to
remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous
editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by
ISBN#, author, title, or keyword for materials in your areas of interest.
Important Notice: Media content referenced within the product description or the product text may not be available in the eBook version.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
CompTIA Linuxþ Guide to Linux
Certification, Fourth Edition
Jason W. Eckert and triOS College
SVP, GM Skills & Global Product
Management: Dawn Gerrain
Product Development Manager:
Leigh Hefferon
Managing Content Developer: Emma Newsom
Senior Content Developer: Natalie Pashoukos
Development Editors: Ann Shaffer,
Kent Williams
© 2016, 2012, 2006 Cengage Learning
WCN: 022-200-203
ALL RIGHTS RESERVED. No part of this work covered by the
copyright herein may be reproduced, transmitted, stored, or used in any form
or by any means graphic, electronic, or mechanical, including but not limited
to photocopying, recording, scanning, digitizing, taping, Web distribution,
information networks, or information storage and retrieval systems, except
as permitted under Section 107 or 108 of the 1976 United States Copyright
Act, without the prior written permission of the publisher.
Source: Fedora
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706
Product Assistant: Scott Finger
Vice President, Marketing Services:
Jennifer Ann Baker
For permission to use material from this text or product,
submit all requests online at www.cengage.com/permissions
Further permissions questions can be e-mailed to
[email protected]
Senior Marketing Manager: Eric La Scola
Production Director: Patty Stephan
Senior Content Project Manager:
Brooke Greenhouse
Managing Art Director: Jack Pendleton
Software Development Manager:
Pavan Ethakota
Technical Edit/Quality Assurance:
Serge Palladino, Danielle Shaw
Cover Image: © HunThomas/Shutterstock.com
Library of Congress Control Number: 2014958602
ISBN: 978-1-305-10714-4
Package ISBN: 978-1-305-10716-8
Cengage Learning
20 Channel Center Street
Boston, MA 02210
USA
Cengage Learning is a leading provider of customized learning solutions with
office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan. Locate your local office at:
www.cengage.com/global
Cengage Learning products are represented in Canada by
Nelson Education, Ltd.
To learn more about Cengage Learning, visit www.cengage.com
Purchase any of our products at your local college store or at our preferred
online store www.cengagebrain.com
Notice to the Reader
Publisher does not warrant or guarantee any of the products described herein or perform any independent analysis in connection with any of the product
information contained herein. Publisher does not assume, and expressly disclaims, any obligation to obtain and include information other than that provided to it
by the manufacturer. The reader is expressly warned to consider and adopt all safety precautions that might be indicated by the activities described herein and to
avoid all potential hazards. By following the instructions contained herein, the reader willingly assumes all risks in connection with such instructions. The publisher
makes no representations or warranties of any kind, including but not limited to, the warranties of fitness for particular purpose or merchantability, nor are any
such representations implied with respect to the material set forth herein, and the publisher takes no responsibility with respect to such material. The publisher
shall not be liable for any special, consequential, or exemplary damages resulting, in whole or part, from the readers’ use of, or reliance upon, this material.
Printed in the United States of America
Print Number: 01
Print Year: 2015
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Brief Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
CHAPTER 1
Introduction to Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Linux Installation and Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
CHAPTER 3
Exploring Linux Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
CHAPTER 4
Linux Filesystem Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
CHAPTER 5
Linux Filesystem Administration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
CHAPTER 6
Linux Server Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
CHAPTER 7
Working with the BASH Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
CHAPTER 8
System Initialization and X Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
CHAPTER 9
Managing Linux Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
CHAPTER 10
Common Administrative Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
CHAPTER 11
Compression, System Backup, and Software Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
CHAPTER 12
Network Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
CHAPTER 13
Configuring Network Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
CHAPTER 14
Troubleshooting, Performance, and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
APPENDIX A
Certification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
APPENDIX B
GNU Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
APPENDIX C
Finding Linux Resources on the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
GLOSSARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
vii
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
CHAPTER 1
Introduction to Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The Linux Operating System. . . . . . . . . . . . .
Versions of the Linux Operating System . .
Identifying Kernel Versions . . . . . . . . . .
Licensing Linux . . . . . . . . . . . . . . . . .
Linux Advantages . . . . . . . . . . . . . . . .
The History of Linux . . . . . . . . .
UNIX . . . . . . . . . . . . . . . .
The Hacker Culture . . . . . . .
Linux . . . . . . . . . . . . . . . .
................................................4
......................................... 4
......................................... 5
......................................... 6
......................................... 9
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Linux Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Common Uses of Linux . . . . . . . . . . . . . . . .
Internet Servers . . . . . . . . . . . . . . . . . .
File and Print Servers . . . . . . . . . . . . . .
Application Servers . . . . . . . . . . . . . . .
Cloud Systems . . . . . . . . . . . . . . . . . .
Supercomputers . . . . . . . . . . . . . . . . .
Scientific/Engineering Workstation . . . . .
Office/Personal Workstation . . . . . . . . .
Mobile Devices . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
.
.
.
.
.
.
.
.
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
CHAPTER 2
Linux Installation and Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Installing Linux . . . . . . . . . . . . . . . . . . . . . .
Preparing for Installation . . . . . . . . . . .
Understanding Installation Media . . . . . .
Performing the Installation . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Basic Linux Usage . . . . . . . . . . . . . . . . . . . .
Shells, Terminals, and the Kernel . . . . . .
Basic Shell Commands . . . . . . . . . . . . .
Shell Metacharacters . . . . . . . . . . . . . .
Getting Command Help . . . . . . . . . . . .
Shutting Down the Linux System . . . . . .
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
ix
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
x
Table of Contents
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Hands-on Projects
Project 2-1. . .
Project 2-2. . .
Project 2-3. . .
Project 2-4. . .
Project 2-5. . .
Project 2-6. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
CHAPTER 3
Exploring Linux Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Linux Directory Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Changing Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Viewing Files and Directories
File Types . . . . . . . . . .
Filenames . . . . . . . . . .
Listing Files . . . . . . . . .
Wildcard Metacharacters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
.
.
.
.
Displaying the Contents of Text Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Displaying the Contents of Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Searching for Text Within Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The grep Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Editing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The vi Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Other Common Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Hands-on Projects
Project 3-1. . .
Project 3-2. . .
Project 3-3. . .
Project 3-4. . .
Project 3-5. . .
Project 3-6. . .
Project 3-7. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
.
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
CHAPTER 4
Linux Filesystem Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
The Filesystem Hierarchy Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Managing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Finding Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
xi
Linking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
File and Directory Permissions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File and Directory Ownership . . . . . . . . . . . . . . . . . . . . . . . .
Managing File and Directory Permissions . . . . . . . . . . . . . . . . .
Default Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Special Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Custom Permissions in the Access Control List (ACL) . . . .
Managing Filesystem Attributes . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
. . . . . . . . . . . . . . . . . . . . . . . . 150
. . . . . . . . . . . . . . . . . . . . . . . . 153
. . . . . . . . . . . . . . . . . . . . . . . . 160
. . . . . . . . . . . . . . . . . . . . . . . . 162
. . . . . . . . . . . . . . . . . . . . . . . . 165
. . . . . . . . . . . . . . . . . . . . . . . . 167
.
.
.
.
.
.
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Hands-on Projects
Project 4-1. . .
Project 4-2. . .
Project 4-3. . .
Project 4-4. . .
Project 4-5. . .
Project 4-6. . .
Project 4-7. . .
Project 4-8. . .
Project 4-9. . .
Project 4-10 . .
Project 4-11 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
.
.
.
.
.
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
CHAPTER 5
Linux Filesystem Administration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
The /dev Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Filesystems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Filesystem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Mounting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Working with Floppy Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Working with CDs, DVDs, and ISO Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Working with Hard Disks and SSDs . . . . . . . . . . . . . . . .
Standard Hard Disk Partitioning . . . . . . . . . . . . . . .
Working with Standard Hard Disk Partitions . . . . . .
Working with the LVM . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Working with USB and Firewire-based Storage Devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Monitoring Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Disk Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Checking Filesystems for Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Hard Disk Quotas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Hands-on Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xii
Table of Contents
Project
Project
Project
Project
Project
Project
Project
5-1.
5-2.
5-3.
5-4.
5-5.
5-6.
5-7.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
245
246
247
247
249
251
252
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
CHAPTER 6
Linux Server Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Understanding Server Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Configuring Server Storage . . . . . . . . . . . . . .
SCSI Hard Disk Configuration . . . . . . . .
RAID Configuration . . . . . . . . . . . . . .
ZFS Configuration . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Installing a Linux Server Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Dealing with Problems During Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Dealing with Problems After Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
System Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Hands-on Projects
Project 6-1. . .
Project 6-2. . .
Project 6-3. . .
Project 6-4. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
CHAPTER 7
Working with the BASH Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Command Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Shell Variables . . . . . . . . . . . . . .
Environment Variables . . . . .
User-Defined Variables . . . . .
Other Variables . . . . . . . . .
Environment Files . . . . . . . .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Shell Scripts . . . . . . . . . . . . . . . .
Escape Sequences . . . . . . . .
Reading Standard Input . . . .
Decision Constructs . . . . . . .
Loop Constructs . . . . . . . . .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
xiii
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Hands-on Projects
Project 7-1. . .
Project 7-2. . .
Project 7-3. . .
Project 7-4. . .
Project 7-5. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
CHAPTER 8
System Initialization and X Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
The Boot Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Boot Loaders . . . . . .
LILO . . . . . . . .
GRUB . . . . . . .
GRUB2. . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Linux Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Working with the UNIX SysV System Initialization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Working with the Systemd System Initialization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
The X Windows System . . . . . . . . . . . . . . . .
Linux GUI Components . . . . . . . . . . . .
Starting and Stopping X Windows . . . . .
Configuring X Windows . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Hands-on Projects
Project 8-1. . .
Project 8-2. . .
Project 8-3. . .
Project 8-4. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
CHAPTER 9
Managing Linux Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Linux Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Viewing Processes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Killing Processes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Process Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Running Processes in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Process Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Scheduling Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Scheduling Commands with atd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Scheduling Commands with cron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xiv
Table of Contents
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Hands-on Projects
Project 9-1. . .
Project 9-2. . .
Project 9-3. . .
Project 9-4. . .
Project 9-5. . .
Project 9-6. . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
CHAPTER 10
Common Administrative Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Printer Administration . . . . . . . . . . . . . . . . .
The Common UNIX Printing System . . . .
Managing Print Jobs . . . . . . . . . . . . . .
The LPD Printing System . . . . . . . . . . .
Configuring Printers . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
.
.
.
.
Log File Administration . . . . . . . . . . . . . . . . . . . . . . . . .
Working with the System Log Daemon . . . . . . . . . .
Working with the Systemd Journal Daemon . . . . . . .
Managing Log Files and the journald Database . . . . .
Administering Users and Groups .
Creating User Accounts . . . .
Modifying User Accounts . . .
Deleting User Accounts . . . . .
Managing Groups . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
.
.
.
.
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Hands-on Projects
Project 10-1 . .
Project 10-2 . .
Project 10-3 . .
Project 10-4 . .
Project 10-5 . .
Project 10-6 . .
Project 10-7 . .
Project 10-8 . .
Project 10-9 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
.
.
.
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
CHAPTER 11
Compression, System Backup, and Software Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Compression . . . . . .
Using compress . .
Using GNU Zip .
Using bzip2 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
System Backup. . . . . . . . . . . . . .
Using Tape Archive (tar) . . . .
Using Copy In/Out (cpio) . . .
Using dump/restore . . . . . . .
Using Burning Software . . . .
xv
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
.
.
.
.
Software Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compiling Source Code into Programs . . . . . . . . . . .
Working with the Red Hat Package Manager (RPM) .
Working with the Debian Package Manager (DPM) . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Hands-on Projects
Project 11-1 . .
Project 11-2 . .
Project 11-3 . .
Project 11-4 . .
Project 11-5 . .
Project 11-6 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
CHAPTER 12
Network Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
The TCP/IP Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
The IPv4 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
The IPv6 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Configuring a Network Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Configuring a PPP Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Name Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Network Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Remote Administration . . . . . . . . . . . . . . . . .
Telnet . . . . . . . . . . . . . . . . . . . . . . . .
Secure Shell (SSH) . . . . . . . . . . . . . . . .
Virtual Network Computing (VNC) . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Hands-on Projects
Project 12-1 . .
Project 12-2 . .
Project 12-3 . .
Project 12-4 . .
Project 12-5 . .
Project 12-6 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
.
.
.
.
.
.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xvi
Table of Contents
Project 12-7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
CHAPTER 13
Configuring Network Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Infrastructure
DHCP . .
DNS . . .
NTP . . .
NIS . . . .
Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
File Sharing Services .
Samba . . . . . . .
NFS . . . . . . . . .
FTP . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
E-mail Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Database Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Configuring PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Configuring PostgreSQL Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Hands-on Projects
Project 13-1 . .
Project 13-2 . .
Project 13-3 . .
Project 13-4 . .
Project 13-5 . .
Project 13-6 . .
Project 13-7 . .
Project 13-8 . .
Project 13-9 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
.
.
.
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
CHAPTER 14
Troubleshooting, Performance, and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Troubleshooting Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Resolving Common System Problems. . . . . . .
Hardware-Related Problems . . . . . . . . .
Software-Related Problems . . . . . . . . . .
User Interface-Related Problems . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Performance Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Monitoring Performance with sysstat Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Other Performance Monitoring Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Securing the Local Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents
xvii
Protecting Against Network Attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Using Encryption to Protect Network Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Detecting Intrusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Review Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Hands-on Projects
Project 14-1 . .
Project 14-2 . .
Project 14-3 . .
Project 14-4 . .
Project 14-5 . .
Project 14-6 . .
Project 14-7 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
.
.
.
.
.
.
.
Discovery Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
APPENDIX A
Certification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
APPENDIX B
GNU Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
APPENDIX C
Finding Linux Resources on the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
GLOSSARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Introduction
“In a future that includes competition from open source, we can expect that the
eventual destiny of any software technology will be to either die or become part
of the open infrastructure itself.”
Eric S. Raymond, The Cathedral & the Bazaar: Musings on Linux
and Open Source by an Accidental Revolutionary
As Eric S. Raymond reminds us, open source software will continue to shape the
dynamics of the computer software industry for the next long while, just as it has for the
last decade. Coined and perpetuated by hackers, the term “open source software” refers to
software in which the source code is freely available to anyone who wishes to improve it
(usually through collaboration). And, of course, at the heart of the open source software
movement lies Linux—an operating system whose rapid growth has shocked the world by
demonstrating the nature and power of the open source model.
However, as Linux continues to grow, so must the number of Linux-educated users, administrators, developers, and advocates. Thus we find ourselves in a time where Linux education is of great importance to the information technology industry. Key to demonstrating
Linux ability is the certification process. This book, CompTIA Linuxþ Guide to Linux Certification, uses carefully constructed examples, questions, and practical exercises to prepare
readers with the necessary information to achieve the sought-after Linuxþ Powered by LPI
xix
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xx
Introduction
certification from CompTIA. Whatever your ultimate goal, you can be assured that reading
this book in combination with study, creativity, and practice will make the open source
world come alive for you as it has for many others.
The Intended Audience
Simply put, this book is intended for those who wish to learn the Linux operating system
and master the Linuxþ certification exam from CompTIA. It does not assume any prior
knowledge of Linux. Also, the topics introduced in this book and the certification exam
are geared towards systems administration, yet they are also well suited for those who will
use or develop programs for Linux systems.
Chapter Descriptions
Chapter 1, “Introduction to Linux,” introduces operating systems as well as the features,
benefits, and uses of the Linux operating system. This chapter also discusses the history
and development of Linux and open source software.
Chapter 2, “Linux Installation and Usage,” outlines the procedures necessary to prepare for
and install Linux on a typical computer system. This chapter also describes how to interact
with a Linux system via a terminal and enter basic commands into a Linux shell, such as
those used to obtain help and properly shut down the system.
Chapter 3, “Exploring Linux Filesystems,” outlines the Linux filesystem structure and the
types of files that can be found within it. This chapter also discusses commands that can
be used to view and edit the content of those files.
Chapter 4, “Linux Filesystem Management,” covers the commands you can use to locate
and manage files and directories on a Linux filesystem. Furthermore, this chapter outlines
the different methods used to link files as well as how to interpret and set file and directory
permissions.
Chapter 5, “Linux Filesystem Administration,” discusses how to create, mount, and manage
filesystems in Linux. This chapter also discusses the various filesystems available for Linux
systems and the device files that are used to refer to the devices that may contain these
filesystems.
Chapter 6, “Linux Server Deployment,” introduces the types of configurations and installation considerations that may prove useful when installing Linux in a server environment.
This chapter also discusses common problems that may occur during installation, system
rescue, and the ZFS distributed filesystem commonly used on large-scale Linux server
systems.
Chapter 7, “Working with the BASH Shell,” covers the major features of the BASH shell,
including redirection, piping, variables, aliases, and environment files. Also, this chapter
details the syntax of basic shell scripts.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Introduction
xxi
Chapter 8, “System Initialization and X Windows,” covers the different bootloaders that
may be used to start the Linux kernel. This chapter also discusses how daemons are started
during system initialization as well as how to start and stop them afterwards. Finally, this
chapter discusses the structure of Linux graphical user interfaces as well as their configuration and management.
Chapter 9, “Managing Linux Processes,” covers the different types of processes as well as
how to view their attributes, change their priority, and kill them. Furthermore, this chapter
discusses how to schedule processes to occur in the future using various utilities.
Chapter 10, “Common Administrative Tasks,” details three important areas of system
administration: printer administration, log file administration, and user administration.
Chapter 11, “Compression, System Backup, and Software Installation,” describes utilities
that are commonly used to compress and back up files on a Linux filesystem. This chapter
also discusses how to install software from source code as well as how to use the Red Hat
Package Manager and the Debian Package Manager.
Chapter 12, “Network Configuration,” introduces networks, network utilities, and the
TCP/IP protocol as well as how to configure the TCP/IP protocol on a NIC or PPP interface. In addition, this chapter details the configuration of name resolution and common networking services and discusses the technologies you can use to administer Linux servers
remotely.
Chapter 13, “Configuring Network Services,” explores the detailed configuration of key
infrastructure, Web, file sharing, e-mail, and database network services. More specifically,
this chapter examines the structure and configuration of DHCP, DNS, NTP, NIS, FTP,
NFS, Samba, Apache Web server, Postfix, and PostgreSQL services.
Chapter 14, “Troubleshooting, Performance, and Security,” discusses the system maintenance cycle as well as good troubleshooting procedures for solving hardware and software
problems. This chapter also describes utilities you can use to monitor and pinpoint the
cause of performance problems as well as utilities and procedures you can use to secure the
Linux system against unauthorized access.
Additional information is contained in the appendices at the rear of the book. Appendix A
discusses the certification process, with emphasis on the Linuxþ Powered by LPI certification. It also explains how the objective list for the Linuxþ Powered by LPI certification
matches each chapter in the textbook. Appendix B provides a copy of the GNU Public
License. Appendix C explains how to find Linux resources on the Internet and lists some
common resources by category.
Features
To ensure a successful learning experience, this book includes the following pedagogical
features:
•
Chapter objectives—Each chapter in this book begins with a detailed list of the concepts
to be mastered within that chapter. This list provides you with a quick reference to the
contents of that chapter as well as a useful study aid.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xxii
Introduction
• Illustrations and tables—Numerous illustrations of server screens and components aid
you in the visualization of common setup steps, theories, and concepts. In addition,
many tables provide details and comparisons of both practical and theoretical information and can be used for a quick review of topics.
• End-of-chapter material—The end of each chapter includes the following features to
reinforce the material covered in the chapter:
° Summary—A bulleted list is provided that gives a brief but complete summary of
the chapter.
° Key Terms list—A list of all new terms and their definitions.
° Review Questions—A list of review questions tests your knowledge of the most
important concepts covered in the chapter.
° Hands-on Projects—Hands-on Projects help you to apply the knowledge gained in
the chapter. These projects are designed to be completed in consecutive order, to
allow students to build on their knowledge throughout the book.
° Discovery Exercises—Additional projects that guide you through real-world
scenarios and advanced topics.
New to This Edition
This edition has been updated to include the concepts and procedures tested on the latest
CompTIA Linuxþ Powered by LPI certification. More specifically, this edition contains:
• Updated information pertinent to the latest Linux distributions
• New and expanded coverage of distribution-specific topics, such as package managers,
with Hands-on Projects designed for both Fedora Linux and Ubuntu Server Linux
installed on the same computer.
• New material on Linux technologies such as Systemd, firewalld, ZFS, and AppArmor.
• Updated material on key job-related networking services, including FTP, NFS, Samba,
Apache, DNS, DHCP, NTP, Postfix, SSH, VNC, and SQL.
• New and expanded material on security practices and technologies, with a focus on
managing server-based Linux systems.
Text and Graphic Conventions
Wherever appropriate, additional information and exercises have been added to this
book to help you better understand what is being discussed in the chapter. Icons
throughout the text alert you to additional materials. The icons used in this textbook
are as follows:
The Note icon is used to present additional helpful material related
to the subject being described.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Introduction
xxiii
Each Hands-on Project in this book is preceded by the Activity icon
and a description of the exercise that follows.
Textbook DVD
The DVD that accompanies this textbook contains the ISO images for Fedora 20 Linux
(64-bit) and Ubuntu Server 14.04 Linux (64-bit). You can use these ISO images to install
each Linux operating system within a virtual machine on your PC in order to complete the
exercises within each chapter.
CertBlaster Test Prep Resources
CompTIA Linuxþ Guide to Linux Certification, Fourth Edition includes CertBlaster test
preparation questions that mirror the look and feel of the CompTIA Linuxþ (Powered by
LPI) certification exam.
To log in and access the CertBlaster test preparation questions for CompTIA Linuxþ Guide
to Linux Certification, Fourth Edition, go to www.certblaster.com/login/.
Activate your CertBlaster license by entering your name, e-mail address, and access code
(found on the card bound in this book) in their fields, and then click Submit.
The CertBlaster user’s online manual describes features and gives navigation instructions.
CertBlaster offers three practice modes and all the types of questions required to simulate
the exams:
•
Assessment mode—Used to determine the student’s baseline level. In this mode,
the timer is on, answers are not available, and the student gets a list of questions
answered incorrectly, along with a Personal Training Plan.
•
Study mode—Helps the student understand questions and the logic behind answers by
giving immediate feedback both during and after the test. Answers and explanations
are available. The timer is optional, and the student gets a list of questions answered
incorrectly, along with a Personal Training Plan.
•
Certification mode—A simulation of the actual exam environment. The timer as well
as the number and format of questions from the exam objectives are set according to
the exam’s format.
For more information about dti test prep products, visit the website at www.dtipublishing.
com.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xxiv
Introduction
Instructor Resources
Instructor Companion Site
Everything you need for your course in one place! This collection of book-specific lecture
and class tools is available online at www.cengage.com/login. Access and download PowerPoint presentations, images, instructor’s manual, test banks, and more.
Instructor’s Manual—The Instructor’s Manual that accompanies this book includes additional material to assist in class preparation, including suggestions for classroom activities,
discussion topics, and additional activities.
Solutions—The instructor resources include solutions to all end-of-chapter material, including review questions, projects, and discovery exercises.
Figure Files—Figure files allow instructors to create their own presentations using figures
taken from the text.
PowerPoint Presentations—This book comes with Microsoft PowerPoint slides for each
chapter. These slides are included as a teaching aid to be used for classroom presentation,
to be made available to students on the network for chapter review, or to be printed for
classroom distribution. Instructors can add their own slides for additional topics they
introduce to the class.
Cognero—Cengage Learning Testing Powered by Cognero is a flexible, online system that
allows you to:
• Author, edit, and manage test bank content from multiple Cengage Learning solutions
• Create multiple test versions in an instant
• Deliver tests from your LMS, your classroom, or wherever you want
• Instructor Companion Site (ISBN 978-1-30510-492-1)
ExamConnection
ExamConnection is an online testing system that automatically grades students and
keeps class and student records. ExamConnection tests against Cengage’s textbook, as
well as against the CompTIA Linuxþ (Powered by LPI) certification exam, including a
quiz for each chapter in the book along with a mid-term and final exam. ExamConnection is managed by the classroom instructor who has 100 percent of the control, 100 percent of the time.
• ExamConnection (ISBN 978-1-30510-507-2)
LabConnection
With powerful computer-based exercises, video demonstrations, lab simulations,
and in-depth remediation capabilities, LabConnection provides a uniquely integrated
supplement to hands-on networking courses and can be used both as a virtual lab and
homework assignment tool. LabConnection supports Cengage Learning’s textbooks, the
corresponding certification exam, and offers remediation towards both. It includes the
following features:
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Introduction
xxv
Enhanced Comprehension—Through the LabConnection labs and guidance, while in the
virtual lab environment, the student develops skills that are accurate and consistently effective.
Exercises—Includes dozens of exercises that assess and prepare the learner for the
virtual labs, establishing and solidifying the skills and knowledge required to complete
the lab.
Virtual Labs—Labs consist of end-to-end procedures performed in a simulated environment where the student can practice the skills required of professionals.
Guided Learning—LabConnection allows learners to make mistakes, but alerts them to
errors made before they can move on to the next step, sometimes offering demonstration
as well.
Video demonstrations—Instructor-led video demonstrations guide the learners step by
step through the labs while providing additional insights to solidify the concepts.
SCORM Compliant Grading and Record Keeping—LabConnection will grade the exercises and record the completion status of the lab Portion, easily porting to and compatible
with distance learning platforms.
To get started with LabConnection, login at https://login.cengage.com. If you do not have
a Cengage faculty account, go to “New Faculty User” to create an account.
•
LabConnection online (ISBN 978-1-30510-797-7)
•
LabConnection on DVD (ISBN 978-1-30510-801-1)
Total Solutions for CompTIA Linuxþ
To access additional materials, please visit www.cengagebrain.com. At the CengageBrain
.com home page, search for the ISBN of your title (from the back cover of your book),
using the search box at the top of the page. This will take you to the product page where
these resources can be found. Additional resources include a Lab Manual, CourseNotes,
assessment, and digital labs.
Companion Lab Manual
The Lab Manual provides you with the hands-on instruction necessary to prepare for the
certification exam. Designed for classroom-led or self-paced study, labs complement main
text content and offer a unique, practical approach to learning that is a key component to
the exams. The Lab Manual includes lab activities, objectives, materials lists, step-by-step
procedures, illustrations, and review questions.
•
Lab Manual (ISBN 978-1-30510-757-1)
CourseNotes
This six-panel quick reference card reinforces the critical knowledge related to CompTIA Linuxþ
and the CompTIA Linuxþ (Powered by LPI) certification exam in a visual and user-friendly
format. CourseNotes will serve as a great reference tool during and after you complete the course.
•
CourseNotes (ISBN 978-1-30510-504-1)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xxvi
Introduction
Becoming a
CompTIA Certified
IT Professional is Easy
It’s also the best way to reach greater
professional opportunities and rewards.
Why Get CompTIA Certified?
Growing Demand
Higher Salaries
Verified Strengths
Universal Skills
Labor estimates predict some
technology fields will
experience growth of over
20% by the year 2020.*
CompTIA certification
qualifies the skills required to
join this workforce.
IT professionals with
certifications on their resume
command better jobs, earn
higher salaries and have more
doors open to new multiindustry opportunities.
91% of hiring managers
indicate CompTIA certifications are valuable in
validating IT expertise,
making certification the best
way to demonstrate your
competency and knowledge
to employers.**
CompTIA certifications are
vendor neutral—which means
that certified professionals
can proficiently work with an
extensive variety of hardware
and software found in most
organizations.
Learn
Certify
Work
Learn more about what
the exam covers by
reviewing the following:
Purchase a voucher at a
Pearson VUE testing center
or at CompTIAstore.com.
Congratulations on your
CompTIA certification!
• Exam objectives for key study
points.
• Register for your exam at a Pearson
VUE testing center:
• Make sure to add your
certification to your resume.
• Sample questions for a general
overview of what to expect on
the exam and examples of
question format.
• Visit pearsonvue.com/CompTIA to
find the closest testing center to
you.
• Check out the CompTIA
Certification Roadmap to plan
your next career move.
• Visit online forums, like
LinkedIn, to see what other IT
professionals say about
CompTIA exams.
• Schedule the exam online. You will
be required to enter your voucher
number or provide payment
information at registration.
• Take your certification exam.
Learn more: Certification.CompTIA.org/linuxplus
* Source: CompTIA 9th Annual Information Security Trends study: 500 U.S. IT and Business Executives Responsible for Security
** Source: CompTIA Employer Perceptions of IT Training and Certification
*** Source: 2013 IT Skills and Salary Report by CompTIA Authorized Partner
© 2014 CompTIA Properties, LLC, used under license by CompTIA Certifications, LLC. All rights reserved. All certification programs and education related to such programs are
operated exclusively by CompTIA Certifications, LLC. CompTIA is a registered trademark of CompTIA Properties, LLC in the U.S. and internationally. Other brands and company names
mentioned herein may be trademarks or service marks of CompTIA Properties, LLC or of their respective owners. Reproduction or dissemination prohibited without written consent
of CompTIA Properties, LLC. Printed in the U.S. 01090‐Sep2014
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Introduction
xxvii
Author Biography
Jason W. Eckert is an experienced technical trainer, consultant, and best-selling author in
the information technology (IT) industry. With over 20 IT certifications, 25 years of IT
experience, 4 published apps, and 22 published textbooks covering topics such as UNIX,
Linux, Apache, MySQL, Windows Server, Security, Microsoft Exchange, PowerShell, BlackBerry Enterprise Server, and video game development, Mr. Eckert brings his expertise to
every class that he teaches at triOS College. He is also the dean of technology at triOS College, where he continues to refine and improve college technology programs. For more
information about Mr. Eckert, visit about.me/jasoneckert.
Acknowledgments
First, I would like to thank the staff at Cengage for an overall enjoyable experience writing a
textbook on Linux that takes a fundamentally different approach than traditional textbooks.
Additionally, I wish to thank Natalie Pashoukos, Brooke Greenhouse, Sumathy Kumaran,
Ann Shaffer, and Kent Williams for working extremely hard to pull everything together and
ensure that the book provides a magnificent Linux experience. I’m also very grateful to our
technical reviewers, Serge Palladino and Danielle Shaw, who checked every chapter top to
bottom for errors. Finally, I wish to thank Frank Gerencser of triOS College for freeing me
up to write this book, the Starbucks Coffee Company for keeping me on schedule, and my
dog Pepper for continually reminding me that taking a break is always a good idea.
Readers are encouraged to e-mail comments, questions, and suggestions regarding CompTIA
Linuxþ Guide to Linux Certification, Fourth Edition, to Jason W. Eckert: [email protected]
trios.com.
Before You Begin
Linux can be a large and intimidating topic if studied in a haphazard way. So, as you begin
your study of Linux, keep in mind that each chapter in this book builds on the preceding
one. To ensure that you gain a solid understanding of core Linux concepts, read the
chapters in consecutive order. You should also participate in a local Linux Users Group
(LUG) and explore the Internet for websites, FAQs, HOWTOs, and newsgroups that will
expand your knowledge of Linux.
Lab Requirements
The following hardware is required at minimum for the Hands-on Projects at the end of
each chapter:
•
A 64-bit CPU
•
4 GB RAM
•
80 GB hard disk
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xxviii
Introduction
• DVD drive
• Internet connection
Similarly, the following lists the software required for the Hands-on Projects at the end of
each chapter:
• Fedora 20 Live Installation Media
• Ubuntu Server 14.04 Installation Media
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
1
Introduction to Linux
After completing this chapter, you will be able to:
•
•
•
•
•
Explain the purpose of an operating system
Outline the key features of the Linux operating system
Describe the origins of the Linux operating system
Identify the characteristics of various Linux distributions and where to find them
Explain the common uses of Linux in industry today
1
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
Chapter 1
Introduction to Linux
Linux technical expertise is essential in today’s computer workplace as more and more
companies switch to Linux to meet their computing needs. Thus, it is important to understand
how Linux can be used, what benefits Linux offers to a company, and how Linux has
developed and continues to develop. In the first half of this chapter, you learn about operating
system terminology and features of the Linux operating system, as well as the history and
development of Linux. Later in this chapter, you learn about the various types of Linux and
about the situations in which Linux is used.
Operating Systems
Every computer has two fundamental types of components: hardware and software. You are
probably familiar with these terms, but it’s helpful to review their meanings so you can more
easily understand how Linux helps them work together.
Hardware consists of the physical components inside a computer that are electrical in nature;
they contain a series of circuits that are used to manipulate the flow of information. A computer can contain many different pieces of hardware, including the following:
• A processor (also known as the central processing unit or CPU), which computes
information
• Physical memory (also known as random access memory or RAM), which stores
information needed by the processor
• Hard disk and solid state disk drives, which store most of the information that you use
• CD and DVD drives, which read and write information to and from CD and DVD discs
• Flash memory card readers, which read and write information to and from
removable memory cards, such as Secure Digital (SD) cards
• Sound cards, which provide sound to external speakers
• Video cards, which display results to the computer monitor
• Circuit boards (also known as mainboards or motherboards), which hold and provide
electrical connections between various hardware components
Software, on the other hand, refers to the sets of instructions or programs that allow the
various hardware components to manipulate data (or files). When a bank teller types
information into the computer behind the counter at a bank, for example, that bank teller
is using a program that understands what to do with your bank records. Programs and
data are usually stored on hardware media, such as hard disks or solid state disks,
although they can also be stored on removable media or even embedded in computer
chips. These programs are loaded into various parts of your computer hardware (such as
your computer’s memory and processor) when you first turn on your computer, and
when you start additional software, such as word processors or Internet browsers. After
a program is executed on your computer’s hardware, that program is referred to as a
process. In other words, a program is a file stored on your computer, whereas a process is
that file in action, performing a certain task.
There are two types of programs. The first type, applications (or apps), includes those programs designed for a specific use and with which you commonly interact, such as word
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Operating Systems
3
processors, computer games, graphical manipulation programs, and computer system utilities.
The second type, operating system (OS) software, consists of a series of software components
used to control the hardware of your computer. Without an operating system, you would not
be able to use your computer. Turning on a computer loads the operating system into computer hardware, which then loads and centrally controls all other application software in the
background. At this point, the user (the person using the computer) is free to interact with
the applications, perhaps by typing on the keyboard or clicking a mouse. Applications then
take the information supplied by the user and relay it to the operating system. The operating
system then uses the computer hardware to carry out the requests. The relationship between
users, application software, operating system software, and computer hardware is illustrated
in Figure 1-1.
The operating system carries out many different tasks by interacting with many different types
of computer hardware. For the operating system to accomplish this, it must contain the
appropriate device driver software for every hardware device in your computer. Each device
driver tells the operating system how to use that specific device. The operating system also
provides a user interface, which is a program that accepts user input indicating what is to be
done, forwards this input to the operating system for completion, and, after it is completed,
gives the results back to the user. The user interface can be a command-line prompt, in
which the user types commands, or it can be a graphical user interface (GUI), which consists
of menus, dialog boxes and symbols (known as icons) that the user can interact with via the
keyboard or the mouse. A typical Linux GUI is shown in Figure 1-2.
Finally, operating systems offer system services, which are applications that handle systemrelated tasks, such as printing, scheduling programs, and network access. These system
services determine most of the functionality that is seen in an operating system. Different
operating systems offer different system services, and many operating systems allow users to
customize the services they offer. A server is a computer with an operating system that is
configured to allow other computers to connect to it from across a network.
Computer users
Application software
Operating system software
Computer hardware
Figure 1-1 The role of operating system software
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
4
Chapter 1
Introduction to Linux
Figure 1-2 A Linux graphical user interface
The Linux Operating System
Linux (pronounced “lih-nucks”) is an operating system that is used today to run a variety of
applications on a variety of different hardware. Similar to other operating systems, the Linux
operating system loads into computer memory when you first power on your computer and
initializes (or activates) all of the hardware components. Next, it loads the programs that display the interface. From within the interface you can execute commands that tell the operating
system and other applications to perform specific tasks. The operating system then uses the
computer hardware to perform the tasks required by the applications.
Linux has the ability to manage thousands of tasks at the same time, including allowing multiple users to access the system simultaneously. Hence, Linux is referred to as a multiuser and
multitasking operating system.
Versions of the Linux Operating System
The core component of the Linux operating system is called the Linux kernel. The Linux kernel and supporting software (called function libraries) are written almost entirely in the C
programming language, which is one of the most common languages that software developers use when creating programs.
Although a variety of software can be used to modify the appearance of Linux, the underlying kernel is common to all types of Linux. The Linux kernel is developed continuously;,
thus, it is important to understand the different version numbers of the Linux kernel to
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Operating System
5
decide which kernel version is appropriate for your needs. Because the Linux kernel is
directly responsible for controlling the computer’s hardware (via device drivers), you might
sometimes need to upgrade the Linux kernel after installing Linux, in order to take advantage
of new technologies or to fix problems (also known as bugs) related to your computer’s
hardware. Consequently, a good understanding of your system’s hardware is important in
deciding which kernel to use.
A complete list of kernels, kernel versions, and their improvements
can be found on the Internet at www.kernel.org.
In some cases, updates in the form of a kernel module or a kernel patch can be used to provide or fix hardware supported by the kernel. Kernel modules and kernel patches are discussed later in this book.
Identifying Kernel Versions
Linux kernel versions are made up of the following three components:
•
Major number
•
Minor number
•
Revision number
Let’s look at a sample Linux kernel version, 3.15.9. In this example, the major number is the
number 3, which indicates the major revision to the Linux kernel. The minor number, represented by the number 15, indicates the minor revision and stability of the Linux kernel. An
odd minor number indicates a developmental kernel, whereas an even minor number indicates
a production kernel. Developmental kernels are not fully tested and imply instability; they are
tested for vulnerabilities by people who develop Linux software. Production kernels are developmental kernels that have been thoroughly tested by several Linux developers and are declared
to be stable. In the previous example, the kernel has a major number of 3 and a minor number
of 15. Because the minor number is odd, you know that this is a developmental kernel. This
kernel will eventually be improved by Linux developers, tested, and declared stable. When this
happens, the version of this kernel will change to 3.16 (indicating a production kernel).
Linux kernel changes occur frequently. Those changes that are very minor are represented by
a revision number indicating the most current changes to the version of the particular kernel
that is being released. For example, a 3.16.12 kernel has a major number of 3, a minor number of 16, and a revision number of 12. This kernel is the 12th release of the 3.16 kernel.
Some kernels might have over 100 different revisions, as a result of developers making constant improvements to the kernel code.
When choosing a kernel for a mission-critical computer such as an
e-mail server, ensure that the minor number is even. This reduces
the chance that you will encounter a bug in the kernel and, hence,
saves you the time needed to change kernels.
Table 1-1 shows the latest revisions of each key major and minor kernel released since the
initial release of Linux.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
6
Chapter 1
Introduction to Linux
Kernel Version
Date Released
Type
0.01
September 1991
First Linux kernel
0.12
January 1992
Production (stable)
0.95
March 1992
Developmental
0.98.6
December 1992
Production (stable)
0.99.15
March 1994
Developmental
1.0.8
April 1994
Production (stable)
1.1.95
March 1995
Developmental
1.2.12
July 1995
Production (stable)
1.3.100
May 1996
Developmental
2.0.36
November 1998
Production (stable)
2.1.132
December 1998
Developmental
2.2.26
February 2004 (latest release;
was developed concurrently
with newer kernels)
Production (stable)
2.3.99
May 2000
Developmental
2.4.17
December 2001
Production (stable)
2.5.75
July 2003
Developmental
2.6.35
August 2010
Production (stable)
3.2.62
January 2012
Production (stable)
3.4.103
May 2012
Production (stable)
3.10.54
June 2013
Production (stable)
3.12.28
November 2013
Production (stable)
3.14.18
March 2014
Production (stable)
3.15.10
March 2014
Developmental
3.16.2
September 2014
Production (stable)
Table 1-1 Latest revisions of common Linux kernels
Licensing Linux
Companies often choose Linux as their operating system because of the rules governing
Linux licensing. Unlike most other operating systems, Linux is freely developed and continuously improved by a large community of software developers. For this reason, it is referred to
as Open Source Software (OSS).
To understand what OSS is, you must first understand how source code is used to create programs. Source code refers to the list of instructions that a software developer writes to make
up a program; an example of source code is depicted in Figure 1-3.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Operating System
7
1
Figure 1-3 Source code
After the software developer finishes writing the instructions, the source code is compiled into
a format (called machine language) that only your computer’s processor can understand and
execute. To edit an existing program, the software developer must edit the source code and
then recompile it.
The format and structure of source code follows certain rules defined by the programming
language in which it was written. Programmers write Linux source code in many different
programming languages. After being compiled into machine language, all programs look the
same to the computer operating system, regardless of the programming language in which
they were written. As a result, software developers choose a programming language to create
source code based on ease of use, functionality, and comfort level.
The fact that Linux is an OSS operating system means that software developers can read
other developers’ source code, modify that source code to make the software better, and
redistribute that source code to other developers who might improve it further. Like all OSS,
Linux source code must be distributed free of charge, regardless of the number of modifications made to it. People who develop OSS commonly use the Internet to share their source
code, manage software projects, and submit comments and fixes for bugs (flaws). In this
way, the Internet acts as the glue that binds together Linux developers in particular and OSS
developers in general.
The complete open source definition can be found at
www.opensource.org.
Here are some implications of the OSS way of developing software:
•
Software is developed very rapidly through widespread collaboration.
•
Software bugs (errors) are noted and promptly fixed.
•
Software features evolve very quickly, based on users’ needs.
•
The perceived value of the software increases because it is based on usefulness and not
on price.
As you can imagine, the ability to share ideas and source code is beneficial to software developers. However, a company’s business model changes drastically when OSS enters the picture. The main issue is this: How can a product that is distributed freely generate revenue?
After all, without revenue any company will go out of business.
The OSS process of software development was never intended to generate revenue directly.
Its goal was to help people design better software by eliminating many of the problems
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
8
Chapter 1
Introduction to Linux
associated with traditional software development, which is typically driven by predefined
corporate plans and rigid schedules. By contrast, OSS development assumes that software
creation is an art in which a particular problem can be solved in many different ways. One
software developer might create a program that measures widgets using four pages of source
code, while another developer might create a program that does the same task in one page of
source code. You might think that this openness to multiple ways of solving a problem
would result in a haphazard software development process, but the sharing of ideas that is
the heart of OSS development keeps developers focused on the best possible solutions. Also,
while OSS developers contribute their strengths to a project, they learn new techniques from
other developers at the same time.
Because the selling of software for profit discourages the free sharing of source code, OSS
generates revenue indirectly. Companies usually make money by selling computer hardware
that runs OSS, by selling customer support for OSS, or by creating closed source software
programs that run on open source products such as Linux.
The OSS development process is, of course, not the only way to develop and license software.
Table 1-2 summarizes the types of software you are likely to encounter. The following section explains these types in more detail.
Types of Open Source Licenses Linux adheres to the GNU General Public License
(GPL), which was developed by the Free Software Foundation (FSF). The GPL stipulates
that the source code of any software published under its license must be freely available. If
someone modifies that source code, that person must also redistribute that source code
freely, thereby keeping the source code free forever.
GNU stands for “GNU’s Not UNIX.”
The GPL is freely available at www.gnu.org and in this book’s
Appendix B, “GNU General Public License.”
Type
Description
Open source
Software in which the source code and software can be obtained free of charge and can be
modified
Closed source
Software in which the source code is not available; although this type of software might be
distributed free of charge, it is usually quite costly
Freeware
Closed source software that is given out free of charge
Shareware
Closed source software that is initially given out free of charge but that requires payment
after a certain period of use
Table 1-2 Software types
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Operating System
9
Another type of open source license is the artistic license, which ensures that the source code
of the program is freely available yet allows the original author of the source code some
control over the changes made to it. Thus, if one developer obtains and improves the source
code of a program, the original author has the right to reject those improvements. As a
result of this restriction, artistic licenses are rarely used because many developers do not
want to work on potentially futile projects.
In addition to the two different open source licenses mentioned, many types of open source
licenses are available that differ only slightly from one another. Those licenses must adhere
to the open source definition but might contain extra conditions that the open source definition does not.
A list of approved open source licenses can be found at www.open
source.org.
Types of Closed Source Licenses Closed source software can be distributed for
free or for a cost; either way, the source code for the software is unavailable from the
original developers. The majority of closed source software is sold commercially
and bears the label of its manufacturer. Each of these software packages can contain a
separate license that restricts free distribution of the program and its source code in
many different ways.
Examples of closed source software are software created by
companies such as Microsoft, Apple, or Electronic Arts (EA).
Another type of closed source software is freeware, in which the software program is distributed free of charge, yet the source code is unavailable. Freeware might also contain licenses
that restrict the distribution of source code. Another approach to this style of closed source
licensing is shareware, which is distributed free of charge, yet after a certain number of
hours of usage, or to gain certain features of the program, payment is required. Although
freeware and shareware do not commonly distribute their source code under an open source
license, some people incorrectly refer to freeware as OSS, assuming that the source code is
free as well.
Linux Advantages
Many operating systems are in use today; the main ones include Linux, Microsoft Windows,
UNIX, and Mac OS X. Notably, Linux is the fastest growing operating system released to
date. Although Linux was only created in 1991, the number of Linux users estimated by
Red Hat in 1998 was 7.5 million, and the number of Linux users estimated by Google in
2010 was over 40 million (including the number of Linux-based Android smartphone and
device users). In 2013, LinuxCounter.net (http://linuxcounter.net) estimated that the number
of Linux users was over 70 million, and Google estimated that over 900 million Linuxbased Android devices had shipped to date. Since 1998, many large companies, including
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
10
Chapter 1
Introduction to Linux
IBM, Hewlett-Packard, Intel, and Dell, have announced support for Linux and OSS. In the
year 2000, IBM announced plans to spend one billion dollars on Linux and Linux development alone.
There are a multitude of reasons that so many people have begun using Linux. The following
advantages are examined in the sections that follow:
• Risk reduction
• Meeting business needs
• Stability and security
• Flexibility for different hardware platforms
• Ease of customization
• Ease of obtaining support
• Cost reduction
Risk Reduction Companies need software to perform mission-critical tasks, such as
database tracking, Internet business (e-commerce), and data manipulation. However,
changes in customer needs and market competition can cause the software a company
uses to change frequently. This can be very costly and time-consuming, but is a risk that
companies must take. Imagine that a fictitious company, ABC, Inc., buys a piece of software from a fictitious software vendor, ACME, Inc., to integrate its sales and accounting
information with customers via the Internet. What would happen if ACME went out of
business or stopped supporting the software due to lack of sales? In either case, ABC
would be using a product that had no software support, and any problems that ABC had
with the software after that time would go unsolved and could result in lost revenue. In
addition, all closed source software is eventually retired some time after it is purchased,
forcing companies to buy new software every so often to obtain new features and maintain
software support.
If ABC instead chose to use an OSS product and the original developers became unavailable
to maintain it, then ABC would be free to take the source code, add features to it, and maintain it themselves provided the source code was redistributed free of charge. Also, most OSS
does not retire after a short period of time because collaborative open source development
results in constant software improvement geared to the needs of the users.
Meeting Business Needs Recall that Linux is merely one product of open source
development. Many thousands of OSS programs are in existence, and new ones are created daily by software developers worldwide. Most open source Internet tools have been
developed for quite some time now, and the focus in the Linux community in the
past few years has been on developing application software for Linux, such as databases
and office productivity suites. Almost all of this software is open source and freely
available, compared to other operating systems, in which most software is closed source
and costly.
OSS is easy to locate on the Web, at sites such as SourceForge (www.sourceforge.net) and
GNU Savannah (http://savannah.gnu.org). New software is published to these sites daily.
SourceForge alone hosts over 430,000 different software developments.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Operating System
11
Common software available for Linux includes but is not limited to the following:
•
Scientific and engineering software
•
Software emulators
•
Web servers, Web browsers, and e-commerce suites
•
Desktop productivity software (e.g., word processors, presentation software,
spreadsheets)
•
Graphics manipulation software
•
Database software
•
Security software
In addition, companies that run the UNIX operating system might find it easy to migrate to
Linux. For those companies, Linux supports most UNIX commands and standards, which
makes transitioning to Linux very easy because the company likely would not need to purchase additional software or retrain staff. For example, suppose a company that tests scientific products has spent much time and energy developing custom software that runs on the
UNIX operating system. If this company transitions to another operating system, its staff
would need to be retrained or hired, and much of the custom software would need to be
rewritten and retested, which could result in a loss of customer confidence. If, however,
that company transitions to Linux, the staff would require little retraining, and little of the
custom software would need to be rewritten and retested, hence saving money and minimizing impact on consumer confidence.
For companies that need to train staff on Linux usage and administration, several educational resources and certification exams exist for various Linux skill levels. Certification benefits and the CompTIA Linux+ Powered by LPI certification are discussed in this book’s
Appendix A, “Certification.”
In addition, for companies that require a certain development environment or need to support custom software developed in the past, Linux provides support for most programming
languages.
Stability and Security OSS is developed by those people who have a use for it. This
collaboration among several developers with a common need speeds up software creation,
and when bugs in the software are found by these users, bug fixes are created very quickly.
Often, the users who identify the bugs can fix the problem because they have the source
code, or they can provide detailed descriptions of their problems so that other developers
can fix them.
By contrast, customers using closed source operating systems must rely on the operating system vendor to fix any bugs. Users of closed source operating systems must report the bug to
the manufacturer and wait for the manufacturer to develop, test, and release a solution to
the problem, known as a hot fix. This process might take weeks or even months to occur.
For most companies and individuals, this process is slow and costly. The thorough and collaborative open source approach to testing software and fixing software bugs increases the
stability of Linux; it is not uncommon to find a Linux system that has been running continuously for months or even years without being turned off.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
12
Chapter 1
Introduction to Linux
Security, a vital concern for most companies and individuals, is another Linux strength.
Because Linux source code is freely available and publicly scrutinized, security loopholes
are quickly identified and fixed by several different developers. In contrast, the source code
for closed source operating systems is not released to the public for scrutiny, which means
customers must rely on the vendor of that closed source operating system to provide security. A security breach unnoticed by the vendor can easily be exploited by the wrong person.
Every day, new malicious software (destructive programs that exploit security loopholes,
such as viruses and malware) is unleashed on the Internet with the goal of infiltrating closed
source operating systems, such as Windows. By contrast, the number of viruses that can
affect Linux is exceedingly low. As of April 2008, Linux had fewer than 100 known viruses,
whereas Windows had more than 1,000,000 known viruses. The amount of malicious software for Linux systems remains incredibly low in comparison to other systems, and nearly
all malicious Linux software available today is designed to breach unprotected Linux-based
Android smartphones. As a result, most desktop and server Linux systems that run antivirus
and anti-malware software today do so because they host files that may be shared with Windows computers.
A list of recent malicious software can be found on the Internet at
www.securelist.com.
Flexibility for Different Hardware Platforms Another important feature of
Linux is that it can run on a variety of different computer hardware platforms frequently
found in different companies. Although Linux is most commonly installed on the Intel
x86/x64 platforms, it can also be installed on other types of hardware, such as the Alpha.
This means that companies can run Linux on very large and expensive hardware for big
tasks, such as graphics rendering or chemical molecular modeling, as well as on older
hardware, such as an old Sun UltraSPARC computer, to extend its lifetime in a company.
Few other operating systems run on more than two different hardware platforms, making
Linux the ideal choice for companies that use a variety of different or specialized
hardwares.
Here is a partial list of hardware platforms on which Linux can run:
• Intel x86/x64
• Itanium
• Mainframe (S/390)
• ARM
• Alpha
• MIPS
• M68K
• PA-RISC
• SPARC / Ultra-SPARC
• PowerPC
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Operating System
13
In addition to the platforms in the preceding list, Linux can be customized to work on most
hardware. Thousands of high-tech companies rely on embedded operating system technology to drive their systems. NASA spacecrafts, Motorola cell phones, Google Android smartphones and tablets, Amazon Kindle eBook readers, TomTom GPS navigation systems, and
Linksys wireless routers all run Linux. This focus on mobile and embedded devices will
become more important in the future as the need for new functionality increases. The rich
set of OSS developers at work today makes Linux an attractive choice for manufacturers of
mobile and embedded devices.
Ease of Customization The ease with which you can control the inner workings of
Linux is another attractive feature, particularly for companies that need their operating system to perform specialized functions. If you desire to use Linux as an Internet Web server,
you can simply recompile the Linux kernel to include only the support needed to be an
Internet Web server. This results in a much smaller and faster kernel.
A small kernel performs faster than a large kernel because it contains
less code for the processor to analyze. On high performance systems,
you should remove any unnecessary features from the kernel to
improve performance.
Today, customizing and recompiling the Linux kernel is a well-documented and easy process; however, it is not the only way to customize Linux. Only software packages necessary
to perform certain tasks need to be installed; thus, each Linux system can have a unique
configuration and set of applications available to the user. Linux also supports several system programming languages, such as shell and PERL, which you can use to automate tasks
or create custom tasks.
Consider a company that needs an application that copies a database file from one computer to
another computer, yet also needs to manipulate the database file (perhaps by checking for duplicate records), summarize the file, and finally print it as a report. This might seem like a task that
would require expensive software; however, in Linux, you can simply write a short PERL script
that uses common Linux commands and programs together to perform these tasks in a few
minutes. This type of customization is invaluable to companies because it allows them to combine several existing applications to perform a certain task, which might be specific only to that
company and, hence, not previously developed by another free software developer. Most Linux
configurations present hundreds of small utilities, which, when combined with shell or PERL
programming, can quickly and easily make new programs that meet many business needs.
Ease of Obtaining Support For those who are new to Linux, the Internet offers a
world of Linux documentation. Frequently asked questions (FAQs) and easy-to-read instructions known as HOWTO documents are arranged by topic and are available to anyone.
HOWTO documents are maintained by their authors yet are centrally collected by the
Linux Documentation Project (LDP), which has several hundred Web sites worldwide that
allow you to search or download HOWTO documents.
A search of the word “HOWTO” on a typical Internet search engine such as www.google.
com displays thousands of results, or you can download the worldwide collection of
HOWTO documents at www.tldp.org.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
14
Chapter 1
Introduction to Linux
In addition, several Internet newsgroups allow Linux users to post messages and reply to
previously posted messages. If someone has a specific problem with Linux, that person can
simply post her problem on an Internet newsgroup and receive help from those who know
the solution to the problem. Linux newsgroups are posted to frequently; thus, you can usually expect a solution to a problem within hours. A list of common Linux newsgroups can
be found at http://groups.google.com.
Appendix C, “Finding Linux Resources on the Internet,” describes how to navigate Internet
resources and lists some common resources useful throughout this book.
Although online support is the most common method of getting help, other methods are
available. Most Linux distributions provide professional telephone support services for a
modest fee, and many organizations exist to give free support to those who ask. The most
common of these groups are referred to as Linux User Groups (LUGs), and most large cities
across the globe have at least one. LUGs are groups of Linux users who meet regularly to
discuss Linux-related issues and problems. An average LUG meeting consists of several new
Linux users (also known as Linux newbies), administrators, developers, and experts (also
known as Linux gurus). LUG meetings are a place to solve problems as well as learn about
the local Linux community. Most LUGs host Internet Web sites that contain a multitude of
Linux resources, including summaries of past meetings and discussions. One common
activity seen at a LUG meeting is referred to as an Installfest; several members bring in their
computer equipment to install Linux and other Linux-related software. This approach to
transferring knowledge is very valuable to LUG members because concepts can be demonstrated and the solutions to problems can be modeled by more experienced Linux users.
To find a list of available LUGs in your region, search for the words
“LUG cityname” on an Internet search engine such as www.google.
com (substituting your city’s name for “cityname”). When searching
for a LUG, keep in mind that LUGs might go by several different
names; for example, the LUG in Hamilton, Ontario, Canada is
known as HLUG (Hamilton Linux Users Group). Many LUGs today
are managed using Facebook groups or meeting sites such as www.
meetup.com.
Cost Reduction Linux is less expensive than other operating systems, such as Windows,
because there is no cost associated with acquiring the software. In addition, a wealth of OSS
can run on a variety of different hardware platforms running Linux, and a large community
of developers is available to diagnose and fix bugs in a short period of time for free. However, although Linux and the Linux source code are distributed freely, implementing Linux
is not cost free. Costs include purchasing the computer hardware necessary for the computers hosting Linux, hiring people to install and maintain Linux, and training users of Linux
software.
The largest costs associated with Linux are the costs associated with hiring people to maintain the Linux system. However, closed source operating systems have this cost in addition
to the cost of the operating system itself. The overall cost of using a particular operating system is known as the total cost of ownership (TCO). Table 1-3 shows an example of the factors involved in calculating the TCO for operating systems.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The History of Linux
Operating System
Linux
Closed Source Operating
System
Operating system cost
$0
Greater than $0
Cost of administration
Low: Stability is high and bugs are fixed
quickly by open source developers.
Moderate/high: Bug fixes are
created by the vendor of the
operating system, which could
result in costly downtime.
Cost of additional software
Low/none: Most software available for
Linux is also open source.
High: Most software available for
closed source operating systems is
also closed source.
Cost of software upgrades
Low/none
Moderate/high: Closed source
software is eventually retired,
and companies must buy
upgrades or new products to gain
functionality and stay competitive.
15
Table 1-3 Calculating the total cost of ownership
The History of Linux
Linux is based on the UNIX operating system developed by Ken Thompson and Dennis
Ritchie of AT&T Bell Laboratories in 1969 and was developed through the efforts of many
people as a result of the hacker culture that formed in the 1980s. Therefore, to understand
how and why Linux emerged on the operating system market, you must first understand
UNIX and the hacker culture. Figure 1-4 illustrates a timeline representing the history of the
UNIX and Linux operating systems.
UNIX
The UNIX operating system has roots running back to 1965, when the Massachusetts Institute of Technology (MIT), General Electric, and AT&T Bell Laboratories began development
of an operating system called Multiplexed Information and Computing Service (MULTICS).
MULTICS was a test project intended to reveal better ways of developing time-sharing operating systems, in which the operating system regulates the amount of time each process has
to use the processor. The project was abandoned in 1969. However, Ken Thompson, who
had worked on the MULTICS operating system, continued to experiment with operating systems after the project was abandoned. In 1969, he developed an operating system called
UNIX that ran on the DEC (Digital Equipment Corporation) PDP-7 computer.
Shortly thereafter, Dennis Ritchie invented the C programming language that was used on
Ken Thompson’s UNIX operating system. The C programming language was a revolutionary
language. Most programs at the time needed to be written specifically for the hardware of
the computer, which involved referencing volumes of information regarding the hardware in
order to write a simple program. However, the C programming language was much easier to
use to write programs, and it was possible to run a program on several different machines
without having to rewrite the code. The UNIX operating system was rewritten in the C
programming language, and by the late-1970s, the UNIX operating system ran on different
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
Approximately 70
million Linux users
(excluding Android users)
2014
1999
Approximately 7.5
million Linux users
Worldwide
adoption of
Linux technology
The Cathedral &
the Bazaar
Rapid Linux
development
1998
Linus Torvalds
creates Linux
1991
1984
The GNU Project
started
MIT Jargon file
released
1973
Development
of hacker
culture and GPL
1983
Creation of UNIX
Rapid UNIX
development
The Hacker’s
Dictionary
MULTICS Project
started
Introduction to Linux
1969
Chapter 1
1965
16
Figure 1-4 Timeline of UNIX and Linux development
hardware platforms, something that the computing world had never seen until that time.
Hence, people called UNIX a portable operating system.
Unfortunately, the company Ken Thompson and Dennis Ritchie worked for (AT&T) was
restricted by a federal court order from marketing UNIX. In an attempt to keep UNIX viable,
AT&T sold the UNIX source code to several different companies, encouraging them to agree
to standards among them. Each of these companies developed its own variety, or flavor, of
UNIX yet adhered to standards agreed upon by all. AT&T also gave free copies of the
UNIX source code to certain universities to promote widespread development of UNIX. One
result of this was a UNIX version developed at the University of California, Berkeley, in the
early 1980s known as BSD (Berkeley Software Distribution). In 1982, one of the companies
to whom AT&T sold UNIX source code (Sun Microsystems) marketed UNIX on relatively
cheaper hardware and sold thousands of computers that ran UNIX to various companies and
universities.
Throughout the 1980s, UNIX found its place primarily in large corporations that had
enough money to purchase the expensive computing equipment needed to run UNIX (usually
a DEC PDP-11, VAX, or Sun Microsystems computer). A typical UNIX system in the 1980s
could cost over $100,000, yet it performed thousands of tasks for client computers (also
known as dumb terminals). Today, UNIX still functions in that environment; most large
companies employ different flavors of UNIX for their heavy-duty, mission-critical tasks,
such as e-commerce and database hosting. Common flavors of UNIX today include Oracle’s
Solaris (formerly SUN Microsystem’s Solaris), Hewlett-Packard’s HP-UX, IBM’s AIX, as well
as Apple’s Macintosh OS X and iOS operating systems.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The History of Linux
17
The Hacker Culture
The term hacker refers to a person who attempts to expand his knowledge of computing
through experimentation. It should not be confused with the term cracker, which refers to
someone who illegally uses computers for personal benefit or to cause damage.
In the early days of UNIX, hackers came primarily from engineering or scientific backgrounds, because those were the fields in which most UNIX development occurred. Fundamental to hacking was the idea of sharing knowledge. A famous hacker, Richard Stallman,
promoted the free sharing of ideas while he worked at the Artificial Intelligence Laboratory at MIT. He believed that free sharing of all knowledge in the computing industry
would promote development. In the mid-1980s, Stallman formed the Free Software Foundation (FSF) to encourage free software development. This movement was quickly
accepted by the academic community in universities around the world, and many university students and other hackers participated in making free software, most of which ran
on UNIX. As a result, the hacker culture was commonly associated with the UNIX operating system.
Unfortunately, UNIX was not free software, and by the mid-1980s some of the collaboration
seen earlier by different UNIX vendors diminished and UNIX development fragmented into
different streams. As a result, UNIX did not represent the ideals of the FSF, and so Stallman
founded the GNU Project in 1984 to promote free development for a free operating system
that was not UNIX.
A description of the FSF and GNU can be found at www.gnu.org.
This development eventually led to the publication of the GNU Public License (GPL), which
legalized free distribution of source code and encouraged collaborative development. Any
software published under this license must be freely available with its source code; any modifications made to the source code must then be redistributed free as well, keeping the software development free forever.
As more and more hackers worked together developing software, a hacker culture developed
with its own implied rules and conventions. Most developers worked together without ever
meeting each other; they communicated primarily via newsgroups and e-mail. The Hacker’s
Dictionary, published by MIT in 1983, detailed the terminology regarding computing and
computing culture that had appeared since the mid-1970s; it, along with the FSF, GNU,
GPL, served to codify the goals and ideals of the hacker culture. But it wasn’t until the publication of Eric S. Raymond’s The Cathedral and the Bazaar, in 1999, that the larger world
was introduced to this thriving culture. Raymond, a hacker himself, described several aspects
of the hacker culture:
•
Software users are treated as codevelopers.
•
Software is developed primarily for peer recognition and not for money.
•
The original author of a piece of software is regarded as the owner of that software
and coordinates the cooperative software development.
•
The use of a particular piece of software determines its value, not its cost.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
18
Chapter 1
Introduction to Linux
• Attacking the author of source code is never done. Instead, bug fixes are either made
or recommended.
• Developers must understand the implied rules of the hacker culture before being
accepted into it.
This hacker culture proved to be very productive, with several thousand free tools and applications created in the 1980s, including the famous Emacs editor, which is a common tool
used in Linux today. During this time, many programming function libraries and UNIX-like
commands also appeared as a result of the work on the GNU Project. Hackers became accustomed to working together via newsgroup and e-mail correspondence. In short, this hacker
culture, which supported free sharing of source code and collaborative development, set the
stage for Linux.
Linux
Although Richard Stallman started the GNU Project to make a free operating system, the
GNU operating system never took off. Much of the experience gained by hackers developing
the GNU Project was later pooled into Linux. A Finnish student named Linus Torvalds first
developed Linux in 1991 when he was experimenting with improving MINIX (Mini-UNIX, a
small educational version of UNIX developed by Andrew Tannenbaum) for the Intel x86
platform. The Intel x86 platform was fast becoming standard in homes and businesses
around the world and was a good choice for any free development at the time. The key feature of the Linux operating system that attracted the development efforts of the hacker culture was the fact that Torvalds had published Linux under the GNU Public License.
Since 1991, when the source code for Linux was released, the number of software developers
dedicated to improving Linux has increased each year. The Linux kernel was developed collaboratively and was centrally managed; however, many Linux add-on packages were developed freely worldwide by those members of the hacker culture who were interested in their
release. Linux was a convenient focal point for free software developers. During the
early- to mid-1990s, Linux development proceeded at full speed, with hackers contributing
large amounts of their time to what turned into a large-scale development project. All of this
effort resulted in several distributions of Linux. A distribution of Linux is a collection or
bundle of software containing the commonly developed Linux operating system kernel and
libraries, combined with add-on software specific to a certain use. Well-known distributions
of Linux include Red Hat, Ubuntu, and OpenSUSE.
This branding of Linux did not imply the fragmentation that UNIX experienced in the
late 1980s. All distributions of Linux had a common kernel and utilities; the fact that they
contained different add-on packages simply made them look different on the surface. Linux
still derived its usefulness from collaborative development.
Linux development continued to expand throughout the late 1990s as more and more developers grew familiar with the form of collaborative software development advocated by the
hacker culture. By 1998, when the term “OSS” first came into use, there were already many
thousands of OSS developers worldwide. Many small companies that offered Linux solutions
for business were formed. People invested in these companies by buying stock in them.
Unfortunately, this trend was short-lived. By the year 2000, most of these companies had
vanished. At the same time, the OSS movement caught the attention and support of many
large companies (such as IBM, Compaq, Dell, and Hewlett-Packard), and there was a shift
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Linux Distributions
19
in Linux development over the following decade to support the larger computing environments and mobile devices.
It is important to note that Linux is a by-product of OSS development. Recall that the OSS
developers are still members of the hacker culture and, as such, are intrinsically motivated to
develop software that has an important use. Thus, OSS development has changed over time;
in the 1980s, the hacker culture concentrated on developing Internet and programming tools,
whereas in the 1990s, it focused on developing the Linux operating system. Since 2000, there
has been great interest in embedded Linux (Linux OSes that run on smaller hardware
devices, such as mobile devices) and developing application programs for use on the Linux
operating system. Graphics programs, games, and custom business tools are only some of
the popular developments that OSS developers have released in the past couple of years.
Because Linux is currently very well developed, more application development can be
expected from the OSS community in the next decade.
For more information on the free software movement and the development of Linux, watch the 2001 television documentary Revolution
OS (available on YouTube). It features interviews with Linus Torvalds,
Richard Stallman, and Eric S. Raymond.
Linux Distributions
It is time-consuming and inefficient to obtain Linux by first downloading and installing the
Linux kernel and then adding desired OSS packages. Instead, it’s more common to download
a distribution of Linux containing the Linux kernel, common function libraries, and a series of
OSS packages.
Remember that although different Linux distributions appear different on the surface, they run the same kernel and contain many of
the same packages.
Despite the fact that varied distributions of Linux are essentially the same under the surface,
they do have important differences. Different distributions might support different hardware
platforms. Also, Linux distributions include predefined sets of software; some Linux distributions include a large number of server-related tools, such as Web servers and database servers,
whereas others include numerous workstation and development software applications. Still
others might include a complete set of open source tools that you can use to customize a
Linux system to perform specific functions. In that case, you simply choose the open source
tools you want to install. For example, you might choose to install a database server.
Linux distributions that include many specialized tools might not contain a graphical user
interface (GUI); an example of this is a Linux distribution that fits on a floppy and can be
used as a router. Most distributions, however, do include a GUI that can be further customized to suit the needs of the user.
The core component of the GUI in Linux is referred to as X Windows. There are two implementations of X Windows: XFree86 and X.org. XFree86 is the traditional implementation of
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
20
Chapter 1
Introduction to Linux
X Windows, and X.org is the latest implementation of X Windows based on the original MIT
X Windows project that was released as OSS in 2004. In addition to X Windows, several
Linux window managers and desktop environments are available, which together affect the
look and feel of the Linux GUI. X Windows in combination with a window manager and
desktop environment is referred to as a GUI environment. The two main competing GUI
environments available in Linux are the GNU Network Object Model Environment
(GNOME) and the K Desktop Environment (KDE). These two GUI environments are more
or less comparable in functionality, although users might have a personal preference for one
desktop over the other. This is often the case when a company wants to do a great deal of
software development in the GUI environment; the GNOME desktop, written in the C programming language, uses the widely available gtk toolkit, whereas the KDE desktop, written
in the Cþþ programming language, uses the qt toolkit. Which language and toolkit best fits
the need will be the one preferred at that time. Most common Linux distributions ship with
both GNOME and KDE GUI environments, whereas others offer support for both so that
either GUI environment can be easily downloaded and installed. A comparison of these two
GUI environments can be seen in Figures 1-5 and 1-6.
In addition to GNOME and KDE, there are several other desktop environments available to Linux systems. One example is XFCE, which is a
lightweight desktop environment designed for Linux systems with few
CPU and RAM resources.
Figure 1-5 The GNOME desktop
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Linux Distributions
21
1
Figure 1-6 The KDE desktop
Another difference between Linux distributions is language support, with some distributions
offering more support for certain languages than others. One example of this is TurboLinux,
which has increased support for Japanese and Chinese. Consequently, TurboLinux is far more
common in Asia than in North America. Many Linux distributions are specialized for different languages, and most Linux documentation, such as HOWTO documents, is available in
many different languages.
Although these differences between Linux distributions can help narrow the choice of
Linux distributions to install, one of the most profound reasons companies choose one
distribution over another is support for package managers. A package manager is a software system that installs and maintains software. It keeps track of installed software,
requires a standard format and documentation, and can manage and remove software
from a system by recording all relevant software information in a central software database on your computer.
A package manager in Linux is similar to the Add/Remove Programs
or Programs and Features applet within the Windows Control Panel.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
22
Chapter 1
Introduction to Linux
The most widely supported package manager is the Red Hat Package Manager (RPM). Most
Linux software is available in RPM format, and the RPM is standard on many Linux distributions. The Debian Package Manager offers the same advantages as the Red Hat Package
Manager, yet few distributions offer it. In addition to obtaining software in package manager
format, you can download software in tarball format. A tarball is merely a compressed
archive of files, like WinZip or RAR files, that usually contain scripts that install the software
contents to the correct location on the system, or source code that can be compiled into a
working program and copied to the system. Unfortunately, tarballs do not update a central
software database and, as a result, are very difficult to manage, upgrade, or remove from the
system. Traditionally, most Linux software was available in tarball format, but package managers have since become the most common method for installing software.
A list of common Linux distributions can be found on the Internet at
www.linux.org.
Anyone can create a Linux distribution by packaging OSS with the Linux kernel. As a result,
over 200 publicly registered Linux distributions exist. Many of these are small, specialized distributions designed to fulfill certain functions, but some are mainstream Linux distributions
used widely throughout the computing world. Typically, a distribution is associated with a
Web site from which the distribution can be downloaded for free. In addition, most Linux distributions can be obtained from several different Web sites, such as http://iso.linuxquestions.
org. Many distributions of Linux are also available on DVDs for a small fee from various
computer stores and Web sites; however, downloading from the Internet is the most common
method of obtaining Linux.
Table 1-4 briefly describes some mainstream Linux distributions, their features, and where to
find them on the Internet.
Common Uses of Linux
As discussed earlier, an important feature of Linux is its versatility. Linux can provide services
meeting the needs of differing companies in a variety of situations. Furthermore, configuring
these services is straightforward given the wide range of documentation freely available on
the Internet; you can simply choose the services that are required and then customize Linux
to provide those services. These services can be used on a local computer workstation, or
you can configure a service to allow other computers to connect to it across a network. Services that are used on the local computer are referred to as workstation services, whereas services that are made available for other computers across a network are known as server
services.
A computer that hosts a server service is commonly referred to
as a server.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Common Uses of Linux
23
Distribution
Features
Platforms
Location
Red Hat Linux
One of the earliest Linux distributions, and
one that is commonly used within
organizations today. Two distributions of Red
Hat are available: the Enterprise distribution
geared for enterprise environments and
the Fedora distribution geared for all
environments (servers, desktops, laptops,
etc.). Both editions ship with GNOME as the
default desktop environment, and with the
RPM package manager.
x86/x64
PPC
Mainframe
ARM
www.redhat.com
www.fedoraproject.org
SUSE Linux
Originally developed primarily in Europe,
SUSE is the oldest business-focused Linux
distribution. Novell purchased SUSE Linux to
replace its NetWare OS and distributes
enterprise versions of SUSE packaged with
Novell software. The current distribution of
SUSE Linux is called OpenSUSE; it ships with
the KDE desktop environment by default and
with the RPM package manager.
x86/x64
www.opensuse.org
www.suse.com
Slackware Linux
The oldest Linux distribution that is currently
maintained (it was the basis for many other
Linux distributions in the 1990s, including
SUSE). It has its own package manager, ships
with GNOME and KDE desktops, boasts many
features similar to UNIX, and has always been
focused on multiprocessor environments.
x86/x64
Mainframe
ARM
www.slackware.com
Debian Linux
Offering the largest number of packages
of all Linux distributions, Debian Linux
contains software packages for any use and
ships with GNOME, KDE, and the Debian
Package Manager.
x86/x64
UltraSPARC
Mainframe
ARM
PPC
MIPS
ARM
www.debian.org
Ubuntu Linux
A Debian-based distribution that is widely
used in all environments. It is designed to be
easy to use and supports nearly all hardware,
including mobile computing devices. It
typically ships with GNOME and the Debian
Package Manager.
x86/x64
UltraSPARC
PPC
MIPS
ARM
www.ubuntu.com
Linux Mint
A relatively new and easy-to-use Linux
distribution that is focused on providing
desktop and mobile user capabilities. It uses
the Debian Package Manager and ships
with GNOME, KDE, and Cinnamon (based on
GNOME) desktop environments.
x86/x64
www.linuxmint.com
TurboLinux
A very common distribution of Linux in Asia
that is famous for its clustering abilities. It
ships with the GNOME and KDE desktop
environments, as well as with the RPM
package manager.
x86/x64
www.turbolinux.com
Table 1-4 Common Linux distributions
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
24
Chapter 1
Introduction to Linux
Although thousands of different server and workstation services are available that you can use
to customize Linux, Linux configurations commonly used today include:
• Internet servers
• File and print servers
• Application servers
• Cloud systems
• Supercomputers
• Scientific workstations
• Office/personal workstations
• Mobile devices
Internet Servers
Linux hosts a wide range of Internet services, and it was from these services that Linux gained
much popularity in the 1990s. All of these services are available free of charge and, like all OSS,
undergo constant improvement, which makes Linux an attractive choice when planning for the
use of Internet services in a company. Companies that use services on a computer to serve client
computers are said to have an Internet server. Linux contains hundreds of network services that
provide the framework for an Internet server; the most common of these services include:
• Mail services
• Routing services
• FTP services
• Firewalls and proxy services
• Web services
• News services
• DNS services
Many of these applications are discussed in more detail later in this book.
Mail Services In the 1980s and early 1990s, e-mail was a service that was found primarily in universities. Today, almost every Internet user has an e-mail account and uses e-mail
on a regular basis. E-mail addresses are easy to acquire and can be obtained free of charge.
E-mail is distributed via a network of e-mail servers, also known as Mail Transfer Agents
(MTAs). Many MTAs are freely available for Linux, including sendmail, postfix, smail,
and exim. Before the user can access his e-mail, it must be downloaded from a MTA; the
service that provides this is known as a Mail Delivery Agent (MDA). Linux also provides
several of these services; procmail and fetchmail are two of the most common. Finally, the
user views her e-mail using a program known as a Mail User Agent (MUA). Common
MUAs available for Linux include mutt, pine, printmail, elm, mail, Netscape, and Eudora.
Routing Routing is a core service that is necessary for the Internet to function. The Internet
is merely a large network of interconnected networks; in other words, it connects company
networks, home networks, and institutional networks so that they can communicate with
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Common Uses of Linux
25
each other. A router is a computer or special hardware device that provides this interconnection;
it contains information regarding the structure of the Internet and sends information from one
network to another. Companies can use routers to connect their internal networks to the Internet as well as to connect their networks together inside the company. Linux is a good choice for
this, as it provides support for routing and is easily customizable; many Linux distributions, each
of which can fit on a small USB flash drive, provide routing capabilities.
FTP Services The most common and efficient method for transferring files over the Internet
is by using the File Transfer Protocol (FTP). In addition, FTP is commonplace when transferring
files on an internal company network because it is very quick and robust. A user simply starts
the FTP service on her computer (now known as an FTP server) and allows users to connect;
users on other computers then connect to this server using an FTP client program and download
any desired files. Most FTP servers available on the Internet allow any user to connect and are,
hence, called anonymous FTP servers. Furthermore, most operating systems, such as Linux,
UNIX, Microsoft Windows, and Macintosh, are distributed with an FTP client program, making it easy for users to connect to these FTP servers.
Although several FTP service software programs are available for
Linux, the most commonly used is the Very Secure FTP Server, which
can be downloaded at https://security.appspot.com/vsftpd.html.
Firewalls and Proxy Services The term “firewall” describes the structures that prevent
a fire from spreading. In the automobile industry, a firewall protects the passengers in a car if a
fire breaks out in the engine compartment. Just as an automobile firewall protects passengers, a
computer firewall protects companies from outside intruders on the Internet. Most firewalls are
computers that are placed between the company network and the company’s connection to the
Internet; all traffic must then pass through this firewall, allowing the company to control traffic
at this firewall, based on a complex set of rules. Linux has firewall support built directly into the
kernel. Utilities such as ipchains and netfilter/iptables, which are included with most distributions, can be used to configure the rules necessary to make a system a firewall.
You can find out more about using netfilter/iptables to configure
Linux firewalls at www.netfilter.org.
Because firewalls are usually located between a company’s internal network and the Internet,
they often provide other services that allow computers inside the company easy access to the
Internet. The most common of these services are known as proxy services. A proxy server
requests Internet resources, such as Web sites and FTP sites, on behalf of the computer inside
the company. In other words, a workstation computer inside the company simply sends a
request to the proxy server connected to the Internet. The proxy server then obtains and returns
the requested information to the workstation computer. One proxy server can allow thousands
of company workstation computers access to the Internet simultaneously without lengthy configuration of the workstation computers; proxy servers keep track of the information passed to
each client by maintaining a Network Address Translation (NAT) table. Although ipchains and
netfilter/iptables can both perform some proxy server functions, the most common proxy server
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
26
Chapter 1
Introduction to Linux
used on Linux is Squid. Squid retains a copy of any requested Internet resources (via a process
known as caching) so that it can respond quicker to future requests for the same resources.
To obtain or find information regarding the Squid proxy server, visit
www.squid-cache.org.
Web Services Although many Internet tools and services are available, the most popular
is the Internet browser, which can connect client computers to servers worldwide hosting
information of many types: text, pictures, music, binary data, video, and much more. The
community of servers that hosts this information is known as the World Wide Web
(WWW), and a server hosting information is known as a Web server. On a basic level, a
Web server is just a server using Hypertext Transfer Protocol (HTTP) to provide information to requesting Web browsers running on client computers; however, Web servers can
also process programs known as Common Gateway Interface (CGI) scripts and provide
secure connections such as Secure Sockets Layer (SSL) or Transport Layer Security (TLS). A
CGI is a program that runs on the Web server and enables connection to a resource running
on another server on the network not connected to the Internet such as a database. This is
very useful, as not all information provided over the Internet needs to reside on Web servers.
CGIs can be written in several programming languages, including C and Cþþ, making them
readily compatible with Linux. SSL and TLS are secure methods of communicating with a
Web server in which the information passing between the client computer and the Web
server is encrypted to keep it secure. This form of transmission is widely used any time confidentiality is required, such as in Internet banking or in transferring a client’s credit card
information in e-commerce. You can tell SSL/TLS is in use when the http:// in the browser’s
address bar changes to https://.
To better understand how SSL/TLS works, visit www.gmail.com and
note the change in the browser’s address bar immediately after the
page has loaded.
Many open source Web server software packages are available for Linux. The most widely
used is the Apache Web Server, comprising more than 55 percent of all Web servers in the
world during 2013.
For more information about the Apache Web Server, visit
httpd.apache.org.
News Services Web servers host valuable information, but most do not provide any
means for users to communicate with each other. This functionality is provided by a news
server, which allows users to post messages in forums called newsgroups and allows other
users to read and reply to those messages. Newsgroups are sometimes referred to as computer bulletin boards and are similar to bulletin boards seen around a school campus and
in other public places; persons having or requiring information or services post a notice
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Common Uses of Linux
27
that others see and respond to. Newsgroup forums are grouped according to topic. Posting
to a newsgroup is often a very quick way to find the solution to a problem because people
who read the posting are likely to have had the same problem and found the solution. In
addition, newsgroup forums can be moderated, in that a person or group responsible for
the forum edits messages before they are posted to ensure they fit the forum’s theme. This
ensures proper newsgroup etiquette, which dictates that before posting a question you
search previous postings to ensure that the question has not already been asked and
answered and that only messages relevant to the newsgroup topic are posted.
Many OSS developers use newsgroups to exchange information and coordinate software
creation. As with e-mails, a special program called a newsreader is necessary to access newsgroups and read postings hosted on news servers. Common Linux newsreaders include
Gnews, PAN, Gnus, Thunderbird, and pine. The most popular open source news server software available for Linux is called InterNetworkNews (INN); it is included with most common Linux distributions and is maintained by an open source organization called the Internet Systems Consortium, which continually develops and improves several open source
Internet technologies.
For more information on how to subscribe to newsgroups, visit
groups.google.com.
DNS Services Each computer on a network needs a unique way to indentify itself and to
refer to other computers. This is accomplished by assigning each computer a number called
an Internet Protocol (IP) address. An IP address is a long string of numbers that would be
very hard for the typical person to remember. Thus, IP addresses are often associated with
more user-friendly names. In particular, servers are identified by names like www.linux.org,
which are known as fully qualified domain names (FQDNs). When you use a Web browser
such as Internet Explorer or Netscape Navigator to request information from a Web server,
you typically type the Web server’s FQDN (e.g., www.linux.org) into your browser’s
address bar. However, FQDNs exist only for the convenience of the human beings who use
computer networks. The computers themselves rely on IP addresses. Thus, before your
browser can retrieve the requested information from the Web server, it needs to know the
IP address associated with the FQDN you typed into the address bar. Your browser gets
this information by contacting a server hosting the Domain Name System (DNS). The DNS
server maintains a list of the proper FQDN to IP mappings and quickly returns the
requested IP address to your browser. Your browser can then use this IP address to connect
to the target Web site.
For companies wanting to create a DNS server, Linux is an inexpensive solution, as many
distributions of Linux ship with a Domain Name System known as BIND (Berkeley Internet
Name Domain).
Each computer participating on the Internet must have an IP address.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
28
Chapter 1
Introduction to Linux
Names for computers on the Internet, such as www.linux.org, are
also known as fully qualified domain names (FQDNs).
You can find the latest version of BIND at the Internet Systems
Consortium Web site, www.isc.org.
File and Print Servers
Networks were created to share resources, primarily printers and information. In business, it
is not cost-effective to purchase and install a printer on the computer of every user who needs
to print. It is far easier and cheaper to install one central printer on a server and let multiple
users print to it across the computer network. Often, information must also be commonly
available to users to allow them to collaborate on projects or perform their daily jobs. Duplicating data on every user machine would consume too much hard drive space, and coordinating changes to this data would be nearly impossible. By employing a network, this information can be made available to all who need it and can be easily kept up to date. Another
benefit to this central storage of information is that a user can access data regardless of the
computer that he logs in to. Central storage also allows a company to safeguard its information by using devices to back up or make copies of stored data on a regular basis in the event
a computer failure occurs. Most companies perform backups of data at least every week to
ensure that if data is lost on the central server, it can be restored from a back-up copy
quickly.
Linux is well suited to the task of centrally sharing resources. It is inherently a fast, light
operating system, and a distribution suited to the task can be installed on the central server.
Linux is not only able to share information with other Linux and UNIX machines using services such as Network File System (NFS), it is also able to share resources with computers
running other operating systems, such as Microsoft Windows or Apple Macintosh. Client
computers are able to access a shared resource on a server running Linux provided that
server has the appropriate service available. The most common service used to allow clients
to connect to shared information and printers on a Linux server is Samba, which makes a
Linux server appear as a Windows server to Windows clients.
Samba can be found at www.samba.org.
Application Servers
An application server is a computer running a program that acts as an intermediary between
a client computer and the information, normally stored in a database that the client computer
needs. A database is an organized collection of data that is arranged into tables of related
information. The client requests some data to be changed or displayed, and the application
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Common Uses of Linux
29
server interacts with the database to manipulate and retrieve the required information. This is
often described as a front-end/back-end relationship. The front end runs on the client computer and is the interface the user sees and interacts with to request data. The front end
takes this request, formulates it so that the server can understand it, and passes the request
along to the back-end application running on the server. This back-end application then
interacts with the database and returns the results to the front-end application on the client
computer, which then puts it into a user-friendly format and displays it to the user.
With the rapid development of the Internet in the 1990s, many companies centralized their key
software elements on Internet application servers, making it possible to serve client computers
worldwide. This approach saves both time and money when changes need to be made to the software. It also means only one central database needs to be maintained. Database Management
Systems (DBMSs) are a collection of programs and tools designed to allow for the creation,
modification, manipulation, maintenance, and access of information from databases.
Several free open source DBMS programs and tools facilitate creation, management of, and
retrieval of data from a database as well as interaction with a variety of closed source databases, including those from Microsoft and Oracle.
The most popular and widely used DBMSs available for Linux today are PostgreSQL,
MySQL (My Structured Query Language), and MariaDB (based on MySQL). All three are
powerful, fast, and light, and they can interact with other databases such as Oracle. They
can also be integrated with most Web servers via CGI scripts for use as an application server
on the Internet, and are supported by most other open source technologies.
To learn more about PostgreSQL, www.postgresql.org. To learn more
about MySQL, visit www.mysql.com. To learn more about MariaDB,
visit mariadb.org.
Application servers need not only be used for interaction with databases; they can provide
management functionality as well, allowing access and administration from anywhere in the
world via the Internet. Management interfaces have taken advantage of the comprehensive
development surrounding client Web browsers and Internet technologies and now offer a
full range of computer management capabilities from the comfortable and standard interface
of the client Web browser. One common open source management interface for Linux is
Webmin, which is a customizable application server that allows users to manage almost all
services available in Linux from anywhere on the Internet.
Webmin can be found at www.webmin.com.
Cloud Systems
The cloud is a widely used but often poorly understood term today. Essentially, the cloud is
another term for the Internet (which has traditionally been represented by a cloud icon on
network diagrams). As a result, any service or server that you contact across the Internet
can be called a cloud service or cloud server, respectively.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
30
Chapter 1
Introduction to Linux
However, to a data architect, the term “cloud” refers to a large system of computers that
work together to provide a wide set of services to users and organizations across the Internet.
Facebook, Amazon, Twitter, Gmail, Office365, PayPal, and eBay are all good examples of
cloud systems; each one consists of thousands of different servers that host specialized software that store and retrieve large amounts of information using specialized server applications and provide mechanisms that prevent data loss.
Most cloud systems on the Internet run Linux in a cloud-based configuration using cloud
software (collectively called a cloud platform). OpenStack is one of the most popular open
source cloud platforms available and is supported by nearly all major Linux distributions.
You can find more information about OpenStack at
www.openstack.org.
Supercomputers
Many companies and institutions use computers to perform extraordinarily large calculations
for which most computers would be unsuitable. To accomplish these tasks, companies either
buy computers with multiple processors or use specialized services to combine several smaller
computers in a way that allows them to function as one large supercomputer. Combining
several smaller computers is called clustering. Companies and individuals requiring this type
of computing make up the supercomputing community, which is growing quickly today as
technology advances in new directions.
Although it might seem logical to purchase computers that have a large number of processors, the performance of a computer relative to the number of processors decreases as you
add processors to a computer. In other words, a computer with 64 processors does not handle 64 times as much work as one processor because of physical limitations within the computer hardware itself; a computer with 64 processors might only perform 50 times as much
work as a single processor. The ability for a computer to increase workload as the number
of processors increases is known as scalability, and most computers, regardless of the operating system used, do not scale well when there are more than 32 processors. As a result of this
limitation, many people in the supercomputing community cluster several smaller computers
together to work as one large computer. This approach results in much better scalability;
64 computers with one processor each working toward a common goal can handle close
to 64 times as much as a single processor.
Most of the supercomputing community has focused on Linux when developing clustering
technology; the most common method of Linux clustering is known as Beowulf clustering,
which is easy to configure and well documented. Although there are many different ways to
implement a Beowulf cluster, the most common method is to have one master computer send
instructions to several slave computers, which compute parts of the calculation concurrently
and send their results back to the master computer using a Message Passing Interface (MPI)
software framework such as OpenMPI. This type of supercomputing breaks tasks down into
smaller units of execution and executes them in parallel on many machines at once; thus, it is
commonly referred to as parallel supercomputing, and many free programs are available
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Common Uses of Linux
31
that are written to run on parallelized computers. Beowulf parallel supercomputer technology
has been aggressively developed since the mid-1990s and has been tested in various environments; currently, thousands of Beowulf clusters exist worldwide in various institutions, companies, and universities.
You can find more information about OpenMPI at www.open-mpi.org.
Scientific/Engineering Workstation
Many of the developers from Richard Stallman’s Free Software Foundation came from the
scientific and engineering community, which needed to develop many programs to run analyses. In the 1980s and early-1990s, this scientific and engineering community largely developed software for the UNIX operating system that was common in universities around the
world; today, this community is focusing on developing software for Linux. Any software
previously made for UNIX can be ported to Linux easily. Scientists and engineers often use
parallel supercomputers to compute large tasks, and OSS developers, with a background in
scientific computing, have done much of the development on Beowulf technology. One example of this is SHARCnet (Shared Hierarchical Academic Research Computing Network) in
Ontario, Canada, in which several universities have developed and tested supercomputing
technology and parallel programs for use in the scientific and engineering fields.
You can find more information about SHARCnet at www.sharcnet.ca.
Often, the programs that are required by the scientific and engineering community must be
custom developed to suit the needs of the people involved; however, many OSS programs,
which you can use or modify, are freely available in many different scientific and engineering
fields, including, but not limited to, the following list:
•
Physics, Astrophysics, and Biophysics
•
Fluid Dynamics and Geophysics
•
Biocomputation
•
Materials and Polymer Chemistry
•
General Mathematics and Optimization
•
Data Mining
•
Number Theory
•
Computer/Linear/Array Algebra
•
Mathematical Visualization and Modeling
•
Statistics and Regression Analysis
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
32
Chapter 1
Introduction to Linux
• Data Plotting and Processing
• Computer Graphics Generation
• Computer Modeling
• Paleontology
• Molecular Modeling
• Electrical Engineering
• Artificial Intelligence
• Geographic Modeling and Earth Sciences
• Oceanography
Office/Personal Workstation
Server services for Linux were the primary focus of OSS development for Linux in the 1990s,
but since 2000 this focus has been expanded to many other types of software, including
workstation software designed to be of benefit to end users in the office and home environments. By definition, a workstation is a single-user computer, more powerful than a typical
home system; however, people commonly call any single-user computer that is not a server a
workstation. It is where users work and interact, running programs and connecting to servers. Today, you will find Linux on desktop and laptop computers running many different
OSS packages that allow the user to create, organize, and manipulate office documents and
graphic art, including but not limited to the following:
• Graphic editing software (such as Gimp)
• Desktop publishing software (such as Scribus)
• Media software (such as VLC)
• Financial software (such as Gnucash)
• Office productivity suites (such as Apache OpenOffice)
• BitTorrent clients (such as qBitTorrent)
Mobile Devices
In the past decade, mobile devices such as tablets and smartphones have grown tremendously
in popularity. Today, mobile computing is the primary means of communication for millions
of people worldwide.
Following the introduction of the Apple iPhone in 2007, several Linux-based smartphone
and tablet operating systems started to appear on the market. The most notable of these was
Google Android in 2008. Linux provides the core architecture used on Google Android
devices, and an additional Android framework provides for user functionality. Android was
acquired by Google in 2005 and is currently developed by Google’s Open Handset Alliance,
which consists of nearly 100 different hardware and software manufacturers.
Due to its open nature and large number of associated smartphone manufacturers, consumer,
professional, and developer support for Android has grown exponentially in recent years.
Today, you can find Android on many other devices that provide specialized user interfaces,
such as televisions, storage systems, cars, wristwatches, and eyeglasses. By January 2014,
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter Summary
33
Android was installed on over 79 percent of smartphones in North America, and the Google
Play Store (Google’s Android app store) enjoys a healthy app ecosystem. In July 2013, Google announced that its store hosted more than 1 million apps and boasted over 50 billion
downloads.
Although Google Android is the most successful mobile application of Linux, there are many
other Linux distributions that you may find on mobile devices, including but not limited to
the following:
•
Bada
•
Firefox OS
•
Maemo
•
MeeGo
•
Moblin
•
Openmoko Linux
•
OpenZaurus
•
Replicant (based on Android)
•
Sailfish OS
•
SHR
•
Tizen
•
Ubuntu Mobile
•
Ubuntu Touch
•
webOS
Chapter Summary
■ Linux is an operating system whose kernel and associated software packages are
freely developed and improved upon by a large community of software developers in
collaboration. It is based on the UNIX operating system and has roots in the hacker
culture perpetuated by the Free Software Foundation.
■ Because Linux is published under the GNU Public License, it is referred to as Open
Source Software (OSS). Most additional software that is run on Linux is also OSS.
■ Companies find Linux a stable, low-risk, and flexible alternative to other operating
systems; it can be installed on several different hardware platforms to meet business
needs and results in a lower TCO.
■ Linux is available in different distributions, all of which have a common kernel but
are packaged with different OSS applications.
■ A wide variety of documentation and resources for Linux exists in the form of
Internet Web sites, HOWTOs, FAQs, newsgroups, and LUGs.
■ Linux is an extremely versatile operating system that can provide a wide range of
workstation and server services to meet most computing needs of companies and
individuals.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
34
Chapter 1
Introduction to Linux
Key Terms
AIX
A version of UNIX developed by IBM.
Android A mobile Linux-based operating system currently developed by Google’s Open
Handset Alliance.
application (app) The software that runs on an operating system and provides the user
with specific functionality (such as word processing or financial calculation). Applications
are commonly referred to as apps today.
artistic license An open source license that allows source code to be distributed freely but
changed at the discretion of the original author.
Beowulf clustering
A popular and widespread method of clustering computers together to
perform useful tasks using Linux.
BSD (Berkeley Software Distribution) A version of UNIX developed out of the original
UNIX source code and given free to the University of California, Berkeley by AT&T.
closed source software
The software whose source code is not freely available from the
original author; Windows 98 is an example.
cloud
Another term for the Internet.
cloud platform
A series of software components that are installed on servers distributed
across the Internet and provide services to a large number of Internet users.
cluster
A grouping of several smaller computers that function as one large supercomputer.
clustering
cracker
database
The act of making a cluster; see also cluster.
A person who uses computer software maliciously for personal profit.
An organized set of data.
Database Management System (DBMS)
Software that manages databases.
developmental kernel A Linux kernel whose minor number is odd and has been recently
developed yet not thoroughly tested.
device driver
A piece of software containing instructions that the kernel of an operating
system uses to control and interact with a specific type of computer hardware.
distribution A complete set of operating system software, including the Linux kernel,
supporting function libraries, and a variety of OSS packages that can be downloaded from
the Internet free of charge. These OSS packages are what differentiate the various
distributions of Linux.
flavor A term that refers to a specific type of UNIX operating system. For example, Solaris
and BSD are two different flavors of UNIX.
Free Software Foundation (FSF)
An organization started by Richard Stallman that
promotes and encourages the collaboration of software developers worldwide to allow the
free sharing of source code and software programs.
freeware
Software distributed by the developer at no cost to the user.
frequently asked questions (FAQs)
An area on a Web site where answers to commonly
posed questions can be found.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
fully qualified domain name (FQDN)
35
A string of words identifying a server on the Internet.
An acronym that stands for “GNU’s Not Unix.”
GNU
GNU General Public License (GPL)
A software license, ensuring that the source code for any
OSS will remain freely available to anyone who wants to examine, build on, or improve
upon it.
GNU Network Object Model Environment (GNOME)
One of the two competing graphical
user interface (GUI) environments for Linux.
GNU Project
A free operating system project started by Richard Stallman.
graphical user interface (GUI) The component of an operating system that provides a userfriendly interface comprising graphics or icons to represent desired tasks. Users can point
and click to execute a command rather than having to know and use proper command-line
syntax.
GUI environment A GUI core component such as X Windows, combined with a window
manager and desktop environment that provides the look and feel of the GUI. Although
functionality might be similar among GUI environments, users might prefer one
environment to another due to its ease of use.
hacker
A person who explores computer science to gain knowledge—not to be confused
with “cracker.”
hardware
The tangible parts of a computer, such as the network boards, video card, hard
disk drives, printers, and keyboards.
hardware platform
A particular configuration and grouping of computer hardware,
normally centered on and determined by processor type and architecture.
hot fix
A solution for a software bug made by a closed source vendor.
HOWTO A task-specific instruction guide to performing any of a wide variety of tasks; it is
freely available from the Linux Documentation Project at www.linuxdoc.org.
HP-UX
A version of UNIX developed by Hewlett-Packard.
Internet Protocol (IP) address
A unique string of numbers assigned to a computer to
uniquely identify it on the Internet.
iOS
A mobile version of UNIX developed by Apple for use on iPhone, iPod and iPad devices.
K Desktop Environment (KDE)
One of the two competing graphical user interfaces (GUIs)
available for Linux.
kernel The central, core program of the operating system. The shared commonality of the
kernel is what defines Linux; the differing OSS applications that can interact with the
common kernel are what differentiates Linux distributions.
Linus Torvalds
A Finnish graduate student who coded and created the first version of
Linux and subsequently distributed it under the GNU Public License.
Linux A software operating system originated by Linus Torvalds. The common core, or
kernel, continues to evolve and be revised. Differing OSS bundled with the Linux kernel is
what defines the wide variety of distributions now available.
Linux Documentation Project (LDP)
A large collection of Linux resources, information,
and help files supplied free of charge and maintained by the Linux community.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
36
Chapter 1
Introduction to Linux
Linux User Group (LUG) The open forums of Linux users who discuss and assist each other
in using and modifying the Linux operating system and the OSS run on it. There are LUGs
worldwide.
Macintosh OS X A version of UNIX developed by Apple for use on Apple desktop
computers and servers.
Mail Delivery Agent (MDA)
The service that downloads e-mail from a mail transfer agent.
Mail Transfer Agent (MTA)
An e-mail server.
Mail User Agent (MUA)
A program that allows e-mail to be read by a user.
major number The number preceding the first dot in the number used to identify a Linux
kernel version. It is used to denote a major change or modification.
Message Passing Interface (MPI) A system that is used on Beowulf clusters to pass
information to several separate computers in a parallel fashion.
MINIX
Mini-UNIX created by Andrew Tannenbaum. Instructions on how to code the
kernel for this version of the Unix operating system were publicly available. Using this as a
starting point, Linus Torvalds improved this version of UNIX for the Intel platform and
created the first version of Linux.
minor number The number following the first dot in the number used to identify a Linux
kernel version, denoting a minor modification. If odd, it is a version under development and
not yet fully tested. See also developmental kernel and production kernel.
Multiplexed Information and Computing Service (MULTICS) A prototype time-sharing
operating system that was developed in the late-1960s by AT&T Bell Laboratories.
multitasking
A type of operating system that is able to manage multiple tasks simultaneously.
multiuser A type of operating system that is able to provide access to multiple users
simultaneously.
newsgroup
An Internet protocol service accessed via an application program called a
newsreader. This service allows access to postings (e-mails in a central place accessible by all
newsgroup users) normally organized along specific themes. Users with questions on specific
topics can post messages, which can be answered by other users.
Open Source Software (OSS) The programs distributed and licensed so that the source
code making up the program is freely available to anyone who wants to examine, utilize, or
improve upon it.
OpenSUSE One of the most popular and prevalent distributions of Linux, originally
developed in Europe.
operating system (OS) The software used to control and directly interact with the
computer hardware components.
package manager
The software used to install, maintain, and remove other software
programs by storing all relevant software information in a central software database on the
computer.
process A program loaded into memory and running on the processor performing a
specific task.
production kernel
A Linux kernel whose minor number (the number after the dot in the
version number) is even, therefore deemed stable for use through widespread testing.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
37
program The sets of instructions that know how to interact with the operating system and
computer hardware to perform specific tasks; stored as a file on some media (e.g., a hard
disk drive).
programming language The syntax used for developing a program. Different
programming languages use different syntaxes.
proxy server A server or hardware device that requests Internet resources on behalf of
other computers.
Red Hat
One of the most popular and prevalent distributions of Linux in North America,
distributed and supported by Red Hat, Inc. Fedora is a Red Hat-based Linux distribution.
revision number
The number after the second dot in the version number of a Linux kernel
that identifies the release number of a kernel.
router A computer running routing software or a special function hardware device that
provides interconnection between networks; it contains information regarding the structure
of the networks and sends information from one component network to another.
scalability
The capability of computers to increase workload as the number of processors
increases.
search engine
An Internet Web site, such as www.google.com, where you simply enter a
phrase representing your search item and receive a list of Web sites that contain relevant material.
server
A computer configured to allow other computers to connect to it from across a network.
server services
The services that are made available for other computers across a network.
shareware
The programs developed and provided at minimal cost to the end user. These
programs are initially free but require payment after a period of time or usage.
software The programs stored on a storage device in a computer, which provide a certain
function when executed.
Solaris
A version of UNIX developed by Sun Microsystems from AT&T source code.
source code
The sets of organized instructions on how to function and perform tasks that
define or constitute a program.
system service
The additional functionality provided by a program that has been
incorporated into and started as part of the operating system.
tarball
A compressed archive of files containing scripts that install Linux software to the
correct locations on a computer system.
total cost of ownership (TCO) The full sum of all accumulated costs, over and above the
simple purchase price of utilizing a product. Includes training, maintenance, additional
hardware, and downtime.
Ubuntu
A major Linux distribution that is widely used in North America.
UNIX The first true multitasking, multiuser operating system, developed by Ken Thompson
and Dennis Ritchie, and from which Linux was originated.
user
A person who uses a computer.
user interface The interface the user sees and uses to interact with the operating system
and application programs.
workstation
A computer used to connect to services on a server.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
38
Chapter 1
Introduction to Linux
workstation services
The services that are used to access shared resources on a network
server.
X Windows
The core component of the GUI in Linux.
Review Questions
1. Every computer consists of physical components and nonphysical components. The
nonphysical components of a computer that understand how to work with the physical
.
components are referred to as
a.
hardware
b. records
c.
software
d. processors
2. The operating system software is necessary for a computer to function. True or False?
3. Linux is a
a.
and
operating system.
production, stable
b. multiuser, multitasking
c.
processing, closed source
d. large, useful
4. The core component of the Linux operating system is the Linux kernel. If you were a
Linux systems administrator for a company, when would you need to upgrade your
Linux kernel? (Choose all that apply.)
a.
when you need support in Linux for new hardware
b. when you need another user interface
c.
when you need to increase the stability of Linux
d. when you need to use kernel modules
5. Which of the following kernels are developmental kernels? (Choose all that apply.)
a.
3.3.4
b. 3.5.5
c.
3.2.7
d. 3.4.4
6. A production kernel refers to a kernel whose
a.
.
revision number is even
b. minor number is odd
c.
major number is odd
d. minor number is even
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
39
7. Many types of software are available today. Which type of software does Linux
represent?
a.
Open Source Software
b. closed source software
c.
freeware
d. shareware
8. Which of the following are characteristics of Open Source Software? (Choose all that
apply.)
a.
The value of the software is directly related to its price.
b. The software is developed collaboratively.
c.
The source code for software is available for a small fee.
d. Any bugs are fixed quickly.
9. To which license does Linux adhere?
a.
open license
b. artistic license
c.
GNU General Public License
d. free source license
10. What are some good reasons for using Linux in a corporate environment? (Choose all
that apply.)
a.
Linux software is unlikely to be abandoned by its developers.
b. Linux is secure and has a lower total cost of ownership than other operating
systems.
c.
Linux is widely available for many platforms and supports many programming
languages.
d. Most Linux software is closed source.
11. Which of the following are common methods for gaining support for Linux?
a.
HOWTO documents at www.linuxdoc.org
b. a local Linux User Group
c.
Internet newsgroups
d. all the above
12. Which two people are credited with creating the UNIX operating system? (Choose two
answers.)
a.
Dennis Ritchie
b. Richard Stallman
c.
Linus Torvalds
d. Ken Thompson
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
40
Chapter 1
Introduction to Linux
13. Who formed the Free Software Foundation to promote open development?
a.
Dennis Ritchie
b. Richard Stallman
c.
Linus Torvalds
d. Ken Thompson
14. Which culture embraced the term “GNU” (GNU’s Not UNIX) and laid the free software groundwork for Linux?
a.
the hacker culture
b. the MIT culture
c.
the cracker culture
d. the Artificial Intelligence culture
15. Linux was developed by
system.
a.
to resemble the
operating
Linus Torvalds, MINIX
b. Linus Torvalds, GNU
c.
Richard Stallman, GNU
d. Richard Stallman, MINIX
16. When the core components of the Linux operating system are packaged together with
.
other Open Source Software, it is called a
a.
new kernel
b. new platform
c.
Linux distribution
d. GNU Project
17. Which common GUI environments are available in most Linux distributions? (Choose
all that apply.)
a.
GNOME
b. CDE
c.
KDE
d. RPM
18. Which of the following are factors that determine which Linux distribution a user will
use? (Choose all that apply.)
a.
package manager support
b. hardware platform
c.
kernel features
d. language support
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
41
19. What is the most common open source Web server available for Linux?
a.
Samba
b. Apache
c.
Squid
d. pine
20. Which of the following can be used on Linux to provide file and print services?
a.
Samba
b. Apache
c.
Squid
d. pine
Discovery Exercises
1. You work for a large manufacturing company that is considering Linux as a solution for
some or all servers in its IT Department. The company hosts an Oracle database on
UNIX, and the UNIX servers that host this database contain several small programs that
were custom-made. Furthermore, Windows 8 is currently used on desktops throughout
the company, and users store their data on Windows Server 2012 file servers. What
considerations must you keep in mind before migrating your company’s servers to
Linux? Which distribution(s) and Open Source Software would you choose to
accomplish this? If you need to create a report detailing the benefits of moving to an
open source solution using Linux, what benefits would you list in the report to persuade
others in the company that Linux lowers the total cost of ownership?
2. At a local Linux User Group (LUG) meeting, some people who are unfamiliar with
Linux ask you to explain what the GPL is and how it relates to OSS. These people also
don’t understand how OSS generates profit and are under the impression that the
quality of OSS is poor compared to commercial software. They suggest that the
potential for OSS in the future might be limited. How do you reply? Include examples to
demonstrate your points. To which Web sites can you direct them for further
information?
3. As a software developer working for a large clothing store chain, you are responsible for
creating software used to connect retail store computers to a central database at the
head office. Recently, some friends of yours suggested that you publish your software
under the GPL. What are some direct benefits to publishing your software under the
GPL? To publish software made for a company under the GPL, you need the company’s
permission because the company owns any software that it pays developers to create.
When you approach people in your company regarding OSS and explain how
companies benefit from releasing software as open source, you are asked what benefits
the company will receive from funding an open source project over time. Your company
also wants to know what the procedure is for releasing and maintaining OSS. What
benefits will you offer them? Where could you send them to gain more information on
procedures involved in the open source community?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
1
42
Chapter 1
Introduction to Linux
4. You are a network administrator who is in charge of a medium-sized Linux network.
The company you work for asks you to implement routing in the network, a topic with
which you are unfamiliar. Where could you go to learn what you must obtain to enable
routing on your Linux network? Provided that you have a functional Web browser and
an Internet connection, explore this topic on the Internet and list the Web sites that you
used to obtain the information required. This information might range from broad
descriptions of what you need to do to accomplish a certain task to detailed guides and
instructions on putting your plan into action. From these sources of information, devise
a report outlining the major steps necessary to implement routing on your network.
5. At a company function, a top executive corners you and complains that your
department is wasting too much money. The executive demands to know why the
company must spend so much money on computers and software, especially operating
systems and related licenses (for closed source programs and operating systems). Write a
report that defends your department by explaining the nature of hardware, software,
and operating systems. In the report, be sure to explain how OSS and the Linux
operating system can be used to reduce these costs in the long term.
6. You are contacted by a project organizer for a university computer science fair. The
project organizer asks you to hold a forum that discusses the origins of the Linux
operating system, including how it has evolved and continues to develop. The main
focus of this forum is to encourage university students toward participating in the open
source community; therefore, it should detail the philosophy, major features, and
methods of the hacker culture. Prepare a bulleted list of the major topics that you will
discuss, and write down some sample questions that you anticipate from the participants
as well as your responses.
7. Research three different distributions of Linux on the Internet. Record where you went
to obtain your information. Compare and contrast the different distributions with
regard to their strengths and the packages available for each. After you finish, locate and
visit two Linux newsgroups. How did you locate them and where did you obtain the
information? What are the topics specific to each? Find two questions per newsgroup
posted by a user in need of a solution to a problem, and follow the thread of responses
suggested by others to solve that problem.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
2
Linux Installation and Usage
After completing this chapter, you will be able to:
•
•
•
•
Prepare for and install Fedora Linux using good practices
Outline the structure of the Linux interface
Enter basic shell commands and find command documentation
Properly shut down the Linux operating system
43
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
44
Chapter 2
Linux Installation and Usage
This chapter explores the concepts and procedures needed to install a Fedora Linux
system. The latter half of the chapter presents an overview of the various components that
you will use when interacting with the operating system, as well as how to enter basic shell
commands, obtain help, and properly shut down the Linux system.
Installing Linux
The installation of Linux requires careful planning as well as the configuration of various
parts of the Linux operating system via an installation program.
Preparing for Installation
An operating system is merely a series of software programs that interact with and control
the computer hardware. Thus, all operating systems require a certain minimum set of computer hardware requirements to function properly. Although most up-to-date hardware is
sufficient to run the Linux operating system, it is nonetheless important to ensure that a computer meets the minimum hardware requirements before performing an installation.
These minimum installation requirements can be obtained from several sources. If you
obtained the operating system on DVD, a printed manual or file on the DVD might specify
these requirements. You can also find the minimum hardware requirements for most operating systems on the vendor’s Web site. For the Fedora 20 Linux operating system, you can
find the minimum hardware requirements at http://docs.fedoraproject.org or in Table 2-1.
Furthermore, each operating system supports only particular types of hardware components.
Although some operating systems such as Linux support a wider variety of hardware components than other operating systems, each individual hardware component in your computer
should be checked against the Hardware Compatibility List (HCL) readily found on the vendor’s Web site.
For Fedora Linux, the HCL can be found at www.fedoraproject.org/
wiki/HCL.
Type of Hardware
Requirement
Central processing unit (CPU)
1GHz or faster Intel x64 CPU
Random access memory (RAM)
1GB
Free disk space (permanent storage)
10GB free space
Additional drives
DVD drive (for DVD-based installation)
Peripheral devices
Fedora-compliant peripheral devices (e.g., video cards, sound cards,
network cards)
Table 2-1 Fedora 20 recommended minimum hardware requirements
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
45
Understanding Installation Media
Before performing a Linux installation, you must choose the source of the Linux packages
and the installation program itself. The most common source of these packages is DVD
media.
To install from DVD, you simply place the Linux DVD in the DVD drive and turn on the
computer. Most computers automatically search for a startup program on the DVD immediately after being turned on; the computer can then use the DVD to start the Linux installation. Alternatively, most modern computers allow you to manually select the boot device
using a special manufacturer-specific key, such as F12, during the startup sequence.
Turning on a computer to load an operating system is commonly
referred to as booting a computer. Because the Linux installation
program on DVD can be loaded when you first turn on the computer, it is referred to as a bootable DVD.
Nearly all Linux distributions provide a Web site from which you can download DVD
images (called ISO images) that have an .iso file extension. These ISO images can be written
to a blank writable DVD using disc burning software on an existing Windows, Linux, or
Macintosh computer and then used to boot your computer to start the Linux installation.
In addition to a standard Linux installation DVD image, many Linux distribution Web sites
allow you to download a bootable live media DVD image. If you write a live media DVD
image to a blank DVD and boot your computer with it, a fully functional graphical Linux
operating system that you can use will be loaded into RAM. This allows you to test the operating system on your computer to ensure that all hardware drivers were detected properly
before installing it to permanent storage, such as hard disk or solid-state drive (SSD). Once
you are satisfied with the functionality of your Linux system loaded from live media, you
can select the appropriate icon on the desktop to start the installation program that will
install the Linux system to permanent storage.
To obtain a standard DVD image or live media DVD image of Fedora
20, you can visit www.fedoraproject.org.
If your computer does not have a DVD drive, you can still install Linux by imaging the
standard DVD or live media DVD ISO image to a USB flash drive, provided that your
computer supports booting from USB flash drive media. To make this process easier,
many distributions provide a program and instructions that can be used to perform the
imaging process. For Fedora Linux, you can download and install the Fedora LiveUSB
Creator tool on an existing Windows system, as shown in Figure 2-1. After selecting the
appropriate ISO image and target USB flash drive, you can click Create Live USB to start
the imaging process.
You can download the Fedora LiveUSB Creator program at
www.fedorahosted.org/liveusb-creator.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
46
Chapter 2
Linux Installation and Usage
Figure 2-1 The Fedora LiveUSB Creator program
After the imagining process is complete, you can simply insert your USB flash drive into a
free USB slot, turn on your computer, use the appropriate key for your computer to select
the target boot device (e.g., F12), and then choose the option to boot from your USB flash
drive.
Many server and workstation computers today have the ability to run multiple operating systems concurrently using virtualization software. There are several virtualization software products available on the market today, including:
• Microsoft Hyper-V
• VMware
• Oracle VM VirtualBox
Each operating system that is run within virtualization software is called a virtual machine,
and the underlying operating system running the virtualization software is called the virtual
machine host. Figure 2-2 shows a Fedora Linux virtual machine running on the Windows 8
operating system using the Microsoft Hyper-V virtualization software.
Most enterprise environments today take advantage of virtualization
software to run multiple server operating systems concurrently on
the same computer hardware. This allows organizations to better utilize their server hardware and reduce costs.
To install Linux as a virtual machine, you simply need to download the standard DVD or
live media DVD ISO image to a directory on your virtual machine host (e.g., Windows).
When you open the virtualization software and choose to create a new virtual machine, you
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
47
2
Figure 2-2 Fedora Linux running as a virtual machine on the Windows 8 operating system
can specify the file location of the appropriate ISO image, and the virtualization software will
boot from the ISO image directly, without the need to write the ISO image to a DVD or
USB flash drive. Figure 2-3 shows the section of the Hyper-V New Virtual Machine Wizard
that allows you to specify the location of an ISO image that contains the Fedora 20 installation media.
Figure 2-3 Selecting installation media within the Hyper-V New Virtual Machine Wizard
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
48
Chapter 2
Linux Installation and Usage
Performing the Installation
Installing the Linux operating system involves interacting with an installation program,
which prompts the user for information regarding the nature of the Linux system being
installed. More specifically, the installation program for Fedora 20 Linux involves the following general stages:
• Starting the installation
• Choosing an installation language as well as localization and system options
• Configuring disk partitions and filesystems
• Configuring user accounts
Starting the Installation As mentioned earlier, to perform an installation of Fedora
Linux, you can simply boot your computer using Fedora installation media. If you are
booting your system from standard Fedora installation media, you will be prompted to
start the installation or perform troubleshooting actions. However, if you boot your system from Fedora live media, you will instead be prompted to start a live Fedora system
(which later allows you to install Fedora) or perform troubleshooting actions, as shown
in Figure 2-4.
If you select the Troubleshooting option from Figure 2-4, you will be presented with four
additional options, as shown in Figure 2-5.
Selecting Start Fedora Live in basic graphics mode from Figure 2-5 will start the Fedora system with generic video drivers and low resolution, which is useful if the live Fedora system
doesn’t detect the correct driver for your video card and cannot display a graphical desktop
as a result.
Defective media and RAM errors are common causes of a failed Linux installation. If you
select Test this media & start Fedora Live in Figure 2-5, the installation media will be
Figure 2-4 Beginning a Fedora installation
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
49
2
Figure 2-5 Fedora installation troubleshooting options
thoroughly tested for defects before the Fedora system is started, and if you select Run a
memory test, the memtest86 utility will start and perform a thorough check of your RAM
for hardware errors, as shown in Figure 2-6.
The Boot from local drive option in Figure 2-5 is useful in case you accidentally leave a
Fedora installation DVD in your DVD drive after the installation has completed and your
system is configured to boot an operating system from your DVD drive before searching
for an operating system on permanent storage.
In most cases, the troubleshooting options shown in Figure 2-5 aren’t necessary when installing Fedora Linux. As a result, you can simply choose Start Fedora Live from Figure 2-4 to
start a live Fedora system. Once the live Fedora system has loaded, you will be presented
with a welcome screen that prompts you to install Fedora Linux on permanent storage
Figure 2-6 The memtest86 utility
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
50
Chapter 2
Linux Installation and Usage
Figure 2-7 The Fedora Live welcome screen
or continue using the live Fedora system loaded from your installation media, as shown in
Figure 2-7.
If you choose Install to Hard Drive in Figure 2-7, the Fedora installation program will start.
Alternatively, if you choose Try Fedora, you will be able to explore the desktop of a live
Fedora system and can later select Install to Hard Drive from the Activities menu in the
upper-left corner of the desktop to start the Fedora installation program.
Choosing an Installation Language as Well as Localization and System
Options After you start a Fedora installation, you will be prompted to select a
language that is used during the installation program, as shown in Figure 2-8. If you
press Continue, you will be prompted to configure the date and time, keyboard layout,
installation destination (e.g., hard disk, SSD), and network configuration, as shown in
Figure 2-9.
You can click on each of the localization and system option icons in Figure 2-9 to modify its
configuration. By default, your keyboard layout is automatically detected, your network
interface is set to obtain network configuration automatically using the DHCP protocol,
and the date and time are automatically obtained from the Internet if your network has
Internet connectivity. However, you must manually select an installation destination before
the installation can continue (hence the warning shown in Figure 2-9). The installation destination is a permanent storage device that will contain the Linux operating system; it is often
a hard disk or SSD.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
51
2
Figure 2-8 Selecting an installation language
Figure 2-9 Configuring localization and system options
Older systems often use Parallel Advanced Technology Attachment (PATA) hard disks
that physically connect to the computer in one of four different configurations. As shown in
Table 2-2, Linux refers to each of these disks according to its configuration on your computer.Ą
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
52
Chapter 2
Linux Installation and Usage
Description
Linux Name
Primary master PATA hard disk
hda
Primary slave PATA hard disk
hdb
Secondary master PATA hard disk
hdc
Secondary slave PATA hard disk
hdd
Table 2-2 PATA hard disk configurations
In the past, PATA hard disks were referred to as Integrated Drive
Electronics (IDE) or Advanced Technology Attachment (ATA) hard
disks.
You can verify your PATA disk configuration by accessing your computer’s BIOS (Basic Input/Output System) configuration, which is
the part of a computer system that contains the programs used to
initialize hardware components at boot time. You can access your
BIOS configuration by pressing the appropriate manufacturer-specific
key, such as F10, during system startup.
Newer systems typically use Serial Advanced Technology Attachment (SATA) hard disks or
SSDs, and some server systems use Small Computer Systems Interface (SCSI) hard disks.
Unlike PATA, you can have more than four SATA and SCSI hard disks or SSDs within a
system. The first SATA/SCSI hard disk or SSD is referred to as sda, the second SATA/SCSI
hard disk or SSD is referred to as sdb, and so on.
If you click the installation destination icon shown in Figure 2-9, you will be presented
with a list of the different permanent storage devices in your system. The system shown in
Figure 2-10 contains a single SATA/SCSI hard disk (sda).
If you have multiple disk devices, you can select the disk that will be used to contain the
Fedora Linux operating system and click Done. Normally, this is a local hard disk or SSD,
but you can also install Linux on an external Storage Area Network (SAN), Direct Access
Storage Device (DASD), or firmware Redundant Array of Inexpensive Disks (RAID) device
if you select Add a disk in Figure 2-10 and supply the appropriate configuration information.
Configuring Disk Partitions and Filesystems Regardless of type, each hard disk
or SSD is divided into sections called partitions. Before you can store files in a partition, you
must format it with a filesystem. A filesystem is a structure that specifies how data should
reside on the hard disk or SSD itself.
In the Windows operating system, each drive letter (e.g., C:, D:, E:)
can correspond to a separate filesystem that resides on a partition
on your hard disk or SSD.
There are limits to the number and types of partitions into which a hard disk or SSD can be
divided. By default, you can create a maximum of four major partitions (called primary
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
53
2
Figure 2-10 Configuring an installation destination
partitions). To overcome this limitation, you can optionally label one of these primary partitions
as “extended”; this extended partition can then contain an unlimited number of smaller partitions called logical drives. Each logical drive within the extended partition and all other primary
partitions can contain a filesystem and be used to store data. The table of all partition information for a certain hard disk or SSD is stored in the first readable sector outside all partitions; it is
called the Master Boot Record (MBR).
The MBR is limited to hard disks that are less than 2TB in size. Newer hard
disks and hard disks larger than 2TB use a GUID Partition Table (GPT)
instead of an MBR. The MBR and GPT are functionally equivalent.
Recall that, in Linux, the first SATA/SCSI device in your system is referred to as sda, the first
primary partition on this device is labeled sda1, the second sda2, and so on. Because there are
only four primary partitions allowed, logical drives inside the extended partition are labeled
sda5, sda6, and so on. An example of this partition strategy is listed in Table 2-3.
For the primary master PATA hard disk device, simply replace sda
with hda in Table 2-3.
Partitioning divides a hard disk into sections, each of which can contain a separate filesystem
used to store data. Each of these filesystems can then be accessed by Linux if it is attached (or
mounted) to a certain directory. When data is stored in that particular directory, it is physically stored on the respective filesystem on the hard disk. The Fedora installation program
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
54
Chapter 2
Linux Installation and Usage
Description
Linux Name
Windows Name
First primary partition on the first SATA/SCSI device
sda1
C:
Second primary partition on the first SATA/SCSI device
sda2
D:
Third primary partition on the first SATA/SCSI device
sda3
E:
Fourth primary partition on the first SATA/SCSI device (EXTENDED)
sda4
F:
First logical drive in the extended partition on the first
SATA/SCSI device
sda5
G:
Second logical drive in the extended partition on the first
SATA/SCSI device
sda6
H:
Third logical drive in the extended partition on the first
SATA/SCSI device
sda7
I:
Table 2-3 Example partitioning scheme for the first SATA/SCSI device
can automatically create partitions based on common configurations; however, it is generally
good practice to manually partition to suit the needs of the specific Linux system.
At minimum, Linux typically requires only two partitions to be created: a partition that is
mounted to the root directory in Linux (/) and that can contain all of the files used by the operating system, applications, and users, and a partition used for virtual memory (also known as
swap memory). Virtual memory consists of an area on the hard disk or SSD that can be used to
store information that would normally reside in physical memory (RAM) if the physical memory
was being used excessively. When programs are executed that require a great deal of resources on
the computer, information is continuously swapped from physical memory to virtual memory,
and vice versa. Traditionally, Linux swap partitions were made to be at least the size of the physical RAM in the computer; however, they can be much larger if the Linux system is intended to
run large applications. A swap partition does not contain a filesystem and is never mounted to a
directory because the Linux operating system is ultimately responsible for swapping information.
Although you might choose to create only root and swap partitions, extra partitions make
Linux more robust against filesystem errors. For example, if the filesystem on one partition
encounters an error, only data on one part of the system is affected and not the entire system (i.e., other filesystems). Because there are some common directories in Linux that are
used vigorously and as a result are more prone to failure, it is good practice to mount these
directories to their own filesystems. Table 2-4 lists directories that are commonly mounted
to separate partitions as well as their recommended sizes.
Each of these filesystems can be of different types. The most common types used today are the
ext2, ext3, ext4, VFAT, and REISER filesystems, although Linux can support upward of 50
different filesystems. Each filesystem essentially performs the same function, which is to store
files on a partition; however, each filesystem offers different features and is specialized for different uses. The ext2 filesystem is the traditional filesystem, and the Virtual File Allocation
Table (VFAT) filesystem is compatible with the FAT and FAT32 filesystems in Windows. The
ext3, ext4, and REISER filesystems, however, are much more robust than the ext2 and VFAT
filesystems, as they perform a function called journaling. A journaling filesystem keeps track of
the information written to the hard disk in a journal. If you copy a file on the hard disk from
one directory to another, that file must pass into RAM and then be written to the new location
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing Linux
55
Directory
Description
Recommended Size
/
Contains all directories not
present on other filesystems
Depends on the size and number of other filesystems
present, but is typically 10GB or more
/boot
Contains the Linux kernel and
boot files
500MB
/home
Default location for user home
directories
200MB per user
/usr
System commands and utilities
Depends on the packages installed—typically 20GB or
more
/usr/local
Location for most additional
programs
Depends on the packages installed—typically 20GB or
more
/opt
An alternate location for
additional programs
Depends on the packages installed—typically 20GB or
more
/var
Contains log files and spools
Depends on whether the Linux system is used as a print
server (which contains a large spool). For print servers,
10GB or more is typical. For other systems, 2GB or more
is usually sufficient.
/tmp
Holds temporary files created by
programs
500MB
Table 2-4 Common Linux filesystems and sizes
on the hard disk. If the power to the computer is turned off during this process, information
might not be transmitted as expected and data might be lost or corrupted. With a journaling
filesystem, each step required to copy the file to the new location is first written to a journal;
this means the system can retrace the steps the system took prior to a power outage and complete the file copy. These filesystems also host a variety of additional improvements compared
to ext2 and VFAT, including faster data transfer and indexing, which makes them common
choices for Linux servers today.
Once you have selected the appropriate hard disk or SSD as an installation destination during
the Fedora Linux installation, the installation program will prompt you to choose the partitioning scheme, whether the number and type of disk partitions should be configured automatically,
and if partition contents should be automatically encrypted, as shown in Figure 2-11.
Figure 2-11 Selecting disk partitioning options
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
56
Chapter 2
Linux Installation and Usage
In addition to the standard partitions that we have discussed already, you can instead choose a
partition scheme that creates logical volumes for your Linux filesystems using the Logical
Volume Manager (LVM) or partitions that support the new B-tree Filesystem (BTRFS).
LVM and BTRFS are discussed in Chapter 5.
To allow for easier system recovery, it is good form to choose a standard partition scheme
and ensure that contents of disk partitions are not encrypted.
If you select the I want to review/modify my disk partitions before continuing option in
Figure 2-11 and click Continue, you will be prompted to create a default partition layout
that you can modify to suit your needs, as shown in Figure 2-12. This layout will consist of
a /boot and a / partition with an ext4 filesystem, as well as a swap partition that is the same
size as the RAM in your computer.
If your system has a hard disk or SSD that uses a GPT instead of an
MBR and does not contain an EFI BIOS on the motherboard, the
installation program may create a small BIOS Boot partition to store
GPT-related information.
Once you are satisfied with your partition and filesystem configuration, you can click Done
in Figure 2-12, confirm your changes in the dialog window that appears, and return to the
localization and system options configuration screen shown in Figure 2-9. If you click the
Begin Installation button in Figure 2-9, partition changes will be written to your storage
device and the Fedora system packages will be installed from the installation media to the
appropriate filesystems. This process will take some time to complete.
Configuring User Accounts All Linux systems require secure access, which means
that each user must log in with a valid user name and password before gaining access to a
user interface. This process is called authentication. During installation, you need to
Figure 2-12 Configuring disk partitions and filesystems
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
57
2
Figure 2-13 Configuring user accounts during a Fedora installation
configure two user accounts: the administrator account (root), which has full rights to the
system, as well as a regular user account, since the root user account should only be used
when performing system administration tasks.
While the Fedora system packages are being installed, the Fedora installation program
prompts you to configure the password for the root user as well as create a new user
account (with a name of your choice) by selecting the associated icons shown in Figure 2-13.
One you have configured a new user account and specified a password for the root user,
you can click the Quit button that appears to exit the installation program. Next, you can
shut down your live Fedora system, remove the installation media from your computer,
and boot your computer into your new Fedora system.
Basic Linux Usage
After the Linux operating system has been installed, you must log in to the system with a
valid user name and password and interact with the user interface to perform useful tasks.
To do this, it is essential to understand the different types of user interface that exist, as well
as basic tasks, such as command execution, obtaining online help, and shutting down the
Linux system.
Shells, Terminals, and the Kernel
Recall that an operating system is merely a collection of software that allows you to use your
computer hardware in a meaningful fashion. Every operating system has a core component,
which loads all other components and serves to centrally control the activities of the computer. This component is known as the kernel, and in Linux it is simply a file, usually called
vmlinuz, that is located on the hard disk and loaded when you first turn on your computer.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
58
Chapter 2
Linux Installation and Usage
Terminal #1
Shell #1
Terminal #2
Shell #2
The Kernel
Terminal #3
Shell #3
Terminal #4
Shell #4
Figure 2-14 Shells, terminals, and the kernel
When a user interacts with her computer, she is interacting with the kernel of the computer’s
operating system. However, this interaction cannot happen directly; it must have a channel
through which it can access the kernel as well as a user interface that passes user input to
the kernel for processing. The channel that allows a certain user to log in is called a terminal,
and there can be many terminals in Linux that allow you to log in to the computer locally or
across a network. After a user logs in to a terminal, he receives a user interface called a shell,
which then accepts input from the user and passes this input to the kernel for processing. The
shell that is used by default in Linux is the BASH shell (short for Bourne Again Shell), which
is an improved version of the Bourne shell from AT&T and is the shell that is used throughout this book. The whole process looks similar to what is shown in Figure 2-14.
As mentioned earlier, Linux is a multiuser and multitasking operating system and, as such,
can allow for thousands of terminals. Each terminal could represent a separate logged-in
user that has its own shell. The four different “channels” shown in Figure 2-14 could be different users logged in to the same Linux computer. Two users could be logged in locally to
the server (seated at the server itself), and the other two could be logged in across a network,
such as the Internet.
By default, when you log in to a terminal, you receive a command-line shell (BASH shell),
which prompts you to type in commands to tell the Linux kernel what to do. However, in
this computing age, most people prefer to use a graphical interface in which they can use a
computer mouse to navigate and start tasks. In this case, you can simply choose to start a
graphical user interface (GUI) environment on top of your BASH shell after you are logged
in to a command-line terminal, or you can switch to a graphical terminal, which allows
users to log in and immediately receive a GUI environment. A typical command-line terminal
login prompt looks like the following:
Fedora release 20 (Heisenbug)
Kernel 3.11.10-301.fc20.x86_64 on an x86_64 (tty2)
server1 login:
A typical graphical terminal login for Fedora Linux (called the GNOME Display Manager or
gdm) is depicted in Figure 2-15.
To access a terminal device at the local server, you can press a combination of keys, such as
CtrlþAltþF2, to change to a separate terminal. If you are logging in across the network, you
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
59
2
Figure 2-15 The GNOME Display Manager (gdm)
Terminal Name
Key Combination
Login Type
tty1 (:0)
CtrlþAltþF1
graphical
tty2
CtrlþAltþF2
command-line
tty3
CtrlþAltþF3
command-line
tty4
CtrlþAltþF4
command-line
tty5
CtrlþAltþF5
command-line
tty6
CtrlþAltþF6
command-line
Table 2-5 Common Linux terminals
can use a variety of programs that connect to a terminal on the Linux computer. A list of
local Linux terminals, along with their names and types, is shown in Table 2-5.
After you are logged in to a command-line terminal, you receive a prompt where you can
enter commands. The following example shows the user logging in as the root (administrator)
user. As you can see in this example, after you log in as the root user, you see a # prompt:
Fedora release 20 (Heisenbug)
Kernel 3.11.10-301.fc20.x86_64 on an x86_64 (tty2)
server1 login: root
Password:
Last login: Mon Aug 16 09:45:42 from tty2
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
60
Chapter 2
Linux Installation and Usage
However, if you log in as a regular user to a command-line terminal (e.g., user1), you see a
$ prompt, as follows:
Fedora release 20 (Heisenbug)
Kernel 3.11.10-301.fc20.x86_64 on an x86_64 (tty2)
server1 login: user1
Password:
Last login: Mon Aug 16 09:45:42 from tty2
[[email protected] ~]$_
When you log in to a graphical terminal, the GUI environment of your choice is started; the
default GUI environment in Fedora Linux is GNOME. After the GUI environment starts, you
can access a command-line Terminal window by accessing the Activites menu in the upper
left of the desktop and navigating to Show Applications, Utilities, Terminal. This will start a
command-line terminal window within your GNOME desktop, as shown in Figure 2-16.
By default, Fedora 20 allows the root user to log into the GNOME
desktop. However, this practice is not recommended for security reasons, as several graphical programs are not designed to be run as
the root user. Instead, you should log into a GUI environment as a
regular user. When you run a graphical administrative utility as a regular user, the GUI environment prompts you for the root user password in order to continue.
Figure 2-16 Using a command-line terminal within the GNOME desktop
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
61
Basic Shell Commands
When using a command-line terminal, the shell ultimately interprets all information the user
enters onto the command line. This information includes the command itself, as well as
options and arguments. Commands indicate the name of the program to execute and are
case sensitive. Options are specific letters that start with a dash “-” and appear after the command name to alter the way the command works. Options are specific to the command in
question; the persons who developed the command determined which options to allow for
that command.
Some options start with two dashes (--); these options are referred
to as POSIX options and are usually composed of a whole word, not
just a letter.
Arguments also appear after the command name, yet they do not start with a dash. They
specify the specific parameters that tailor the command to your particular needs. Suppose,
for example, that you want to list all of the files in the /etc/rpm directory on the hard disk.
You could use the ls command with the –a option (which tells the ls command to list all
files) and the /etc/rpm argument (which tells ls to look in the /etc/rpm directory), as
shown in the following example:
[[email protected] root]# ls –a /etc/rpm
. macros.color macros.fjava macros.imgcreate
.. macros.dist macros.gconf2 macros.jpackage
[[email protected] root]#_
After you type the command and press Enter in the preceding output, the ls command
shows us that there are six files in the /etc/rpm directory. The command prompt then reappears, so that you can enter another command.
Commands, options, and arguments are case sensitive; an uppercase
letter (A), for instance, is treated differently than a lowercase letter (a).
Always put a space between the command name, options, and
arguments; otherwise, the shell does not understand that they are
separate, and your command might not work as expected.
Although you can pass options and arguments to commands, not all commands need to have
arguments or options supplied on the command line to work properly. The date command,
which simply prints the current date and time, is an example:
[[email protected] root]# date
Sun Aug 19 08:46:57 EDT 2015
[[email protected] root]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
62
Chapter 2
Linux Installation and Usage
Table 2-6 lists some common commands that you can use without specifying any options or
arguments.
If the output of a certain command is too large to fit on the terminal screen, simply use the
Shift and Page Up keys simultaneously to view previous screens of information. Also, Shift
and Page Down, when pressed simultaneously, can be used to navigate in the opposite
direction.
You can recall commands previously entered in the BASH shell using the keyboard cursor
keys (the up, down, right, and left arrow keys). Thus, if you want to enter the same command again, simply cycle through the list of available commands with the keyboard cursor
keys and press Enter to reexecute that command.
As a Linux administrator, you will regularly run commands that only the root user can run
to perform system configuration. Even if you are logged in to the system as a regular user
account, you can easily switch to the root user to perform any administrative tasks using the
su (switch user) command. To switch to the root user and load the root user’s environment
variables, you can run the su command with the – option and supply the root user’s password when prompted:
[[email protected] ~]$ su - root
Password:
Last login: Mon Aug 16 09:45:42 EDT 2015 from tty2
[[email protected] root]#_
Alternatively, to run a single command as the root only, you can run the su –c “command”
root and specify the root user’s password when prompted.
Command
Description
clear
Clears the terminal screen
reset
Resets your terminal to use default terminal settings
who
Displays currently logged-in users
w
Displays currently logged-in users and their tasks
whoami
Displays your login name
id
Displays the numbers associated with your user account name and group names; these are
commonly referred to as User IDs (UIDs) and Group IDs (GIDs)
date
Displays the current date and time
cal
Displays the calendar for the current month
exit
Exits out of your current shell
uname
Displays system information
Table 2-6 Some common Linux commands
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
63
If you do not specify the user name when using the su command, the root user is assumed.
Additionally, the root user can use the su command to switch to any other user account without
specifying a password:
[[email protected] root]# su – user1
Last login: Mon Aug 16 10:22:21 EDT 2015 from tty2
[[email protected] ~]$_
Shell Metacharacters
Another important part of the shell are shell metacharacters, which are keyboard characters
that have special meaning. One of the most commonly used metacharacters is the $ character, which tells the shell that the following text refers to a variable. A variable is simply a
piece of information that is stored in memory; variable names are typically uppercase words,
and most variables are set by the Linux system automatically when you log in. An example
of how you might use the $ metacharacter to refer to a variable is by using the echo command (which prints text to the terminal screen):
[[email protected] root]# echo Hi There!
Hi There!
[[email protected] root]# echo My Shell is $SHELL
My Shell is /bin/bash
[[email protected] root]#_
Notice from the preceding output that $SHELL was translated into its appropriate value
from memory (/bin/bash, the BASH shell). The shell recognized SHELL as a variable because
it was prefixed by the $ metacharacter. Table 2-7 presents a list of common BASH shell
metacharacters that are discussed throughout this book.
It is good practice to avoid metacharacters when typing commands unless you need to take
advantage of their special functionality, as the shell readily interprets them, which might
lead to unexpected results.
Metacharacter(s)
Description
$
Shell variable
~
Special home directory variable
&
Background command execution
;
Command termination
< << > >>
Input/Output redirection
|
Command piping
* ? [ ]
Shell wildcards
’ " \
Metacharacter quotes
‘
Command substitution
( ) { }
Command grouping
Table 2-7 Common BASH shell metacharacters
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
64
Chapter 2
Linux Installation and Usage
If you accidentally use one of these characters and your shell does
not return you to the normal prompt, simply press the Ctrl and
c keys in combination and your current command is canceled.
There are some circumstances in which you might need to use a metacharacter in a command and prevent the shell from interpreting its special meaning. To do this, simply
enclose the metacharacters in single quotation marks ´ ´. Single quotation marks protect
those metacharacters from being interpreted specially by the shell (that is, a $ is interpreted as a $ character and not a variable identifier). You can also use double quotation
marks " " to perform the same task; however, double quotation marks do not protect $,
\, and ` characters. If only one character needs to be protected from shell interpretation,
you can precede that character by a \ rather than enclosing it within quotation marks. An
example of this type of quoting follows:
[[email protected] root]# echo
My Shell is /bin/bash
[[email protected] root]# echo
My Shell is $SHELL
[[email protected] root]# echo
My Shell is /bin/bash
[[email protected] root]# echo
My Shell is $SHELL
[[email protected] root]#_
My Shell is $SHELL
‘My Shell is $SHELL’
"My Shell is $SHELL"
My Shell is \$SHELL
As shown in Table 2-7, not all quote characters protect characters from the shell. The back
quote characters ` ` can be used to perform command substitution; anything between back
quotes is treated as another command by the shell, and its output is substituted in place of
the back quotes. Take the expression `date` as an example:
[[email protected] root]# echo Today is `date`
Today is Tue Mar 29 09:28:11 EST 2015
[[email protected] root]#_
Getting Command Help
Most distributions of Linux contain more than 1000 different Linux commands in their standard configurations, and thus it is impractical to memorize the syntax and use of each command. Fortunately, Linux stores documentation for each command in central locations so
that it can be accessed easily. The most common form of documentation for Linux commands
is manual pages (commonly referred to as man pages). Simply type the man command followed by a command name, and extensive information about that Linux command is displayed page-by-page on the terminal screen. This information includes a description of the
command and its syntax, as well as available options, related files, and related commands.
For example, to receive information on the format and usage of the whoami command, you
can use the following command:
[[email protected] root]# man whoami
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
65
The manual page is then displayed page by page on the terminal screen. You can use the cursor (arrow) keys on the keyboard to scroll though the information or press q to quit. The
manual page for whoami is similar to the following:
WHOAMI(1)
User Commands
WHOAMI(1)
NAME
whoami - print effective userid
SYNOPSIS
whoami [OPTION]…
DESCRIPTION
Print the user name associated with the current effective
user id. Same as id -un.
- -help
display this help and exit
- -version
output version information and exit
AUTHOR
Written by Richard Mlynarik.
REPORTING BUGS
Report bugs to <[email protected]>.
GNU coreutils home page:<http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report whoami translation bugs to <http://translationproject.org/>
COPYRIGHT
Copyright©2013 Free Software Foundation, Inc. License GPLv3þ: GNU
GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
The full documentation is maintained as a Texinfo manual.
If the info and whoami programs are installed at your site,
the command
info coreutils ‘whoami invocation’
should give you access to the complete manual.
GNU coreutils 8.21
[[email protected] root]#_
August 2013
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
66
Chapter 2
Linux Installation and Usage
Manual Page
Section
Description
1
Commands that any user can execute
2
Linux system calls
3
Library routines
4
Special device files
5
File formats
6
Games
7
Miscellaneous
8
Commands that only the root user can execute
9
Linux kernel routines
n
New commands not categorized yet
Table 2-8 Manual page section numbers
Notice that the whoami command is displayed as WHOAMI(1) at the top of the preceding
manual page output. The (1) denotes a section of the manual pages; section (1) means that
whoami is a command that can be executed by any user. All manual pages contain certain
section numbers that describe the category of the command in the manual page database;
you can find a list of the different manual page section numbers in Table 2-8.
Sometimes, there is more than one command, library routine, or file that has the same name.
If you run the man command with that name as an argument, Linux returns the manual
page with the lowest section number. For example, if there is a file called whoami as well
as a command named whoami and you type man whoami, the manual page for the whoami
command (section 1 of the manual pages) is displayed. To display the manual page for the
whoami file format instead, you simply type man 5 whoami to display the whoami file format (section 5 of the manual pages).
Recall that many commands are available to the Linux user; thus, it might be cumbersome to
find the command that you need to perform a certain task without using a Linux command dictionary. Fortunately, you have the ability to search the manual pages by keyword. To find all of
the commands that have the word “usb” in their names or descriptions, type the following:
[[email protected] root] # man –k usb
This command produces the following output:
fxload
lsusb
sane-canon630u
sane-cardscan
sane-epjitsu
(8) - Firmware download to EZ-USB devices
(8) - list USB devices
(5) - SANE backend for the Canon 630u USB flatbed
scanner
(5) - SANE backend for Corex CardScan usb
scanners
(5) - SANE backend for Epson-based Fujitsu USB
scanners.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
67
sane-find-scanner
(1) - find SCSI and USB scanners and their device
files
sane-genesys
(5) - SANE backend for GL646, GL841, GL843,
GL847 and GL124 based USB flatbed scanners
sane-gt68xx
(5) - SANE backend for GT-68XX based USB flatbed
scanners
sane-kvs1025
(5) - SANE backend for Panasonic KV-S102xC USB
ADF scanners.
sane-kvs20xx
(5) - SANE backend for Panasonic KV-S20xxC USB/
SCSI ADF scanners.
sane-kvs40xx
(5) - SANE backend for Panasonic KV-S40xxC USB/
SCSI ADF scanners.
sane-ma1509
(5) - SANE backend for Mustek BearPaw 1200F USB
scanner
sane-mustek_usb
(5) - SANE backend for Mustek USB flatbed
scanners
sane-mustek_usb2
(5) - SANE backend for SQ113 based USB flatbed
scanners
sane-plustek
(5) - SANE backend for LM983[1/2/3] based USB
flatbed scanners
sane-sm3600
(5) - SANE backend for Microtek scanners with
M011 USB chip
sane-sm3840
(5) - SANE backend for Microtek scanners with
SCAN08 USB chip
sane-u12
(5) - SANE backend for Plustek USB flatbed
scanners, based on older parport designs
sane-usb
(5) - USB configuration tips for SANE
usb-devices
(1) - print USB device details
usb_modeswitch
(1) - switch mode of "multi-state" USB devices
usb_modeswitch_dispatcher (1) - dispatcher not intended for direct
invocation.
usbhid-dump
(8) - dump USB HID device report descriptors and
streams
[[email protected] root]#_
After you find the command needed, you can simply run the man command on that command without the –k option to find out detailed information about the command.
You can also use the apropos usb command to perform the
same function as the man –k usb command. Both commands
yield the exact same output on the terminal screen.
If you do not see any output from the man –k or apropos command following a Linux installation, you might need to run the
mandb command to index the manual page database.
Another utility, originally intended to replace the man command in Linux, is the GNU info
pages. You can access this utility by typing the info command followed by the name of the
command in question. The info command returns an easy-to-read description of each command and also contains links to other information pages (called hyperlinks). Today,
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
68
Chapter 2
Linux Installation and Usage
however, both the info pages and the manual pages are used to find documentation because
manual pages have been utilized in Linux since its conception, and for over two decades in
the UNIX operating system. An example of using the info utility to find information about
the whoami command follows:
[[email protected] root]# info whoami
The info page is then displayed interactively:
File: coreutils.info, Node: who invocation, Prev: users invocation,
Up: User information
20.6 ‘who’: Print who is currently logged in
============================================
‘who’ prints information about users who are currently logged on.
Synopsis:
who [OPTION] [FILE] [am i]
If given no non-option arguments, ‘who’ prints the following
information for each user currently logged on: login name, terminal line,
login time, and remote hostname or X display.
If given one non-option argument, ‘who’ uses that instead of a default
system-maintained file (often ‘/var/run/utmp’ or ‘/var/run/utmp’) as the
name of the file containing the record of users logged on. ‘/var/log/wtmp’
is commonly given as an argument to ‘who’ to look at who has previously
logged on.
If given two non-option arguments, ‘who’ prints only the entry for * bash:
(bash). The Bourne Again Shell. the hostname. Traditionally, the two
arguments given are ‘am i’, as in ‘who am i’.
Time stamps are listed according to the time zone rules specified by the
‘TZ’ environment variable, or by the system default rules if ‘TZ’ is not
set. *Note Specifying the Time Zone with ‘TZ’: (libc)TZ Variable.
The program accepts the following options. Also see *note Common
options::.
‘-a’
‘--all’
Same as ‘-b -d --login -p -r -t -T -u’.
‘-b’
‘--boot’
Print the date and time of last system boot.
--zz-Info: (coreutils.info.gz)who invocation, 103 lines --Top-----[[email protected] root]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Basic Linux Usage
69
While in the info utility, press the ? key or Ctrlþh key combination to
display a help screen that describes the usage of info. As with the
man command, you can use the q key to quit.
2
Some commands do not have manual pages or info pages. These commands are usually functions that are built into the BASH shell itself. To find help on these commands, you must use
the help command, as follows:
[[email protected] root]# help echo
echo: echo [-neE] [arg …]
Write arguments to the standard output.
Display the ARGs on the standard output followed by a newline.
Options:
-n
do not append a newline
-e
enable interpretation of the following backslash escapes
-E
explicitly suppress interpretation of backslash escapes
‘echo’ interprets the following backslash-escaped characters:
\a
alert (bell)
\b
backspace
\c
suppress further output
\e
escape character
\f
form feed
\n
new line
\r
carriage return
\t
horizontal tab
\v
vertical tab
\\
backslash
\0nnn the character whose ASCII code is NNN (octal). NNN can be
0 to 3 octal digits
\xHH the eight-bit character whose value is HH (hexadecimal). HH
can be one or two hex digits
Exit Status:
Returns success unless a write error occurs.
[[email protected] root]#_
Shutting Down the Linux System
Because the operating system handles writing data from computer memory to the disk drives
in a computer, simply turning off the power to the computer might result in damaged user
and system files. Thus, it is important to prepare the operating system for shutdown before
turning off the power to the hardware components of the computer. To do this, you can
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
70
Chapter 2
Linux Installation and Usage
Command
Description
shutdown −H +4
Halts your system in four minutes
shutdown −r +4
Reboots your system in four minutes
shutdown −H now
Halts your system immediately
shutdown −r now
Reboots your system immediately
shutdown −c
Cancels a scheduled shutdown
halt
Halts your system immediately
poweroff
Halts your system immediately and powers down the computer
reboot
Reboots your system immediately
Table 2-9 Commands to halt and reboot the Linux operating system
issue the shutdown command, which can halt or reboot (restart) your computer after a certain period of time. To halt your system in 15 minutes, for example, you could type:
[[email protected] root] # shutdown –H +15
This produces output similar to the following:
Broadcast message from [email protected] (Wed 2014-09-17 15:36:48 EDT):
The system is going down for power-off at Wed 2014-09-17 15:51:48 EDT!
Shutdown scheduled for Wed 2014-09-17 15:51:48 EDT, use ‘shutdown -c’ to
cancel.
Notice from the preceding output that you do not receive the command prompt back again
after the shutdown command has started. Thus, to stop the shutdown, simply press the
Ctrl and c keys in combination to return to a command prompt and then issue the command
shutdown –c to cancel the shutdown.
To halt your system now, you could type:
[[email protected] root] # poweroff
Other examples of the shutdown command and their descriptions are shown in Table 2-9.
Chapter Summary
■ Prior to installation, you should verify hardware requirements using the HCL.
■ You can obtain Linux installation media by downloading an ISO image from the
Internet that can be written to a DVD or USB flash drive or can be used directly by
virtualization software.
■ A typical Linux installation prompts the user for information, such as language, date,
time zone, keyboard layout, network configuration, user account configuration, and
permanent storage configuration.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
71
■ Users must log in to a terminal and receive a shell before they are able to interact with
the Linux system and kernel. A single user can log in several different times
simultaneously to several different terminals locally or across a network.
■ Regardless of the type of terminal that you use (graphical or command-line), you are
able to enter commands, options, and arguments at a shell prompt to perform system
tasks, obtain command help, or shut down the Linux system. The shell is case
sensitive and understands a variety of special characters called shell metacharacters,
which should be protected if their special meaning is not required.
Key Terms
Advanced Technology Attachment (ATA)
See Parallel Advanced Technology Attachment.
arguments The text that appears after a command name, does not start with a dash
(the - character), and specifies information that the command requires to work properly.
authentication The process whereby each user must log in with a valid user name and
password before gaining access to the user interface of a system.
B-tree Filesystem (BTRFS) An experimental Linux filesystem that contains advanced
features such as storage pools and filesystem snapshots.
BASH shell Also known as the Bourne Again Shell, this is the default command-line
interface in Linux.
BIOS (Basic Input/Output System)
The part of a computer system that contains the
programs used to initialize hardware components at boot time.
command A program that exists on the filesystem and is executed when typed on the
command line.
ext2
A nonjournaling Linux filesystem.
ext3
A journaling Linux filesystem.
ext4
An improved version of the ext3 filesystem, with an extended feature set and better
performance.
extended partition A partition on a hard disk or SSD that can be further subdivided into
components called logical drives.
filesystem
The way in which a hard disk or SSD partition is formatted to allow data to
reside on the physical media; common Linux filesystems include ext2, ext3, ext4, REISERFS,
and VFAT.
GUID Partition Table (GPT)
The area of a large hard disk (> 2TB) outside a partition that
stores partition information and boot loaders.
Hardware Compatibility List (HCL)
A list of hardware components that have been tested
and deemed compatible with a given operating system.
info pages
A set of local, easy-to-read command syntax documentation available by
typing the info command-line utility.
Integrated Drive Electronics (IDE)
See Parallel Advanced Technology Attachment.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
72
Chapter 2
Linux Installation and Usage
ISO image
A file that contains the content of a DVD. ISO images of Linux installation
media can be downloaded from the Internet.
journaling
A filesystem function that keeps track of the information that needs to be
written to the hard disk or SSD in a journal; common Linux journaling filesystems include
ext3, ext4, and REISER.
live media
Linux installation media that provides a fully functional Linux operating
system in RAM prior to installation on permanent storage.
logical drives
The smaller partitions contained within an extended partition on a hard
disk.
Logical Volume Manager (LVM)
A set of services that are used to manage logical volumes
stored on one or more hard disks.
manual pages
The most common set of local command syntax documentation, available
by typing the man command-line utility. Also known as man pages.
man pages
See manual pages.
Master Boot Record (MBR)
The area of a typical hard disk (< 2TB) outside a partition that
stores partition information and boot loaders.
memtest86
A common RAM-checking utility.
metacharacters
The key combinations that have special meaning in the Linux operating
system.
options
The specific letters that start with a dash (the - character) or two and appear after
the command name to alter the way the command works.
Parallel Advanced Technology Attachment (PATA) A legacy hard disk technology that uses
ribbon cables to typically attach up to four hard disk devices to a single computer.
partitions
A section of a hard disk or SSD. Partitions can be primary or extended.
primary partitions The separate divisions into which a hard disk or SSD can be divided
(up to four are allowed per hard disk).
Redundant Array of Inexpensive Disks (RAID)
A type of storage that can be used to
combine hard disks together for performance and/or fault tolerance.
REISER
A journaling filesystem used in Linux.
Serial Advanced Technology Attachment (SATA) A technology that allows for fast data
transfer along a serial cable for hard disks and SSDs. It is commonly used in newer
workstation and server-class computers.
shell A user interface that accepts input from the user and passes the input to the kernel
for processing.
Small Computer Systems Interface (SCSI)
A high-performance hard disk technology that is
commonly used in server-class computers.
solid-state drive (SSD)
A type of disk drive that functions within a computer like a hard
disk drive but instead uses fast flash memory chips to store data.
swap memory
See virtual memory.
terminal The channel that allows a certain user to log in and communicate with the kernel
via a user interface.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
VFAT (Virtual File Allocation Table)
73
A non-journaling filesystem that might be used in
Linux.
virtual machine
An operating system that is running within virtualization software.
virtual machine host
An operating system that runs virtualization software.
virtual memory
An area on a hard disk or SSD (swap partition) that can be used to store
information that normally resides in physical memory (RAM), if the physical memory is
being used excessively.
virtualization software
A set of programs that can be used to run an operating system
within an existing operating system concurrently.
Review Questions
1. What is the default shell in Linux called?
a.
SH
b. BSH
c.
CSH
d. BASH
2. What equivalent to the man command generally provides an easier-to-read description
of the queried command and also contains links to other related information?
a.
who
b. man help
c.
man -descriptive
d. info
3. What command can you use to safely shut down the Linux system immediately?
a.
shutdown -c
b. shutdown -r
c.
down
d. halt
4. What command is equivalent to the man –k keyword command?
a.
find keyword
b. man keyword
c.
apropos keyword
d. appaloosa keyword
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
74
Chapter 2
Linux Installation and Usage
5. Which of the following is not a piece of information that the Fedora installation program prompts you for?
a.
time zone
b. installation destination
c.
firewall settings
d. keyboard layout
6. Linux commands entered via the command line are not case sensitive. True or False?
7. Which command blanks the terminal screen, erasing previously displayed output?
a.
erase
b. clean
c.
blank
d. clear
8. When sitting at a computer running Linux, what key combination is pressed to open the
graphical terminal?
a.
CtrlþAltþG
b. CtrlþAltþF4
c.
CtrlþAltþF1
d. Ctrlþ7
9. To install Linux within a virtual machine, you can specify the path to an ISO image that
contains the Linux installation media within virtualization software without having to
first write the ISO image to a DVD or USB flash drive. True or False?
10. After you log in to a terminal, you receive a user interface called a
a.
.
GUID
b. shell
c.
text box
d. command screen
11. Users enter commands directly to the kernel of the Linux operating system. True or
False?
12. How can you protect a metacharacter (such as the $ character) from shell
interpretation?
a.
Precede it with a /.
b. Follow it with a \.
c.
Precede it with a $.
d. It cannot be done because metacharacters are essential.
e.
Precede it with a \.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
75
13. You know a Linux command will perform a desired function for you, but you cannot
remember the full name of the command. You do remember it will flush a variable from
your system. Which command typed at a command prompt displays a list of commands
that would likely contain the command you desire?
a.
man –k flush
b. man –k find all
c.
man flush
d. man –key flush
14. Which command displays the users who are currently logged in to the Linux system?
a.
finger
b. who
c.
id
d. date
15. Which prompt does the root user receive when logged in to the system?
a.
$
b. @
c.
#
d. !
16. Which prompt do regular users receive when logged in to the system?
a.
$
b. @
c.
#
d. !
17. Which of the following refers to the third primary partition on the second SCSI hard
disk within Linux?
a.
hdb2
b. sda3
c.
hdb3
d. sdb3
18. Which two partitions do you typically create at minimum during a Fedora Linux installation? (Choose two answers.)
a.
/
b. /boot
c.
swap
d. /home
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
76
Chapter 2
Linux Installation and Usage
19. If you boot your computer from Linux live media, you will be able to use a fully functional Linux system prior to installing Linux on permanent storage. True or False?
20. Which of the following is not an example of virtualization software that can be used to
install Linux within another operating system?
a.
Oracle VirtualBox
b. Microsoft Hyper-V
c.
Spiceworks
d. VMware
Hands-on Projects
These projects should be completed in the order given and should take a total
of three hours to complete. The software and hardware requirements for these
projects include the following:
• A 64-bit computer with at least 4GB of RAM, 80GB of permanent disk
storage, and a DVD drive
• A Windows operating system that contains a virtualization software product, a Web
browser, and an Internet connection
• The ISO image for Fedora 20 live media (Fedora-Live-Desktop-x86_64-20-1.iso)
Project 2-1
In this hands-on project, you install Fedora 20 Linux within a virtual machine on a Windows
computer.
1. In your virtualization software, create a new virtual machine called Fedora Linux that
has the following characteristics:
a.
2GB of memory
b. an Internet connection via your PC’s network card (preferably using bridged mode)
c.
a 20GB virtual hard disk
d. the virtual machine DVD drive attached to the ISO file for Fedora 20 live media
(Fedora-Live-Desktop-x86_64-20-1.iso)
2. Start and then connect to your Fedora Linux virtual machine using your virtualization
software.
3. At the Fedora Live welcome screen, press Enter to boot Fedora Live.
4. Once the graphical desktop and Welcome to Fedora screen has loaded, select the option
Install to Hard Drive to start the Fedora installation program.
5. At the Welcome to Fedora 20 screen, select English (United States) if necessary, and
press Continue.
6. On the Installation Summary page, click Date & Time, ensure that your time zone is
selected, and press Done.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
77
7. On the Installation Summary page, click Installation Destination. You should see that
your 20GB virtual disk is already selected and called sda. Press Done when finished.
8. When the Installation Options screen appears, select I want to review/modify my disk
partitions before continuing, choose a partition scheme of Standard Partition, and press
Continue.
9. At the Manual Partitioning screen, select the link Click here to create them automatically. This will create several partitions.
a.
Highlight the / (sda3) partition, reduce the Desired Capacity to 12 GB, and click
Update Settings. This will leave some free unpartitioned space on our first disk for a
later exercise.
b. Press Done and then click Accept Changes when prompted.
10. At the Installation Summary page, click Begin Installation.
11. During the installation process, click Root Password, supply a password of LNXrocks!,
and press Done. Next, click User Creation and create a user with a name and description of user1 and password of LNXrocks!. Ensure that you select the option to make
this user an administrator user, then press Done.
12. When the installation has finished, click Quit. This will return you to your Fedora live
desktop.
13. Click the power icon in the upper-right corner, select the power icon that appears, and
click Power Off to shut down your Fedora Live installation image.
14. In the Settings for your virtual machine in your virtualization software, ensure that the
DVD drive is no longer attached to the Fedora ISO image.
15. Finally, start your Fedora Linux virtual machine using your virtualization software to
boot into your new Fedora Linux OS.
Project 2-2
In this hands-on project, you explore some command-line terminals on a Linux system and
enter some basic commands into the BASH shell.
1. After your Linux system has been loaded, you are placed at a graphical terminal (tty1).
Instead of logging in to this graphical terminal, press CtrlþAltþF2 to switch to a
command-line terminal (tty2) and then log in to the terminal using the user name of
root and the password of LNXrocks!. Which prompt did you receive and why?
2. At the command prompt, type date and press Enter to view the current date and time.
Now, type Date and press Enter. Why did you receive an error message? Can you tell
which shell gave you the error message?
3. Switch to a different command-line terminal (tty5) by pressing CtrlþAltþF5 and log in
to the terminal using the user name of user1 and the password of LNXrocks!. Which
prompt did you receive and why?
4. At the command prompt, type who and press Enter to view the users logged in to the
system. Who is logged in and on which terminal?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
78
Chapter 2
Linux Installation and Usage
5. Switch back to the terminal tty2 by pressing CtrlþAltþF2. Did you need to log in? Are
the outputs from the date and Date commands still visible?
6. Try typing in each command listed in Table 2-6 in order (pressing Enter after each) and
observe the output. What did the last command (exit) do?
7. Switch to the terminal tty5 by pressing CtrlþAltþF5 and type exit to log out of your
shell.
Project 2-3
In this hands-on project, you log in to a graphical terminal in Fedora Linux and interact with
the GNOME and KDE desktops.
1. Switch to the graphical terminal (tty1) by pressing CtrlþAltþF1, click user1, supply the
password of LNXrocks!, and click Sign In. Which desktop is started and why?
2. The first time you log into the desktop, you will be prompted to select desktop
preferences.
a.
At the Welcome screen, ensure that English (United States) is selected and click
Next.
b. At the Input Sources screen, ensure that the English (US) keyboard layout is selected
and click Next.
c.
At the Online Accounts screen, click Next to bypass personal account configuration.
d. On the Thank You screen, click Start using Fedora.
3. Observe the GNOME desktop. Use your mouse to select the Activities menu in the
upper-left corner of your screen, select the Show Applications icon (at the bottom of the
application panel), and then navigate to Utilities, Terminal to open a BASH shell
prompt. What prompt do you receive and why?
4. At the command prompt, type who and press Enter to view the users logged in to the
system. Note that tty1 is listed as running on port 0 (:0) and that your Terminal application is running as a pseudo-terminal session (pts/0) within your GUI environment.
5. At the command prompt, type su - and press Enter to switch to the root user. Supply
the root user password (LNXrocks!) when prompted.
6. By default, the KDE desktop is not installed in Fedora 20. To download and install
the KDE desktop from the Internet, type yum groupinstall “KDE Plasma
Workspaces” and press Enter. Press y when prompted to download and install the software packages for the KDE desktop. This may take several minutes, depending on your
Internet speed. The yum command will be discussed in more detail in Chapter 11.
7. By default, the log out option is not enabled in Fedora 20 (it is useful if you are going to
switch desktops). Type gsettings set org.gnome.shell always-show-log-out
true to enable the log out option, and press Enter when finished.
8. Type reboot to restart Fedora Linux. After the system has rebooted, log into the
GNOME desktop as user1 with the password LNXrocks!.
9. Click the power icon in the upper-right of the GNOME desktop and note that there is
now a section that lists the currently logged-in user (user1). Click user1, Log Out, and
then click Log Out again to log out of the GNOME desktop.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
79
10. At the Fedora login screen, click user1 and then click the cog wheel icon next to the
Sign In button. Select KDE Plasma Workspace, supply the password for user1
(LNXrocks!), and click Sign In.
11. Click the Fedora start button in the lower left of the desktop and navigate to Applications, System, Terminal to start a command-line shell. How does application navigation
in the KDE desktop differ from the GNOME desktop?
12. At the command prompt, type echo $SHELL and press Enter to view your current
shell. Is this shell the same shell used for command-line terminals?
13. Click the Fedora start button, navigate to Leave, Log out, and click Logout to exit the
KDE desktop.
Project 2-4
In this hands-on project, you use and protect shell metacharacters.
1. Switch to a command-line terminal (tty2) by pressing CtrlþAltþF2 and log in to the
terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type date;who and press Enter to run the date command
immediately followed by the who command. Use the information in Table 2-7 to
describe the purpose of the ; metacharacter.
3. At the command prompt, type echo This is OK and press Enter to display a message
on the terminal screen.
4. At the command prompt, type echo Don’t do this and press Enter. Which character
needs to be protected in the previous command? Press the Ctrl and c keys together to
cancel your command and return to a BASH shell prompt.
5. At the command prompt, type echo “Don’t do this” and press Enter. What is displayed on the terminal screen?
6. At the command prompt, type echo Don\’t do this and press Enter. What is displayed on the terminal screen?
7. At the command prompt, type echo $SHELL and press Enter to view the expansion of
a variable using a shell metacharacter. What is displayed on the terminal screen? Next,
type echo $TEST and press Enter to find out what happens when a variable that does
not exist is used in a command. What is displayed?
8. At the command prompt, type echo You have $4.50 and press Enter. What is displayed? Why? Which character needs to be protected in the previous command? What
are two different ways that you can protect this character from interpretation by the
shell?
9. At the command prompt, type echo ‘You have $4.50’ and press Enter. What is displayed on the terminal screen? Did the single quotation marks protect this metacharacter from shell interpretation?
10. At the command prompt, type echo “You have $4.50” and press Enter. What is
displayed on the terminal screen? Did the double quotation marks protect this metacharacter from shell interpretation?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
80
Chapter 2
Linux Installation and Usage
11. At the command prompt, type echo You have \$4.50 and press Enter. What is displayed on the terminal screen? Did the backslash protect this metacharacter from shell
interpretation?
12. At the command prompt, type echo My name is ‘whoami‘ and press Enter. What
function do back quotes perform?
13. Type exit and press Enter to log out of your shell.
Project 2-5
In this hands-on project, you find information about commands using help utilities.
1. Press CtrlþAltþF2 to switch to a command-line terminal (tty2), and then log in to the
terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type man –k cron and press Enter to view a list of manual
pages that have the word “cron” in the name or description. Use Table 2-8 to determine
what type of manual pages are displayed. How many manual pages are there for
crontab? Are they different types of manual pages?
If you do not see any output from the man –k command, run the
mandb command to generate the manual pages index.
3. At the command prompt, type man crontab and press Enter to view the manual page
for the crontab command. Observe the syntax of the crontab command and press q
when finished to quit the manual page and return to your command prompt.
4. At the command prompt, type man 5 crontab and press Enter to view the manual
page for the crontab file format. Observe the syntax of the crontab file format and press
q when finished to quit the manual page and return to your command prompt.
5. At the command prompt, type info and press Enter to view a list of available GNU
info pages. When finished, press q to quit the info utility.
6. At the command prompt, type info date and press Enter to view syntax information
regarding the date command, and press q to quit the info utility when finished.
7. At the command prompt, type help to view a list of BASH shell functions that have
documentation. If the list is too long for your terminal, press the Shift and Page Up keys
simultaneously to shift one page up to view the top of the list. Then press the Shift and
Page Down keys simultaneously to shift one page down to view your command prompt
again.
8. At the command prompt, type help exit to view information on the exit command,
a function of your BASH shell.
9. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
81
Project 2-6
In this hands-on project, you properly shut down your Linux system.
1. Press CtrlþAltþF2 to switch to a command-line terminal (tty2), and then log in to the
terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type poweroff to shut down your Linux system immediately. Which commands from Table 2-9 can also be used to shut down your Linux
system?
Discovery Exercises
1. You are the network administrator for Slimjim, a peripheral device company. The
network uses Linux, and you need information on some commands to perform your job.
Open the manual pages and find all the commands that have the word “copy” in their
name or description. What command did you use to accomplish this task? Are there any
commands in this list that only a root user can execute? How are they indicated? Select
any two of them and compare their info and manual pages. Access and read the manual
pages on three other commands that interest you either by using the command name or
searching for them by related keyword (try using apropos).
2. Identify the errors with the following commands and indicate possible solutions. (Hint:
Try typing them at a shell prompt to view the error message.)
Echo “This command does not work properly”
date –z
apropos man –k
help date
shutdown –c now
echo “I would like lots of $$$”
man 8 date
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
3
Exploring Linux Filesystems
After completing this chapter, you will be able to:
• Understand and navigate the Linux directory structure using relative and
absolute pathnames
• Describe the various types of Linux files
• View filenames and file types
• Use shell wildcards to specify multiple filenames
• Display the contents of text files and binary files
• Search text files for regular expressions using grep
• Use the vi editor to manipulate text files
• Identify common alternatives to the vi text editor used today
83
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
84
Chapter 3
Exploring Linux Filesystems
An understanding of the structure and commands surrounding the Linux filesystem is
essential for effectively using Linux to manipulate data. In the first part of this chapter, you
explore the Linux filesystem hierarchy by changing your position in the filesystem tree and
listing filenames of various types. Next, you examine the shell wildcard metacharacters used
to specify multiple filenames as well as view the contents of files using standard Linux commands. You then learn about the regular expression metacharacters used when searching for
text within files and are introduced to the vi text editor and its equivalents.
The Linux Directory Structure
Fundamental to using the Linux operating system is an understanding of how Linux stores files
on the filesystem. Typical Linux systems could have thousands of data and program files; thus,
a structure that organizes those files is necessary to make it easier to find and manipulate data
and run programs. Recall from the previous chapter that Linux uses a logical directory tree to
organize files into different directories (also known as folders). When a user stores files in a certain directory, the files are physically stored in the filesystem of a certain partition on a hard
disk or SSD inside the computer. Most people are familiar with the Windows operating system
directory tree structure as depicted in Figure 3-1; each filesystem on a hard drive partition
is referred to by a drive letter (such as C: or D:) and has a root directory (indicated by the
\ character) containing subdirectories that together form a hierarchical tree.
It is important to describe directories in the directory tree properly; the absolute pathname to
a file or directory is the full pathname of a certain file or directory starting from the root
directory. In Figure 3-1, the absolute pathname for the color directory is C:\windows\color
and the absolute pathname for the sue directory is D:\home\sue. In other words, we refer
to C:\windows\color as the color directory below the windows directory below the root of
C drive. Similarly, we refer to D:\home\sue as the sue directory below the home directory
below the root of D drive.
Linux uses a similar directory structure, but with no drive letters. The structure contains a single
root (referred to using the / character), with different filesystems on hard drive partitions
mounted (or attached) to different directories on this directory tree. The directories that each filesystem is mounted to are transparent to the user. An example of a sample Linux directory tree
equivalent to the Windows sample directory tree shown in Figure 3-1 is depicted in Figure 3-2.
Note that the subdirectory named “root” in Figure 3-2 is different from the root (/) directory.
You’ll learn more about the root subdirectory in the next section.
data
system
windows
D:\
etc
mary
home
bob
color
C:\
root
sue
Figure 3-1 The Windows filesystem structure
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The Linux Directory Structure
85
/
home
windows
etc
root
3
data
system
color
mary
sue
bob
Figure 3-2 The Linux filesystem structure
In Figure 3-2, the absolute pathname for the color directory is /windows/color and the absolute pathname for the sue directory is /home/sue. In other words, we refer to the /windows/
color directory as the color directory below the windows directory below the root of the system (the / character). Similarly, we refer to the /home/sue directory as the sue directory below
the home directory below the root of the system.
Changing Directories
When you log into a Linux system, you are placed in your home directory, which is a place
unique to your user account for storing personal files. Regular users usually have a home
directory named after their user account under the /home directory, as in /home/sue. The
root user, however, has a home directory called root under the root directory of the system
(/root), as shown in Figure 3-2. Regardless of your user name, you can always refer to your
own home directory using the ~ metacharacter.
To confirm the system directory that you are currently in, simply observe the name at the end of
the shell prompt or run the pwd (print working directory) command at a command-line prompt.
If you are logged in as the root user, the following output is displayed on the terminal screen:
[[email protected] ~]# pwd
/root
[[email protected] ~]#_
However, if you are logged in as the user sue, you see the following output:
[[email protected] ~]$ pwd
/home/sue
[[email protected] ~]$_
To change directories, you can issue the cd (change directory) command with an argument
specifying the destination directory. If you do not specify a destination directory, the cd command returns you to your home directory:
[[email protected] ~]# cd /home/mary
[[email protected] mary]# pwd
/home/mary
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
86
Chapter 3
Exploring Linux Filesystems
[[email protected]
[[email protected]
/etc
[[email protected]
[[email protected]
/root
[[email protected]
mary]# cd /etc
etc]# pwd
etc]# cd
~]# pwd
~]#_
You can also use the ~ metacharacter to refer to another user’s home directory by appending
a user name at the end:
[[email protected]
[[email protected]
/home/mary
[[email protected]
[[email protected]
/root
[[email protected]
~]# cd ~mary
mary]# pwd
mary]# cd ~
~]# pwd
~]#_
In many of the examples discussed earlier, the argument specified after the cd command is an
absolute pathname to a directory, meaning that the system has all the information it needs to
find the destination directory because the pathname starts from the root (/) of the system.
However, in most Linux commands, you can also use a relative pathname in place of an
absolute pathname to reduce typing. A relative pathname is the pathname of a target file or
directory relative to your current directory in the tree. To specify a directory underneath your
current directory, simply refer to that directory by name (do not start the pathname with a /
character). To refer to a directory one step closer to the root of the tree (also known as a
parent directory), simply use two dots (..). An example of using relative pathnames to move
around the directory tree is shown next:
[[email protected] ~]# cd /home/mary
[[email protected] mary]# pwd
/home/mary
[[email protected] mary]# cd ..
[[email protected] home]# pwd
/home
[[email protected] home]# cd mary
[[email protected] mary]# pwd
/home/mary
[[email protected] mary]#_
In the preceding example, we used “..” to move up one parent directory and then used the
word “mary” to specify the mary subdirectory relative to our current location in the tree;
however, you can also move more than one level up or down the directory tree:
[[email protected]
[[email protected]
/home/mary
[[email protected]
[[email protected]
/
~]# cd /home/mary
mary]# pwd
mary]# cd ../..
/]# pwd
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Viewing Files and Directories
87
[[email protected] /]# cd home/mary
[[email protected] mary]# pwd
/home/mary
[[email protected] mary]#_
You can also use one dot (.) to refer to the current directory. Although
this is not useful when using the cd command, you do use one dot
later in this book.
Although absolute pathnames are straightforward to use as arguments to commands when
specifying the location of a certain file or directory, relative pathnames can save you a great
deal of typing and reduce the potential for error if your current directory is far away from the
root directory. Suppose, for example, that the current directory is /home/sue/projects/acme/
plans and you need to change to the /home/sue/projects/acme directory. Using an absolute
pathname, you would type cd /home/sue/projects/acme; however, using a relative
pathname, you only need to type cd .. to perform the same task because the /home/sue/
projects/acme directory is one parent directory above the current location in the directory tree.
An alternate method for saving time when typing pathnames as arguments to commands is to
use the Tab-completion feature of the BASH shell. To do this, type enough unique letters of
a directory and press the Tab key to allow the BASH shell to find the intended file or directory being specified and fill in the appropriate information. If there is more than one possible
match, the Tab-completion feature alerts you with a beep; pressing the Tab key again after
this beep presents you with a list of possible files or directories.
Observe the directory structure in Figure 3-2. To use the Tab-completion feature to change
the current directory to /home/sue, you simply type cd /h and then press the Tab key. This
changes the previous characters on the terminal screen to display cd /home/ (the BASH shell
was able to fill in the appropriate information because the /home directory is the only directory underneath the / directory that starts with the letter “h”). Then, you could add an s
character to the command, so that the command line displays cd /home/s, and press the
Tab key once again to allow the shell to fill in the remaining letters. This results in the command cd /home/sue/ being displayed on the terminal screen (the sue directory is the only
directory that begins with the s character underneath the /home directory). At this point,
you can press Enter to execute the command and change the current directory to /home/sue.
In addition to directories, the Tab-completion feature of the BASH
shell can be used to specify the pathname to files and executable
programs.
Viewing Files and Directories
The point of a directory structure is to organize files into an easy-to-use format. In order to
locate the file you need to execute, view, or edit, you need to be able to display a list of the
contents of a particular directory. You’ll learn how to do that shortly, but first you need to
learn about the various types of files and filenames, as well as the different commands used
to select filenames for viewing.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
88
Chapter 3
Exploring Linux Filesystems
File Types
Fundamental to viewing files and directories is a solid understanding of the various types of
files present on most Linux systems. Several different types of files can exist on a Linux system; the most common include the following:
• Text files
• Binary data files
• Executable program files
• Directory files
• Linked files
• Special device files
• Named pipes and sockets
Most files on a Linux system that contain configuration information are text files. Another
type of files are programs that exist on the hard drive before they are executed in memory
to become processes. A program is typically associated with several supporting binary data
files that store information such as common functions and graphics. In addition, directories
themselves are actually files; they are special files that serve as placeholders to organize other
files. When you create a directory, a file is placed on the hard drive to represent that
directory.
Linked files are files that have an association with one another; they can represent the
same data or they can point to another file (also known as a shortcut file). Special device
files are less common than the other file types that have been mentioned, yet they are
important for systems administrators because they represent different devices on the system, such as hard disks and serial ports. These device files are used in conjunction with
commands that manipulate devices on the system; special device files are typically found
only in the /dev directory and are discussed in later chapters of this book. As with special
device files, named pipe files are uncommon and used primarily by administrators.
Named pipes identify a channel that passes information from one process in memory to
another, and in some cases they can be mediated by files on the hard drive. Writes to
the file are processed while another process reads from it to achieve this passing of information. Another variant of a named pipe file is a socket file, which allows a process on
another computer to write to a file on the local computer while another process reads
from that file.
Filenames
Files are recognized by their filenames, which can include up to 255 characters yet are rarely
longer than 20 characters on most Linux systems. Filenames are typically composed of alphanumeric characters, the underscore ( _ ) character, the dash ( — ) character, and the period ( . )
character.
It is important to avoid using the shell metacharacters discussed in
the previous chapter when naming files. Using a filename that contains a shell metacharacter as an argument to a Linux command
might produce unexpected results.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Viewing Files and Directories
89
Filenames that start with a period ( . ) are referred to as hidden files.
You need to use a special command to display them in a file list. This
command is discussed later in this chapter.
Filenames used by the Windows operating system typically end with a period and three characters
that indentify the file type—for example, document.txt (a text file) and server.exe (an executable
program file). However, most files on the hard drive of a Linux system do not follow this pattern,
although some files on the Linux filesystem do contain characters at the end of the filename
that indicate the file type. These characters are commonly referred to as filename extensions.
Table 3-1 lists common examples of filename extensions and their associated file types.
Listing Files
Linux hosts a variety of commands that can be used to display files and their types in various
directories on hard drive partitions. By far, the most common method for displaying files is to
use the ls command. Following is an example of a file listing in the root user’s home directory:
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls
current myprogram project project12 project2 project4
Desktop myscript project1 project13 project3 project5
[[email protected] ~]#_
Metacharacter
Description
.c
C programming language source code files
.cc .cpp
C++ programming language source code files
.html .htm
HTML (Hypertext Markup Language) files
.ps
Files formatted for printing with postscript
.txt
Text files
.tar
Archived files (contain other files within)
.gz .bz2 .Z
Compressed files
.tar .gz .tgz .tar .bz2 .tar .Z
Compressed archived files
.conf .cfg
Configuration files (contain text)
.so
Shared object (programming library) files
.o
Compiled object files
.pl
PERL (Practical Extraction and Report Language) programs
.tcl
Tcl (Tool Command Language) programs
.jpg .jpeg .png .tiff .xpm .gif
Binary files that contain graphical images
.sh
Shell scripts (contain text that is executed by the shell)
Table 3-1 Common filename extensions
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
90
Chapter 3
Exploring Linux Filesystems
The files listed previously and discussed throughout this chapter are
for example purposes only. The Hands-on Projects use different files.
The ls command displays all the files in the current directory in columnar format; however,
you can also pass an argument to the ls command indicating the directory to be listed if the
current directory listing is not required. In the following example, the files are listed underneath the /home/bob directory without changing the current directory.
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls /home/bob
assignment1 file1 letter letter2 project1
[[email protected] ~]#_
When running the ls command, you will notice that files of different types are often represented as different colors; however, the specific colors used to represent files of certain types might vary from
terminal to terminal and distribution to distribution. As a result, do
not assume color alone indicates the file type.
Windows uses the dir command to list files and directories; to simplify the learning of Linux for Windows users, there is a dir command in Linux, which is simply a pointer or shortcut to the ls
command.
Recall from the previous chapter that you can use switches to alter the behavior of commands. To view a list of files and their type, use the –F switch to the ls command:
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls -F
[email protected] myprogram* project
project12 project2 project4
Desktop/ myscript* project1 project13 project3 project5
[[email protected] ~]#_
The ls –F command appends a special character at the end of each filename displayed to
indicate the type of file. In the preceding output, note that the filenames current, Desktop,
myprogram, and myscript have special characters appended to their names. The @ symbol
indicates a linked file, the * symbol indicates an executable file, the / indicates a subdirectory,
the ¼ character indicates a socket, and the | character indicates a named pipe. All other file
types do not have a special character appended to them and could be text files, binary data
files, or special device files.
It is common convention to name directories starting with an uppercase letter, such as the D in the Desktop directory shown in the preceding output. This allows you to quickly determine which names
refer to directories when running the ls command without any
options that specify file type.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Viewing Files and Directories
91
Although the ls –F command is a quick way of getting file type information in an easyto-read format, at times you need to obtain more detailed information about each file. The
ls –l command can be used to provide a long listing for each file in a certain directory.
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls -l
total 548
lrwxrwxrwx
1 root root
drwx----3 root root
-rwxr-xr-x
1 root root
-rwxr-xr-x
1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
-rw-r--r-1 root root
[[email protected] ~]#_
9 Apr 7
4096 Mar 29
519964 Apr 7
20 Apr 7
71 Apr 7
71 Apr 7
71 Apr 7
0 Apr 7
71 Apr 7
90 Apr 7
99 Apr 7
108 Apr 7
09:56
10:01
09:59
09:58
09:58
09:59
09:59
09:56
09:59
10:01
10:01
10:01
current -> project12
Desktop
myprogram
myscript
project
project1
project12
project13
project2
project3
project4
project5
Each file listed in the preceding example has eight components of information listed in columns from left to right:
1. A file type character
• The d character represents a directory.
• The l character represents a symbolically linked file (discussed in Chapter 4).
• The b or c characters represent special device files (discussed in Chapter 5).
• The n character represents a named pipe.
• The s character represents a socket.
• The – character represents all other file types (text files, binary data files).
2. A list of permissions on the file (also called the mode of the file)
3. A hard link count (discussed in Chapter 5)
4. The owner of the file (discussed in Chapter 5)
5. The group owner of the file (discussed in Chapter 5)
6. The file size
7. The most recent modification time of the file
8. The filename (Some files are shortcuts or pointers to other files and indicated with an
arrow ->, as with the file called “current” in the preceding output; these are known as
symbolic links and are discussed in Chapter 5.)
For the file named “project” in the previous example, you can see that this file is a regular
file because the long listing of it begins with a – character, the permissions on the file are
rw-r--r--, the hard link count is 1, the owner of the file is the root user, the group owner of
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
92
Chapter 3
Exploring Linux Filesystems
the file is the root group, the size of the file is 71 bytes, and the file was modified last on
April 7th at 9:58 a.m.
If SELinux is enabled on your system, you may also notice a period
( . ) immediately following the permissions on a file or directory that
is managed by SELinux. SELinux will be discussed later in Chapter 14.
On most Linux systems, a shortcut to the ls command can be used
to display the same columns of information as the ls –l command.
Some users prefer to use this shortcut, commonly known as an alias,
which is invoked when a user types ll at a command prompt. This
is known as the ll command.
The ls –F and ls –l commands are valuable to a user who wants to display file types;
however, neither of these commands can display all file types using special characters. To
display the file type of any file, you can use the file command; simply give the file command an argument specifying what file to analyze. You can also pass multiple files as arguments or use the * metacharacter to refer to all files in the current directory. An example of
using the file command in the root user’s home directory is:
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls
current myprogram project
project12 project2 project4
Desktop myscript
project1 project13 project3 project5
[[email protected] ~]# file Desktop
Desktop: directory
[[email protected] ~]# file project Desktop
project:
ASCII text
Desktop:
directory
[[email protected] ~]# file *
Desktop:
directory
current: symbolic link to project12
myprogram: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically
linked (uses shared libs), stripped
myscript: Bourne-Again shell script text executable
project:
ASCII text
project1: ASCII text
project12: ASCII text
project13: empty
project2: ASCII text
project3: ASCII text
project4: ASCII text
project5: ASCII text
[[email protected] ~]#_
As shown in the preceding example, the file command can also identify the differences between
types of executable files. The myscript file is a text file that contains executable commands (also
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Viewing Files and Directories
93
known as a shell script), whereas the myprogram file is a 32-bit executable compiled program.
The file command also identifies empty files such as project13 in the previous example.
Some filenames inside each user’s home directory represent important configuration files or
program directories. Because these files are rarely edited by the user and can clutter up the
listing of files, they are normally hidden from view when using the ls and file commands.
Recall that filenames for hidden files start with a period character ( . ). To view them, simply
pass the –a option to the ls command. Some hidden files that are commonly seen in the
root user’s home directory are shown next:
[[email protected] ~]# ls
current myprogram project
Desktop myscript
project1
[[email protected] ~]# ls -a
.
..
.bash_history
.bash_logout
.bash_profile
.bashrc
.cshrc
current
.DCOPserver_server1_0
Desktop
.first_start_kde
.galeon
.gconf
.gconfd
[[email protected] ~]#_
project12
project13
project2
project3
.gimp-2.8
.gnome
.gnome-desktop
.gnome_private
.gtkrc
.ICEauthority
.kde
.mcop
.MCOP-random-seed
.mcoprc
.mozilla
myprogram
myscript
.nautilus
project4
project5
project
project1
project12
project13
project2
project3
project4
project5
.sane
.sawfish
.tcshrc
.Xauthority
.Xresources
.xsession-errors
As discussed earlier, the ( . ) character refers to the current working directory and the ( .. )
character refers to the parent directory relative to your current location in the directory tree.
Each of these pointers is seen as a special (or fictitious) file when using the ls –a command,
as each starts with a period.
You can also specify several options simultaneously for most commands on the command
line and receive the combined functionality of all the options. For example, to view all hidden
files and their file types, you could type:
[[email protected] ~]# ls -aF
./
../
.bash_history
.bash_logout
.bash_profile
.bashrc
.cshrc
[email protected]
[email protected]
Desktop/
.gimp-2.8/
project
.gnome/
project1
.gnome-desktop/
project12
.gnome_private/
project13
.gtkrc
project2
.ICEauthority
project3
.kde/
project4
.mcop/
project5
.MCOP-random-seed .sane/
.mcoprc
.sawfish/
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
94
Chapter 3
Exploring Linux Filesystems
.first_start_kde
.galeon/
.gconf/
.gconfd/
[[email protected] ~]#_
.mozilla/
.tcshrc
myprogram .Xauthority
myscript
.Xresources
.nautilus/ .xsession-errors
The aforementioned options to the ls command ( –l, –F, –a ) are the most common
options you would use when navigating the Linux directory tree; however, many options are
available in the ls command that alter the listing of files on the filesystem. Table 3-2 depicts
the most common of these options and their descriptions.
Option
Description
-a
--all
Lists all filenames
-A
--almost-all
Lists most filenames (excludes the . and .. special files)
-C
Lists filenames in column format
- -color=n
Lists filenames without color
-d
--directory
Lists directory names instead of their contents
-f
Lists all filenames without sorting
-F
--classify
Lists filenames classified by file type
- -full-time
Lists filenames in long format and displays the full modification time
-l
Lists filenames in long format
-lh
-l - -human-readable
Lists filenames in long format with human-readable (easy-to-read) file sizes
-lG
-l - -no-group
-o
Lists filenames in long format but omits the group information
-r
--reverse
Lists filenames reverse sorted
-R
--recursive
Lists filenames in the specified directory and all subdirectories
-s
Lists filenames and their associated sizes in kilobytes (KB)
-S
Lists filenames sorted by file size
-t
Lists filenames sorted by modification time
-U
Lists filenames without sorting
-x
Lists filenames in rows rather than in columns
Table 3-2 Common options to the ls command
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Displaying the Contents of Text Files
Metacharacter
Description
*
Matches 0 or more characters in a filename
?
Matches 1 character in a filename
[aegh]
Matches 1 character in a filename—provided this character is either an a, e, g, or h
[a-e]
Matches 1 character in a filename—provided this character is either an a, b, c, d, or e
[!a-e]
Matches 1 character in a filename—provided this character is NOT an a, b, c, d, or e
95
Table 3-3 Wildcard metacharacters
Wildcard Metacharacters
In the previous section, you saw that the * metacharacter stands for all the files in the current
directory, much like a wildcard stands for, or matches, certain cards in a card game. As a
result, the * metacharacter is called a wildcard metacharacter. Wildcard metacharacters can
simplify commands that specify more than one filename on the command line, as you saw
with the file command earlier. They match certain portions of filenames or the entire filename itself. Since they are interpreted by the shell, they can be used with most common
Linux filesystem commands, including a few that have already been mentioned (ls, file,
and cd). Table 3-3 displays a list of wildcard metacharacters and their descriptions.
Wildcards can be demonstrated using the ls command. Examples of using wildcard metacharacters to narrow the listing produced by the ls command are shown next.
[[email protected] ~]# ls
current myprogram project
project12
Desktop myscript
project1 project13
[[email protected] ~]# ls project*
project project1 project12 project13
project5
[[email protected] ~]# ls project?
project1 project2 project3 project4
[[email protected] ~]# ls project??
project12 project13
[[email protected] ~]# ls project[135]
project1 project3 project5
[[email protected] ~]# ls project[!135]
project2 project4
[[email protected] ~]# _
project2 project4
project3 project5
project2 project3 project4
project5
Displaying the Contents of Text Files
So far, this chapter has discussed commands that can be used to navigate the Linux directory
structure and view filenames and file types; it is usual now to display the contents of these
files. By far, the most common file type that users display is text files. These files are usually
small and contain configuration information or instructions that the shell interprets (called a
shell script) but can also contain other forms of text, as in e-mail letters. To view an entire
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
96
Chapter 3
Exploring Linux Filesystems
text file on the terminal screen (also referred to as concatenation), you can use the cat
command. The following is an example of using the cat command to display the contents
of an e-mail message (in the fictitious file project4):
[[email protected] ~]# ls
current myprogram project
project12 project2 project4
Desktop myscript
project1 project13 project3 project5
[[email protected] ~]# cat project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
[[email protected] ~]#_
You can also use the cat command to display the line number of each line in the file in addition
to the contents by passing the –n option to the cat command. In the following example, the
number of each line in the project4 file is displayed:
[[email protected] ~]# cat –n project4
1 Hi there, I hope this day finds you well.
2
3 Unfortunately we were not able to make it to your dining
4 room this year while vacationing in Algonquin Park - I
5 especially wished to see the model of the Highland Inn
6 and the train station in the dining room.
7
8 I have been reading on the history of Algonquin Park but
9 no where could I find a description of where the Highland
10 Inn was originally located on Cache lake.
11
12 If it is no trouble, could you kindly let me know such that
13 I need not wait until next year when I visit your lodge?
14
15 Regards,
16 Mackenzie Elizabeth
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Displaying the Contents of Text Files
97
In some cases, you might want to display the contents of a certain text file in reverse order,
which is useful when displaying files that have text appended to them continuously by system
services. These files, also known as log files, contain the most recent entries at the bottom of
the file. To display a file in reverse order, use the tac command (tac is cat spelled backwards), as shown next with the file project4:
[[email protected] ~]# tac project4
Mackenzie Elizabeth
Regards,
I need not wait until next year when I visit your lodge?
If it is no trouble, could you kindly let me know such that
Inn was originally located on Cache lake.
no where could I find a description of where the Highland
I have been reading on the history of Algonquin Park but
and the train station in the dining room.
especially wished to see the model of the Highland Inn
room this year while vacationing in Algonquin Park - I
Unfortunately we were not able to make it to your dining
Hi there, I hope this day finds you well.
[[email protected] ~]#_
If the file displayed is very large and you only want to view the first few lines of it, you can
use the head command. The head command displays the first 10 lines (including blank
lines) of a text file to the terminal screen but can also take a numeric option specifying a different number of lines to display. The following shows an example of using the head command to view the top of the project4 file:
[[email protected] ~]# head project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
[[email protected] ~] # head -3 project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
[[email protected] ~]#_
Just as the head command displays the beginning of text files, the tail command can
be used to display the end of text files. By default, the tail command displays the final
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
98
Chapter 3
Exploring Linux Filesystems
10 lines of a file, but it can also take a numeric option specifying the number of lines to display on the terminal screen, as shown in the following example with the project4 file:
[[email protected] ~]# tail project4
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
[[email protected] ~]# tail -2 project4
Regards,
Mackenzie Elizabeth
[[email protected] ~]#_
Although some text files are small enough to be displayed completely on the terminal screen,
you might encounter text files that are too large to fit in a single screen. In this case, the cat
command sends the entire file contents to the terminal screen; however, the screen only displays as much of the text as it has room for. To display a large text file in a page-by-page
fashion, you need to use the more and less commands.
The more command gets its name from the pg command once used on UNIX systems. The
pg command displayed a text file page by page on the terminal screen, starting at the beginning of the file; pressing the spacebar or Enter key displays the next page, and so on. The
more command does more than pg did, because it displays the next complete page of a text
file if you press the spacebar but displays only the next line of a text file if you press Enter. In
that way, you can browse the contents of a text file page by page or line by line. The fictitious
file project5 is an excerpt from Shakespeare’s tragedy Macbeth and is too large to be displayed fully on the terminal screen using the cat command. Using the more command to
view its contents results in the following output:
[[email protected] ~]# more project5
Go bid thy mistress, when my drink is ready,
She strike upon the bell. Get thee to bed.
Is this a dagger which I see before me,
The handle toward my hand? Come, let me clutch thee.
I have thee not, and yet I see thee still.
Art thou not, fatal vision, sensible
To feeling as to sight? or art thou but
A dagger of the mind, a false creation,
Proceeding from the heat-oppressed brain?
I see thee yet, in form as palpable
As this which now I draw.
Thou marshall’st me the way that I was going;
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Displaying the Contents of Text Files
99
And such an instrument I was to use.
Mine eyes are made the fools o’ the other senses,
Or else worth all the rest; I see thee still,
And on thy blade and dudgeon gouts of blood,
Which was not so before. There’s no such thing:
It is the bloody business which informs
Thus to mine eyes. Now o’er the one halfworld
Nature seems dead, and wicked dreams abuse
The curtain’d sleep; witchcraft celebrates
Pale Hecate’s offerings, and wither’d murder,
Alarum’d by his sentinel, the wolf,
--More--(71%)
As you can see in the preceding output, the more command displays the first page without
returning you to the shell prompt. Instead, the more command displays a prompt at the bottom of the terminal screen that indicates how much of the file is displayed on the screen as a
percentage of the total file size. In the preceding example, 71 percent of the project5 file is displayed. At this prompt, you can press the spacebar to advance one whole page, or you can
press the Enter key to advance to the next line. In addition, the more command allows other
user interactions at this prompt. Pressing the h character at the prompt displays a help screen,
which is shown in the following output, and pressing the q character quits the more command completely without viewing the remainder of the file.
--More--(71%)
Most commands optionally preceded by integer argument k. Defaults in brackets. Star (*) indicates argument becomes new default.
-––––––––––––––––––––––––––––––––––––––––––––––––––––––––––<space>
Display next k lines of text [current screen size]
z
Display next k lines of text [current screen size]
<return>
Display next k lines of text [1]
d or ctrl-D
Scroll k lines [current scroll size, initially 11]
q or Q or <interrupt>
Exit from more
s
Skip forward k lines of text [1]
f
Skip forward k screenfuls of text [1]
b or ctrl-B
Skip backward k screenfuls of text [1]
’
Go to place where previous search started
=
Display current line number
/<regular expression> Search for kth occurrence of regular expression[1]
n
Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd>
Execute <cmd> in a subshell
v
Start up /usr/bin/vi at current line
ctrl-L
Redraw screen
:n
Go to kth next file [1]
:p
Go to kth previous file [1]
:f
Display current filename and line number
.
Repeat previous command
-––––––––––––––––––––––––––––––––––––––––––––––––––––––––––--More—(71%)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
100
Chapter 3
Exploring Linux Filesystems
Just as the more command was named as a result of allowing more user functionality, the
less command is named for doing more than the more command (remember that “less is
more,” more or less). Like the more command, the less command can browse the contents
of a text file page by page by pressing the spacebar and line by line by pressing the Enter key;
however, you can also use the cursor keys on the keyboard to scroll up and down the contents
of the file. The output of the less command, when used to view the project5 file, is as follows:
[[email protected] ~]# less project5
Go bid thy mistress, when my drink is ready,
She strike upon the bell. Get thee to bed.
Is this a dagger which I see before me,
The handle toward my hand? Come, let me clutch thee.
I have thee not, and yet I see thee still.
Art thou not, fatal vision, sensible
To feeling as to sight? or art thou but
A dagger of the mind, a false creation,
Proceeding from the heat-oppressed brain?
I see thee yet, in form as palpable
As this which now I draw.
Thou marshall’st me the way that I was going;
And such an instrument I was to use.
Mine eyes are made the fools o’ the other senses,
Or else worth all the rest; I see thee still,
And on thy blade and dudgeon gouts of blood,
Which was not so before. There’s no such thing:
It is the bloody business which informs
Thus to mine eyes. Now o’er the one halfworld
Nature seems dead, and wicked dreams abuse
The curtain’d sleep; witchcraft celebrates
Pale Hecate’s offerings, and wither’d murder,
Alarum’d by his sentinel, the wolf,
Whose howl’s his watch, thus with his stealthy pace.
project5
Like the more command, the less command displays a prompt at the bottom of the file
using the : character or the filename of the file being viewed (project5 in our example), yet
the less command contains more keyboard shortcuts for searching out text within files. At
the prompt, you can press the h key to obtain a help screen or the q key to quit. The first
help screen for the less command is shown next:
SUMMARY OF LESS COMMANDS
Commands marked with * may be preceded by a number, N.
Notes in parentheses indicate the behavior if N is given.
A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.
h H
Display this help.
q :q Q :Q ZZ
Exit.
-––––––––––––––––––––––––––––––––––––––––––––––––––––––––––Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Displaying the Contents of Text Files
101
MOVING
e ^E j ^N CR
y ^Y k ^K ^P
f ^F ^V SPACE
b ^B ESC-v
z
w
ESC-SPACE
d ^D
u ^U
ESC-( RightArrow
ESC-) LeftArrow
F
*
*
*
*
*
*
*
*
*
*
*
Forward one line(or N lines).
Backward one line or N lines).
Forward one window (or N lines).
Backward one window (or N lines).
Forward one window (and set window to N).
Backward one window (and set window to N).
Forward one window, but don’t stop at end-of-file.
Forward one half-window(and set half-window to N)
Backward one half-window(and set half window to N)
Left 8 character positions (or N positions).
Right 8 character positions (or N positions).
Forward forever; like "tail -f".
HELP -- Press RETURN for more, or q when done
The more and less commands can also be used in conjunction with the output of commands
if that output is too large to fit on the terminal screen. To do this, simply use the | metacharacter after the command, followed by either the more or less command, as follows:
[[email protected] ~]# cd /etc
[[email protected] etc]# ls -l | more
total 3688
-rw-r--r-- 1 root
root
15276 Mar 22 12:20
-rw-r--r-- 1 root
root
2562 Mar 22 12:20
drwxr-xr-x 4 root
root
4096 Jun 11 08:45
-rw-r--r-- 1 root
root
46 Jun 16 16:42
drwxr-xr-x 2 root
root
4096 Jun 11 08:47
-rw-r--r-- 1 root
root
688 Feb 17 00:35
-rw-r--r-- 1 root
root
703 Feb 17 00:35
drwxr-xr-x 4 root
root
4096 Jun 11 08:47
-rw-r--r-- 1 root
root
1419 Jan 26 10:14
-rw-r----- 1 root
smmsp
12288 Jun 17 13:17
drwxr-xr-x 2 root
root
4096 Jun 11 11:11
drwxr-xr-x 3 amanda disk
4096 Jun 11 10:16
-rw-r--r-- 1 amanda disk
0 Mar 22 12:28
-rw––––– – 1 root
root
688 Mar 4 22:34
-rw-r----- 1 root
root
105 Mar 4 22:34
-rw-r--r-- 1 root
root
317 Feb 15 14:33
-rw-r--r-- 1 root
root
331 May 5 08:07
-rw-r--r-- 1 root
root
6200 Jun 16 16:42
drwxr-xr-x 3 root
root
4096 Jun 11 10:37
-rw-––––– 1 root
root
1 May 5 13:39
-rw-r--r-- 1 root
root
325 Apr 14 13:39
-rw-r--r-- 1 root
root
581 Apr 14 13:39
--More--
a2ps.cfg
a2ps-site.cfg
acpi
adjtime
aep
aep.conf
aeplog.conf
alchemist
aliases
aliases.db
alternatives
amanda
amandates
amd.conf
amd.net
anacrontab
ant.conf
asound.state
atalk
at.deny
auto.master
auto.misc
In the preceding example, the output of the ls –l command was redirected to the more
command, which displays the first page of output on the terminal. You can then advance
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
102
Chapter 3
Exploring Linux Filesystems
through the output page by page or line by line. This type of redirection is discussed in
Chapter 7.
Displaying the Contents of Binary Files
It is important to employ text file commands, such as cat, tac, head, tail, more, and
less, only on files that contain text; otherwise, you might find yourself with random output
on the terminal screen or even a dysfunctional terminal. To view the contents of binary files,
you typically use the program that was used to create the file. However, some commands can
be used to safely display the contents of most binary files. The strings command searches
for text characters in a binary file and outputs them to the screen. In many cases, these text
characters might indicate what the binary file is used for. For example, to find the text characters inside the /bin/echo binary executable program page by page, you could use the following
command:
[[email protected] ~]# strings /bin/echo | more
/lib/ld-linux.so.2
PTRh|
<nt7<e
|[^_]
[^_]
[^_]
Try ‘%s --help’ for more information.
Usage: %s [OPTION]... [STRING]...
Echo the STRING(s) to standard output.
-n
do not output the trailing newline
-e
enable interpretation of the backslashescaped characters listed below
-E
disable interpretation of those sequences in STRINGs
--help
display this help and exit
--version output version information and exit
Without -E, the following sequences are recognized and interpolated:
\NNN
the character whose ASCII code is NNN (octal)
\\
backslash
\a
alert (BEL)
\b
backspace
\c
suppress trailing newline
\f
form feed
\n
new line
--More-Although this output might not be easy to read, it does contain portions of text that can point
a user in the right direction to find out more about the /bin/echo command. Another command that is safe to use on binary files and text files is the od command, which displays the
contents of the file in octal format (numeric base 8 format). An example of using the od command to display the first five lines of the file project4 is shown in the following example:
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Searching for Text Within Files
[[email protected] ~]# od project4 | head -5
0000000 064510 072040 062550 062562
0000020 072040 064550 020163 060544
0000040 067571 020165 062567 066154
0000060 071157 072564 060556 062564
0000100 062562 067040 072157 060440
[[email protected] ~]#_
020054
020171
006456
074554
066142
020111
064546
006412
073440
020145
067550
062156
052412
020145
067564
103
062560
020163
063156
062567
066440
You can use the –x option to the od command to display a file in
hexadecimal format (numeric base 16 format).
Searching for Text Within Files
Recall that Linux was modeled after the UNIX operating system. The UNIX operating system
is often referred to as the “grandfather” of all operating systems because it is over 40 years
old and has formed the basis for most advances in computing technology. The major use of
the UNIX operating system in the past 40 years involved simplifying business and scientific
management through database applications. As a result, many commands (referred to as text
tools) were developed for the UNIX operating system that could search for and manipulate
text, such as database information, in many different and advantageous ways. A set of text
wildcards was also developed to ease the searching of specific text information. These text
wildcards are called regular expressions (regexp) and are recognized by several text tools and
programming languages, including, but not limited to, the following:
•
grep
•
awk
•
sed
•
vi
•
Emacs
•
ex
•
ed
•
Cþþ
•
PERL
•
Tcl
Because Linux is a close relative of the UNIX operating system, these text tools and regular
expressions are available to Linux as well. By combining text tools, a typical Linux system
can search for and manipulate data in almost every way possible (as you will see later). As a
result, regular expressions and the text tools that use them are commonly used in business
today.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
104
Chapter 3
Exploring Linux Filesystems
Regular Expressions
As mentioned earlier, regular expressions allow you to specify a certain pattern of text within
a text document. They work similarly to wildcard metacharacters in that they are used to
match characters, yet they have many differences:
• Wildcard metacharacters are interpreted by the shell, whereas regular expressions are
interpreted by a text tool program.
• Wildcard metacharacters match characters in filenames (or directory names) on a
Linux filesystem, whereas regular expressions match characters within text files on a
Linux filesystem.
• Wildcard metacharacters typically have different definitions than regular expression
metacharacters.
• More regular expression metacharacters are available than wildcard metacharacters.
In addition, regular expression metacharacters are divided into two different categories: common regular expressions and extended regular expressions. Common regular expressions are
available to most text tools; however, extended regular expressions are less common and
available in only certain text tools. Table 3-4 shows definitions and examples of some common and extended regular expressions.
The grep Command
The most common way to search for information using regular expressions is the grep command. The grep command (the command name is short for global regular expression print)
is used to display lines in a text file that match a certain common regular expression. To display lines of text that match extended regular expressions, you must use the egrep command
(or the –E option to the grep command). In addition, the fgrep command (or the –F
option to the grep command) does not interpret any regular expressions and consequently
returns results much faster. Take, for example, the project4 file shown earlier:
[[email protected] ~]# cat project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Searching for Text Within Files
Regular
Expression
105
Description
Example
Type
*
Matches 0 or more occurrences of the
previous character
letter* matches lette, letter, letterr,
letterrrr, letterrrrr, and so on
Common
?
Matches 0 or 1 occurrences of the
previous character
letter? matches lette, letter
Extended
+
Matches 1 or more occurrences of the
previous character
letterþ matches letter, letterr, letterrrr,
letterrrrr, and so on
Extended
. (period)
Matches 1 character of any type
letter. matches lettera, letterb, letterc,
letter1, letter2, letter3, and so on
Common
[…]
Matches one character from the range
specified within the braces
letter[1238] matches letter1, letter2,
letter3, and letter8;letter[a-c] matches
lettera, letterb, and letter
Common
[^…]
Matches one character NOT from the
range specified within the braces
letter[^1238] matches letter4, letter5,
letter6, lettera , letterb, and so on
(any character except 1, 2, 3, or 8)
Common
{ }
Matches a specific number or range of
the previous character
letter{3} matches letterrr letter,
whereas letter {2,4} matches letterr,
letterrr, and letterrrr
Extended
^
Matches the following characters if they
are the first characters on the line
^letter matches letter if letter is the
first set of characters in the line
Common
$
Matches the previous characters if they
are the last characters on the line
letter$ matches letter if letter is the
last set of characters in the line
Common
(… | …)
Matches either of two sets of characters
(mother|father) matches the word
“mother” or “father”
Extended
Table 3-4 Regular expressions
The grep command requires two arguments at minimum, the first argument specifies which
text to search for, and the remaining arguments specify the files to search. If a pattern of text
is matched, the grep command displays the entire line on the terminal screen. For example,
to list only those lines in the file project4 that contain the words “Algonquin Park,” enter the
following command:
[[email protected] ~]# grep "Algonquin Park" project4
room this year while vacationing in Algonquin Park - I
I have been reading on the history of Algonquin Park but
[[email protected] ~]#_
To return the lines that do not contain the text “Algonquin Park,” you can use the –v option
of the grep command to reverse the meaning of the previous command:
[[email protected] ~]# grep -v "Algonquin Park" project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
especially wished to see the model of the Highland Inn
and the train station in the dining room.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
106
Chapter 3
Exploring Linux Filesystems
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
[[email protected] ~]#_
Keep in mind that the text being searched is case sensitive; to perform a case-insensitive
search, use the –i option to the grep command:
[[email protected] ~]# grep "algonquin park" project4
[[email protected] ~]#_
[[email protected] ~]# grep –i "algonquin park" project4
room this year while vacationing in Algonquin Park - I
I have been reading on the history of Algonquin Park but
[[email protected] ~]#_
Another important note to keep in mind regarding text tools such as grep is that they match
only patterns of text; they are unable to discern words or phrases unless they are specified.
For example, if you want to search for the lines that contain the word “we,” you can use
the following grep command:
[[email protected] ~]# grep "we" project4
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
[[email protected] ~]#_
However, notice from the preceding output that the first line displayed does not contain the
word “we”; the word “well” contains the text pattern “we” and is displayed as a result. To
display only lines that contain the word “we,” you can type the following to match the letters “we” surrounded by space characters:
[[email protected] ~]# grep "we" project4
Unfortunately we were not able to make it to your dining
[[email protected] ~]#_
All of the previous grep examples did not use regular expression metacharacters to search
for text in the project4 file. Some examples of using regular expressions (see Table 3-4)
when searching this file are shown throughout the remainder of this section.
To view lines that contain the word “toe” or “the” or “tie,” you can enter the following
command:
[[email protected] ~]# grep "t.e" project4
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
107
To view lines that start with the word “I,” you can enter the following command:
[[email protected] ~]# grep "^I" project4
I have been reading on the history of Algonquin Park but
I need not wait until next year when I visit your lodge?
[[email protected] ~]#_
To view lines that contain the text “lodge” or “lake,” you need to use an extended regular
expression and the egrep command, as follows:
[[email protected] ~]# egrep "(lodge|lake)" project4
Inn was originally located on Cache lake.
I need not wait until next year when I visit your lodge?
[[email protected] ~]#_
Editing Text Files
Recall that text files are the most common type of file modified by Linux users and administrators. Most system configuration is stored in text files, as is commonly accessed information
such as e-mail and program source code. Consequently, most Linux distributions come with
an assortment of text editors, and many more are available for Linux systems via the Internet.
Text editors come in two varieties: editors that can be used on the command line, including vi
(vim), nano, and Emacs, and editors that can be used in a GUI environment, including Emacs
(graphical version) and gedit.
The vi Editor
The vi editor (pronounced “vee eye”) is one of the oldest and most popular visual text editors available for UNIX operating systems. Its Linux equivalent (known as vim, which is
short for “vi improved”) is therefore standard on almost every Linux distribution. Although
the vi editor is not the easiest of the editors to use when editing text files, it has the advantage
of portability. A Fedora Linux user who is proficient in using the vi editor will find editing
files on all other UNIX and Linux systems easy because the interface and features of the vi
editor are nearly identical across Linux and UNIX systems. In addition, the vi editor supports
regular expressions and can perform over 1000 different functions for the user.
To open an existing text file for editing, you can type vi filename (or vim filename),
where filename specifies the file to be edited. To open a new file for editing, simply type vi
or vim at the command line:
[[email protected] ~]# vi
The vi editor then runs interactively and replaces the command-line interface with the following output:
~
~
~
~
~
~
VIM - Vi IMproved
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
108
Chapter 3
Exploring Linux Filesystems
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
version 7.4.27
by Bram Moolenaar et al.
Modified by <[email protected]>
Vim is open source and freely distributable
type
Become a registered Vim user!
:help register<Enter>
for information
type
type
type
:q<Enter>
:help <Enter> or <F1>
:help version7<Enter>
to exit
for on-line help
for version info
The tilde ~ characters on the left indicate the end of the file; they are pushed further down
the screen as you enter text. The vi editor is called a bimodal editor because it functions in
one of two modes: command mode and insert mode. The vi editor opens command mode,
in which you must use the keyboard to perform functions, such as deleting text, copying
text, saving changes to a file, and exiting the vi editor. To insert text into the document, you
must enter insert mode by typing one of the characters listed in Table 3-5. One such method
to enter insert mode is to type the i key on the keyboard while in command mode; the vi
editor then displays --INSERT- - at the bottom of the screen and allows the user to enter
a sentence such as the following:
This is a sample sentence.
~
~
~
~
Key
Description
i
Changes to insert mode and places the cursor before the current character for entering text
a
Changes to insert mode and places the cursor after the current character for entering text
o
Changes to insert mode and opens a new line underneath the current line for entering text
I
Changes to insert mode and places the cursor at the beginning of the current line for entering text
A
Changes to insert mode and places the cursor at the end of the current line for entering text
O
Changes to insert mode and opens a new line above the current line for entering text
Esc
Changes back to command mode while in insert mode
Table 3-5 Common keyboard keys used to change to and from insert mode
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
109
~
~
~
~
~
-- INSERT -When in insert mode, you can use the keyboard to type text as required, but when finished
you must press the Esc key to return to command mode to perform other functions via keys
on the keyboard. Table 3-6 provides a list of keys useful in command mode and their associated functions. After you are in command mode, to save the text in a file called samplefile in
the current directory, you need to press the : character (by pressing the Shift and ; keys simultaneously) to reach a : prompt where you can enter a command to save the contents of the
current document to a file, as shown in the following example and in Table 3-7.
This is a sample sentence.
~
~
~
~
~
~
~
~
~
:w samplefile
As shown in Table 3-7, you can quit the vi editor by pressing the : character and entering q!,
which then returns the user to the shell prompt:
This is a sample sentence.
~
~
~
~
~
~
~
~
~
:q!
[[email protected] ~]# _
The vi editor also offers some advanced features to Linux users, as explained in Table 3-7.
Examples of some of these features are discussed next, using the project4 file shown earlier in this
chapter. To edit the project4 file, simply type vi project4 and view the following screen:
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
110
Chapter 3
Exploring Linux Filesystems
Key
Description
w, W
Moves the cursor forward one word to the beginning of the next word
e, E
Moves the cursor forward one word to the end of the next word
b, B
Moves the cursor backward one word
53G
Moves the cursor to line 53
G
Moves the cursor to the last line in the document
0, ^
Moves the cursor to the beginning of the line
$
Moves the cursor to the end of the line
X
Deletes the character the cursor is on
3x
Deletes three characters starting from the character the cursor is on
dw
Deletes one word starting from the character the cursor is on
d3w, 3dw
Deletes three words starting from the character the cursor is on
dd
Deletes one whole line starting from the line the cursor is on
d3d, 3dd
Deletes three whole lines starting from the line the cursor is on
d$
Deletes from cursor character to the end of the current line
d^, d0
Deletes from cursor character to the beginning of the current line
yw
Copies one word (starting from the character the cursor is on) into a temporary buffer in memory
for later use
y3w, 3yw
Copies three words (starting from the character the cursor is on) into a temporary buffer in memory
for later use
yy
Copies the current line into a temporary buffer in memory for later use
y3y, 3yy
Copies three lines (starting from the current line) into a temporary buffer in memory for later use
y$
Copies the current line from the cursor to the end of the line into a temporary buffer in memory
for later use
y^, y0
Copies the current line from the cursor to the beginning of the line into a temporary buffer in
memory for later use
p
Pastes the contents of the temporary memory buffer underneath the current line
P
Pastes the contents of the temporary memory buffer above the current line
J
Joins the line underneath the current line to the current line
Ctrl+g
Displays current line statistics
u
Undoes the last function (undo)
.
Repeats the last function (repeat)
/pattern
Searches for the first occurrence of pattern in the forward direction
?pattern
Searches for the first occurrence of pattern in the reverse direction
n
Repeats the previous search in the forward direction
N
Repeats the previous search in the reverse direction
Table 3-6 Key combinations commonly used in command mode
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
111
Function
Description
:q
Quits from the vi editor if no changes were made
:q!
Quits from the vi editor and does not save any changes
:wq
Saves any changes to the file and quits from the vi editor
:w filename
Saves the current document to a file called filename
:!date
Executes the
:r !date
Reads the output of the
:r filename
Reads the contents of the text file called filename into the document under the current line
:set all
Displays all vi environment settings
:set …….
Sets a vi environment setting to a certain value
:s/the/THE/g
Searches for the regular expression “the” and replaces each occurrence globally throughout
the current line with the word “THE”
:1,$ s/the/THE/g
Searches for the regular expression “the” and replaces each occurrence globally from line 1 to
the end of the document with the word “THE”
date command using a BASH shell
date command into the document under the current line
Table 3-7 Key combinations commonly used at the command mode : prompt
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
~
~
~
~
~
~
~
"project4" 17L, 583C
Note that the name of the file as well as the number of lines and characters in total are displayed at the bottom of the screen (project4 has 17 lines and 583 characters in this example).
To insert the current date and time at the bottom of the file, you can simply move the cursor
to the final line in the file and type the following at the : prompt while in command mode:
Hi there, I hope this day finds you well.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
112
Chapter 3
Exploring Linux Filesystems
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
~
~
~
~
~
~
~
~
:r !date
When you press Enter, the output of the date command is inserted below the current line:
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
Sat Aug 7 18:33:10 EDT 2015
~
~
~
~
~
~
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
113
To change all occurrences of the word “Algonquin” to “ALGONQUIN,” you can simply
type the following at the : prompt while in command mode:
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
Sat Aug 7 18:33:10 EDT 2015
~
~
~
~
~
~
:1,$ s/Algonquin/ALGONQUIN/g
The output changes to the following:
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in ALGONQUIN Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of ALGONQUIN Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
Sat Aug 7 18:33:10 EDT 2015
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
114
Chapter 3
Exploring Linux Filesystems
~
~
~
~
~
~
~
Another attractive feature of the vi editor is its ability to customize the user environment
through settings that can be altered at the : prompt while in command mode. Simply
type set all at this prompt to observe the list of available settings and their current
values:
:set all
--- Options --aleph=224
fileencoding=
noarabic
fileformat=unix
arabicshape
filetype=
noallowrevins
nofkmap
noaltkeymap
foldclose=
ambiwidth=single foldcolumn=0
noautoindent
foldenable
noautoread
foldexpr=0
menuitems=25
swapsync=fsync
modeline
switchbuf=
modelines=5
syntax=
modifiable
tabstop=8
modified
tagbsearch
more
taglength=0
mouse=
tagrelative
mousemodel=
tagstack
extend
noautowrite
foldignore=#
mousetime=500
term=xterm
noautowriteall
foldlevel=0
nonumber
notermbidi
background=light foldlevelstart=-1 nopaste
termencoding=
backspace=2
foldmethod= manual pastetoggle=
noterse
nobackup
foldminlines=1
patchexpr=
textauto
backupcopy=auto
foldnestmax=20
patchmode=
notextmode
backupext=~
formatoptions=tcq nopreserveindent
textwidth=0
backupskip=/tmp/* formatprg=
previewheight=12 thesaurus=
nobinary
nogdefault
nopreviewwindow notildeop
nobomb
helpheight=20
printdevice=
timeout
bufhidden=
helplang=en
printencoding=
timeoutlen=1000
buflisted
nohidden
printfont=
notitle
courier
buftype=
history=50
printoptions=
titlelen=85
-- More -Note in the preceding output that most settings are set to either on or off; those that are
turned off are prefixed with a “no.” In the preceding example, line numbering is turned off
(nonumber in the preceding output); however, you can turn it on by typing set number at
the : prompt while in command mode. This results in the following output in vi:
1 Hi there, I hope this day finds you well.
2
3 Unfortunately we were not able to make it to your dining
4 room this year while vacationing in ALGONQUIN Park - I
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
5
6
7
8
9
10
11
12
13
14
15
16
17
18
115
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of ALGONQUIN Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
Sat Aug 7 18:33:10 EDT 2010
~
~
~
~
~
~
:set number
Conversely, to turn off line numbering, you could simply type set nonumber at the :
prompt while in command mode.
Other Common Text Editors
Although the vi editor is the most common text editor used on Linux and UNIX systems,
there are other text editors that are easier to use.
An alternative to the vi editor that offers an equal set of functionality is the GNU Emacs
(Editor MACroS) editor. Emacs is not installed by default in Fedora 20. To install it, you
can run the command yum install emacs at a command prompt to obtain Emacs from a
free software repository on the Internet. Next, to open the project4 file in the Emacs editor in
a command-line terminal, simply type emacs project4 and the following is displayed on
the terminal screen:
File Edit Options Buffers Tools Help
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
116
Chapter 3
Exploring Linux Filesystems
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
-UU-:----F1 project4 All L1 (Fundamental)----------------------For information about the GNU Emacs and the GNU system, type C-h C-a.
The Emacs editor uses the Ctrl key in combination with certain letters to perform special
functions, can be used with the LISP (LISt Processing) artificial intelligence programming language, and supports hundreds of keyboard functions, such as the vi editor. Table 3-8 shows
a list of some common keyboard functions used in the Emacs editor.
Unfortunately, the Emacs editor is not an easy-to-use editor because the user must memorize
several key combinations to work effectively or use advanced features. If you run Emacs
within a GUI environment, a graphical version of the Emacs editor is started. The graphical
Emacs editor is much easier to use because the command-line key combinations are replaced
by graphical icons, menus, and optional sidebars for many features. If you type emacs
project4 at a terminal within a GUI environment, you will start a graphical Emacs session,
as shown in Figure 3-3.
Another text editor that uses Ctrl key combinations for performing functions is the nano
editor (based on the Pine UNIX editor). Unlike vi or Emacs, nano is a very basic and easyto-use editor that many Linux administrators use to quickly modify configuration files if
they don’t need advanced functionality. As with Emacs, the nano editor is not installed by
default in Fedora 20. However, you can run the command yum install nano at a
Key
Description
Ctrl+a
Moves the cursor to the beginning of the line
Ctrl+e
Moves the cursor to the end of the line
Ctrl+h
Displays Emacs documentation
Ctrl+d
Deletes the current character
Ctrl+k
Deletes all characters between the cursor and the end of the line
Esc+d
Deletes the current word
Ctrl+x + Ctrl+c
Exits the Emacs editor
Ctrl+x + Ctrl+s
Saves the current document
Ctrl+x + Ctrl+w
Saves the current document as a new filename
Ctrl+x + u
Undoes the last change
Table 3-8 Keyboard functions commonly used in the GNU Emacs editor
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Editing Text Files
117
3
Figure 3-3 A graphical emacs session
command prompt to obtain nano from a free software repository on the Internet. If you type
nano project4, you will see the following displayed on the terminal screen:
GNU nano 2.3.2
File: project4.txt
Hi there, I hope this day finds you well.
Unfortunately we were not able to make it to your dining
room this year while vacationing in Algonquin Park - I
especially wished to see the model of the Highland Inn
and the train station in the dining room.
I have been reading on the history of Algonquin Park but
no where could I find a description of where the Highland
Inn was originally located on Cache lake.
If it is no trouble, could you kindly let me know such that
I need not wait until next year when I visit your lodge?
Regards,
Mackenzie Elizabeth
[ Read 16 lines ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C CurPos
^X Exit
^J Justify ^W Where Is ^V Next Page ^U UnCut Txt ^T Spell
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
118
Chapter 3
Exploring Linux Filesystems
Figure 3-4 The gedit text editor
The bottom of the screen lists all the Ctrl key combinations. The ^ symbol represents the Ctrl
key. This means that, to exit nano, you can press Ctrl+X (^X ¼ CtrlþX).
If you are using a GUI environment, you can instead use the gedit editor to quickly edit text
files. Although the gedit editor does not have the advanced functionality that vi or Emacs
has, it is the easiest editor to use as it is functionally analogous to the Windows Wordpad
and Notepad editors. If you type gedit project4 in a GUI environment, you will see the
screen shown in Figure 3-4.
Chapter Summary
■ The Linux filesystem is arranged hierarchically using a series of directories to store files.
The location of these directories and files can be described using a relative or absolute
pathname. The Linux filesystem can contain many types of files, such as text files,
binary data, executable programs, directories, linked files, and special device files.
■ The ls command can be used to view filenames and offers a wide range of options to
modify this view.
■ Wildcard metacharacters are special keyboard characters. They can be used to
simplify the selection of several files when using common Linux file commands.
■ Text files are the most common file type whose contents can be viewed by several
utilities, such as head, tail, cat, tac, more, and less.
■ Regular expression metacharacters can be used to specify certain patterns of text
when used with certain programming languages and text tool utilities, such as grep.
■ Although many command-line and graphical text editors exist, vi (vim) is a powerful,
bimodal text editor that is standard on most UNIX and Linux systems.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
119
Key Terms
~ metacharacter
A metacharacter used to represent a user’s home directory.
absolute pathname
The full pathname to a certain file or directory starting from the root
directory.
binary data file A file that contains machine language (binary 1s and 0s) and stores
information (such as common functions and graphics) used by binary compiled programs.
cat command A Linux command used to display (or concatenate) the entire contents of a
text file to the screen.
cd (change directory) command
A Linux command used to change the current directory
in the directory tree.
command mode One of the two modes in vi; it allows a user to perform any available
text-editing task that is not related to inserting text into the document.
concatenation The joining of text together to make one larger whole. In Linux, words and
strings of text are joined together to form a displayed file.
directory
A special file on the filesystem used to organize other files into a logical tree
structure.
A variant of the grep command used to search files for patterns, using
extended regular expressions.
egrep command
Emacs (Editor MACroS) editor A popular and widespread text editor more conducive to
word processing than vi. It was originally developed by Richard Stallman.
executable program A file that can be executed by the Linux operating system to run in
memory as a process and perform a useful function.
fgrep command
A variant of the grep command that does not allow the use of regular
expressions.
file command
filename
A Linux command that displays the file type of a specified filename.
The user-friendly identifier given to a file.
filename extension A series of identifiers following a dot ( . ) at the end of a filename,
used to denote the type of the file; the filename extention .txt denotes a text file.
gedit editor
A common text editor used within GUI environments.
grep command
A Linux command that searches files for patterns of characters using
regular expression metacharacters. The command name is short for “global regular
expression print.”
head command
A Linux command that displays the first set of lines of a text file; by
default, the head command displays the first 10 lines.
home directory
A directory on the filesystem set aside for users to store personal files and
information.
insert mode One of the two modes in vi; it allows the user to insert text into the document
but does not allow any other functionality.
less command
A Linux command used to display a text file page-by-page on the terminal
screen; users can then use the cursor keys to navigate the file.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
120
Chapter 3
Exploring Linux Filesystems
linked file
The files that represent the same data as other files.
An alias for the ls –l command; it gives a long file listing.
ll command
log file
A file that contains past system events.
ls command
A Linux command used to list the files in a given directory.
more command
A Linux command used to display a text file page by page and line by line
on the terminal screen.
named pipe file A temporary connection that sends information from one command or
process in memory to another; it can also be represented by a file on the filesystem.
nano editor
A user-friendly terminal text editor that uses Ctrl key combinations to
perform basic functions.
od command
A Linux command used to display the contents of a file in octal format.
parent directory
The directory that is one level closer to the root directory in the directory
tree relative to your current directory.
pwd (print working directory) command
A Linux command used to display the current
directory in the directory tree.
regexp
See regular expressions.
regular expressions The special metacharacters used to match patterns of text within text
files; they are commonly used by text tool commands, including grep.
relative pathname
The pathname of a target directory relative to your current directory in
the tree.
socket file
A named pipe connecting processes on two different computers; it can also be
represented by a file on the filesystem.
special device file
A file used to identify hardware devices such as hard disks and serial
ports.
strings command
A Linux command used to search for and display text characters in a
binary file.
subdirectory
A directory that resides within another directory in the directory tree.
Tab-completion feature A feature of the BASH shell that fills in the remaining characters
of a unique filename or directory name when the user presses the Tab key.
tac command A Linux command that displays a file on the screen, beginning with the last
line of the file and ending with the first line of the file.
tail command
A Linux command used to display lines of text at the end of a file; by
default, the tail command displays the last 10 lines of the file.
text file
text tools
A file that stores information in a readable text format.
The programs that allow for the creation, modification, and searching of text
files.
vi editor
A powerful command-line text editor available on most UNIX and Linux
systems.
wildcard metacharacters The metacharacters used to match certain characters in a file or
directory name; they are often used to specify multiple files.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
121
Review Questions
1. A directory is a type of file. True or False?
2. Which command would a user type on the command line to find out which directory in
the directory tree he is currently located in?
a.
3
pd
b. cd
c.
where
d. pwd
3. Which of the following is an absolute pathname? (Choose all that apply.)
a.
Home/resume
b. C:\myfolder\resume
c.
resume
d. /home/resume
e.
C:home/resume
4. A special device file is used to
a.
.
enable proprietary custom-built devices to work with Linux
b. represent hardware devices such as hard disk drives and ports
c.
keep a list of device settings specific to each individual user
d. do nothing in Linux
5. If a user’s current directory is /home/mary/project1, which command could she use to
move to the etc directory directly under the root?
a.
cd ..
b. cd /home/mary/etc
c.
cd etc
d. cd /etc
e.
cd \etc
6. After typing the ls –a command, you notice that there is a file whose filename begins
with a dot ( . ). What does this mean?
a.
It is a binary file.
b. It is a system file.
c.
It is a file in the current directory.
d. It is a hidden file.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
122
Chapter 3
Exploring Linux Filesystems
7. After typing the ls –F command, you notice a filename that ends with an * (asterisk)
character. What does this mean?
a.
It is a hidden file.
b. It is a linked file.
c.
It is a special device file.
d. It is an executable file.
8. The vi editor can function in which two of the following modes? (Choose both that
apply.)
a.
text
b. command
c.
input
d. interactive
e.
insert
9. The less command offers less functionality than the more command. True or False?
10. Which command searches for and displays any text contents of a binary file?
a.
text
b. strings
c.
od
d. less
11. How can a user switch from insert mode to command mode when using the vi editor?
a.
Press the CtrlþAltþDel keys simultaneously.
b. Press the Del key.
c.
Type in a : character.
d. Press the Esc key.
12. If “resume” is the name of a file in the home directory off the root of the filesystem and
your present working directory is home, what is the relative name for the file named
resume?
a.
/home/resume
b. /resume
c.
resume
d. \home\resume
13. What will the following wildcard regular expression return: file[a-c]?
a.
filea-c
b. filea, filec
c.
filea, fileb, filec
d. fileabc
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
123
14. What will typing q! at the : prompt in command mode do when using the vi editor?
a.
quit because no changes were made
b. quit after saving any changes
c.
nothing because the ! is a metacharacter
d. quit without saving any changes
15. A user types in the command head /poems/mary. What will be displayed on the
terminal screen?
a.
the first line of the file mary
b. the header for the file mary
c.
the first 20 lines of the file mary
d. the last 10 lines of the file mary
e.
the first 10 lines of the file mary
16. The tac command
a.
.
is not a valid Linux command
b. displays the contents of hidden files
c.
displays the contents of a file in reverse order, last word on the line first and first
word on the line last
d. displays the contents of a file in reverse order, last line first and first line last
17. How can you specify a text pattern that must be at the beginning of a line of text using
a regular expression?
a.
Precede the string with a /.
b. Follow the string with a \.
c. Precede the string with a $.
d. Precede the string with a ^.
18. Linux has only one root directory per directory tree. True or False?
19. Using wildcard metacharacters, how can you indicate a character that is NOT a or b or
c or d?
a. [^abcd]
b. not [a-d]
c.
[!a-d]
d. !a-d
20. A user typed in the command pwd and saw the output: /home/jim/sales/pending. How
could that user navigate to the /home/jim directory?
a.
cd ..
b. cd /jim
c.
cd ../..
d. cd ./.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
124
Chapter 3
Exploring Linux Filesystems
Hands-on Projects
These projects should be completed in the order given. The hands-on projects
presented in this chapter should take a total of three hours to complete. The
requirements for this lab include:
• A computer with Fedora Linux installed according to Hands-on Project 2-1.
Project 3-1
In this hands-on project, you log in to the computer and navigate the file structure.
1. Boot your Fedora Linux virtual machine. After your Linux system has been loaded,
switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type pwd and press Enter to view the current working directory. What is your current working directory?
3. At the command prompt, type cd and press Enter. At the command prompt, type pwd
and press Enter to view the current working directory. Did your current working directory change? Why or why not?
4. At the command prompt, type cd . and press Enter. At the command prompt, type pwd
and press Enter to view the current working directory. Did your current working directory change? Why or why not?
5. At the command prompt, type cd .. and press Enter. At the command prompt, type
pwd and press Enter to view the current working directory. Did your current working
directory change? Why or why not?
6. At the command prompt, type cd root and press Enter. At the command prompt, type
pwd and press Enter to view the current working directory. Did your current working
directory change? Where are you now? Did you specify a relative or absolute pathname
to your home directory when you used the cd root command?
7. At the command prompt, type cd etc and press Enter. What error message did you
receive and why?
8. At the command prompt, type cd /etc and press Enter. At the command prompt, type
pwd and press Enter to view the current working directory. Did your current working
directory change? Did you specify a relative or absolute pathname to the /etc directory
when you used the cd /etc command?
9. At the command prompt, type cd / and press Enter. At the command prompt, type pwd
and press Enter to view the current working directory. Did your current working directory change? Did you specify a relative or absolute pathname to the / directory when
you used the cd / command?
10. At the command prompt, type cd ~user1 and then press Enter. At the command
prompt, type pwd and press Enter to view the current working directory. Did your current working directory change? Which command discussed earlier performs the same
function as the cd ~ command?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
125
11. At the command prompt, type cd Desktop and press Enter (be certain to use a capital
D). At the command prompt, type pwd and press Enter to view the current working
directory. Did your current working directory change? Where are you now? What kind
of pathname did you use here (absolute or relative)?
12. Currently, you are in a subdirectory of user1’s home folder, three levels below the root. To
go up three parent directories to the / directory, type cd ../../.. and press Enter at the
command prompt. Next, type pwd and press Enter to ensure that you are in the / directory.
13. At the command prompt, type cd /etc/samba and press Enter to change the current
working directory using an absolute pathname. Next, type pwd and press Enter at the command prompt to ensure that you have changed to the /etc/samba directory. Now, type in
the command cd ../sysconfig at the command prompt and press Enter. Type pwd
and press Enter to view your current location. Explain how the relative pathname seen in
the cd ../sysconfig command specified your current working directory.
14. At the command prompt, type cd ../../home/user1/Desktop and press Enter to
change your current working directory to the Desktop directory underneath user1’s
home directory. Verify that you are in the target directory by typing the pwd command
at a command prompt and press Enter. Would it have been more advantageous to use
an absolute pathname to change to this directory instead of the relative pathname that
you used?
15. Type exit and press Enter to log out of your shell.
Project 3-2
In this hands-on project, you navigate the Linux filesystem using the Tab-completion feature
of the BASH shell.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type cd / and press Enter.
3. Next, type cd ro at the command prompt and press Tab. What is displayed on the
screen and why? How many subdirectories under the root begin with “ro”?
4. Press the Ctrl and c keys simultaneously to cancel the command and return to an empty
command prompt.
5. At the command prompt, type cd b and press Tab. Did the display change?
6. Press the Tab key again. How many subdirectories under the root begin with “b”?
7. Type the letter i. Notice that the command now reads “cd bi.” Press the Tab key again.
Which directory did it expand to? Why? Press the Ctrl and c keys simultaneously to
cancel the command and return to an empty command prompt.
8. At the command prompt, type cd m and press Tab. Press Tab once again after hearing
the beep. How many subdirectories under the root begin with “m”?
9. Type the letter i. Notice that the command now reads “cd me.” Press Tab.
10. Press Enter to execute the command at the command prompt. Next, type the pwd command and press Enter to verify that you are in the /media directory.
11. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
126
Chapter 3
Exploring Linux Filesystems
Project 3-3
In this hands-on project, you examine files and file types using the ls and file commands.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type cd /etc and press Enter. Verify that you are in the /etc
directory by typing pwd at the command prompt and press Enter.
3. At the command prompt, type ls and press Enter. What do you see listed in the four
columns? Do any of the files have extensions? What is the most common extension you
see and what does it indicate? Is the list you are viewing on the screen the entire contents of /etc?
4. At the command prompt, type ls | more and then press Enter (the | symbol is usually
near the Enter key on the keyboard and is obtained by pressing the Shift and \ keys in
combination). What does the display show? Notice the highlighted - -More-- prompt at
the bottom of the screen. Press Enter. Press Enter again. Press Enter once more. Notice
that each time you press Enter, you advance one line further into the file. Now, press
the spacebar. Press the spacebar again. Notice that with each press of the spacebar, you
advance one full page into the displayed directory contents. Press the h key to get a help
screen. Examine the command options.
5. Press the q key to quit the more command and return to an empty command prompt.
6. At the command prompt, type ls | less and then press Enter. What does the display
show? Notice the : at the bottom of the screen. Press Enter. Press Enter again. Press
Enter once more. Notice that each time you press Enter, you advance one line further
into the file. Now press the spacebar. Press the spacebar again. Notice that with each
press of the spacebar, you advance one full page into the displayed directory contents.
Press the h key to get a help screen. Examine the command options, and then press q
to return to the command output.
7. Press the " (up arrow) key. Press " again. Press " once more. Notice that each time you
press the " key, you go up one line in the file display toward the beginning of the file.
Now, press the # (down arrow) key. Press # again. Press # once more. Notice that each
time you press the # key, you move forward into the file display.
8. Press the q key to quit the less command and return to a shell command prompt.
9. At the command prompt, type cd and press Enter. At the command prompt, type pwd
and press Enter. What is your current working directory? At the command prompt,
type ls and press Enter.
10. At the command prompt, type ls /etc and press Enter. How does this output compare with what you saw in Step 9? Has your current directory changed? Verify your
answer by typing pwd at the command prompt and press Enter. Notice that you were
able to list the contents of another directory by giving the absolute name of it as an
argument to the ls command without leaving the directory in which you are currently
located.
11. At the command prompt, type ls /etc/skel and press Enter. Did you see a listing of
any files? At the command prompt, type ls -a /etc/skel and press Enter. What is
special about these files? What do the first two entries in the list ( . and .. ) represent?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
127
12. At the command prompt, type ls -aF /etc/skel and press Enter. Which file types
are available in the /etc/skel directory?
13. At the command prompt, type ls /bin and press Enter. Did you see a listing of any
files? At the command prompt, type ls -F /bin and press Enter. What file types are
present in the /bin directory?
14. At the command prompt, type ls /boot and press Enter. Next, type ls –l /boot and
press Enter. What additional information is available on the screen? What types of files
are available in the /boot directory? At the command prompt, type ll /boot and press
Enter. Is the output any different from that of the ls –l /boot command you just
entered? Why or why not?
15. At the command prompt, type file /etc and press Enter. What kind of file is etc?
16. At the command prompt, type file /etc/inittab and press Enter. What type of file
is /etc/inittab?
17. At the command prompt, type file /boot/* to see the types of files in the /boot directory. Is this information more specific than the information you gathered in Step 14?
18. Type exit and press Enter to log out of your shell.
Project 3-4
In this hands-on project, you display file contents using the cat, tac, head, tail,
strings, and od commands.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2, and then log in to
the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type cat /etc/hosts and press Enter to view the contents
of the file hosts, which reside in the directory /etc. Next, type cat –n /etc/hosts
and press Enter. How many lines does the file have? At the command prompt, type
tac /etc/hosts and press Enter to view the same file in reverse order. The output of
both commands should be visible on the same screen. Compare them.
3. To see the contents of the same file in octal format instead of ASCII text, type od /
etc/hosts at the command prompt and press Enter.
4. At the command prompt, type cat /etc/inittab and press Enter.
5. At the command prompt, type head /etc/inittab and press Enter. What is displayed on the screen? How many lines are displayed, which ones are they, and why?
6. At the command prompt, type head -5 /etc/inittab and press Enter. How many
lines are displayed and why? Next, type head -3 /etc/inittab and press Enter.
How many lines are displayed and why?
7. At the command prompt, type tail /etc/inittab and press Enter. What is displayed on the screen? How many lines are displayed; which ones are they and why?
8. At the command prompt, type tail -5 /etc/inittab and press Enter. How many
lines are displayed and why? Type the cat –n /etc/inittab command at a command
prompt and press Enter to justify your answer.
9. At the command prompt, type file /bin/nice and press Enter. What type of file is
it? Should you use a text tool command on this file?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
128
Chapter 3
Exploring Linux Filesystems
10. At the command prompt, type strings /bin/nice and press Enter. Notice that
you are able to see some text within this binary file. Next, type strings /bin/nice |
more to view the same content page-by-page. When finished, press q to quit the more
command.
11. Type exit and press Enter to log out of your shell.
Project 3-5
In this hands-on project, you create and edit text files using the vi editor.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type pwd, press Enter, and ensure that /root is displayed,
showing that you are in the root user’s home folder. At the command prompt, type vi
sample1 and press Enter to open the vi editor and create a new text file called sample1. Notice that this name appears at the bottom of the screen along with the indication that it is a new file.
3. At the command prompt, type My letter and press Enter. Why was nothing displayed
on the screen? To switch from command mode to insert mode to allow the typing of
text, press i. Notice that the word Insert appears at the bottom of the screen. Next,
type My letter and notice that this text is displayed on the screen. What types of
tasks can be accomplished in insert mode?
4. Press Esc. Did the cursor move? What mode are you in now? Press
two times until
the cursor is under the last t in letter. Press the x key. What happened? Next, type i to
enter insert mode, then type the letter “h.” Did the letter “h” get inserted before or after
the cursor?
5. Press Esc to switch back to command mode and then move your cursor to the end of
the line. Next, type the letter “o” to open a line underneath the current line and enter
insert mode.
6. Type the following:
It might look like I am doing nothing, but at the cellular level
I can assure you that I am quite busy. And to a typical cell, it may
seem like they are doing all the work!
Notice that the line wraps to the next line partway through the sentence. Though displayed
over two lines on the screen, this sentence is treated as one continuous line of text in vi.
Press Esc to return to command mode, and then press ". Where does the cursor move?
Use the cursor keys to navigate to the letter “l” at the beginning of the word “level,” and
then press the i key to enter insert mode. Press the Enter key while in insert mode. Next,
press Esc to return to command mode, and then press ". Where does the cursor move?
7. Type dd three times to delete all lines in the file.
8. Type i to enter insert mode, and then type:
Hi there, I hope this day finds you well.
and press Enter. Press Enter again. Type:
Unfortunately we were not able to make it to your dining
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
129
and press Enter. Type:
room this year while vacationing in Algonquin Park – I
and press Enter. Type:
especially wished to see the model of the Highland Inn
and press Enter. Type:
and the train station in the dining room.
and press Enter. Press Enter again. Type:
I have been reading on the history of Algonquin Park but
and press Enter. Type:
no where could I find a description of where the Highland
and press Enter. Type:
Inn was originally located on Cache lake.
and press Enter. Press Enter again. Type:
If it is no trouble, could you kindly let me know such that
and press Enter. Type:
I need not wait until next year when I visit your lodge?
and press Enter. Press Enter again. Type:
Regards,
and press Enter. Type:
Mackenzie Elizabeth
and press Enter. You should now have the sample letter used in this chapter on your
screen. It should resemble the letter in Figure 3-4.
9. Press Esc to switch to command mode. Next press the Shift and ; keys simultaneously to
open the : prompt at the bottom of the screen. At this prompt, type w and press Enter to
save the changes you have made to the file. What is displayed at the bottom of the file
when you are finished?
10. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type q, and then press Enter to exit the vi editor.
11. At the command prompt, type ls and press Enter to view the contents of your current
directory. Notice that there is now a file called sample1 listed.
12. Next, type file sample1 and press Enter. What type of file is sample1? At the command prompt, type cat sample1 and press Enter.
13. At the command prompt, type vi sample1 and press Enter to open the letter again in
the vi editor. What is displayed at the bottom of the screen? How does this compare
with Step 9?
14. Use the cursor keys to navigate to the bottom of the document. Press the Shift and ;
keys simultaneously to open the : prompt at the bottom of the screen again, type
!date and press Enter. The current system date and time appear at the bottom of the
screen. As indicated, press Enter to return to the document.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
130
Chapter 3
Exploring Linux Filesystems
15. Press the Shift and ; keys simultaneously again to open the : prompt at the bottom of
the screen again, type r !date and press Enter. What happened and why?
16. Use the cursor keys to position your cursor on the line in the document that displays the
current date and time, and type yy to copy it to the buffer in memory. Next, use the
cursor keys to position your cursor on the first line in the document, and type P (capitalized) to paste the contents of the memory buffer above your current line. Does the
original line remain at the bottom of the document?
17. Use the cursor keys to position your cursor on the line at the end of the document that
displays the current date and time, and type dd to delete it.
18. Use the cursor keys to position your cursor on the t in the word “there” on the second
line of the file that reads Hi there, I hope this day finds you well., and press dw to delete
the word. Next, type i to enter insert mode, type the word Bob, and then press Esc to
switch back to command mode.
19. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type w sample2 and press Enter. What happened and why?
20. Press i to enter insert mode, and type the word test. Next, press Esc to switch to command mode. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the screen again, type q, and press Enter to quit the vi editor. Were you able
quit? Why not?
21. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type q!, and press Enter to quit the vi editor and discard any changes
since the last save.
22. At the command prompt, type ls and press Enter to view the contents of your current
directory. Notice that there is now a file called sample2, which was created in Step 19.
23. At the command prompt, type vi sample2 and press Enter to open the letter again in
the vi editor.
24. Use the cursor keys to position your cursor on the line that reads Hi Bob, I hope this
day finds you well.
25. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen, type s/Bob/Barb/g, and press Enter to change all occurrences of “Bob” to
“Barb” on the current line.
26. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type 1,$ s/to/TO/g, and press Enter to change all occurrences of the
word “to” to “TO” for the entire file.
27. Press the u key to undo the last function performed. What happened and why?
28. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type wq, and press Enter to save your document and quit the vi editor.
29. At the command prompt, type vi sample3 and press Enter to open a new file called
sample3 in the vi editor. Type i to enter insert mode. Next, type P.S. How were the
flies this year? Press the Esc key when finished.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
131
30. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type wq, and press Enter to save your document and quit the vi editor.
31. At the command prompt, type vi sample1, press Enter to open the file sample1 again,
and use the cursor keys to position your cursor on the line that reads “Mackenzie
Elizabeth.”
32. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type r sample3, and press Enter to insert the contents of the file sample3
below your current line.
33. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen, type s/flies/flies and bears/g and press Enter. What happened and
why?
34. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type set number, and press Enter to turn on line numbering.
35. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type set nonumber, and press Enter to turn off line numbering.
36. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type set all, and press Enter to view all vi parameters. Press Enter to
advance through the list, and press q when finished to return to the vi editor.
37. Press the Shift and ; keys simultaneously to open the : prompt at the bottom of the
screen again, type wq, and press Enter to save your document and quit the vi editor.
38. Type exit and press Enter to log out of your shell.
Project 3-6
In this hands-on project, you use the ls command alongside wildcard metacharacters in your
shell to explore the contents of your home directory.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type pwd, press Enter, and ensure /root is displayed showing
that you are in the root user’s home folder. At the command prompt, type ls. How
many files with a name beginning with the word “sample” exist in /root?
3. At the command prompt, type ls * and press Enter. What is listed and why?
4. At the command prompt, type ls sample and press Enter. What is listed and why?
5. At the command prompt, type ls sample? and press Enter. What is listed and why?
6. At the command prompt, type ls sample?? and press Enter. What is listed and why?
7. At the command prompt, type ls sample[13] and press Enter. What is listed and
why?
8. At the command prompt, type ls sample[!13] and press Enter. What is listed and
why? How does this compare to the results from Step 7?
9. At the command prompt, type ls sample[1-3] and press Enter. What is listed and
why?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
132
Chapter 3
Exploring Linux Filesystems
10. At the command prompt, type ls sample[!1-3] and press Enter. What is listed and
why? How does this compare to the results from Step 9?
11. Type exit and press Enter to log out of your shell.
Project 3-7
In this hands-on project, you use the grep and egrep commands alongside regular expression metacharacters to explore the contents of text files.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type grep “Inn” sample1 and press Enter. What is displayed and why?
3. At the command prompt, type grep -v “Inn” sample1 and press Enter. What is displayed and why? How does this compare to the results from Step 2?
4. At the command prompt, type grep “inn” sample1 and press Enter. What is displayed and why?
5. At the command prompt, type grep -i “inn” sample1 and press Enter. What is displayed and why? How does this compare to the results from Steps 2 and 4?
6. At the command prompt, type grep “I” sample1 and press Enter. What is displayed
and why?
7. At the command prompt, type grep “ I ” sample1 and press Enter. What is
displayed and why? How does it differ from the results from Step 6 and why?
8. At the command prompt, type grep “t.e” sample1 and press Enter. What is displayed and why?
9. At the command prompt, type grep
displayed and why?
“w…e”
sample1 and press Enter. What is
10. At the command prompt, type grep “ ^I” sample1 and press Enter. What is displayed and why?
11. At the command prompt, type grep “^I” sample1 and press Enter. What is displayed
and why? How does this differ from the results in Step 10 and why?
12. At the command prompt, type grep “(we|next)” sample1 and press Enter. Is anything displayed? Why?
13. At the command prompt, type egrep “(we|next)” sample1 and press Enter. What
is displayed and why?
14. At the command prompt, type grep “Inn$” sample1 and press Enter. What is
displayed and why?
15. At the command prompt, type grep “?$” sample1 and press Enter. What is displayed
and why? Does the ? metacharacter have special meaning here? Why?
16. At the command prompt, type grep “^$” sample1 and press Enter. Is anything displayed? (Hint: Be certain to look closely!) Can you explain the output?
17. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
133
Discovery Exercises
1. You are the systems administrator for a scientific research company that employs over
100 scientists who write and run Linux programs to analyze their work. All of these
programs are stored in each scientist’s home directory on the Linux system. One scientist
has left the company, and you are instructed to retrieve any work from that scientist’s
home directory. When you enter the home directory for that user, you notice that there
are very few files and only two directories (one named Projects and one named Lab). List
the commands that you would use to navigate through this user’s home directory and
view filenames and file types. If there are any text files, what commands could you use to
view their contents?
2. When you type the pwd command, you notice that your current location on the Linux
filesystem is the /usr/local directory. Answer the following questions, assuming that your
current directory is /usr/local for each question.
a.
Which command could you use to change to the /usr directory using an absolute
pathname?
b. Which command could you use to change to the /usr directory using a relative
pathname?
c.
Which command could you use to change to the /usr/local/share/info directory using
an absolute pathname?
d. Which command could you use to change to the /usr/local/share/info directory using
a relative pathname?
e.
Which command could you use to change to the /etc directory using an absolute
pathname?
f.
Which command could you use to change to the /etc directory using a relative
pathname?
3. Using wildcard metacharacters and options to the ls command, view the following
a.
All the files that end with .cfg under the /etc directory
b. All hidden files in the /home/user1 directory
c.
The directory names that exist under the /var directory
d. All the files that start with the letter “a” underneath the /bin directory
e.
All the files that have exactly three letters in their filename in the /bin directory
f.
All files that have exactly three letters in their filename and end with either the letter
“t” or the letter “h” in the /bin directory
4. Explore the manual pages for the ls, grep, cat, od, tac, head, tail, pwd, cd,
strings, and vi commands. Experiment with what you learn using the file sample1
that you created earlier.
5. The famous quote “To be or not to be” (from Shakespeare’s Hamlet) can be represented
by the following regular expression:
(2b|[^b]{2})
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
3
134
Chapter 3
Exploring Linux Filesystems
If you used this expression when searching a text file using the egrep command
(egrep "(2b|[^b]{2})" filename), what would be displayed? Try this command
on a file that you have created. Why does it display what it does? That is the question.
6. By default, a minimal version of the vi editor is installed on Fedora 20. Run the
command yum update vim-minimal to update this editor to the latest version from
software repositories on the Internet and then type yum install vim to install the full vi
editor package. The full vi editor package comes with a short 30-minute tutorial on its
usage. Start this tutorial by typing vimtutor at a command prompt and then follow the
directions. Provided you have a functional Web browser and an Internet connection,
explore the resources available at www.vim.org.
7. Enter the following text into a new document called question7 using the vi editor. Next,
use the vi editor to fix the mistakes in the file using the information in Table 3-5,
Table 3-6, and Table 3-7 as well as the examples provided in this chapter.
Hi there,
Unfortunately we were not able to make it to your dining room
Unfortunately we were not able to make it to your dining room
this year while vacationing in Algonuin Park - I especially wished
to see the model of the highland inn and the train station in the
dining rooms.
I have been readng on the history of Algonuin Park but
no where could I find a description of where the Highland Inn was
originally located on Cache lake.
If it is not trouble, could you kindly let me that I need
not wait until next year when we visit Lodge?
I hope this day finds you well.
Regard
Elizabeth Mackenzie
8. The knowledge gained from using the vi editor can be transferred easily to the Emacs
editor. Perform Question 7 using the Emacs editor instead of the vi editor.
9. When you use the vi editor and change environment settings at the : prompt, such as :
set number to enable line numbering, those changes are lost when you exit the vi
editor. To continuously apply the same environment settings, you can choose to put the
set commands in a special hidden file in their home directory called .exrc; this .exrc file
is then applied each time you open the vi editor. Enter the vi editor and find three
environment settings that you want to change in addition to line numbering. Then create
a new file called .exrc in your home directory and enter the four lines changing these vi
environment settings (do not start each line with a : character, just enter the set
command—e.g., set number). When finished, open the vi editor to edit a new file and
test to see whether the settings were applied automatically.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
4
Linux Filesystem Management
After completing this chapter, you will be able to:
•
•
•
•
•
•
•
•
•
•
Find files and directories on the filesystem
Understand and create linked files
Explain the function of the Filesystem Hierarchy Standard
Use standard Linux commands to manage files and directories
Modify file and directory ownership
Define and change Linux file and directory permissions
Identify the default permissions created on files and directories
Apply special file and directory permissions
Modify the default access control list (ACL)
View and set filesystem attributes
135
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
136
Chapter 4
Linux Filesystem Management
In the previous chapter, you learned about navigating the Linux filesystem as well as
viewing and editing files. This chapter focuses on the organization of files on the Linux filesystem
as well as their linking and security. First, you explore standard Linux directories using the Filesystem Hierarchy Standard. Next, you explore common commands used to manage files and directories as well as learn methods that are used to find files and directories on the filesystems. Finally,
you learn about file and directory linking, permissions, special permissions, and attributes.
The Filesystem Hierarchy Standard
The many thousands of files on a typical Linux system are organized into directories in the
Linux directory tree. It’s a complex system, made even more complex in the past by the fact
that different Linux distributions were free to place files in different locations. This meant
that you could waste a great deal of time searching for a configuration file on a Linux system
with which you were unfamiliar. To simplify the task of finding specific files, the Filesystem
Hierarchy Standard (FHS) was created.
FHS defines a standard set of directories for use by all Linux and UNIX systems as well as the
file and subdirectory contents of each directory. This ensures that, because the filename and
location follow a standard convention, a Fedora Linux user will find the correct configuration
file on a SuSE Linux or Hewlett-Packard UNIX computer with little difficulty. The FHS also
gives Linux software developers the ability to locate files on a Linux system regardless of the
distribution, allowing them to create software that is not distribution-specific.
A comprehensive understanding of the standard types of directories found on Linux systems is
valuable when locating and managing files and directories; some standard UNIX and Linux
directories defined by FHS and their descriptions are found in Table 4-1. These directories
are discussed throughout this chapter and subsequent chapters.
To read the complete Filesystem Hierarchy Standard definition, go to
http://refspecs.linuxfoundation.org/fhs.shtml.
Managing Files and Directories
As mentioned earlier, using a Linux system involves navigating several directories and manipulating the files inside them. Thus, an efficient Linux user must understand how to create
directories as needed, copy or move files from one directory to another, and delete files and
directories. These tasks are commonly referred to as file management tasks.
Following is an example of a directory listing displayed by a user who is logged in as the root
user:
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls -F
[email protected] myprogram* project
project12 project2 project4
Desktop/ myscript
project1 project13 project3 project5
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Managing Files and Directories
137
Directory
Description
/bin
Contains binary commands for use by all users
/boot
Contains the Linux kernel and files used by the boot loader
/dev
Contains device files
/etc
Contains system-specific configuration files
/home
Is the default location for user home directories
/lib
Contains shared program libraries (used by the commands in /bin and /sbin) as well as kernel modules
/media
A directory that contains subdirectories used for accessing (mounting) filesystems on removable
media devices such as floppy disks, DVDs, and USB flash drives
/mnt
An empty directory used for temporarily accessing filesystems on removable media devices
/opt
Stores additional software programs
/proc
Contains process and kernel information
/root
Is the root user’s home directory
/sbin
Contains system binary commands (used for administration)
/tmp
Holds temporary files created by programs
/usr
Contains most system commands and utilities—contains the following directories:
/usr/bin—User binary commands
/usr/games—Educational programs and games
/usr/include—C program header files
/usr/lib—Libraries/usr/local—Local programs
/usr/sbin—System binary commands
/usr/share—Files that are architecture independent
/usr/src—Source code
/usr/X11R6—The X Window system (sometimes replaced by /etc/X11)
/usr/local
Is the location for most additional programs
/var
Contains log files and spools
Table 4-1 Linux directories defined by the Filesystem Hierarchy Standard
As shown in the preceding output, only one directory (Desktop), two executable files (myprogram and myscript), and several project-related files (project) exist on this example system.
Although this directory structure is not cluttered and appears in an easy-to-read format on the
terminal screen, typical home directories on a Linux system contain many more files; typical
Linux users might have over 100 files in their home directories. As a result, it is good practice
to organize these files into subdirectories based on file purpose. Because several project files are
in the root user’s home directory in the preceding output, you could create a subdirectory
called proj_files to contain the project-related files and decrease the size of the directory listing.
To do this, you use the mkdir (make directory) command, which takes arguments specifying
the absolute or relative pathnames of the directories to create. To create a proj_files directory
underneath the current directory, you can use the mkdir command with a relative pathname:
[[email protected] ~]# mkdir proj_files
[[email protected] ~]# ls -F
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
138
Chapter 4
Linux Filesystem Management
[email protected] myprogram* project project12 project2 project4 proj_files/
Desktop/ myscript* project1 project13 project3 project5
[[email protected] ~]#_
Now, you can move the project files into the proj_files subdirectory by using the mv (move)
command. The mv command requires two arguments at minimum: the source file/directory
and the target file/directory. For example, to move the /etc/sample1 file to the /root directory,
you could use the command mv /etc/sample1 /root.
If you want to move several files, you include one source argument for each file you want to
move and then include the target directory as the last argument. For example, to move the /etc/
sample1 and /etc/sample2 files to the /root directory, you could use the command mv /etc/
sample1 /etc/sample2 /root.
Note that both the source (or sources) and the destination can be absolute or relative pathnames
and the source can contain wildcards if several files are to be moved. For example, to move all
of the project files to the proj_files directory, you could type mv with the source argument
project* (to match all files starting with the letters “project”) and the target argument proj_
files (relative pathname to the destination directory), as shown in the following output:
[[email protected] ~]# mv project* proj_files
[[email protected] ~]# ls -F
[email protected] Desktop/ myprogram* myscript* proj_files/
[[email protected] ~]# ls -F proj_files
project project1 project12 project13 project2 project3 project4
project5
[[email protected] ~]#_
In the preceding output, the current directory listing does not show the project files anymore,
yet the listing of the proj_files subdirectory indicates that they were moved successfully.
If the target is the name of a directory, the mv command moves
those files to that directory. If the target is a filename of an existing
file in a certain directory and there is one source file, the mv command overwrites the target with the source. If the target is a filename of a nonexistent file in a certain directory, the mv command
creates a new file with that filename in the target directory and
moves the source file to that file.
Another important use of the mv command is to rename files, which is simply moving a file to
the same directory but with a different filename. To rename the myscript file from earlier
examples to myscript2, you can use the following mv command:
[[email protected] ~]# ls -F
[email protected] Desktop/ myprogram* myscript* proj_files/
[[email protected] ~]# mv myscript myscript2
[[email protected] ~]# ls -F
[email protected] Desktop/ myprogram* myscript2* proj_files/
[[email protected] ~]#_
Similarly, the mv command can rename directories. If the source is the name of an existing
directory, it is renamed to whatever directory name is specified as the target.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Managing Files and Directories
139
The mv command works similarly to a cut-and-paste operation in which the file is copied to a
new directory and deleted from the source directory. In some cases, however, you might want
to keep the file in the source directory and instead insert a copy of the file in the target directory. You can do this using the cp (copy) command. Much like the mv command, the cp
command takes two arguments at minimum. The first argument specifies the source file/directory to be copied and the second argument specifies the target file/directory. If several files
need to be copied to a destination directory, simply specify several source arguments, with
the final argument on the command line serving as the target directory. Each argument can
be an absolute or relative pathname and can contain wildcards or the special metacharacters
“.” (which specifies the current directory) and “..” (which specifies the parent directory). For
example, to make a copy of the file /etc/hosts in the current directory (/root), you can specify
the absolute pathname to the /etc/hosts file (/etc/hosts) and the relative pathname indicating
the current directory ( . ):
[[email protected] ~]# cp /etc/hosts .
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts myprogram* myscript2* proj_files/
[[email protected] ~]#_
You can also make copies of files in the same directory. To make a copy of the hosts file
called hosts2 in the current directory and view the results, type the following commands:
[[email protected] ~]# cp hosts hosts2
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts hosts2 myprogram* myscript2* proj_files/
[[email protected] ~]#_
Despite their similarities, the mv and cp commands work on directories differently. The mv
command renames a directory, whereas the cp command creates a whole new copy of the
directory and its contents. To copy a directory full of files in Linux, you must tell the cp command that the copy will be recursive (involve files and subdirectories too) by using the –R
option. The following example demonstrates copying the proj_files directory and all of its
contents to the /home/user1 directory without and with the –R option:
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts myprogram* myscript2* proj_files/
[[email protected] ~]# ls -F /home/user1
Desktop/
[[email protected] ~]# cp proj_files /home/user1
cp: omitting directory ‘proj_files’
[[email protected] ~]# ls -F /home/user1
Desktop/
[[email protected] ~]# cp -R proj_files /home/user1
[[email protected] ~]# ls -F /home/user1
Desktop/ proj_files/
[[email protected] ~]#_
If the target is a file that exists, both the mv and cp commands warn the user that the target
file will be overwritten and will ask whether to continue. This is not a feature of the command
as normally invoked, but it is a feature of the default configuration in Fedora Linux because
the BASH shell in Fedora Linux contains aliases to the cp and mv commands.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
140
Chapter 4
Linux Filesystem Management
Aliases are special variables in memory that point to commands; they
are fully discussed in Chapter 7.
When you type mv, you are actually running the mv command with the –i option without
realizing it. If the target file already exists, both the mv command and the mv command with
the –i option interactively prompt the user to choose whether to overwrite the existing file.
Similarly, when you type the cp command, the cp –i command is actually run to prevent
the accidental overwriting of files. To see the aliases present in your current shell, simply type
alias, as shown in the following output:
[[email protected] ~]# alias
alias cp=‘cp -i’
alias egrep=‘egrep --color=auto’
alias fgrep=‘fgrep --color=auto’
alias grep=‘grep --color=auto’
alias l.=‘ls -d .* --color=auto’
alias ll=‘ls -l --color=auto’
alias ls=‘ls --color=auto’
alias mv=‘mv -i’
alias rm=‘rm -i’
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot
--show-tilde’
[[email protected] ~]#_
If you want to override this interactive option, which is known as interactive mode, use the –f
(force) option to override the choice, as shown in the following example. In this example,
the root user tries to rename the hosts file using the name “hosts2,” a name already assigned
to an existing file. The example shows the user attempting this task both without and with
the –f option to the mv command:
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts hosts2 myprogram* myscript2* proj_files/
[[email protected] ~]# mv hosts hosts2
mv: overwrite ‘hosts2’? n
[[email protected] ~]# mv -f hosts hosts2
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts2 myprogram* myscript2* proj_files/
[[email protected] ~]#_
Creating directories, copying, and moving files are file management tasks that preserve or create data on the hard disk. To remove files or directories, you must use either the rm command
or the rmdir command.
The rm (remove) command takes a list of arguments specifying the absolute or relative pathnames of files to remove. As with most commands, wildcards can be used to simplify the process of removing multiple files. After a file has been removed from the filesystem, it cannot be
recovered. As a result, the rm command is aliased in Fedora Linux to the rm command with
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Managing Files and Directories
141
the –i option, which interactively prompts the user to choose whether to continue with the
deletion. Like the cp and mv commands, the rm command accepts the –f option to override
this choice and immediately delete the file. An example demonstrating the use of the rm and
rm –f commands to remove the current and hosts2 files is shown here:
[[email protected] ~]# ls -F
[email protected] Desktop/ hosts2 myprogram* myscript2* proj_files/
[[email protected] ~]# rm current
rm: remove ‘current’? y
[[email protected] ~]# rm -f hosts2
[[email protected] ~]# ls -F
Desktop/ myprogram* myscript2* proj_files/
[[email protected] ~]# _
To remove a directory, you can use the rmdir (remove directory) command; however, the
rmdir command only removes a directory if it contains no files. To remove a directory and
the files inside, you must use the rm command and specify that a directory full of files should
be removed. As explained earlier in this chapter, you need to use the recursive option (–R)
with the cp command to copy directories; to remove a directory full of files, you can also
use a recursive option (–R) with the rm command. If, for example, the root user wants to
remove the proj_files subdirectory and all of the files within it without being prompted to confirm each file deletion, the command she must use is rm –Rf proj_files, as shown in the
following example:
[[email protected] ~]# ls -F
Desktop/ myprogram* myscript2* proj_files/
[[email protected] ~]# rmdir proj_files
rmdir: ‘proj_files’: Directory not empty
[[email protected] ~]# rm -Rf proj_files
[[email protected] ~]# ls -F
Desktop/ myprogram* myscript2*
[[email protected] ~]# _
In many commands, such as rm and cp, both the –r and the –R
options have the same meaning (recursive).
The –R option to the rm command is dangerous if you are not certain which files exist in the directory to be deleted recursively. As a
result, the –R option to the rm command is commonly referred to
as the -résumé option; if you use it incorrectly in a production
server environment, you might need to prepare your résumé.
It is important to note that the aforementioned file management commands are commonly
used by Linux users, developers, and administrators alike. Table 4-2 shows a summary of
these common file management commands.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
142
Chapter 4
Linux Filesystem Management
Command
Description
mkdir
Creates directories
rmdir
Removes empty directories
mv
Moves/renames files and directories
cp
Copies files and directories full of files (with the
alias
Displays BASH shell aliases
rm
Removes files and directories full of files (with the
–R option)
–R option)
Table 4-2 Common Linux file management commands
Finding Files
Before using the file management commands mentioned in the preceding section, you must know
the locations of the files involved. The fastest method to search for files in the Linux directory
tree is to use the locate command. For example, to view all of the files underneath the root
directory with the file name “inittab” or with “inittab” as part of the filename, you can simply
type locate inittab at a command prompt, which produces the following output:
[[email protected] ~]# locate inittab
/etc/inittab
/usr/share/vim/vim74/syntax/inittab.vim
[[email protected] ~]# _
The locate command looks in a premade database that contains a list of all the files on the system.
This database is indexed much like a textbook for fast searching, yet can become outdated as files
are added and removed from the system, which happens on a regular basis. As a result, the database
used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically
and can be updated manually by running the updatedb command at a command prompt.
You can configure the directories that are searched by the updatedb command by editing the
/etc/updatedb.conf file.
As the locate command searches all files on the filesystem, it returns too much information
to display on the screen. To make the output easier to read, you can use the more (or less)
command to pause the output, as in locate inittab | more. To prevent the problem
entirely, you can do more specific searches.
A slower yet more versatile method for locating files on the filesystem is to use the find command. The find command does not use a premade index of files; instead, it searches the directory
tree recursively, starting from a certain directory for files that meet a certain criterion. The format
of the find command is find <start directory> -criteria <what to find>
For example, to find any files named “inittab” underneath the /etc directory, you can use the
command find /etc –name inittab and receive the following output:
[[email protected] ~]# find /etc -name inittab
/etc/inittab
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Finding Files
143
You can also use wildcard metacharacters with the find command; however, these wildcards
must be protected from shell interpretation, as they must only be interpreted by the find command. To do this, ensure that any wildcard metacharacters are enclosed within quote characters.
An example of using the find command with wildcard metacharacters to find all files that start
with the letters “host” underneath the /etc directory is shown in the following output:
[[email protected] ~]# find /etc -name "host*"
/etc/hosts
/etc/hosts.allow
/etc/host.conf
/etc/selinux/targeted/modules/active/modules/hostname.pp
/etc/avahi/hosts
/etc/hostname
/etc/hosts.deny
[[email protected] ~]# _
Although searching by name is the most common criteria used with the find command, many
other criteria can be used with the find command as well. To find all files starting from the /var
directory that have a size greater than 4096K (kilobytes), you can use the following command:
[[email protected] ~]# find /var -size +4096k
/var/lib/rpm/Packages
/var/lib/rpm/Basenames
/var/lib/PackageKit/system.package-list
/var/log/journal/034ec8ccdf4642f7a2493195e11d7df6/user-1000.journal
/var/log/journal/034ec8ccdf4642f7a2493195e11d7df6/user-42.journal
/var/log/journal/034ec8ccdf4642f7a2493195e11d7df6/system.journal
/var/cache/yum/x86_64/20/updates/gen/prestodelta.xml
/var/cache/yum/x86_64/20/updates/gen/primary_db.sqlite
/var/cache/yum/x86_64/20/updates/gen/filelists_db.sqlite
/var/cache/yum/x86_64/20/updates/gen/updateinfo.xml
/var/cache/yum/x86_64/20/updates/gen/other_db.sqlite
/var/cache/dnf/x86_64/20/fedora-filenames.solvx
/var/cache/dnf/x86_64/20/fedora.solv
/var/cache/dnf/x86_64/20/updates-filenames.solvx
/var/tmp/kdecache-user1/plasma_theme_internal-system-colors.kcache
/var/tmp/kdecache-user1/plasma_theme_Heisenbug_v19.90.2.kcache
/var/tmp/kdecache-user1/icon-cache.kcache
[[email protected] ~]# _
As well, if you want to find all the directories only underneath the /boot directory, you can
type the following command:
[[email protected] ~]# find /boot -type d
/boot
/boot/lost+found
/boot/grub2
/boot/grub2/locale
/boot/grub2/themes
/boot/grub2/themes/system
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
144
Chapter 4
Linux Filesystem Management
/boot/grub2/i386-pc
/boot/grub2/fonts
/boot/extlinux
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/BOOT
/boot/efi/EFI/fedora
/boot/efi/EFI/fedora/fonts
/boot/efi/System
/boot/efi/System/Library
/boot/efi/System/Library/CoreServices
[[email protected] ~]# _
Table 4-3 provides a list of some common criteria used with the find command.
Although the find command can be used to search for files based on many criteria, it
might take several minutes to complete the search if the number of directories and files
being searched is large. To reduce the time needed to search, narrow down the directories
searched by specifying a subdirectory when possible. It takes less time to search the /usr/
local/bin directory and its subdirectories, compared to searching the /usr directory and all
of its subdirectories. As well, if the filename that you are searching for is an executable
file, that file can likely be found in less time using the which command. The which
command only searches directories that are listed in a special variable called the PATH
variable in the current BASH shell. Before exploring the which command, you must
understand the usage of PATH.
Executable files can be stored in directories scattered around the directory tree. Recall
from FHS that most executable files are stored in directories named bin or sbin, yet
there are over 20 bin and sbin directories scattered around the directory tree after a
typical Fedora Linux installation. To ensure that users do not need to specify the full
pathname to commands such as ls (which is the executable file /usr/bin/ls), there exists
a special variable called PATH that is placed into memory each time a user logs in to the
Linux system. Recall that you can see the contents of a certain variable in memory by
using the $ metacharacter with the echo command:
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]# _
The PATH variable lists directories that are searched for executable files if a relative or
absolute pathname was not specified when executing a command on the command line.
In the preceding output, when a user types the ls command on the command line and
presses Enter, the system recognizes that the command was not an absolute pathname
(e.g., /usr/bin/ls) or relative pathname (e.g., ../../usr/bin/ls) and then proceeds to look for
the ls executable file in the /usr/local/sbin directory, then the /usr/local/bin directory,
then the /usr/sbin directory, and so on. If all the directories in the PATH variable are
searched and no ls command is found, the shell gives an error message to the user stating that the command was not found. In the preceding output, the /usr/bin directory is in
the PATH variable and, thus, the ls command is found and executed, but not until the
previous directories in the PATH variable are searched first.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Finding Files
145
Criteria
Description
-amin -x
Searches for files that were accessed less than x minutes ago
-amin +x
Searches for files that were accessed more than x minutes ago
-atime -x
Searches for files that were accessed less than x days ago
-atime +x
Searches for files that were accessed more than x days ago
-empty
Searches for empty files or directories
-fstype x
Searches for files if they are on a certain filesystem x (where n could be ext2, ext3, and so on)
-group x
Searches for files that are owned by a certain group or GID (x)
-inum x
Searches for files that have an inode number of x
-mmin -x
Searches for files that were modified less than x minutes ago
-mmin +x
Searches for files that were modified more than x minutes ago
-mtime -x
Searches for files that were modified less than x days ago
-mtime +x
Searches for files that were modified more than x days ago
-name x
Searches for a certain filename x (x can contain wildcards)
-regex x
Searches for certain filenames using regular expressions instead of wildcard metacharacters
-size -x
Searches for files with a size less than x
-size x
Searches for files with a size of x
-size +x
Searches for files with a size greater than x
-type x
Searches for files of type x where x is:
• b for block files
• c for character files
• d for directory files
• p for named pipes
• f for regular files
• l for symbolic links (shortcuts)
• s for sockets
-user x
Searches for files owned by a certain user or UID (x)
Table 4-3 Common criteria used with the find command
To search the directories in the PATH variable for the file called “grep,” you could use the word
“grep” as an argument for the which command and receive the following output:
[[email protected] ~]# which grep
alias grep=‘grep --color=auto’
/usr/bin/grep
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
146
Chapter 4
Linux Filesystem Management
As shown in the previous output, the which command will also list any command aliases for
a particular command. In our example, the grep command has the path /usr/bin/grep, but it
also has an alias that ensures that each time the user runs the grep command, it runs it using
the - -color=auto option.
If the file being searched does not exist in the PATH variable directories, the which command
lets you know in which directories it was not found, as shown in the following output:
[[email protected] ~]# which grepper
/usr/bin/which: no grepper in
(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[[email protected] ~]# _
In addition to locate, find, and which, many different commands can be used to search for files on the filesystem. For example,
whereis is a simpler version of find.
Linking Files
Files can be linked to one another in two ways. In a symbolic link, or symlink, one file is a
pointer, or shortcut, to another file. In a hard link, two files share the same data.
To better understand how files are linked, you must understand how files are stored on a filesystem.
On a structural level, a filesystem has three main sections: superblock, inode table and data blocks.
The superblock is the section that contains information about the filesystem in general, such
as the number of inodes and data blocks, as well as how much data a data block stores, in
kilobytes. The inode table consists of several inodes (information nodes); each inode describes
one file or directory on the filesystem, and contains a unique inode number for identification.
What is more important, the inode stores information such as the file size, data block locations, last date modified, permissions, and ownership. When a file is deleted, only its inode
(which serves as a pointer to the actual data) is deleted. The data that makes up the contents
of the file as well as the filename are stored in data blocks, which are referenced by the inode.
In filesystem-neutral terminology, blocks are known as allocation units because they are the
unit by which disk space is allocated for storage.
Each file and directory must have an inode. All files except for special
device files also have data blocks associated with the inode. Special
device files are discussed in Chapter 5.
Recall that directories are simply files that are used to organize other
files; they too have an inode and data blocks, but their data blocks
contain a list of filenames that are located within the directory.
Hard-linked files share the same inode. As a result, they share the same inode number and
data blocks, but the data blocks allow for multiple filenames. Thus, when one hard-linked
file is modified, the other hard-linked files are updated as well. This relationship between
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Linking Files
FILE1:
INODE
FILE2:
Inode Number: 1204
147
DATA BLOCKS
Figure 4-1 The structure of hard-linked files
hard-linked files can be seen in Figure 4-1. You can hard-link a file an unlimited number of
times; however, the hard-linked files must reside on the same filesystem.
To create a hard link, you must use the ln (link) command and specify two arguments: the
existing file to hard-link and the target file that will be created as a hard link to the existing
file. Each argument can be the absolute or relative pathname to a file. Take, for example, the
following contents of the root user’s home directory:
[[email protected] ~]# ls -l
total 520
drwx-----3 root
root
-rwxr-xr-x
1 root
root
-rwxr-xr-x
1 root
root
[[email protected] ~]# _
4096 Apr 8 07:12 Desktop
519964 Apr 7 09:59 file1
1244 Apr 27 18:17 file3
Suppose you want to make a hard link to file1 and call the new hard link file2, as shown in
Figure 4-1. To accomplish this, you issue the command ln file1 file2 at the command
prompt; a file called file2 is created and hard-linked to file1. To view the hard-linked filenames after creation, you can use the ls –l command:
[[email protected] ~]# ln file1 file2
[[email protected] ~]# ls -l
total 1032
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-rwxr-xr-x
2 root
root
519964 Apr 7 09:59 file1
-rwxr-xr-x
2 root
root
519964 Apr 7 09:59 file2
-rwxr-xr-x
1 root
root
1244 Apr 27 18:17 file3
[[email protected] ~]# _
Notice from the preceding long listing that file1 and file2 share the same inode and data section, as they have the same size, permissions, ownership, modification date, and so on. Also
note that the link count (the number after the permission set) for file1 has increased from the
number one to the number two in the preceding output. A link count of one indicates that
only one inode is shared by the file. A file that is hard-linked to another file shares two inodes
and, thus, has a link count of two. Similarly, a file that is hard-linked to three other files
shares four inodes and, thus, has a link count of four.
Although hard links share the same inode and data section, deleting a hard-linked file does
not delete all the other hard-linked files; it simply removes one filename reference. Removing
a hard link can be achieved by removing one of the files, which then lowers the link count.
To view the inode number of hard-linked files to verify that they are identical, you can use the
–i option to the ls command in addition to any other options. The inode number is placed
on the left of the directory listing on each line, as shown in the following output:
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
148
Chapter 4
Linux Filesystem Management
[[email protected] ~]# ls -li
total 1032
37595 drwx-----3 root
1204 -rwxr-xr-x
2 root
1204 -rwxr-xr-x
2 root
17440 -rwxr-xr-x
1 root
[[email protected] ~]# _
root
root
root
root
4096 Apr 8 07:12 Desktop
519964 Apr 7 09:59 file1
519964 Apr 7 09:59 file2
1244 Apr 27 18:17 file3
Directory files are not normally hard-linked, as the result would consist
of two directories that contain the same contents. However, the root
user has the ability to hard-link directories in some cases, using the –
F or –d option to the ln command. Only directories that have files
regularly added and need to maintain identical file contents are typically hard-linked. For example, on Fedora 20, the /etc/init.d directory
is symbolically linked to the /etc/rc.d/init.d directory.
Symbolic links (shown in Figure 4-2) are different from hard links because they do not share
the same inode and data blocks with their target file; one is merely a pointer to the other, thus
both files have different sizes. The data blocks in a symbolically linked file contain only the
pathname to the target file. When a user edits a symbolically linked file, he is actually editing
the target file. Thus, if the target file is deleted, the symbolic link serves no function, as it
points to a nonexistent file.
Symbolic links are sometimes referred to as “soft links” or
“symlinks.”
To create a symbolic link, you use the –s option to the ln command. To create a symbolic
link to file3 called file4, as in Figure 4-2, you can type ln –s file3 file4 at the command prompt. As with hard links, the arguments specified can be absolute or relative
FILE3:
INODE
DATA BLOCKS
Inode Number: 17440
FILE4:
INODE
Inode Number: 19926
DATA
BLOCKS
Figure 4-2 The structure of symbolically linked files
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
149
pathnames. To view the symbolically linked filenames after creation, you can use the
ls –l command, as shown in the following example:
[[email protected] ~]# ln -s
[[email protected] ~]# ls -l
total 1032
drwx-----3 root
-rwxr-xr-x
2 root
-rwxr-xr-x
2 root
-rwxr-xr-x
1 root
lrwxrwxrwx
1 root
[[email protected] ~]# _
file3 file4
root
root
root
root
root
4096 Apr 8 07:12 Desktop
519964 Apr 7 09:59 file1
519964 Apr 7 09:59 file2
1244 Apr 27 18:17 file3
5 Apr 27 19:05 file4 -> file3
Notice from the preceding output that file4 does not share the same inode, because the permissions, size, and modification date are different from file3. In addition, symbolic links are easier
to identify than hard links; the file type character (before the permissions) is l, which indicates a
symbolic link, and the filename points to the target using an arrow. The ls –F command also
indicates symbolic links by appending an @ symbol, as shown in the following output:
[[email protected] ~]# ls -F
Desktop/ file1* file2*
[[email protected] ~]# _
file3*
[email protected]
Another difference between hard links and symbolic links is that symbolic links need not
reside on the same filesystem as their target. Instead, they point to the target filename and do
not require the same inode, as shown in the following output:
[[email protected] ~]# ls
total 1032
37595 drwx-----1204 -rwxr-xr-x
1204 -rwxr-xr-x
17440 -rwxr-xr-x
19926 lrwxrwxrwx
[[email protected] ~]# _
-li
3 root
2 root
2 root
1 root
1 root
root
root
root
root
root
4096 Apr 8 07:12 Desktop
519964 Apr 7 09:59 file1
519964 Apr 7 09:59 file2
1244 Apr 27 18:17 file3
5 Apr 27 19:05 file4 -> file3
Unlike hard links, symbolic links are commonly made to directories to
simplify navigating the filesystem tree. Also, symbolic links made to
directories are typically used to maintain compatibility with other UNIX
and Linux systems. For example, on Fedora 20, the /usr/tmp directory
is symbolically linked to the /var/tmp directory for this reason.
File and Directory Permissions
Recall that all users must log in with a user name and password to gain access to a Linux system. After logging in, a user is identified by her user name and group memberships; all access
to resources depends on whether the user name and group memberships have the required
permission. Thus, a firm understanding of ownership and permissions is necessary to operate
a Linux system in a secure manner and to prevent unauthorized users from having access to
sensitive files, directories, and commands.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
150
Chapter 4
Linux Filesystem Management
File and Directory Ownership
When a user creates a file or directory, that user’s name and primary group become the owner
and group owner of the file, respectively. This affects the permission structure, as you see in the
next section; however, it also determines who has the ability to modify file and directory permissions and ownership. Only two users on a Linux system can modify permissions on a file
or directory or change its ownership: the owner of the file or directory and the root user.
To view your current user name, you can use the whoami command. To view your group
memberships and primary group, you can use the groups command. An example of these
two commands when logged in as the root user is shown in the following output:
[[email protected] ~]# whoami
root
[[email protected] ~]# groups
root bin daemon sys adm disk wheel
[[email protected] ~]# _
Notice from the preceding output that the root user is a member of seven groups, yet the root
user’s primary group is also called “root,” as it is the first group mentioned in the output of
the group’s command.
On Fedora 20, the root user is only a member of one group by
default (the “root” group).
If the root user creates a file, the owner is “root” and the group owner is also “root.” To
quickly create an empty file, you can use the touch command:
[[email protected]
[[email protected]
total 4
drwx------rw-r--r-[[email protected]
~]# touch file1
~]# ls -l
3 root
1 root
~]# _
root
root
4096 Apr 8 07:12 Desktop
0 Apr 29 15:40 file1
Notice from the preceding output that the owner of file1 is “root,” and the group owner is
the “root” group. To change the ownership of a file or directory, you can use the chown
(change owner) command, which takes two arguments at minimum: the new owner and the
files or directories to change. Both arguments can be absolute or relative pathnames, and you
can also change permissions recursively throughout the directory tree using the –R option to
the chown command. To change the ownership of file1 to the user user1 and the ownership
of the directory Desktop and all of its contents to user1 as well, you can enter the following
commands:
[[email protected]
[[email protected]
[[email protected]
total 4
drwx------rw-r--r--
~]# chown user1 file1
~]# chown -R user1 Desktop
~]# ls -l
3 user1
1 user1
root
root
4096 Apr 8 07:12 Desktop
0 Apr 29 15:40 file1
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
151
[[email protected] ~]# ls -l Desktop
total 16
-rw-----1 user1
root
163 Mar 29 09:58 Floppy
-rw-r--r-1 user1
root
3578 Mar 29 09:58 Home
-rw-r--r-1 user1
root
1791 Mar 29 09:58 Start Here
drwx-----2 user1
root
4096 Mar 29 09:58 Trash
[[email protected] ~]# _
Recall that the owner of a file or directory and the root user have the ability to change ownership of a particular file or directory. If a regular user changes the ownership of a file or
directory that he owns, that user cannot gain back the ownership. Instead, the new owner
of that file or directory must change it to the original user. However, the root user always
has the ability to regain the ownership:
[[email protected] ~]# chown root file1
[[email protected] ~]# chown -R root Desktop
[[email protected] ~]# ls -l
total 4
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-rw-r--r-1 root
root
0 Apr 29 15:40 file1
[[email protected] ~]# ls -l Desktop
total 16
-rw-----1 root
root
163 Mar 29 09:58 Floppy
-rw-r--r-1 root
root
3578 Mar 29 09:58 Home
-rw-r--r-1 root
root
1791 Mar 29 09:58 Start Here
drwx-----2 root
root
4096 Mar 29 09:58 Trash
[[email protected] ~]# _
Just as the chown (change owner) command can be used to change the owner of a file or
directory, you can use the chgrp (change group) command to change the group owner of a
file or directory. The chgrp command takes two arguments at minimum: the new group
owner and the files or directories to change. As with the chown command, the chgrp command also accepts the –R option to change group ownership recursively throughout the
directory tree. To change the group owner of file1 and the Desktop directory recursively
throughout the directory tree, you can execute the following commands:
[[email protected] ~]# chgrp sys file1
[[email protected] ~]# chgrp -R sys Desktop
[[email protected] ~]# ls -l
total 4
drwx-----3 root
sys
4096 Apr 8 07:12 Desktop
-rw-r--r-1 root
sys
0 Apr 29 15:40 file1
[[email protected] ~]# ls -l Desktop
total 16
-rw------1 root
sys
163 Mar 29 09:58 Floppy
-rw-r--r-1 root
sys
3578 Mar 29 09:58 Home
-rw-r--r-1 root
sys
1791 Mar 29 09:58 Start Here
drwx-----2 root
sys
4096 Mar 29 09:58 Trash
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
152
Chapter 4
Linux Filesystem Management
Regular users can change the group of a file or directory only to a
group to which they belong.
Normally, you change both the ownership and group ownership on a file when that file
needs to be maintained by someone else. As a result, you can change both the owner and
the group owner at the same time using the chown command. To change the owner to
user1 and the group owner to root for file1 and the directory Desktop recursively, you can
enter the following commands:
[[email protected]
[[email protected]
[[email protected]
total 4
drwx------rw-r--r-[[email protected]
total 16
-rw-------rw-r--r--rw-r--r-drwx-----[[email protected]
~]# chown user1.root file1
~]# chown -R user1.root Desktop
~]# ls -l
3 user1
root
1 user1
root
~]# ls -l Desktop
4096 Apr 8 07:12 Desktop
0 Apr 29 15:40 file1
1 user1
1 user1
1 user1
2 user1
~]# _
163 Mar 29 09:58 Floppy
3578 Mar 29 09:58 Home
1791 Mar 29 09:58 Start Here
4096 Mar 29 09:58 Trash
root
root
root
root
Note that there must be no spaces before and after the . character in the chown commands
shown in the preceding output.
You can also use the : character instead of the . character in the
chown command to change both the owner and group ownership
(e.g., chown –R user1:root Desktop).
To protect your system’s security, you should ensure that most files residing in a user’s home
directory are owned by that user; some files in a user’s home directory (especially the hidden
files and directories) require this to function properly. To change the ownership back to the
root user for file1 and the Desktop directory to avoid future problems, you can type the
following:
[[email protected]
[[email protected]
[[email protected]
total 4
drwx------rw-r--r-[[email protected]
total 16
-rw-------rw-r--r--
~]# chown root.root file1
~]# chown -R root.root Desktop
~]# ls -l
3 root
root
4096 Apr 8 07:12 Desktop
1 root
root
0 Apr 29 15:40 file1
root]# ls -l Desktop
1 root
1 root
root
root
163 Mar 29 09:58 Floppy
3578 Mar 29 09:58 Home
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
-rw-r--r-1 root
drwx------ 2 root
[[email protected] ~]# _
root
root
153
1791 Mar 29 09:58 Start Here
4096 Mar 29 09:58 Trash
You can override who is allowed to change ownership and permissions using a system setting. Many Linux distributions, including
Fedora 20 Linux, use this system setting by default to restrict regular
(non-root) users from changing the ownership and group ownership
of files and directories. This prevents these users from bypassing disk
quota restrictions, which rely on the ownership of files and directories to function properly. Disk quotas are discussed in Chapter 5.
Managing File and Directory Permissions
Every file and directory file on a Linux filesystem contains information regarding permissions
in its inode. The section of the inode that stores permissions is called the mode of the file and
is divided into three sections based on the user(s) who receive(s) the permissions to that file
or directory:
•
User (owner) permissions
•
Group (group owner) permissions
•
Other (everyone else on the Linux system) permissions
Furthermore, you can assign to each of these users the following regular permissions:
•
Read
•
Write
•
Execute
Interpreting the Mode Recall that the three sections of the mode and the permissions
that you can assign to each section are viewed when you perform an ls –l command; a
detailed depiction of this is shown in Figure 4-3. It is important to note that the root user
supersedes all file and directory permissions; in other words, the root user has all permissions
to every file and directory regardless of what the mode of the file or directory indicates.
Read Write Execute
User
Group
Other
Mode
Figure 4-3 The structure of a mode
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
154
Chapter 4
Linux Filesystem Management
Consider the root user’s home directory listing, shown in the following example:
[[email protected] ~]# ls -l
total 28
drwx-----3 root
-r---w---x
1 bob
-------rwx
1 root
-rwxrwxrwx
1 root
---------1 root
-rw-r--r-1 root
-rw-r--r-1 user1
[[email protected] ~]# _
root
proj
root
root
root
root
sys
4096 Apr 8 07:12 Desktop
282 Apr 29 22:06 file1
282 Apr 29 22:06 file2
282 Apr 29 22:06 file3
282 Apr 29 22:06 file4
282 Apr 29 22:06 file5
282 Apr 29 22:06 file6
Note from the preceding output that all permissions (as shown in Figure 4-3) need not be on a
file or directory; if the permission is unavailable, a dash (the hyphen character on your keyboard, - ) replaces its position in the mode. Be certain not to confuse the character to the left
of the mode (which determines the file type) with the mode, as it is unrelated to the permissions on the file or directory. From the preceding output, the Desktop directory gives the user
or owner of the directory (the root user) read, write, and execute permission, yet members of
the group (the root group) do not receive any permissions to the directory. Note that other
(everyone on the system) does not receive permissions to this directory either.
Permissions are not additive; the system assigns the first set of permissions that are matched
in the mode order: user, group, other. Let us assume that the bob user is a member of the
proj group. In this case, the file called file1 in the preceding output gives the user or owner
of the file (the bob user) read permission, gives members of the group (the proj group) write
permission, and gives other (everyone else on the system) execute permission only. Because
permissions are not additive, the bob user will only receive read to file1 from the system.
Linux permissions should not be assigned to other only. Although file2 in our example does
not give the user or group any permissions, all other users receive read, write, and execute
permission via the other category. Thus, file2 should not contain sensitive data because
many users have full access to it. For the same reason, it is bad form to assign all permissions to a file that contains sensitive data, as shown with file3 in the preceding example.
On the contrary, it is also possible to have a file that has no permissions assigned to it, as
shown in the preceding example with respect to file4. In this case, the only user who has
permissions to the file is the root user.
The permission structure that you choose for a file or directory might result in too few or
too many permissions. You can follow some general guidelines to avoid these situations.
The owner of a file or directory is typically the person who maintains it; members of the
group are typically users in the same company department and must have limited access to
the file or directory. As a result, most files and directories that you find on a Linux filesystem have more permissions assigned to the user of the file/directory than to the group of
the file/directory, and the other category has either the same permissions or less than the
group of the file/directory, depending on how private that file or directory is. The file file5
in the previous output depicts this common permission structure. In addition, files in a
user’s home directory are typically owned by that user; however, you might occasionally
find files that are not. For these files, their permission definition changes, as shown in the
previous example with respect to file1 and file6. The user or owner of file6 is user1, who
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
155
has read and write permissions to the file. The group owner of file6 is the sys group; thus,
any members of the sys group have read permission to the file. Finally, everyone on the system receives read permission to the file via the other category. Regardless of the mode, the
root user receives all permissions to this file.
Interpreting Permissions After you understand how to identify the permissions that
are applied to user, group, and other on a certain file or directory, you can then interpret
the function of those permissions. Permissions for files are interpreted differently from those
for directories. Also, if a user has a certain permission on a directory, that user does not
have the same permission for all files or subdirectories within that directory; file and directory permissions are treated separately by the Linux system. Table 4-4 shows a summary of
the different permissions and their definitions.
The implications of the permission definitions described in Table 4-4 are important to
understand. If a user has the read permission to a text file, that user can use, among others,
the cat, more, head, tail, less, strings, and od commands to view its contents. That
same user can also open that file with a text editor such as vi; however, the user does not
have the ability to save any changes to the document unless that user has the write permission to the file as well.
Recall from earlier that some text files contain instructions for the shell to execute and are
called shell scripts. Shell scripts can be executed in much the same way that binary compiled
programs are; the user who executes the shell script must then have execute permission to
that file to execute it as a program.
It is important to avoid giving execute permission to files that are not
programs or shell scripts. This ensures that these files will not be executed accidentally, causing the shell to interpret the contents.
Remember that directories are simply special files that have an inode and a data section but
what the data section contains is a list of that directory’s contents. If you want to read that
list (using the ls command for example), then you require the read permission to the directory. To modify that list, by adding or removing files, you require the write permission to
the directory. Thus, if you want to create a new file in a directory with a text editor such
as vi, you must have the write permission to that directory. Similarly, when a source file is
copied to a target directory with the cp command, a new file is created in the target
Permission
Definition for Files
Definition for Directories
Read
Allows a user to open and read
the contents of a file
Allows a user to list the contents of the directory
(if she has also been given execute permission)
Write
Allows a user to open, read, and
edit the contents of a file
Allows a user to add or remove files to and from
the directory (if she has also been given execute
permission)
Execute
Allows a user to execute the file in
memory (if it is a program file or script)
Allows a user to enter the directory and work with
directory contents
Table 4-4 Linux permissions
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
156
Chapter 4
Linux Filesystem Management
directory and you must have the write permission to the target directory for the copy to be
successful. Conversely, to delete a certain file, you must have the write permission to the
directory that contains that file. It is also important to note that a user who has the write
permission to a directory has the ability to delete all files and subdirectories within it.
The execute permission on a directory is sometimes referred to as the search permission, and
it works similarly to a light switch. When a light switch is turned on, you can navigate a
room and use the objects within it. However, when a light switch is turned off, you cannot
see the objects in the room, nor can you walk around and view them. A user who does not
have the execute permission to a directory is prevented from listing the directory’s contents,
adding and removing files, and working with files and subdirectories inside that directory,
regardless of what permissions the user has to them. In short, a quick way to deny a user
from accessing a directory and all of its contents in Linux is to take away the execute permission on that directory. Because the execute permission on a directory is crucial for user
access, it is commonly given to all users via the other category, unless the directory must be
private.
Changing Permissions To change the permissions for a certain file or directory, you
can use the chmod (change mode) command. The chmod command takes two arguments at
minimum; the first argument specifies the criteria used to change the permissions (Table 4-5),
and the remaining arguments indicate the filenames to change.
Take, for example, the directory list used earlier:
[[email protected] ~]# ls -l
total 28
drwx------ 3 root
root
-r---w---x 1 bob
proj
-------rwx 1 root
root
-rwxrwxrwx
1 root
root
---------- 1 root
root
-rw-r--r-- 1 root
root
-rw-r--r-- 1 user1 sys
[[email protected] ~]# _
4096 Apr 8 07:12 Desktop
282 Apr 29 22:06 file1
282 Apr 29 22:06 file2
282 Apr 29 22:06 file3
282 Apr 29 22:06 file4
282 Apr 29 22:06 file5
282 Apr 29 22:06 file6
To change the mode of file1 to rw-r- -r--, you must add the write permission to the user of
the file, add the read permission and take away the write permission for the group of the
file, and add the read permission and take away the execute permission for other.
Category
Operation
Permission
u (user)
+ (adds a permission)
r (read)
g (group)
- (removes a permission)
w (write)
o (other)
= (makes a permission equal to)
x (execute)
a (all categories)
Table 4-5 Criteria used within the chmod command
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
157
From the information listed in Table 4-5, you can use the following command:
[[email protected] ~]# chmod u+w,g+r-w,o+r-x file1
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-rw-r--r-1 bob
proj
282 Apr 29 22:06 file1
----r--rwx
1 root
root
282 Apr 29 22:06 file2
-rwxrwxrwx
1 root
root
282 Apr 29 22:06 file3
---------1 root
root
282 Apr 29 22:06 file4
-rw-r--r-1 root
root
282 Apr 29 22:06 file5
-rw-r--r-1 user1 sys
282 Apr 29 22:06 file6
[[email protected] ~]# _
You should ensure that there are no spaces between any criteria
used in the chmod command because all criteria make up the first
argument only.
You can also use the = criteria from Table 4-5 to specify the exact permissions to change.
To change the mode on file2 in the preceding output to the same as file1 (rw-r- -r--), you
can use the following chmod command:
[[email protected] ~]# chmod u=rw,g=r,o=r file2
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-rw-r--r-1 bob
proj
282 Apr 29 22:06 file1
-rw-r--r-1 root
root
282 Apr 29 22:06 file2
-rwxrwxrwx
1 root
root
282 Apr 29 22:06 file3
---------1 root
root
282 Apr 29 22:06 file4
-rw-r--r-1 root
root
282 Apr 29 22:06 file5
-rw-r--r-1 user1 sys
282 Apr 29 22:06 file6
[[email protected] ~]# _
If the permissions to be changed are identical for the user, group, and other categories, you
can use the “a” character to refer to all categories, as shown in Table 4-5 and in the following example, when adding the execute permission to user, group, and other for file1:
[[email protected] ~]# chmod a+x file1
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
-rwxr-xr-x
1 bob
proj
-rw-r--r-1 root
root
-rwxrwxrwx
1 root
root
---------1 root
root
-rw-r--r-1 root
root
-rw-r--r-1 user1 sys
[[email protected] ~]# _
4096 Apr 8 07:12 Desktop
282 Apr 29 22:06 file1
282 Apr 29 22:06 file2
282 Apr 29 22:06 file3
282 Apr 29 22:06 file4
282 Apr 29 22:06 file5
282 Apr 29 22:06 file6
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
158
Chapter 4
Linux Filesystem Management
However, if there is no character specifying the category of user to affect, all users are
assumed, as shown in the following example when adding the execute permission to user,
group, and other for file2:
[[email protected]
[[email protected]
total 28
drwx------rwxr-xr-x
-rwxr-xr-x
-rwxrwxrwx
----------rw-r--r--rw-r--r-[[email protected]
~]# chmod +x file2
~]# ls -l
3 root
1 bob
1 root
1 root
1 root
1 root
1 user1
~]# _
root
proj
root
root
root
root
sys
4096 Apr 8 07:12 Desktop
282 Apr 29 22:06 file1
282 Apr 29 22:06 file2
282 Apr 29 22:06 file3
282 Apr 29 22:06 file4
282 Apr 29 22:06 file5
282 Apr 29 22:06 file6
All of the aforementioned chmod examples use the symbols listed in Table 4-5 as the criteria
used to change the permissions on a file or directory. You might instead choose to use
numeric criteria with the chmod command to change permissions. All permissions are stored
in the inode of a file or directory as binary powers of two:
• read = 22 = 4
• write = 21 = 2
• execute = 20 = 1
Thus, the mode of a file or directory can be represented using the numbers 421421421
instead of rwxrwxrwx. Because permissions are grouped into the categories user, group,
and other, you can then simplify this further by using only three numbers, one for each category that represents the sum of the permissions, as depicted in Figure 4-4.
Similarly, to represent the mode rw-r--r--, you can use the numbers 644 because user has read
and write (4 þ 2 = 6), group has read (4), and other has read (4). The mode rwxr-x--- can
also be represented by 750 because user has read, write, and execute (4 þ 2 þ 1 = 7), group
has read and execute (4 þ 1 = 5), and other has nothing (0). Table 4-6 provides a list of the
different permissions and their corresponding numbers.
To change the mode of the file1 file used earlier to r-xr- - -- -, you can use the command
chmod 540 file1, as shown in the following example:
4
2
User
7
1
4
2
1
4
Group
7
2
1
Other
7
4+2+1=7
Figure 4-4 Numeric representation of the mode
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
Mode (One Section Only)
Corresponding Number
rwx
4þ2þ1=7
rw-
4þ2=6
r-x
4þ1=5
r- -
4
-wx
2þ1=3
-w-
2
- -x
1
---
0
159
Table 4-6 Numeric representations of the permissions in a mode
[[email protected] ~]# chmod 540 file1
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-r-xr----1 bob
proj
282 Apr 29 22:06 file1
-rwxr-xr-x
1 root
root
282 Apr 29 22:06 file2
-rwxrwxrwx
1 root
root
282 Apr 29 22:06 file3
---------1 root
root
282 Apr 29 22:06 file4
-rw-r--r-1 root
root
282 Apr 29 22:06 file5
-rw-r--r-1 user1 sys
282 Apr 29 22:06 file6
[[email protected] ~]# _
Similarly, to change the mode of all files in the directory that start with the word “file” to
644 (which is common permissions for files), you can use the following command:
[[email protected] ~]# chmod 644 file*
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
4096 Apr 8 07:12 Desktop
-rw-r--r-1 bob
proj
282 Apr 29 22:06 file1
-rw-r--r-1 root
root
282 Apr 29 22:06 file2
-rw-r--r-1 root
root
282 Apr 29 22:06 file3
-rw-r--r-1 root
root
282 Apr 29 22:06 file4
-rw-r--r-1 root
root
282 Apr 29 22:06 file5
-rw-r--r-1 user1 sys
282 Apr 29 22:06 file6
[[email protected] ~]# _
Like the chown and chgrp commands, the chmod command can be used to change the permission on a directory and all of its contents recursively by using the –R option, as shown in
the following example when changing the mode of the Desktop directory:
[[email protected] ~]# chmod –R 755 Desktop
[[email protected] ~]# ls -l
total 28
drwxr-xr-x
3 root
root
4096 Apr 8 07:12 Desktop
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
160
Chapter 4
Linux Filesystem Management
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r-[[email protected]
total 16
-rwxr-xr-x
-rw-r-xr-x
-rw-r-xr-x
drwxr-xr-x
[[email protected]
1 bob
proj
1 root
root
1 root
root
1 root
root
1 root
root
1 user1 sys
~]# ls -l Desktop
1 root
1 root
1 root
2 root
~]# _
root
root
root
root
282 Apr 29 22:06 file1
282 Apr 29 22:06 file2
282 Apr 29 22:06 file3
282 Apr 29 22:06 file4
282 Apr 29 22:06 file5
282 Apr 29 22:06 file6
163 Mar 29 09:58 Floppy
3578 Mar 29 09:58 Home
1791 Mar 29 09:58 Start Here
4096 Mar 29 09:58 Trash
Default Permissions
Recall that permissions provide security for files and directories by allowing only certain
users access and that there are common guidelines for setting permissions on files and directories so that permissions are not too strict or too permissive. Also important to maintaining
security are the permissions that are given to new files and directories after they are created.
New files are given rw-rw-rw- by the system when they are created (because execute should
not be given unless necessary), and new directories are given rwxrwxrwx by the system
when they are created. These default permissions are too permissive for most files, as they
allow other full access to directories and nearly full access to files. Hence, a special variable
on the system called the umask (user mask) takes away permissions on new files and directories immediately after they are created. The most common umask that you will find is
022, which specifies that nothing (0) is taken away from the user, write permission (2) is
taken away from members of the group, and write permission (2) is taken away from other
on new files and directories when they are first created and given permissions by the system.
Keep in mind that the umask applies only to newly created files and
directories; it is never used to modify the permissions of existing files
and directories. You must use the chmod command to modify existing permissions.
An example of how a umask of 022 can be used to alter the permissions of a new file or
directory after creation is shown in Figure 4-5.
New Files
New Directories
rw-rw-rw-
rwxrwxrwx
- umask
0 2 2
0 2 2
= resulting permissions
rw-r--r--
rwxr-xr-x
Permissions assigned by system
Figure 4-5 Performing a umask 022 calculation
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
161
To verify the umask used, you can use the umask command and note the final three digits in
the output. To ensure that the umask functions as shown in Figure 4-5, simply create a new
file using the touch command and a new directory using the mkdir command, as shown in
the following output:
[[email protected] ~]# ls -l
total 28
drwx-----3 root
root
[[email protected] ~]# umask
0022
[[email protected] ~]# mkdir dir1
[[email protected] ~]# touch file1
[[email protected] ~]# ls -l
total 8
drwx-----3 root
root
drwxr-xr-x
2 root
root
-rw-r--r-1 root
root
[[email protected] ~]# _
4096 Apr 8 07:12 Desktop
4
4096 Apr 8 07:12 Desktop
4096 May 3 21:39 dir1
0 May 3 21:40 file1
Because the umask is a variable stored in memory, it can be changed. To change the current
umask, you can specify the new umask as an argument to the umask command. Suppose, for
example, you want to change the umask to 007; the resulting permissions on new files and
directories is calculated in Figure 4-6.
To change the umask to 007 and view its effect, you can type the following commands on
the command line:
[[email protected] ~]# ls -l
total 8
drwx-----3 root
root
drwxr-xr-x
2 root
root
-rw-r--r-1 root
root
[[email protected] ~]# umask 007
[[email protected] ~]# umask
0007
[[email protected] ~]# mkdir dir2
[[email protected] ~]# touch file2
Permissions assigned by system
- umask
= resulting permissions
4096 Apr 8 07:12 Desktop
4096 May 3 21:39 dir1
0 May 3 21:40 file1
New Files
New Directories
rw-rw-rw-
rwxrwxrwx
0 0 7
0 0 7
rw-rw----
rwxrwx---
Figure 4-6 Performing a umask 007 calculation
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
162
Chapter 4
Linux Filesystem Management
[[email protected]
total 12
drwx-----drwxr-xr-x
drwxrwx---rw-r--r--rw-rw---[[email protected]
~]# ls -l
3 root
2 root
2 root
1 root
1 root
~]# _
root
root
root
root
root
4096 Apr
4096 May
4096 May
0 May
0 May
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
Special Permissions
Read, write, and execute are the regular file permissions that you would use to assign security
to files; however, you can optionally use three more special permissions on files and directories:
• SUID (Set User ID)
• SGID (Set Group ID)
• Sticky bit
Defining Special Permissions The SUID has no special function when set on a
directory; however, if the SUID is set on a file and that file is executed, the person who
executed the file temporarily becomes the owner of the file while it is executing. Many
commands on a typical Linux system have this special permission set; the passwd command (/usr/bin/passwd) that is used to change your password is one such file. Because this
file is owned by the root user, when a regular user executes the passwd command to
change his own password, that user temporarily becomes the root user while the passwd
command is executing in memory. This ensures that any user can change his own password since the default setting on Linux systems only allows the root user to change passwords. Furthermore, the SUID can only be applied to binary compiled programs. The
Linux kernel does not let you apply the SUID to a shell script because shell scripts are
easy to edit and thus pose a security hazard to the system.
Contrary to the SUID, the SGID has a function when applied to both files and directories.
Just as the SUID allows regular users to execute a binary compiled program and become
the owner of the file for the duration of execution, the SGID allows regular users to execute a binary compiled program and become a member of the group that is attached to
the file. Thus, if a file is owned by the group “sys” and also has the SGID permission,
any user who executes that file will be a member of the group “sys” during execution. If
a command or file requires the user executing it to have the same permissions applied to
the sys group, setting the SGID on the file simplifies assigning rights to the file for user
execution.
The SGID also has a special function when placed on a directory. When a user creates a file,
recall that that user’s name and primary group become the owner and group owner of the
file, respectively. However, if a user creates a file in a directory that has the SGID permission set, that user’s name becomes the owner of the file and the directory’s group becomes
the group owner of the file.
Finally, the sticky bit was used on files in the past to lock them in memory; however, today
the sticky bit performs a useful function only on directories. As explained earlier in this
chapter, the write permission applied to a directory allows you to add and remove any file
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
163
to and from that directory. Thus, if you have the write permission to a certain directory but
no permission to files within it, you could delete all of those files. Consider a company that
requires a common directory that gives all employees the ability to add files; this directory
must give everyone the write permission. Unfortunately, the write permission also gives all
employees the ability to delete all files and directories within, including the ones that others
have added to the directory. If the sticky bit is applied to this common directory in addition
to the write permission, employees can add files to the directory but only delete those files
that they have added and not others.
Note that all special permissions also require the execute permission
to work properly; the SUID and SGID work on executable files, and
the SGID and sticky bit work on directories (which must have execute permission for access).
Setting Special Permissions The mode of a file that is displayed using the ls –l
command does not have a section for special permissions. However, because special permissions require execute, they mask the execute permission when displayed using the ls –l
command, as shown in Figure 4-7.
The system allows you to set special permissions even if the file or directory does not have
execute permission. However, the special permissions will not perform their function. If the
special permissions are set on a file or directory without execute permissions, then the ineffective special permissions are capitalized as seen in Figure 4-8.
To set the special permissions, you can visualize them to the left of the mode, as shown in
Figure 4-9.
Thus, to set all of the special permissions on a certain file or directory, you can use the command chmod 7777 name, as indicated from Figure 4-9. However, the SUID and SGID bits
are typically set on files. To change the permissions on the file1 file used earlier such that
SUID
SGID
Sticky
Bit
Figure 4-7 Representing special permissions in the mode
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
164
Chapter 4
Linux Filesystem Management
SUID
Sticky
Bit
SGID
Figure 4-8 Representing special permission in the absence of the execute permission
1
Sticky Bit
SUID
2
SGID
4
4
Special
7
2
User
7
1
4
2
1
Group
7
4
2
1
Other
7
Figure 4-9 Numeric representation of regular and special permissions
other has the ability to view and execute the file as the owner and a member of the group,
you can use the command chmod 6755 file1, as shown in the following example:
[[email protected]
total 12
drwx-----drwxr-xr-x
drwx------rw-r--r--rw------[[email protected]
[[email protected]
total 12
drwx-----drwxr-xr-x
drwx------rwsr-sr-x
-rw------[[email protected]
~]# ls -l
3 root
2 root
2 root
1 root
1 root
~]# chmod
~]# ls -l
root
4096 Apr 8 07:12 Desktop
root
4096 May 3 21:39 dir1
root
4096 May 3 21:41 dir2
root
0 May 3 21:40 file1
root
0 May 3 21:41 file2
6755 file1
3 root
2 root
2 root
1 root
1 root
~]# _
root
root
root
root
root
4096 Apr
4096 May
4096 May
0 May
0 May
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
Similarly, to set the sticky bit permission on the directory dir1 used earlier, you can use the command chmod 1777 dir1, which allows all users (including other) to add files to the dir1 directory. This is because you gave the write permission; however, users can only delete the files that
they own in dir1 because you set the sticky bit. This is shown in the following example:
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
File and Directory Permissions
[[email protected] ~]# ls -l
total 12
drwx-----3 root
root
drwxr-xr-x
2 root
root
drwx-----2 root
root
-rwsr-sr-x
1 root
root
-rw------1 root
root
[[email protected] ~]# chmod 1777 dir1
[[email protected] ~]# ls -l
total 12
drwx-----3 root
root
drwxrwxrwt
2 root
root
drwx-----2 root
root
-rwsr-sr-x
1 root
root
-rw------1 root
root
[[email protected] ~]# _
4096 Apr
4096 May
4096 May
0 May
0 May
4096 Apr
4096 May
4096 May
0 May
0 May
165
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
Also, remember that assigning special permissions without execute renders those permissions
useless. For example, you may forget to give execute permission to either user, group, or
other, and the long listing covers the execute permission with a special permission. In that
case, the special permission is capitalized, as shown in the following example when dir2 is
not given execute underneath the position in the mode that indicates the sticky bit (t):
[[email protected] ~]# ls -l
total 12
drwx-----3 root
root
drwxrwxrwt
2 root
root
drwx-----2 root
root
-rwsr-sr-x
1 root
root
-rw------1 root
root
[[email protected] ~]# chmod 1770 dir2
[[email protected] ~]# ls -l
total 12
drwx-----3 root
root
drwxrwxrwt
2 root
root
drwxrwx--T
2 root
root
-rwsr-sr-x
1 root
root
-rw------1 root
root
[[email protected] ~]# _
4096 Apr
4096 May
4096 May
0 May
0 May
4096 Apr
4096 May
4096 May
0 May
0 May
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
8 07:12 Desktop
3 21:39 dir1
3 21:41 dir2
3 21:40 file1
3 21:41 file2
Setting Custom Permissions in the Access Control List (ACL)
An access control list (ACL) is a list of users or groups that you can assign permissions to.
As discussed earlier, the default ACL used in Linux consists of three entities: user, group,
and other. However, there may be situations where you need to assign a specific set of permissions on a file or directory to an individual user or group.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
166
Chapter 4
Linux Filesystem Management
Take, for example, the file doc1:
[[email protected] ~]# ls –l doc1
-rw-rw---1 user1
acctg
[[email protected] ~]# _
0 May 2 22:01 doc1
The owner of the file (user1) has read and write permission, the group (acctg) has read and
write permission, and everyone else has no access to the file.
Now imagine that you need to give read permission to the bob user without giving permissions to anyone else. The solution to this problem is to modify the ACL on the doc1 file and
add a special entry for bob only. This can be accomplished by using the following setfacl
(set file ACL) command:
[[email protected] ~]# setfacl -m u:bob:r-- doc1
[[email protected] ~]# _
The –m option in the command above modifies the ACL, and you can use g instead of u to
add an additional group to the ACL.
Now, when you perform a long listing of the file doc1, you will see a + symbol next to the
mode to indicate that there are additional entries in the ACL for this file. To see these additional members, simply use the getfacl (get file ACL) command:
[[email protected] ~]# ls –l doc1
-rw-rw----+
1 user1
acctg
[[email protected] ~]# getfacl doc1
# file: doc1
# owner: user1
# group: acctg
user::rwuser:bob:r-group::rwmask::rwother::--[[email protected] ~]# _
0 May 2 22:01 doc1
After running the getfacl command, you will notice an extra node in the output: the mask.
The mask is compared to all additional user and group permissions in the ACL. If the mask
is more restrictive, it takes precedence when it comes to permissions. For example, if the
mask is set to r-- and the user bob has rw-, then the user bob actually gets r-- to the
file. When you run the setfacl command, the mask is always made equal to the least
restrictive permission assigned so that it does not affect additional ACL entries. The mask
was created as a mechanism that could easily revoke permissions on a file that had several
additional users and groups added to the ACL.
To remove all extra ACL assignments on the doc1 file, simply use the –b option to the
setfacl command:
[[email protected] ~]# setfacl –b doc1
[[email protected] ~]# ls –l doc1
-rw-rw---1 user1
acctg
[[email protected] ~]# _
0 May 2 22:01 doc1
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter Summary
167
Managing Filesystem Attributes
As with the Windows operating system, Linux has file attributes that can be set, if necessary.
These attributes work outside Linux permissions and are filesystem-specific. In this section,
we’ll examine attributes for the ext4 filesystem that you configured earlier during Lab 2.1.
Filesystem types will be discussed in more depth in Chapter 5.
To see the filesystem attributes that are currently assigned to a file, you can use the lsattr
(list attributes) command, as shown here for the doc1 file:
[[email protected] ~]# lsattr doc1
-------------e-- doc1
[[email protected] ~]# _
By default, all files have the e attribute, which writes to the file in “extent” blocks (rather
than immediately in a byte-by-byte fashion). If you would like to add or remove attributes,
you can use the chattr (change attributes) command. The following example assigns the
immutable attribute (i) to the doc1 file and displays the results:
[[email protected] ~]# chattr +i doc1
[[email protected] ~]# lsattr doc1
----i--------e-- doc1
[[email protected] ~]# _
The immutable attribute is the most commonly used filesystem attribute and prevents the file
from being modified in any way. Since attributes are applied at a filesystem level, not even
the root user can modify a file that has the immutable attribute set.
Most filesystem attributes are rarely set, as they provide for low-level
filesystem functionality. To view a full listing of filesystem attributes,
visit the manual page for the chattr command.
Similarly, to remove an attribute, simply use the chattr command with the – option, as
shown here with the doc1 file:
[[email protected] ~]# chattr -i doc1
[[email protected] ~]# lsattr doc1
-------------e-- doc1
[[email protected] ~]# _
Chapter Summary
■ The Linux directory tree obeys the Filesystem Hierarchy Standard, which allows
Linux users and developers to locate system files in standard directories.
■ Many file management commands exist to create, change the location of, or remove files
and directories. The most common of these include cp, mv, rm, rmdir, and mkdir.
■ You can find files on the filesystem using a preindexed database (the locate command)
or by the searching directories listed in the PATH variable (the which command).
However, the most versatile command used to find files is the find command, which
searches for files based on a wide range of criteria.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
168
Chapter 4
Linux Filesystem Management
■ Files can be linked two different ways. In a symbolic link, one file serves as a pointer
to another file. In a hard link, one file is a linked duplicate of another file.
■ Each file and directory has an owner and a group owner. In the absence of system
restrictions, the owner of the file or directory has the ability to change permissions
and give ownership to others.
■ Permissions can be set on the user or owner of a file, members of the group of the file,
as well as everyone on the system (other).
■ There are three regular file and directory permissions (read, write, execute) and three
special file and directory permissions (SUID, SGID, sticky bit). The definitions of these
permissions are different for files and directories.
■ Permissions can be changed using the chmod command by specifying symbols or
numbers to represent the changed permissions.
■ To ensure security, new files and directories receive default permissions from the
system, less the value of the umask variable.
■ The root user has all permissions to all files and directories on the Linux filesystem.
Similarly, the root user can change the ownership of any file or directory on the Linux
filesystem.
■ The default ACL on a file or directory can be modified to include additional users or
groups.
■ Filesystem attributes can be set on Linux files to provide low-level functionality such
as immutability.
Key Terms
access control list (ACL)
The section within an inode of a file or directory that lists the
permissions assigned to users and groups on the file or directory.
chattr (change attributes) command
The command used to change filesystem attributes
for a Linux file.
chgrp (change group) command
The command used to change the group owner of a file
or directory.
chmod (change mode) command
The command used to change the mode (permissions) of
a file or directory.
chown (change owner) command
The command used to change the owner and group
owner of a file or directory.
cp (copy) command
The command used to create copies of files and directories.
data blocks
A filesystem allocation unit in which the data that makes up the contents of
the file as well as the filename are stored.
Filesystem Hierarchy Standard (FHS)
A standard outlining the location of set files and
directories on a Linux system.
find command
The command used to find files on the filesystem using various criteria.
getfacl (get file ACL) command
The command used to list all ACL entries for a
particular Linux file or directory.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
169
group When used in the mode of a certain file or directory, the collection of users who
have ownership of that file or directory.
hard link
A file joined to other files on the same filesystem that shares the same inode.
inode The portion of a file that stores information on the file’s attributes, access
permissions, location, ownership, and file type.
inode table
The collection of inodes for all files and directories on a filesystem.
interactive mode The mode that file management commands use when a file can be
overwritten; the system interacts with a user asking for the user to confirm the action.
ln (link) command
locate command
The command used to create hard and symbolic links.
The command used to locate files from a file database.
lsattr (list attributes) command
The command used to list filesystem attributes for a
Linux file.
mkdir (make directory) command
mode
The part of the inode that stores information on access permissions.
mv (move) command
other
The command used to create directories.
The command used to move/rename files and directories.
When used in the mode of a certain file or directory, all the users on the Linux system.
owner
The user whose name appears in a long listing of a file or directory and who has
the ability to change permissions on that file or directory.
PATH variable A variable that stores a list of directories that will be searched in order
when commands are executed without an absolute or relative pathname.
permissions
A list that identifies who can access a file or folder and their level of access.
primary group
The default group to which a user belongs.
recursive
A term referring to itself and its own contents; a recursive search includes all
subdirectories in a directory and their contents.
rm (remove) command
The command used to remove files and directories.
rmdir (remove directory) command
setfacl (set file ACL) command
The command used to remove empty directories.
The command used to modify ACL entries for a
particular Linux file or directory.
source file/directory The portion of a command that refers to the file or directory from
which information is taken.
superblock The portion of a filesystem that stores critical information, such as the inode
table and block size.
symbolic link
A pointer to another file on the same or another filesystem; commonly
referred to as a shortcut.
target file/directory
The portion of a command that refers to the file or directory to which
information is directed.
touch command
The command used to create new files. It was originally used to update
the time stamp on a file.
umask
A special variable used to alter the permissions on all new files and directories by
taking away select default file and directory permissions.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
170
Chapter 4
Linux Filesystem Management
umask command
user
The command used to view and change the umask variable.
When used in the mode of a certain file or directory, the owner of that file or directory.
which command
The command used to locate files that exist within directories listed in the
PATH variable.
Review Questions
1. A symbolic link is also known as a soft link and is depicted by an @ symbol appearing
at the beginning of the filename when viewed using the ls –l command. True or False?
2. What was created to define a standard directory structure and common file location for
Linux?
a.
FSH
b. X.500
c.
FHS
d. root directory
3. There is no real difference between the “S” and “s” special permissions when displayed
using the ls –l command. One just means it is on a file, and the other means that it is
on a directory. True or False?
4. The default permissions given by the system prior to analyzing the umask are _________
for directories and _____________ for files.
a.
rw-rw-rw- and rw-rw-rw-
b. rw-rw-rw- and r--r- -r- c.
rw-rw-rw- and rwxrwxrwx
d. rwxrwxrwx and rw-rw-rwe.
rwxrw-rw- and rwx-rw-rw-
5. What must a user do to run cp or mv interactively and be asked if she wants to overwrite an existing file?
a.
There is no choice because the new file will overwrite the old one by default.
b. Type interactive cp or interactive mv.
c.
Type cp -i or mv -i.
d. Type cp –interactive or mv -interactive.
e.
Just type cp or mv because they run in interactive mode by default.
6. The root user utilizes the chgrp command to give ownership of a file to another user.
What must the root user do to regain ownership of the file?
a.
Run chgrp again listing the root user as the new owner.
b. Nothing, because this is a one-way, one-time action.
c.
Have the new owner run chgrp, and list the root user as the new owner.
d. Run chown and list the root user as the new owner.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
171
7. After typing the ls –F command, you see the following line in the output:
-rw-r-xr- -
1 user1
root
0 Apr 29 15:40 file1
What does this mean?
a.
User1 has read and write, members of the root group have read and execute, and all
others have read permissions to the file.
b. Members of the root group have read and write, user1 has read and execute, and all
others have read permissions to the file.
c.
All users have read and write, members of the root group have read and execute,
and user1 has read permissions to the file.
d. User1 has read and write, all others have read and execute, and members of the
root group have read permissions to the file.
8. After typing the command umask 731, the permissions on all subsequently created files
and directories will be affected. In this case, what will be the permissions on all new files?
a.
rw-rw-rw-
b. rwxrw-r-c.
---r- -rw-
d. --- -wx- -x
9. You noticed a file in your home directory that has a þ symbol appended to the mode.
What does this indicate?
a.
Special permissions have been set on the file.
b. The file has one or more files on the filesystem that are hard linked to it.
c.
The sticky bit directory permission has been set on the file and will remain inactive
as a result.
d. Additional entries exist within the ACL of the file that can be viewed using the
getfacl command.
10. When you change the data in a file that is hard-linked to three others, _____________.
a.
only the data in the file you modified is affected
b. only the data in the file you modified and any hard-linked files in the same directory
are affected
c.
the data in the file you modified and the data in all hard-linked files are modified
because they have different inodes
d. the data in the file you modified as well as the data in all hard-linked files are modified because they share the same data and all have the same inode and file size
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
172
Chapter 4
Linux Filesystem Management
11. The command chmod 317 file1 would produce which of the following lines in the
ls command?
a.
--w-r- -rwx 1 user1 root 0 Apr 29 15:40 file1
b. --wx- -xrwx 1 user1 root 0 Apr 29 15:40 file1
c.
-rwxrw-r-x 1 user1 root 0 Apr 29 15:40 file1
d. --w-rw-r-e 1 user1 root 0 Apr 29 15:40 file1
12. Which of the following commands will change the user ownership and group ownership
of file1 to user1 and root, respectively?
a.
chown user1:root file1
b. chown user1 : root file1
c.
This cannot be done because user and group ownership properties of a file must be
modified separately.
d. chown root:user1file1
e.
chown root : user1file1
13. What does the /var directory contain?
a.
various additional programs
b. spools and log files
c.
temporary files
d. files that are architecture-independent
e.
local variance devices
14. What does the mv command do? (Choose all that apply.)
a.
It makes a volume.
b. It makes a directory.
c.
It moves a directory.
d. It moves a file.
15. A file has the following permissions: r-- - -x-w-. The command chmod 143 would have
the same effect as the command _______________. (Choose all that apply.)
a.
chmod u+x-r,g+r-x,o+w file1
b. chmod u=w,g=rw,o=rx file1
c.
chmod u-r-w,g+r-w,o+r-x file1
d. chmod u=x,g=r,o=wx file1
e.
chmod u+w,g+r-w,o+r-x file1
f.
chmod u=rw,g=r,o=r file1
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
173
16. The which command _______________.
a.
can only be used to search for executables
b. searches for a file in all directories, starting from the root
c.
is not a valid Linux command
d. searches for a file only in directories that are in the PATH variable
17. Hard links need to reside on the same filesystem as the target, whereas symbolic links
need not be on the same filesystem as the target. True or False?
18. When applied to a directory, the SGID special permission _______________.
a.
causes all new files created in the directory to have the same group membership as
the directory, and not the entity that created them
b. cannot be used, because it is applied only to files
c. allows users the ability to use more than two groups for files that they create within
the directory
d. causes users to have their permissions checked before they are allowed to access files
in the directory
19. Which command do you use to rename files and directories?
a. cp
b. mv
c. rn
d. rename
20. What are the three standard Linux permissions?
a.
full control, read-execute, write
b. read, write, modify
c.
execute, read, write
d. read, write, examine
21. Given the following output from the ls command, how many files are linked with file1?
drwxr-xr-x
-rw-r- -r- -rw-r- -r- -rw-r- -r- -rw-r- -r- -rw-r- -r- -rw-r- -r- a.
3
3
1
4
2
1
1
root
root
root
root
root
root
user1
root
root
root
root
root
root
sys
4096
282
282
282
282
282
282
Apr
Apr
Apr
Apr
Apr
Apr
Apr
8
29
29
29
29
29
29
07:12
22:06
22:06
22:06
22:06
22:06
22:06
Desktop
file1
file2
file3
file4
file5
file6
one
b. two
c.
three
d. four
22. Only the root user can modify a file that has the immutable attribute set. True or False?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
174
Chapter 4
Linux Filesystem Management
Hands-on Projects
These projects should be completed in the order given. The hands-on projects
presented in this chapter should take a total of three hours to complete. The
requirements for this lab include:
• A computer with Fedora 20 installed according to Hands-on Project 2-1
• Completion of all Hands-on Projects in Chapter 3
Project 4-1
In this hands-on project, you log in to the computer and create new directories.
1. Boot your Fedora Linux virtual machine. After your Linux system has loaded, switch to
a command-line terminal (tty2) by pressing Ctrl+Alt+F2. Log in to the terminal using
the user name of root and the password of LNXrocks!.
2. At the command prompt, type ls -F and press Enter. Note the contents of your home folder.
3. At the command prompt, type mkdir mysamples and press Enter. Next type ls -F at
the command prompt, and press Enter. How many files and subdirectories are there? Why?
4. At the command prompt, type cd mysamples and press Enter. Next, type ls -F at the
command prompt and press Enter. What are the contents of the subdirectory mysamples?
5. At the command prompt, type mkdir undermysamples and press Enter. Next, type
ls -F at the command prompt and press Enter. What are the contents of the subdirectory mysamples?
6. At the command prompt, type mkdir todelete and press Enter. Next, type ls -F at
the command prompt and press Enter. Does the subdirectory todelete you just created
appear listed in the display?
7. At the command prompt, type cd .. and press Enter. Next, type ls -R and press
Enter. Notice that the subdirectory mysamples and its subdirectory undermysamples
are both displayed. You have used the recursive option with the ls command.
8. At the command prompt, type cd .. and press Enter. At the command prompt, type
pwd and press Enter. What is your current directory?
9. At the command prompt, type mkdir foruser1 and press Enter. At the command
prompt, type ls -F and press Enter. Does the subdirectory you just created appear
listed in the display?
10. Type exit and press Enter to log out of your shell.
Project 4-2
In this hands-on project, you copy files using the cp command.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal
using the user name of root and the password of LNXrocks!.
2. Next, type ls -F at the command prompt and press Enter. Note the contents of your
home folder.
3. At the command prompt, type cp sample1 and press Enter. What error message was
displayed and why?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
175
4. At the command prompt, type cp sample1 sample1A and press Enter. Next, type
ls -F at the command prompt and press Enter. How many files are there, and what
are their names? Why?
5. At the command prompt, type cp sample1 mysamples/sample1B and press Enter.
Next, type ls -F at the command prompt and press Enter. How many files are there,
and what are their names? Why?
6. At the command prompt, type cd mysamples and press Enter. Next, type ls -F at the
command prompt and press Enter. Was sample1B copied successfully?
7. At the command prompt, type cp /root/sample2 . and press Enter. Next, type ls -F at
the command prompt and press Enter. How many files are there, and what are their names?
Why?
8. At the command prompt, type cp sample1B .. and press Enter. Next, type cd .. at
the command prompt and press Enter. At the command prompt, type ls -F and press
Enter. Was the sample1B file copied successfully?
9. At the command prompt, type cp sample1 sample2 sample3 mysamples and press
Enter. What message do you get and why? Choose y and press Enter. Next, type cd
mysamples at the command prompt and press Enter. At the command prompt, type
ls -F and press Enter. How many files are there, and what are their names? Why?
10. At the command prompt, type cd .. and press Enter. Next, type cp mysamples
mysamples2 at the command prompt and press Enter. What error message did you
receive? Why?
11. At the command prompt, type cp –R mysamples mysamples2 and press Enter. Next,
type ls -F at the command prompt, and press Enter. Was the directory copied successfully? Type ls –F mysamples2 at the command prompt and press Enter. Were the
contents of mysamples successfully copied to mysamples2?
12. Type exit and press Enter to log out of your shell.
Project 4-3
In this hands-on project, you use the mv command to rename files and directories.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. Next, type ls -F at the command prompt and press Enter. Note the contents of your
home folder.
3. At the command prompt, type mv sample1 and press Enter. What error message was
displayed and why?
4. At the command prompt, type mv sample1 sample4 and press Enter. Next, type ls -F
at the command prompt and press Enter. How many files are listed and what are their
names? What happened to sample1?
5. At the command prompt, type mv sample4 mysamples and press Enter. Next, type
ls -F at the command prompt and press Enter. How many files are there, and what
are their names? Where did sample4 go?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
176
Chapter 4
Linux Filesystem Management
6. At the command prompt, type cd mysamples and press Enter. Next, type ls -F at the
command prompt and press Enter. Notice that the sample4 file you moved in Step 5
was moved here.
7. At the command prompt, type mv sample4 .. and press Enter. Next, type ls -F at
the command prompt and press Enter. How many files are there, and what are their
names? Where did the sample4 file go?
8. At the command prompt, type cd .. and press Enter. Next, type ls -F at the command prompt and press Enter to view the new location of sample4.
9. At the command prompt, type mv sample4 mysamples/sample2 and press Enter.
What message appeared on the screen and why?
10. Type y and press Enter to confirm you want to overwrite the file in the destination folder.
11. At the command prompt, type mv sample? mysamples and press Enter. Type y and
press Enter to confirm you want to overwrite the file sample3 in the destination folder.
12. At the command prompt, type ls -F and press Enter. How many files are there and why?
13. At the command prompt, type mv sample1* mysamples and press Enter. Type y and
press Enter to confirm you want to overwrite the file sample1B in the destination
directory.
14. At the command prompt, type ls -F and press Enter. Notice that there are no sample
files in the /root directory.
15. At the command prompt, type cd mysamples and press Enter. Next, type ls -F at the
command prompt and press Enter. Notice that all files originally in /root have been
moved to this directory.
16. At the command prompt, type cd .. and press Enter. Next, type ls -F at the command prompt and press Enter. Type mv mysamples samples and press Enter. Next,
type ls -F at the command prompt and press Enter. Why did you not need to specify
the recursive option to the mv command to rename the mysamples directory to samples?
17. Type exit and press Enter to log out of your shell.
Project 4-4
In this hands-on project, you make and view links to files and directories.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type cd samples and press Enter. Next, type ls -F at the
command prompt and press Enter. What files do you see? Next, type ls -l at the command prompt and press Enter. What is the link count for the sample1 file?
3. At the command prompt, type ln sample1 hardlinksample and press Enter. Next,
type ls -F at the command prompt and press Enter. Does anything in the terminal output indicate that sample1 and hardlinksample are hard-linked? Next, type ls -l at the
command prompt and press Enter. Does anything in the terminal output indicate that
sample1 and hardlinksample are hard-linked? What is the link count for sample1 and
hardlinksample? Next, type ls -li at the command prompt and press Enter to view the
inode numbers of each file. Do the two hard-linked files have the same inode number?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
177
4. At the command prompt, type ln sample1 hardlinksample2 and press Enter.
Next, type ls -l at the command prompt and press Enter. What is the link count for
the files sample1, hardlinksample, and hardlinksample2? Why?
5. At the command prompt, type vi sample1 and press Enter. Enter a sentence of your
choice into the vi editor, and then save your document and quit the vi editor.
6. At the command prompt, type cat sample1 and press Enter. Next, type cat hardlinksample at the command prompt and press Enter. Next, type cat hardlinksample2 at the command prompt and press Enter. Are the contents of each file the
same? Why?
7. At the command prompt, type ln –s sample2 symlinksample and press Enter. Next,
type ls -F at the command prompt and press Enter. Does anything in the terminal output
indicate that sample2 and symlinksample are symbolically linked? Which file is the target
file? Next, type ls -l at the command prompt and press Enter. Does anything in the terminal output indicate that sample2 and symlinksample are symbolically linked? Next, type
ls -li at the command prompt and press Enter to view the inode numbers of each file.
Do the two symbolically linked files have the same inode number?
8. At the command prompt, type vi symlinksample and press Enter. Enter a sentence
of your choice into the vi editor, and then save your document and quit the vi editor.
9. At the command prompt, type ls -l and press Enter. What is the size of the symlinksample file compared to sample2? Why? Next, type cat sample2 at the command
prompt and press Enter. What are the contents and why?
10. At the command prompt, type ln –s /etc/sysconfig/network-scripts netscripts and press Enter. Next, type ls -F at the command prompt and press Enter.
What file type is indicated for netscripts? Next, type cd netscripts at the command
prompt and press Enter. Type pwd at the command prompt and press Enter to view
your current directory. What is your current directory? Next, type ls -F at the command prompt and press Enter. What files are listed? Next, type ls -F
/etc/sysconfig/network-scripts at the command prompt and press Enter.
Note that your netscripts directory is merely a pointer to the /etc/sysconfig/networkscripts directory. How can this type of linking be useful?
11. Type exit and press Enter to log out of your shell.
Project 4-5
In this hands-on project, you find files on the filesystem using the find, locate, and
which commands.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type touch newfile and press Enter. Next, type locate
newfile at the command prompt and press Enter. Did the locate command find the
file? Why?
3. At the command prompt, type updatedb and press Enter. When the command is finished, type locate newfile at the command prompt and press Enter. Did the
locate command find the file? If so, how quickly did it find it? Why?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
178
Chapter 4
Linux Filesystem Management
4. At the command prompt, type find / -name "newfile" and press Enter. Did the
find command find the file? If so, how quickly did it find it? Why?
5. At the command prompt, type find /root -name "newfile" and press Enter. Did
the find command find the file? How quickly did it find it? Why?
6. At the command prompt, type which newfile and press Enter. Did the which command find the file? Why? Type echo $PATH at the command prompt and press Enter.
Is the /root directory listed in the PATH variable? Is the /bin directory listed in the
PATH variable?
7. At the command prompt, type which grep and press Enter. Did the which command
find the file? Why?
8. At the command prompt, type find /root –name "sample" and press Enter. What
files are listed? Why?
9. At the command prompt, type find /root –type l and press Enter. What files are
listed? Why?
10. At the command prompt, type find /root –size 0 and press Enter. What types of
files are listed? Type find /root –size 0 | more to see all of the files listed.
11. Type exit and press Enter to log out of your shell.
Project 4-6
In this hands-on project, you delete files and directories using the rmdir and rm commands.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type cd samples and press Enter. At the command
prompt, type ls -R and press Enter. Note the two empty directories todelete and
undermysamples.
3. At the command prompt, type rmdir undermysamples todelete and press Enter.
Did the command work? Why? Next, type ls –F at the command prompt and press
Enter. Were both directories deleted successfully?
4. At the command prompt, type rm sample1* and press Enter. What message is displayed? Answer n to all three questions.
5. At the command prompt, type rm –f sample1* and press Enter. Why were you not
prompted to continue? Next, type ls –F at the command prompt and press Enter.
Were all three files deleted successfully?
6. At the command prompt, type cd .. and press Enter. Next, type rmdir samples at
the command prompt and press Enter. What error message do you receive and why?
7. At the command prompt, type rm -Rf samples and press Enter. Next, type ls –F at
the command prompt and press Enter. Was the samples directory and all files within it
deleted successfully?
8. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
179
Project 4-7
In this hands-on project, you apply and modify access permissions on files and directories
and test their effects.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type touch permsample and press Enter. Next, type chmod
777 permsample at the command prompt and press Enter.
3. At the command prompt, type ls -l and press Enter. Who has permissions to this file?
4. At the command prompt, type chmod 000 permsample and press Enter. Next, type
ls –l at the command prompt and press Enter. Who has permissions to this file?
5. At the command prompt, type rm –f permsample and press Enter. Were you able to
delete this file? Why?
6. At the command prompt, type cd / and press Enter. Next, type pwd at the command
prompt and press Enter. What directory are you in? Type ls –F at the command
prompt and press Enter. What directories do you see?
7. At the command prompt, type ls –l and press Enter to view the owner, group owner,
and permissions on the foruser1 directory created in Hands-on Project 5-1. Who is the
owner and group owner? If you were logged in as the user user1, in which category
would you be placed (user, group, other)? What permissions do you have as this category (read, write, execute)?
8. At the command prompt, type cd /foruser1 and press Enter to enter the foruser1
directory. Next, type ls -F at the command prompt and press Enter. Are there any
files in this directory? Type cp /etc/hosts . at the command prompt and press
Enter. Next, type ls -F at the command prompt and press Enter to ensure that a copy
of the hosts file was made in your current directory.
9. Switch to a different command-line terminal (tty3) by pressing Ctrl+Alt+F3 and log in to
the terminal using the user name of user1 and the password of LNXrocks!.
10. At the command prompt, type cd /foruser1 and press Enter. Were you successful?
Why? Next, type ls -F at the command prompt and press Enter. Were you able to see
the contents of the directory? Why? Next, type rm –f hosts at the command prompt
and press Enter. What error message did you see? Why?
11. Switch back to your previous command-line terminal (tty2) by pressing Ctrl+Alt+F2.
Note that you are logged in as the root user on this terminal.
12. At the command prompt, type chmod o+w /foruser1 and press Enter. Were you able
to change the permissions on the /foruser1 directory successfully? Why?
13. Switch back to your previous command-line terminal (tty3) by pressing Ctrl+Alt+F3.
Note that you are logged in as the user1 user on this terminal.
14. At the command prompt, type cd /foruser1 and press Enter. Next, type rm –f
hosts at the command prompt and press Enter. Were you successful now? Why?
15. Switch back to your previous command-line terminal (tty2) by pressing Ctrl+Alt+F2.
Note that you are logged in as the root user on this terminal.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
180
Chapter 4
Linux Filesystem Management
16. At the command prompt, type cd /foruser1 and press Enter to enter the foruser1
directory. Type cp /etc/hosts . at the command prompt and press Enter to place
another copy of the hosts file in your current directory.
17. At the command prompt, type ls –l and press Enter. Who is the owner and group
owner of this file? If you were logged in as the user user1, in which category would
you be placed (user, group, other)? What permissions do you have as this category
(read, write, execute)?
18. Switch back to your previous command-line terminal (tty3) by pressing Ctrl+Alt+F3.
Note that you are logged in as the user1 user on this terminal.
19. At the command prompt, type cd /foruser1 and press Enter to enter the foruser1
directory. Type cat hosts at the command prompt and press Enter. Were you successful? Why? Next, type vi hosts at the command prompt to open the hosts file in the vi
editor. Delete the first line of this file and save your changes. Were you successful?
Why? Exit the vi editor and discard your changes.
20. Switch back to your previous command-line terminal (tty2) by pressing Ctrl+Alt+F2.
Note that you are logged in as the root user on this terminal.
21. At the command prompt, type chmod o+w /foruser1/hosts and press Enter.
22. Switch back to your previous command-line terminal (tty3) by pressing Ctrl+Alt+F3.
Note that you are logged in as the user1 user on this terminal.
23. At the command prompt, type cd /foruser1 and press Enter to enter the foruser1
directory. Type vi hosts at the command prompt to open the hosts file in the vi editor. Delete the first line of this file and save your changes. Why were you successful
this time? Exit the vi editor.
24. At the command prompt, type ls -l and press Enter. Do you have permission to execute the hosts file? Should you make this file executable? Why? Next, type ls –l /bin
at the command prompt and press Enter, Note how many of these files to which you
have execute permission. Type file /bin/* at the command prompt and press Enter
to view the file types of the files in the /bin directory. Should these files have the execute
permission?
25. Type exit and press Enter to log out of your shell.
26. Switch back to your previous command-line terminal (tty2) by pressing Ctrl+Alt+F2.
Note that you are logged in as the root user on this terminal.
27. Type exit and press Enter to log out of your shell.
Project 4-8
In this hands-on project, you view and manipulate the default file and directory permissions
using the umask variable.
1. Switch to a command-line terminal (tty3) by pressing Ctrl+Alt+F3 and log in to the terminal using the user name of user1 and the password of LNXrocks!.
2. At the command prompt, type ls -l and press Enter. What files do you see?
3. At the command prompt, type umask and press Enter. What is the default umask variable?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
181
4. At the command prompt, type touch utest1 and press Enter. Next, type ls –l at the
command prompt and press Enter. What are the permissions on the utest1 file? Do
these agree with the calculation in Figure 5-5? Create a new directory by typing
the command mkdir udir1 at the command prompt and pressing Enter. Next, type
ls –l at the command prompt and press Enter. What are the permissions on the udir1
directory? Do these agree with the calculation in Figure 5-5?
5. At the command prompt, type umask 007 and press Enter. Next, type umask at the command prompt and press Enter to verify that your umask variable has been changed to 007.
6. At the command prompt, type touch utest2 and press Enter. Next, type ls –l at the
command prompt and press Enter. What are the permissions on the utest2 file? Do
these agree with the calculation in Figure 5-6? Create a new directory by typing
the command mkdir udir2 at the command prompt and pressing Enter. Next, type
ls –l at the command prompt and press Enter. What are the permissions on the udir2
directory? Do these agree with the calculation in Figure 5-6?
7. Type exit and press Enter to log out of your shell.
Project 4-9
In this hands-on project, you view and change file and directory ownership using the chown
and chgrp commands.
1. Switch to a command-line terminal (tty3) by pressing Ctrl+Alt+F3 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type touch ownersample and press Enter. Next, type
mkdir ownerdir at the command prompt and press Enter. Next, type ls –l at the
command prompt and press Enter to verify that the file ownersample and directory
ownerdir were created and that user1 is the owner and group owner of each.
3. At the command prompt, type chgrp sys owner* and press Enter to change the group
ownership to the sys group for both ownersample and ownerdir. Why were you successful?
4. At the command prompt, type chown user1 owner* and press Enter to change the
ownership to the root user for both ownersample and ownerdir. Why were you
successful?
5. At the command prompt, type chown root.root owner* and press Enter to change
the ownership and group ownership back to the root user for both ownersample and
ownerdir. Although you are not the current owner of these files, why did you not
receive an error message?
6. At the command prompt, type mv ownersample ownerdir and press Enter. Next,
type ls –lR at the command prompt and press Enter to note that the ownersample file
now exists within the ownerdir directory and that both are owned by root.
7. At the command prompt, type chown –R user1 ownerdir and press Enter. Next,
type ls –lR at the command prompt and press Enter. Who owns the ownerdir directory and ownersample file? Why?
8. At the command prompt, type rm -Rf ownerdir and press Enter. Why were you able
to delete this directory without being the owner of it?
9. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
182
Chapter 4
Linux Filesystem Management
Project 4-10
In this hands-on project, you view and set special permissions on files and directories as well
as modify the default ACL on a file.
1. Switch to a command-line terminal (tty3) by pressing Ctrl+Alt+F3 and log in to the terminal using the user name of user1 and the password of LNXrocks!.
2. At the command prompt, type touch specialfile and press Enter. Next, type ls –l
at the command prompt and press Enter to verify that specialfile was created successfully.
Who is the owner and group owner of specialfile?
3. At the command prompt, type chmod 4777 specialfile and press Enter. Next, type
ls -l at the command prompt and press Enter. Which special permission is set on this
file? If this file were executed by another user, who would that user be during execution?
4. At the command prompt, type chmod 6777 specialfile and press Enter. Next, type
ls -l at the command prompt and press Enter. Which special permissions are set on
this file? If this file were executed by another user, who would that user be during execution, and which group would that user be a member of?
5. At the command prompt, type chmod 6444 specialfile and press Enter. Next, type
ls -l at the command prompt and press Enter. Can you tell if execute is not given
underneath the special permission listings? Would the special permissions retain their
meaning in this case?
6. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
7. At the command prompt, type mkdir /public and press Enter. Next, type chmod
1777 /public at the command prompt and press Enter. Which special permission is
set on this directory? Who can add or remove files to and from this directory?
8. At the command prompt, type touch /public/rootfile and press Enter.
9. Type exit and press Enter to log out of your shell.
10. Switch back to your previous command-line terminal (tty3) by pressing Ctrl+Alt+F3.
Note that you are logged in as the user1 user on this terminal.
11. At the command prompt, type touch /public/user1file and press Enter. Next,
type ls -l /public at the command prompt and press Enter. What files exist in this
directory, and who are the owners?
12. At the command prompt, type rm /public/user1file and press Enter. Were you
prompted to confirm the deletion of the file?
13. At the command prompt, type rm /public/rootfile and press Enter. What message
did you receive? Why? Press y. Note the error message that you receive.
14. Type exit and press Enter to log out of your shell.
15. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
16. At the command prompt, type touch aclfile and press Enter. Next, type getfacl
aclfile at the command prompt and press Enter. Are there any additional entries
beyond user, group, and other?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
183
17. At the command prompt, type setfacl –m u:user1:r-- aclfile and press Enter.
Next, type ls -l aclfile at the command prompt and press Enter. Is there a þ symbol following the mode? Next, type getfacl aclfile at the command prompt and
press Enter. Explain the permissions. When would this permission set be useful?
18. At the command prompt, type setfacl –b aclfile and press Enter. Next, type ls -l
aclfile at the command prompt and press Enter. Is there a þ symbol following the
mode?
19. Type exit and press Enter to log out of your shell.
Project 4-11
In this hands-on project, you configure and research filesystem attributes.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type touch toughfile and press Enter. Next, type lsattr
toughfile at the command prompt and press Enter. What filesystem attributes are set
on toughfile by default?
3. At the command prompt, type chattr +i toughfile and press Enter. Next, type
lsattr toughfile at the command prompt and press Enter. Is the immutable attribute set on toughfile?
4. At the command prompt, type vi toughfile and press Enter. Does the vi editor warn
you that the file is read-only? Add a line of text of your choice in the vi editor and
attempt to save your changes using w! at the : prompt. Were you successful? Quit the
vi editor using q! at the : prompt.
5. At the command prompt, type rm –f file1 and press Enter. Were you successful?
Why?
6. At the command prompt, type chattr –i file1 and press Enter. Next, type rm –f
file1 and press Enter. Were you successful this time? Why?
7. At the command prompt, type man chattr and press Enter. Search the manual page
for other filesystem attributes. Which attribute tells the Linux kernel to automatically
compress/decompress the file as it is written and read from the filesystem? Which attribute causes the data blocks of a file to be immediately overwritten once the file has
been deleted? Type q and press Enter to quit out of the manual page when finished.
8. Type exit and press Enter to log out of your shell.
Discovery Exercises
1. Use the ls command with the -F option to explore directories described in the
Filesystem Hierarchy Standard starting with /bin. Do you recognize any of the
commands in /bin? Explore several other FHS directories and note their contents. Refer
to Table 4-1 for a list of directories to explore. Further, visit http://refspecs.
linuxfoundation.org/fhs.shtml and read about the Filesystem Hierarchy Standard.
What benefits does it offer Linux?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4
184
Chapter 4
Linux Filesystem Management
2. Write the commands required for the following tasks. Try out each command on your
system to ensure that it is correct:
a.
Make a hierarchical directory structure under /root that consists of one directory
containing three subdirectories.
b. Copy two files into each of the subdirectories.
c.
Create one more directory with three subdirectories beneath it and move files from
the subdirectories containing them to the counterparts you just created.
d. Hard-link three of the files. Examine their inodes.
e.
Symbolically link two of the files and examine their link count and inode information.
f.
Make symbolic links from your home directory to two directories in this structure
and examine the results.
g.
Delete the symbolic links in your home directory and the directory structure you
created under /root.
3. Write the command that can be used to answer the following questions. (Hint: Try each
out on the system to check your results.)
a.
Find all files on the system that have the word “test” as part of their filename.
b. Search the PATH variable for the pathname to the awk command.
c.
Find all files in the /usr directory and subdirectories that are larger than 50 kilobytes in size.
d. Find all files in the /usr directory and subdirectories that are less than 70 kilobytes
in size.
e.
Find all files in the / directory and subdirectories that are symbolic links.
f.
Find all files in the /var directory and subdirectories that were accessed less than
60 minutes ago.
g.
Find all files in the /var directory and subdirectories that were accessed less than six
days ago.
h. Find all files in the /home directory and subdirectories that are empty.
i.
Find all files in the /etc directory and subdirectories that are owned by the group
bin.
4. For each of the following modes, write the numeric equivalent (e.g., 777):
a.
rw-r- -r- -
b. r--r- -r- c.
---rwxrw-
d. -wxr-xrwe.
rw-rw-rwx
f.
-w-r- - - --
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
185
5. Fill in the permissions in Table 4-7 with checkmarks, assuming that all four files are in
the directory /public, which has a mode of rwxr-xr-x.
6. Fill in the permissions in Table 4-8 with checkmarks, assuming that all four files are in
the directory /public, which has a mode of rwx--x- --.
7. For each of the following umasks, calculate the default permissions given to new files
and new directories:
a.
017
4
b. 272
c.
777
d. 000
e.
077
f.
027
Filename
Mode
Read
sample1
rw-rw-rw-
User
Group
Other
sample2
r--r- -- --
User
Group
Other
sample3
rwxr-x- - -
User
Group
Other
sample4
r-x- -- -- -
User
Group
Other
Edit
Execute
List
Delete
Table 4-7 Permissions table for Discovery Exercise 5
Filename
Mode
Read
sample1
rwxr--r- -
User
Group
Other
sample2
r-xr- -rw-
User
Group
Other
sample3
--xr-x- - -
User
Group
Other
sample4
r-xr- -r- -
User
Group
Other
Edit
Execute
List
Delete
Table 4-8 Permissions table for Discovery Exercise 6
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
186
Chapter 4
Linux Filesystem Management
8. For each of the umasks in Question 7, list the umasks that are reasonable to use to increase
security on your Linux system and explain why.
9. Starting from the Linux default permissions for file and directories, what umask would
you use to ensure that for all new_______________?
a.
directories, the owner would have read, write, and execute; members of the group
would have read and execute; and others would have read
b. files, the owner would have read and execute; the group would have read, write,
and execute; and others would have execute
c.
files, the owner would have write; the group would have read, write, and execute;
and others would have read and write
d. directories, the owner would have read, write, and execute; the group would have
read, write, and execute; and others would have read, write, and execute
e.
directories, the owner would have execute; the group would have read, write, and
execute; and others would have no permissions
f.
files, the owner would have read and write; the group would have no permissions;
and others would have write
g.
directories, the owner would have read, write, and execute; the group would have
read; and others would have read and execute
h. directories, the owner would have write; the group would have read, write, and
execute; and others would have read, write, and execute
i.
files, the owner would have no permissions; the group would have no permissions;
and others would have no permissions
10. What chmod command would you use to impose the following permissions?
a.
on a directory such that: the owner would have read, write, and execute; the group
would have read and execute; and others would have read
b. on a file such that: the owner would have read and write; the group would have no
permissions; and others would have write
c.
on a file such that: the owner would have write; the group would have read, write,
and execute; and others would have read and write
d. on a file such that: the owner would have read and execute; the group would have
read, write, and execute; and others would have execute
e.
on a directory such that: the owner would have execute; the group would have
read, write, and execute; and others would have no permissions
f.
on a directory such that: the owner would have write; the group would have read,
write, and execute; and others would have read, write, and execute
g.
on a directory such that: the owner would have read, write, and execute; the group
would have read; and others would have read and execute
h. on a directory such that: the owner would have read, write, and execute; the group
would have read, write, and execute; and others would have read, write, and execute
i.
on a file such that: the owner would have no permissions; the group would have no
permissions; and others would have no permissions
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
5
Linux Filesystem Administration
After completing this chapter, you will be able to:
•
•
•
•
•
•
•
•
•
Identify the structure and types of device files in the /dev directory
Understand common filesystem types and their features
Mount and unmount filesystems to and from the Linux directory tree
Create and manage filesystems on floppy disks, CDs, DVDs, USB storage
devices, Firewire storage devices, and hard disk partitions
Create and use ISO images
Use the LVM to create and manage logical volumes
Monitor free space on mounted filesystems
Check filesystems for errors
Use hard disk quotas to limit user space usage
187
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
188
Chapter 5
Linux Filesystem Administration
Navigating the Linux directory tree and manipulating files are common tasks that
are performed on a daily basis by all users. However, administrators must provide this
directory tree for users, as well as manage and fix the disk devices that support it. In this
chapter, you learn about the various device files that represent disk devices and the different
filesystems that can be placed on those devices. Next, you learn how to create and manage
filesystems on a wide variety of different storage devices, as well as learn standard hard disk
partitioning, LVM configuration, and filesystem management. The chapter concludes with a
discussion of disk usage, filesystem errors, and restricting the ability for users to store files.
The /dev Directory
Fundamental to administering the disks used to store information is an understanding of how
these disks are specified by the Linux operating system. Most devices on a Linux system (such
as disks, terminals, and serial ports) are represented by a file on the hard disk called a device
file. There is one file per device, and these files are typically created by the udev system and
found in the /dev directory. This allows you to specify devices on the system by using the
pathname to the file that represents it in the /dev directory.
Perhaps the simplest example of a disk device is a floppy disk, which was primarily used
on legacy Linux systems to transfer small amounts of data before the advent of USB flash
drives. To specify the first floppy disk in the Linux system, you can type the pathname
/dev/fd0 (floppy disk 0) in the appropriate section of a command, and to represent the second floppy disk in the Linux system, you can specify the pathname to the file /dev/fd1
(floppy disk 1).
Furthermore, each device file specifies how data should be transferred to and from the device.
You have two methods for transferring data to and from a device. The first method involves
transferring information character-by-character to and from the device. Devices that transfer
data in this fashion are referred to as character devices. The second method transfers chunks
or blocks of information at a time by using physical memory to buffer the transfer. Devices
that use this method of transfer are called block devices; they can transfer information much
faster than character devices. Device files that represent disks, such as floppy disks, CDs,
DVDs, USB flash drives, and hard disks, are typically block device files because a fast data
transfer rate is preferred. Tape drives and most other devices, however, are typically represented by character device files.
To see whether a particular device transfers data character-by-character or block-by-block,
recall that the ls –l command displays a c or b character in the type column indicating the
type of device file. To view the type of the file /dev/fd0, you can use the following command:
[[email protected] ~]# ls –l /dev/fd0
brw-rw---- 1 root
floppy
[[email protected] ~]#_
2,
0 Feb 23 16:02
/dev/fd0
Floppy disk drives are not standard on newer computers today. As a
result, Fedora 20 does not load the floppy driver and create the
/dev/fd0 device file by default. To load the floppy driver manually
(which also creates the /dev/fd0 device file), you can type modprobe
floppy at a command prompt as the root user.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
The /dev Directory
Device File
Description
Block or Character
/dev/fd0
First floppy disk on the system
Block
/dev/fd1
Second floppy disk on the system
Block
/dev/hda1
First primary partition on the first PATA hard disk drive
(primary master)
Block
/dev/hdb1
First primary partition on the second PATA hard disk drive
(primary slave)
Block
/dev/hdc1
First primary partition on the third PATA hard disk drive
(secondary master)
Block
/dev/hdd1
First primary partition on the fourth PATA hard disk drive
(secondary slave)
Block
/dev/sda1
First primary partition on the first SATA/SCSI hard disk drive
Block
/dev/sdb1
First primary partition on the second SATA/SCSI hard disk drive
Block
/dev/loop0
First loopback interface
Block
/dev/tty1
First local terminal on the system (Ctrl+Alt+F1)
Character
/dev/tty2
Second local terminal on the system (Ctrl+Alt+F2)
Character
/dev/ttyS0
First serial port on the system (COM1)
Character
/dev/ttyS1
Second serial port on the system (COM2)
Character
/dev/psaux
PS/2 mouse port
Character
/dev/lp0
First parallel port on the system (LPT1)
Character
/dev/null
Device file that represents nothing; any data sent to this
device is discarded
Character
/dev/st0
First SCSI tape device in the system
Character
/dev/bus/usb/*
USB device files
Character
189
Table 5-1 Common device files
From the leftmost character in the preceding output, you can see that the /dev/fd0 file is a
block device file. Table 5-1 provides a list of some common device files that you may find on
your Linux system and their types.
After a typical Fedora Linux installation, you will find several hundred different device files in
the /dev directory that represent devices on the system. This large number of device files on a
Linux system does not require much disk space because all device files consist of inodes and
no data blocks; as a result, the entire contents of the /dev directory is usually less than 100
kilobytes in size. When using the ls –l command to view device files, the portion of the listing describing the file size in kilobytes is replaced by two numbers: the major number and the
minor number. The major number of a device file points to the device driver for the device in
the Linux kernel; several different devices can share the same major number if they are of the
same general type (that is, two different floppy disk drives might share the same major number). The minor number indicates the particular device itself; the first floppy disk drive in the
computer will have a different minor number than the second floppy disk drive in the
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
190
Chapter 5
Linux Filesystem Administration
computer. In the following output, you see that /dev/fd0 and /dev/fd1 share the same major
number of 2, yet the minor number for /dev/fd0 is 0 and the minor number for /dev/fd1 is 1,
which differentiates them from each other.
[[email protected] ~]# ls –l/dev/fd0 /dev/fd1
brw-rw---1 root
floppy
2,
brw-rw---1 root
floppy
2,
[[email protected] ~]#_
0 Feb 23 16:02 /dev/fd0
1 Feb 23 16:02 /dev/fd1
Together, the device file type (block or character), the major number (device driver), and the
minor number (specific device) make up the unique characteristics of each device file. To create a device file, you simply need to know these three pieces of information.
If a device file becomes corrupted, it is usually listed as a regular file instead of a block or
character special file. Recall from Chapter 4 that the find /dev –type f command can be
used to search for regular files underneath the /dev directory to identify whether corruption
has taken place. If you find a corrupted device file or accidentally delete a device file, the
mknod command can be used to re-create the device file if you know the type, major, and
minor numbers. An example of re-creating the /dev/fd0 block device file used earlier with a
major number of 2 and a minor number of 0 is shown in the following example:
[[email protected]
brw-rw---[[email protected]
[[email protected]
[[email protected]
[[email protected]
brw-r--r-[[email protected]
~]# ls –l /dev/fd0
1 root
floppy
2, 0 Feb 23 16:02 /dev/fd0
~]# rm -f /dev/fd0
~]# ls –l /dev/fd0
~]# mknod /dev/fd0 b 2 0
~]# ls –l /dev/fd0
1 root
root
2,
0 May
8 13:26 /dev/fd0
~]#_
To see a list of devices that are currently used on the system and their major numbers, you
can view the contents of the /proc/devices file, as shown here:
[[email protected] ~]# cat /proc/devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
10 misc
13 input
14 sound
21 sg
29 fb
116 alsa
128 ptm
136 pts
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Filesystems
162
180
189
202
203
226
250
251
252
253
254
191
raw
usb
usb_device
cpu/msr
cpu/cpuid
drm
hidraw
usbmon
bsg
pcmcia
rtc
Block devices:
1 ramdisk
2 fd
7 loop
8 sd
9 md
11 sr
65 sd
69 sd
70 sd
71 sd
128 sd
134 sd
135 sd
253 device-mapper
254 mdp
[[email protected] ~]#_
Filesystems
Recall from Chapter 2 that files must be stored on the hard disk in a defined format called a
filesystem so that the operating system can work with them. The type of filesystem used determines how files are managed on the physical hard disk. Each filesystem can have different
methods for storing files and features that make the filesystem robust against errors. Although
many different types of filesystems are available, all filesystems share three common components, as discussed in Chapter 4: the superblock, the inode table, and the data blocks. On a
structural level, these three components work together to organize files and allow rapid access
to and retrieval of data. All storage media, such as floppy disks, hard disks, and DVDs, need
to contain a filesystem before they can be used.
Creating a filesystem on a device is commonly referred to as
formatting.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
192
Chapter 5
Linux Filesystem Administration
Filesystem Types
As mentioned, many filesystems are available for use in the Linux operating system. Each has
its own strengths and weaknesses, thus some are better suited to some tasks and not as well
suited to others. One benefit of Linux is that you need not use only one type of filesystem on
the system; you can use several different devices formatted with different filesystems under
the same directory tree. In addition, files and directories appear the same throughout the
directory tree regardless of whether there is one filesystem or 20 different filesystems in use
by the Linux system. Table 5-2 lists some common filesystems available for use in Linux.
For a full listing of filesystem types and their features, you can refer to the Filesystem
HOWTO at www.tldp.org/HOWTO/Filesystems-HOWTO.html.
Filesystem support is typically built into the Linux kernel or added as
a package on most distributions. Fedora 20 has native support for
btrfs, cramfs, ext2, ext3, ext4, hfs, hfsplus, iso9660, ntfs, reiserfs,
vfat, and xfs by default.
Mounting
The term mounting originated in the 1960s when information was stored on large tape reels
that had to be mounted on computers to make the data available. Today, mounting still
refers to making data available. More specifically, it refers to the process whereby a device
is made accessible to users via the logical directory tree. This device is attached to a certain
directory on the directory tree called a mount point. Users can then create files and subdirectories in this mount point directory, which are then stored on the filesystem that was
mounted to that particular directory.
Remember that directories are merely files that do not contain data; instead, they contain a
list of files and subdirectories organized within them. Thus, it is easy for the Linux system to
cover up directories to prevent user access to that data. This is essentially what happens
when a device is mounted to a certain directory; the mount point directory is temporarily
covered up by that device while the device remains mounted. Any file contents that were
present in the mount point directory prior to mounting are not lost; when the device is
unmounted, the mount point directory is uncovered, and the previous file contents are
revealed. Suppose, for example, that you mount a floppy disk device that contains a filesystem to the /mnt directory. The /mnt directory is an empty directory that is commonly used
as a temporary mount point for mounting removable media devices. Before mounting, the
directory structure would resemble that depicted in Figure 5-1. After the floppy is mounted
to the /mnt directory, the contents of the /mnt directory would be covered up by the floppy
filesystem, as illustrated in Figure 5-2.
If a user then stores a file in the /mnt directory, as shown in Figure 5-2, that file will be
stored on the floppy disk device. Similarly, if a user creates a subdirectory under the /mnt
directory depicted in Figure 5-2, that subdirectory will be made on the floppy disk.
It is important to note that any existing directory can be used as a mount point. If a user
mounts a floppy device to the /bin directory, all files in the /bin directory are covered up during the time the floppy disk is mounted, including the command used to unmount the floppy.
Thus, it is safe practice to create empty directories used specifically for mounting devices to
avoid making existing files inaccessible to users.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Filesystems
193
Filesystem
Description
bfs
Boot File System—A small, bootable filesystem used to hold the files necessary for system
startup; it is commonly used on UNIX systems.
btrfs
B-tree File System—A new filesystem for Linux systems that includes many features that are
geared towards large-scale storage. Although still in development, it is envisioned to be a
replacement for the ext4 and reiserfs filesystems in the long term. Its name is commonly
pronounced as “Butter F S.”
cramfs
Compressed ROM filesystem—A read-only filesystem typically used on embedded Linux
systems to host system files in a small amount of storage space.
ext2
Second extended filesystem—The traditional filesystem used on Linux, it supports access
control lists (individual user permissions). In addition, it retains its name from being the new
version of the original extended filesystem, based on the Minix filesystem.
ext3
Third extended filesystem—A variation on ext2 that allows for journaling and, thus, has a
faster startup and recovery time.
ext4
Fourth extended filesystem—A variation on ext3 that has larger filesystem support and speed
enhancements.
hfs, hfsplus
Hierarchical File System—A filesystem native to Apple Macintosh computers.
hpfs
High Performance File System—An IBM proprietary OS/2 filesystem that provides long
filename support and is optimized to manipulate data on large disk volumes.
iso9660
ISO 9660 filesystem—A filesystem that originated from the International Standards
Organization recommendation 9660 and is used to access data stored on CDs and DVDs.
minix
MINIX filesystem—The filesystem used by Linus Torvalds in the early days of Linux development.
msdos
DOS FAT filesystem.
ntfs
New Technology File System—A Microsoft proprietary filesystem developed for its Windows
operating systems.
reiserfs
Reiser File System—A journalizing filesystem similar to ext3, and more suited for use with
databases.
udf
Universal Disk Format filesystem—A filesystem used by software programs that write to a
CD-RW or DVD-RW drive.
vfat
DOS FAT filesystem with long filename support.
vxfs
Veritas File System—A journalizing filesystem that offers large file support and supports
access control lists (individual user permissions) and is commonly used by major versions of
UNIX.
xfs
X File System—A high-performance filesystem created by Silicon Graphics for use on their
IRIX UNIX systems. Many Linux administrators prefer to use xfs on systems that need to
quickly write large numbers of files to the hard disk.
zfs
Zettabyte File System—A very high-performance filesystem and volume manager originally
created by Sun Microsystems that protects against data corruption and has features that
support very large distributed storage systems. Many large-scale Linux server systems in
industry use the zfs filesystem to store and manage large amounts of data. You will learn
how to configure zfs in Chapter 6.
Table 5-2 Common Linux filesystems
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
194
Chapter 5
Linux Filesystem Administration
/
home
mnt
etc
root
data
mary
sue
bob
Figure 5-1 The directory structure prior to mounting
/
home
mnt
etc
root
data
mary
sue
bob
Figure 5-2 The directory structure after mounting a floppy device
Most systems today have several removable media devices—such as
CDs, DVDs, USB flash drives, and USB hard drives—that may be connected to your PC for long periods of time. As a result, it is considered
good form to create subdirectories under the /media directory on your
Linux system to mount these removable media devices and only use
the /mnt directory to temporarily mount devices. For example, you
could mount your floppy to the /media/floppy directory and your DVD
to the /media/DVD directory. You can then access the files on your
floppy disk by navigating to the /media/floppy directory, as well as
access the files on your DVD by navigating to the /media/DVD directory.
When the Linux system is first turned on, a filesystem present on the hard drive is mounted
to the / directory. This is referred to as the root filesystem and contains most of the operating
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Floppy Disks
195
system files. Other filesystems present on hard disks inside the computer can also be mounted
to various mount point directories underneath the / directory at boot time, as well as via
entries in the filesystem table (/etc/fstab) discussed in the following sections.
The mount command is used to mount devices to mount point directories, and the umount
command is used to unmount devices from mount point directories; both of these commands
are discussed throughout the remainder of this chapter.
Working with Floppy Disks
Traditionally, it was commonplace to use floppy disk removable media to store small files,
such as spreadsheets, that needed to be transferred from computer to computer. Today, floppy
disks are not available on all systems, but we will examine them here as they are an easy way
to practice and learn the commands that are commonly used to work with filesystems and
they are emulated in virtualization software.
Recall that each disk device must be formatted with a filesystem prior to being used to store
files. To do this, you can use the mkfs (make filesystem) command and specify the filesystem
type using the –t switch and the device file representing the floppy disk device. To format the
floppy disk inside the first floppy disk drive in the computer with the ext2 filesystem, place a
floppy disk in the floppy disk drive and type the following command:
[[email protected] ~]# mkfs –t ext2 /dev/fd0
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
184 inodes, 1440 blocks
72 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=1572864
1 block group
8192 blocks per group, 8192 fragments per group
184 inodes per group
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]#_
Alternatively, you can specify a different filesystem after the –t option, such as the DOS FAT
filesystem. This results in a different output from the mkfs command, as shown in the following example:
[[email protected] ~]# mkfs –t msdos /dev/fd0
mkfs.msdos 3.0.23 (2013-10-15)
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
196
Chapter 5
Linux Filesystem Administration
If you do not specify the filesystem using the mkfs command, the default filesystem assumed
is the ext2 filesystem, as shown in the following example:
[[email protected] ~]# mkfs /dev/fd0
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
184 inodes, 1440 blocks
72 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=1572864
1 block group
8192 blocks per group, 8192 fragments per group
184 inodes per group
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]#_
Although the most common command to create filesystems is the mkfs command, other variants and shortcuts to the mkfs command exist. For example, to create an ext2 filesystem,
you could type mke2fs /dev/fd0 on the command line. Other alternatives to the mkfs
command are listed in Table 5-3.
After a floppy disk has been formatted with a filesystem, it must be mounted on the directory
tree before it can be used. A list of currently mounted filesystems can be obtained by using the
mount command with no options or arguments, which reads the information listed in the
/etc/mtab (mount table) file, as shown in the following sample output:
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[[email protected] ~]# cat /etc/mtab
/dev/sda1 / ext4 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Floppy Disks
Command
Filesystem It Creates
mkfs
filesystems of most types
mkdosfs
mkfs.msdos
mkfs.vfat
FAT
mkfs.ext2
mke2fs
mke2fs –t ext2
ext2
mkfs.ext3
mke2fs –t ext3
ext3
mkfs.ext4
mke2fs –t ext4
ext4
mkisofs
ISO
mkreiserfs
mkfs.reiserfs
REISERFS
mkfs.xfs
XFS
mkntfs
mkfs.ntfs
NTFS
197
Table 5-3 Commands used to create filesystems
From the preceding example output, you can see that the device /dev/sda1 is mounted on the
/ directory and contains an ext4 filesystem. The other filesystems listed are special filesystems
that are used by the system and are discussed later in this book.
To mount a device on the directory tree, you can use the mount command with options and
arguments to specify the filesystem type, the device to mount, and the directory on which to
mount the device (mount point). It is important to ensure that no user is currently using the
mount point directory; otherwise, the system gives you an error message, and the disk is not
mounted. To check whether the /media/floppy directory is being used by any users, you can
use the fuser command with the –u option, as shown in the following output:
[[email protected] ~]# fuser -u /media/floppy
[[email protected] ~]#_
The preceding output indicates the /media/floppy directory is not being used by any user processes. To mount the first floppy device formatted with the ext2 filesystem to the /media/
floppy directory, simply type the following command:
[[email protected] ~]# mount –t ext2 /dev/fd0 /media/floppy
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
198
Chapter 5
Linux Filesystem Administration
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/fd0 on /media/floppy type ext2 (rw)
[[email protected] ~]#_
If you omit the –t option to the mount command, it attempts to
automatically detect the filesystem on the device. Thus, the command
mount /dev/fd0 /media/floppy will perform the same
action as the mount command shown in the preceding output.
Notice that /dev/fd0 appears mounted to the /media/floppy directory in the preceding output
of the mount command. To access and store files on the floppy device, you can now treat
the /media/floppy directory as the root of the floppy disk. When an ext2 filesystem is created
on a disk device, one directory called lostþfound is created by default and used by the fsck
command discussed later in this chapter. To explore the recently mounted floppy filesystem,
you can use the following commands:
[[email protected] ~]# cd /media/floppy
[[email protected] floppy]# pwd
/media/floppy
[[email protected] floppy]# ls -F
lost+found/
[[email protected] floppy]#_
To copy files to the floppy device, simply specify the /media/floppy directory as the target for
the cp command, as shown next:
[[email protected] floppy]# cd /etc
[[email protected] etc]# cat issue
Fedora release 20 (Heisenbug)
Kernel \r on an \m (\l)
[[email protected] etc]# cp issue /media/floppy
[[email protected] etc]# cd /media/floppy
[[email protected] floppy]# ls -F
issue lost+found/
[[email protected] floppy]# cat issue
Fedora release 20 (Heisenbug)
Kernel \r on an \m (\l)
[[email protected] floppy]#_
Similarly, you can also create subdirectories underneath the floppy device to store files; these
subdirectories are referenced underneath the mount point directory. To make a directory
called workfiles on the floppy mounted in the previous example and copy the /etc/inittab file
to it, you can use the following commands:
[[email protected] floppy]# pwd
/media/floppy
[[email protected] floppy]# ls -F
issue lost+found/
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Floppy Disks
199
[[email protected] floppy]# mkdir workfiles
[[email protected] floppy]# ls -F
issue lost+found/ workfiles/
[[email protected] floppy]# cd workfiles
[[email protected] workfiles]# pwd
/media/floppy/workfiles
[[email protected] workfiles]# cp /etc/inittab.
[[email protected] workfiles]# ls -F
inittab
[[email protected] workfiles]#_
Even though you can eject the floppy disk from the floppy disk drive without permission from
the system, doing so is likely to cause error messages to appear on the terminal screen. Before
a floppy is ejected, it must be properly unmounted using the umount command. The umount
command can take the name of the device to unmount or the mount point directory as an
argument. Similar to mounting a floppy disk, unmounting a floppy disk requires that the
mount point directory has no users using it. If you try to unmount the floppy disk mounted
to the /media/floppy directory while it is being used, you receive an error message similar to
the one in the following example:
[[email protected] floppy]# pwd
/media/floppy
[[email protected] floppy]# umount /media/floppy
umount: /media/floppy: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1).)
[[email protected] floppy]# fuser -u /media/floppy
/media/floppy:
17368c(root)
[[email protected] floppy]# cd /root
[[email protected] ~]# umount /media/floppy
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[[email protected] ~]#_
Notice from the preceding output that you were still using the /media/floppy directory because
it was the current working directory. The fuser command also indicated that the root user
had a process using the directory. After the current working directory was changed, the
umount command was able to unmount the floppy from the /media/floppy directory, and
the output of the mount command indicated that the floppy disk was no longer mounted.
Recall that mounting simply attaches a disk device to the Linux directory tree so that you can
treat the device like a directory full of files and subdirectories. A device can be mounted to any
existing directory. However, if the directory contains files, those files are inaccessible until the
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
200
Chapter 5
Linux Filesystem Administration
device is unmounted. Suppose, for example, that you create a directory called /flopper for
mounting floppy disks and a file inside called samplefile, as shown in the following output:
[[email protected]
[[email protected]
[[email protected]
samplefile
[[email protected]
~]# mkdir /flopper
~]# touch /flopper/samplefile
~]# ls -F /flopper
~]#_
If the floppy disk used earlier is mounted to the /flopper directory, a user who uses the /flopper directory will be using the floppy disk; however, when nothing is mounted to the /flopper
directory, the previous contents are available for use:
[[email protected] ~]# mount -t ext2 /dev/fd0 /flopper
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[[email protected] ~]# ls -F /flopper
issue lost+found/ workfiles/
[[email protected] ~]# umount /flopper
[[email protected] ~]# ls -F /flopper
samplefile
[[email protected] ~]#_
The mount command used in the preceding output specifies the filesystem type, the device to
mount, and the mount point directory. To save time typing on the command line, you can
alternatively specify one argument and allow the system to look up the remaining information
in the /etc/fstab (filesystem table) file. The /etc/fstab file has a dual purpose; it is used to mount
devices at boot time and is consulted when a user does not specify enough arguments on the
command line when using the mount command.
There are six fields present in the /etc/fstab file:
<device to mount> <mount point> <type> <mount options> <dump#> <fsck#>
The device to mount can be the path to a device file (i.e., /dev/sda1) or the boot loader label
that describes the volume to mount (e.g. UUID=db545f1d-c1ee-4b70-acbe-3dc61b41db20).
The mount point specifies where to mount the device. The type can be a specific value (such
as ext4) or can be automatically detected. The mount options are additional options that the
mount command accepts when mounting the volume (such as read only, or “ro”). Any filesystems with the mount option “noauto” are not automatically mounted at boot time; a complete list of options that the mount command accepts can be found by viewing the manual
page for the mount command.
The dump# is used by the dump command (discussed in Chapter 11) when backing up filesystems; a 1 in this field indicates that the filesystem should be backed up, whereas a 0 indicates
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Floppy Disks
201
that no backup is necessary. The fsck# is used by the fsck command discussed later in this
chapter when checking filesystems at boot time for errors; any filesystems with a 1 in this
field are checked before any filesystems with a number 2, and filesystems with a number 0
are not checked.
To mount all filesystems in the /etc/fstab file that are intended to
mount at boot time, you can simply type the mount –a command.
The following output displays the contents of a sample /etc/fstab file:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
#
# Accessible filesystems, by reference, are maintained under’/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for info
#
/dev/sda1
/
ext4
defaults
11
/dev/sda2
swap
swap
defaults
00
tmpfs
/dev/shm
tmpfs
defaults
00
devpts
/dev/pts
devpts
gid=5,mode=620
00
sysfs
/sys
sysfs
defaults
00
proc
/proc
proc
defaults
00
/dev/fd0
/media/floppy
auto
noauto
00
[[email protected] ~]# _
Thus, to mount the first floppy device (/dev/fd0) to the /mnt/floppy directory and automatically detect the type of filesystem on the device, simply specify enough information for the
mount command to find the appropriate line in the /etc/fstab file:
[[email protected] ~]# mount /dev/fd0
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/fd0 on /media/floppy type ext2 (rw)
[[email protected] ~]# umount /dev/fd0
[[email protected] ~]#_
The mount command in the preceding output succeeded because a line in /etc/fstab described
the mounting of the /dev/fd0 device.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
202
Chapter 5
Linux Filesystem Administration
Command
Description
mount
Displays mounted filesystems
mount –t <type> <device> <mount point>
Mounts a
fuser –u <directory>
Displays the users using a particular directory
umount <mount point> or umount <device>
Unmounts a
directory
<device> of a certain <type> to a
<mount point> directory
<device> from its <mount point>
Table 5-4 Useful commands when mounting and unmounting filesystems
Alternatively, you could specify the mount point as an argument to the mount command to
mount the same device via the correct entry in /etc/fstab:
[[email protected] ~]# mount /media/floppy
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/fd0 on /media/floppy type ext2 (rw)
[[email protected] ~]# umount /media/floppy
[[email protected] ~]#_
Table 5-4 lists commands that are useful when mounting and unmounting floppy disks.
Working with CDs, DVDs, and ISO Images
Most software that is not downloaded from the Internet is packaged on CDs and DVDs
because they have a much larger storage capacity than floppy disks; one DVD can store
more than 3000 times the data a floppy disk can store. Like floppies, CDs and DVDs can be
mounted with the mount command and unmounted with the umount command, as shown in
Table 5-4; however, the device file used with these commands is different. The device files
used by CD and DVD depend on the technology used by the drive itself. If your computer
has a PATA CD or DVD drive, you can refer to it using one of the following four standard
PATA hard drive configurations (discussed in Chapter 2):
• Primary master (/dev/hda)
• Primary slave (/dev/hdb)
• Secondary master (/dev/hdc)
• Secondary slave (/dev/hdd)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with CDs, DVDs, and ISO Images
203
However, if you have a SATA or SCSI CD or DVD drive, Linux may use many different
names, depending on your actual CD or DVD drive:
•
First SATA/SCSI drive (/dev/sda, /dev/scd0, /dev/sr0, and /dev/sg0)
•
Second SATA/SCSI drive (/dev/sdb, /dev/scd1, /dev/sr1, and /dev/sg1)
•
Third SATA/SCSI drive (/dev/sdc, /dev/scd2, /dev/sr2, and /dev/sg2)
•
And so on
To make the identification of your CD or DVD drive easier, Fedora Linux creates a file called
/dev/cdrom, which is merely a symbolic link to the correct device file for your first CD or
DVD drive. For example, if your system contains a SATA CD/DVD drive, a long listing of
/dev/cdrom may show the following:
[[email protected] ~]# ls -l /dev/cdrom
lrwxrwxrwx 1 root root 3 Aug 10 12:39 /dev/cdrom -> sr0
[[email protected] ~]#_
In this case, you could use /dev/cdrom or /dev/sr0 to mount CDs or DVDs. To write to a CD
or DVD in this same drive, however, you must use disc-burning software that knows how to
write data to /dev/cdrom or /dev/sr0.
Nearly all DVD-ROM and DVD-RW drives can also work with CDs.
There are many OSS disc burning software applications available for
Linux. One example is the graphical Brasero Disc Burner program
that you can install by running the command yum install
brasero as the root user.
In addition, CDs and DVDs typically use the iso9660 filesystem type and are read-only
when accessed using Linux (recall that you must use disc-burning software to record to a
CD or DVD). Thus, to mount a CD or DVD to a directory, you should use the filesystem
type of iso9660 and add the –r (read-only) option to the mount command. To mount a
sample CD to the /media/cd directory and view its contents, you could use the following
commands:
[[email protected] ~]# mount –r –t iso9660 /dev/cdrom /media/cd
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/cdrom on /media/cd type iso9660 (ro)
[[email protected] ~]# ls –l /media/cdrom
autorun.inf*
install*
graphics/
jungle/
jungle.txt*
joystick/
[[email protected] ~]# umount /media/cdrom
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
204
Chapter 5
Linux Filesystem Administration
The /media/cd directory must exist for the mount command to succeed in the previous example.
As with floppies, you can modify the /etc/fstab file, such that you can specify only a single
argument to the mount command to mount a CD or DVD. Also remember that the mount
point directory must not be in use to successfully mount or unmount CDs and DVDs; the
fuser command can be used to verify this.
Unlike floppy disks, CDs and DVDs cannot be ejected from the drive until they are properly
unmounted, because the mount command locks the CD/DVD drive as a precaution.
When you insert a CD or DVD while in a GUI environment, it is automatically mounted by
the system to a directory underneath the /run/media/username directory (where username is
the name of the user logged into the GUI environment) that is named for the label on the CD
or DVD (specified in the disc-burning software). For example, if you insert a DVD with a
label of “Cafe_Scientifique” into your system while logged into a GUI environment as user1,
the system will automatically create a /run/media/user1/Cafe_Scientifique directory and mount
the device to this directory, as shown here:
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
gvfs-fuse-daemon on /home/bob/.gvfs type fuse.gvfs-fuse-daemon (rw,
nosuid,nodev,user=bob)
/dev/sr0 on /run/media/user1/Cafe_Scientifique type iso9660
(ro,nosuid,nodev,uhelper=udisks,uid=501,gid=501,iocharset=utf8,
mode=0400,dmode=0500)
[[email protected] ~]#_
In addition, the system will also place a shortcut to the /run/media/user1/Cafe_Scientifique
directory in the Files application within the GUI environment so that you can easily access
the contents of your DVD, as shown in Figure 5-3.
When you are finished accessing the files on the DVD, you can click the eject icon next to
Cafe_Scientifique, shown in Figure 5-3, to unmount the DVD from the /run/media/user1/
Cafe_Scientifique directory and force the DVD drive to eject the DVD.
The iso9660 filesystem type is not limited to CDs and DVDs. You can also create image files,
called ISO images, that contain other files. Recall from Chapter 2 that most operating system
software available for download from the Internet, such as Linux distributions, are available
as an ISO image file. Once downloaded, ISO images can be easily written to a CD or DVD
using disc-burning software or mounted and accessed by your Linux system. If you download
an ISO image called sample.iso, you can mount it to the /mnt directory as a loopback device,
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with CDs, DVDs, and ISO Images
205
5
Figure 5-3 Accessing a DVD within the GNOME desktop
which allows your system to access the contents of the sample.iso file, using the following
command:
[[email protected] ~]# mount
[[email protected] ~]# mount –o loop –r –t iso9660 lala.iso /mnt
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/loop0 on /mnt type iso9660 (ro)
[[email protected] ~]# ls /mnt
setup.exe tools binaries
[[email protected] ~]#_
You can then manipulate, execute, edit files within the /mnt directory, or copy files from the
/mnt directory to another directory to extract the contents of the ISO image file.
To create an ISO image from a directory of files, simply use the mkisofs command. The
following command creates an ISO image called newimage.iso from all the information in
the /data directory with additional support for the Joliet (-J) and Rock Ridge (-R) standards:
[[email protected] ~]# mkisofs –RJ –o newimage.iso /data
[[email protected] ~]# mount –o loop –r –t iso9660 lala.iso /mnt
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
206
Chapter 5
Linux Filesystem Administration
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/loop0 on /mnt type iso9660 (ro)
[[email protected] ~]#_
Before running the mkisofs command on Fedora 20, you must
first run the yum install genisoimage command as the root
user to install the ISO image tools.
Working with Hard Disks and SSDs
Hard disks typically come in three flavors: PATA, SATA, and SCSI. PATA hard disks must be
set to one of four configurations, each of which has a different device file:
• Primary master (/dev/hda)
• Primary slave (/dev/hdb)
• Secondary master (/dev/hdc)
• Secondary slave (/dev/hdd)
SATA and SCSI hard disks typically have faster data transfer speeds than PATA hard disks,
and most systems allow for the connection of more than four SATA or SCSI hard disks. As a
result of these benefits, both SATA and SCSI hard disks are well suited to Linux servers that
require a great deal of storage space for programs and user files. However, SATA and SCSI
hard disks have different device files associated with them:
• First SCSI hard disk drive (/dev/sda)
• Second SCSI hard disk drive (/dev/sdb)
• Third SCSI hard disk drive (/dev/sdc)
• Fourth SCSI hard disk drive (/dev/sdd)
• Fifth SCSI hard disk drive (/dev/sde)
• Sixth SCSI hard disk drive (/dev/sdf)
• And so on
SSDs are a newer technology that use much faster NAND flash storage yet provide a hard
disk compatible interface so that they can function as a drop-in replacement for hard
disks. Most SSDs on the market are SATA and are treated like a SATA hard disk by the
Linux operating system. As a result, /dev/sda could refer to the first SATA hard disk or
the first SATA SSD, /dev/sdb could refer to the second SATA hard disk or second SATA
SSD, and so on.
For simplicity, we’ll refer primarily to hard disk drives when discussing
permanent storage throughout the remainder of this book. However,
all of the concepts related to hard disk drives apply equally to SSDs.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
207
Standard Hard Disk Partitioning
Recall that hard disks have the largest storage capacity of any device that you use to store
information on a regular basis. As helpful as this storage capacity can be, it also poses some
problems; as the size of a disk increases, organization becomes more difficult and the chance
of error increases. To solve these problems, Linux administrators typically divide a hard disk
into smaller, more usable sections called partitions. Each partition can contain a separate filesystem and can be mounted to different mount point directories. Recall from Chapter 2 that
Linux requires two partitions at minimum: a partition that is mounted to the root directory
(the root partition) and a partition used to hold virtual memory (the swap partition). The
swap partition does not require a filesystem, because it is written to and maintained by the
operating system alone. It is good practice, however, to use more than just two partitions on
a Linux system. This division allows you to do the following:
•
Segregate different types of data—for example, home directory data is stored on a
separate partition mounted to /home.
•
Allow for the use of more than one type of filesystem on one hard disk drive—for
example, some filesystems are tuned for database use.
•
Reduce the chance that filesystem corruption will render a system unusable; if the
partition that is mounted to the /home directory becomes corrupted, it does not affect
the system because operating system files are stored on a separate partition mounted to
the / directory.
•
Speed up access to stored data by keeping filesystems as small as possible.
Segregation of data into physically separate areas of the hard disk drive can be exceptionally
useful from an organizational standpoint. Keeping different types of data on different
partitions gives you the ability to manipulate one type of data without affecting the rest of
the system. This also reduces the likelihood that filesystem corruption will affect all files
in the directory tree. Access speed is improved because a smaller area needs to be searched
in a hard disk drive to locate data. This process is similar to searching for a penny in a
20,000-square-foot warehouse. It takes much less time to find the penny if that warehouse is
divided into four separate departments of 5,000 square feet each and you know in which
department the penny is located. Searching and maneuvering is much quicker and easier in a
smaller, defined space than in a larger one.
On a physical level, hard disks are circular metal platters that spin at a fast speed. Data is read
off these disks in concentric circles called tracks; each track is divided into sectors of information, and sectors are combined into more usable blocks of data, as shown in Figure 5-4. Most
hard disk drives have several platters inside them, organized on top of each other such that
they can be written to simultaneously speed up data transfer. A series consisting of the same
concentric track on all of the metal platters inside a hard disk drive is known as a cylinder.
SSDs use circuitry within the drive itself to divide data into logical
tracks, sectors, blocks, and cylinders to ensure that the OS can work
with SSDs like any hard disk. This allows you to create partitions and
filesystems on an SSD in the same way that you would a hard disk.
Today, SSDs are much faster at reading and writing data compared
to hard disks. As a result, they are typically used on production
Linux servers.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
208
Chapter 5
Linux Filesystem Administration
Track
Sector
Block
Figure 5-4 The physical areas of a hard disk
Partition definitions are stored in the first readable sector of the hard disk known as the
Master Boot Record (MBR) or master boot block (MBB). Large hard disks (> 2TB) and
many newer hard disks use a GUID Partition Table (GPT) in place of a MBR to allow for
the additional addressing of sectors. If the MBR or GPT area of the hard disk becomes
corrupted, the entire contents of the hard disk might be lost.
It is common for Linux servers to have several hard disks. In these
situations, it is also common to configure one partition on each hard
disk and mount each partition to different directories on the directory
tree. Thus, if one partition fails, an entire hard disk can be replaced
with a new one and the data retrieved from a back-up source.
Recall from Chapter 2 that hard disks with a MBR normally contain up to four primary partitions; to overcome this limitation, you can use an extended partition in place of one of these
primary partitions. An extended partition can then contain many more subpartitions called
logical drives. Because it is on these partitions that you place a filesystem, there exist device
files that refer to the various types of partitions that you can have on a hard disk. These
device files start with the name of the hard disk (/dev/hda, /dev/hdb, /dev/sda, /dev/sdb, and
so on) and append a number indicating the partition on that hard disk. The first primary
partition is given the number 1, the second primary partition is given the number 2, the
third primary partition is given the number 3, and the fourth primary partition is given the
number 4. If any one of these primary partitions is labeled as an extended partition, the logical drives within are named starting with number 5. Table 5-5 lists some common hard disk
partition names.
Note from Table 5-5 that any one of the primary partitions can be labeled as an extended
partition. Also, for different disk drives than those listed in Table 5-5 (e.g., /dev/hdc), the partition numbers remain the same (e.g., /dev/hdc1, /dev/hdc2, and so on).
Hard disk partitions can be created specific to a certain filesystem. To create a partition that
will later be formatted with an ext2, ex3, or ext4 filesystem, you should create a Linux partition (also known as type 83). Similarly, you should create a Linux swap partition (also
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
Partition
PATA Device Name
(Assuming /dev/hda)
SATA/SCSI Device Name
(Assuming /dev/sda)
1st primary partition
/dev/hda1
/dev/sda1
2nd primary partition
/dev/hda2
/dev/sda2
3rd primary partition
/dev/hda3
/dev/sda3
4th primary partition
/dev/hda4
/dev/sda4
1st logical drive in the extended partition
/dev/hda5
/dev/sda5
2nd logical drive in the extended partition
/dev/hda6
/dev/sda6
3rd logical drive in the extended partition
/dev/hda7
/dev/sda7
4th logical drive in the extended partition
/dev/hda8
/dev/sda8
5th logical drive in the extended partition
/dev/hda9
/dev/sda9
nth logical drive in the extended partition
/dev/hdan
/dev/sdan
209
Table 5-5 Common hard disk partition device files for /dev/hda and /dev/sda
known as type 82) if that partition is intended for use as virtual memory. This explicit choice
of partition type allows for partitions that better suit the needs of a filesystem.
A typical Linux hard disk structure for the primary master PATA hard disk (/dev/hda) can
contain a partition for the / filesystem (/dev/hda1) and an extended partition (/dev/hda2) that
further contains a swap partition (/dev/hda5) and some free space, as shown in Figure 5-5.
A more complicated Linux hard disk structure for the first SATA/SCSI hard disk might
involve preserving the Windows operating system partition, allowing a user to boot into and
use the Linux operating system or boot into and use the Windows operating system. This is
known as dual booting and is discussed in Chapter 6.
free
space
hda1
(primary)
swap
1GB
/(Linux)
9GB
MBR
(MBB)
hda5
(logical)
hda5 + free space = hda2
(extended)
Figure 5-5 A sample Linux partitioning strategy
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
210
Chapter 5
Linux Filesystem Administration
sda1
(primary)
C: (WinXP)
30GB
free
space
swap
16GB
/(Linux)
128GB
/home
20GB
sda5
(logical)
MBR
(MBB)
sda7
(logical)
sda6
(logical)
sda5 + sda6 + sda7 + free space = sda2
(extended)
Figure 5-6 A sample dual-boot Linux partitioning strategy
In Figure 5-6, the Windows partition was created as a primary partition (/dev/sda1) and
the Linux partitions are contained within the extended partition (/dev/sda2). Figure 5-6
also creates a separate filesystem for users’ home directories mounted to /home (/dev/
sda6).
Unlike hard disks that use a MBR, GPT hard disks don’t need to
adhere to the limitation of 4 primary partitions. Instead, you can create as many as 128 primary partitions (/dev/sda1 to /dev/sda128).
Consequently, there is no need for extended partitions or logical
drives on a hard disk that uses a GPT.
Working with Standard Hard Disk Partitions
Recall that the creation of partitions can be accomplished at installation using the graphical installation program. To create partitions after installation, you can use the fdisk
command to create partitions that will be stored in a MBR on the hard disk. To use the
fdisk command, simply specify the hard disk to partition as an argument. An example
of using fdisk to work with the first SATA hard disk (/dev/sda) is shown in the following
output:
[[email protected] ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.24).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
211
Note from the preceding output that the fdisk command displays a prompt for the user to
accept fdisk commands; a list of possible fdisk commands can be seen if the user presses
the m key at this prompt, as shown in the following example:
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty
G create a new empty
o create a new empty
s create a new empty
GPT
SGI
DOS
Sun
partition table
(IRIX) partition table
partition table
partition table
Command (m for help):_
To print a list of the partitions currently set on /dev/sda, you could press the p key at the
fdisk prompt:
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
212
Chapter 5
Linux Filesystem Administration
Device
/dev/sda1
/dev/sda2
/dev/sda3
Boot
*
Start
2048
4196352
30679040
End
4196351
30679039
41943039
Blocks
2097152
13241344
5632000
Id System
82 Linux swap / Solaris
83 Linux
83 Linux
Command (m for help):_
Notice that the device names appear on the left side of the preceding output, including the
partition that is booted to (/dev/sda2), the start and end location of partitions on the physical
hard disk (start and end cylinders), the number of total blocks for storing information in the
partition, as well as the partition type and description (83 is a Linux partition, 82 is a Linux
swap partition). A Linux partition can contain a Linux filesystem—for example, ext4.
To remove the /dev/sda3 partition and all the data contained on the filesystem within, you
could use the d key noted earlier:
Command (m for help): d
Partition number (1-3, default 3): 3
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Device
/dev/sda1
/dev/sda2
Boot
*
Start
2048
4196352
End
4196351
30679039
Blocks
2097152
13241344
Id
82
83
System
Linux swap / Solaris
Linux
Command (m for help):_
To create an extended partition using the fourth primary partition (/dev/sda4) with two logical drives (/dev/sda5 and /dev/sda6), you could use the n key noted earlier and specify the
partition to create, the starting cylinder on the hard disk, and the size in blocks (þ5G makes
a 5GB partition):
Command (m for help): n
Partition type:
p primary partition (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (3,4, default 3): 4
First sector (30679040-41943039, default 30679040): 30679040
Last sector, +sectors or +size{K,M,G,T,P} (30679040-41943039, default
41943039): +5G
Created a new partition 3 of type ‘Extended’ and of size 5 GiB.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
213
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Device
/dev/sda1
/dev/sda2
/dev/sda4
Boot
*
Start
End
Blocks
2048 4196351
2097152
4196352 30679039 13241344
30679040 41164799
5242880
Id
82
83
5
System
Linux swap / Solaris
Linux
Extended
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (30681088-41164799, default 30681088): 30681088
Last sector, +sectors or +size{K,M,G,T,P} (30681088-41164799, default
41164799): +3GB
Created a new partition 5 of type ‘Linux’ and of size 2.8 GiB.
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Device
/dev/sda1
/dev/sda2
/dev/sda4
/dev/sda5
Boot
*
Start
End
Blocks
2048 4196351
2097152
4196352 30679039 13241344
30679040 41164799
5242880
30681088 36540415
2929664
Id
82
83
5
83
System
Linux swap / Solaris
Linux
Extended
Linux
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
214
Chapter 5
Linux Filesystem Administration
Adding logical partition 6
First sector (36542464-41164799, default 36542464): 36542464
Last sector, +sectors or +size{K,M,G,T,P} (36542464-41164799, default
41164799): +2G
Created a new partition 6 of type ‘Linux’ and of size 2 GiB.
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Device
/dev/sda1
/dev/sda2
/dev/sda4
/dev/sda5
/dev/sda6
Boot
*
Start
2048
4196352
30679040
30681088
36542464
End
Blocks
4196351
2097152
30679039 13241344
41164799
5242880
36540415
2929664
40736767
2097152
Id
82
83
5
83
83
System
Linux swap / Solaris
Linux
Extended
Linux
Linux
Command (m for help):_
Notice from the preceding output that the default type for new partitions created with
fdisk is 83 (Linux); to change this, you can type the t key at the fdisk prompt. To change
the /dev/sda6 partition to type 82 (Linux swap), you can do the following at the fdisk
prompt:
Command (m for help): t
Partition number (1,2,4-6, default 6): 6
Hex code (type L to all list codes): L
0
Empty
24
NEC DOS
81
Minix / old Lin
bf
Solaris
1
2
FAT12
XENIX root
39
3c
Plan 9
PartitionMagic
82
83
Linux swap / So
Linux
c1
c4
DRDOS/sec (FATDRDOS/sec (FAT-
3
XENIX usr
40
Venix 80286
84
OS/2 hidden C:
c6
DRDOS/sec (FAT-
4
5
FAT16 <32M
Extended
41
42
PPC PReP Boot
SFS
85
86
Linux extended
NTFS volume set
c7
da
Syrinx
Non-FS data
6
FAT16
4d
QNX4.x
87
NTFS volume set
db
CP/M / CTOS / .
7
8
HPFS/NTFS
AIX
4e
4f
QNX4.x 2nd part
QNX4.x 3rd part
88
8e
Linux plaintext
Linux LVM
de
df
Dell Utility
BootIt
50
9
AIX bootable
OnTrack DM
93
Amoeba
e1
DOS access
a
b
OS/2 Boot Manag 51
W95 FAT32
52
OnTrack DM6 Aux
CP/M
94
9f
Amoeba BBT
BSD/OS
e3
e4
DOS R/O
SpeedStor
c
W95 FAT32 (LBA) 53
OnTrack DM6 Aux
a0
IBM Thinkpad hi
eb
BeOS fs
e
f
W95 FAT16 (LBA) 54
W95 Ext’d (LBA) 55
OnTrackDM6
EZ-Drive
a5
a6
FreeBSD
OpenBSD
ee
ef
GPT
EFI (FAT-12/16/
OPUS
Golden Bow
a7
NeXTSTEP
f0
Linux/PA-RISC b
10
56
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
11
Hidden FAT12
12
14
Compaq diagnost 61
Hidden FAT16 <3 63
5c
16
Hidden FAT16
Novell Netware
17
18
Hidden HPFS/NTF 65
AST SmartSleep 70
Novell Netware
DiskSecure Mult
1b
Hidden W95 FAT3 75
PC/IX
1c
1e
Hidden W95 FAT3 80
Hidden W95 FAT1
Old Minix
64
215
Priam Edisk
a8
Darwin UFS
f1
SpeedStor
SpeedStor
GNU HURD or Sys
a9
ab
NetBSD
Darwin boot
f4
f2
SpeedStor
DOS secondary
af
HFS / HFS+
fb
VMware VMFS
b7
b8
BSDI fs
BSDI swap
fc
fd
VMware VMKCORE
Linux raid auto
bb
Boot Wizard hid
fe
LANstep
be
Solaris boot
ff
BBT
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.
Command (m for help): p
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000996b
Device
/dev/sda1
/dev/sda2
/dev/sda4
/dev/sda5
/dev/sda6
Boot
*
Start
2048
4196352
30679040
30681088
36542464
End
Blocks
4196351
2097152
30679039 13241344
41164799
5242880
36540415
2929664
40736767
2097152
Id
82
83
5
83
82
System
Linux swap / Solaris
Linux
Extended
Linux
Linux swap / Solaris
Command (m for help):_
Finally, to save partition changes to the hard disk and attempt to reload the new partition
information back into memory, simply use the w key at the fdisk prompt:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8).
[[email protected] ~]#_
If the fdisk command indicates that the new partition information must be reloaded manually (as in the preceding example), simply reboot your machine. This is the most reliable way
to ensure that the MBR is reloaded into memory successfully. For this reason, it is good form
to always reboot your machine after running fdisk.
An easier alternative to fdisk is the cfdisk utility. When you run the cfdisk command,
you see the interactive graphical utility shown in Figure 5-7. You can use this utility to
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
216
Chapter 5
Linux Filesystem Administration
Figure 5-7 The cfdisk utility
quickly create, manipulate, and delete partitions using choices at the bottom of the screen
that you can navigate using your cursor keys. As with fdisk, after making changes within
the cfdisk utility, you should reboot your system to ensure that the MBR is reloaded into
memory successfully.
After your computer has rebooted, you can use the mkfs, mount, and umount commands
discussed earlier, specifying the partition device file as an argument. To create an ext4 filesystem on the /dev/sda5 partition created earlier, you can use the following command:
[[email protected] ~]# mkfs -t ext4 /dev/sda5
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
183264 inodes, 732416 blocks
36620 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=750780416
23 block groups
32768 blocks per group, 32768 fragments per group
7968 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
217
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information:done
[[email protected] ~]#_
To mount this ext4 filesystem to a new mount point directory called /data and view the contents, you can use the following commands:
[[email protected] ~]# mkdir /data
[[email protected] ~]# mount -t ext4 /dev/sda5 /data
[[email protected] ~]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
/dev/sda2 on / type ext4 (rw,relatime,seclabel,data=ordered)
tmpfs on /tmp type tmpfs (rw,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda5 on /data type ext4 (rw,relatime,seclabel,data=ordered)
[[email protected] ~]# ls –F /data
lost+found/
[[email protected] ~]#_
To allow the system to mount this filesystem automatically at every boot, simply edit the /etc/
fstab file, such that it has the following entry for /dev/sda5:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Sep 24 15:33:17 2015
#
# Accessible filesystems, by reference, are maintained under’/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for info
#
/dev/sda2
/
ext4
defaults
11
/dev/sda1
swap
swap
defaults
00
/dev/sda5
/data
ext4
defaults
00
[[email protected] ~]#_
Although swap partitions do not contain a filesystem, you must still prepare swap partitions
and activate them for use on the Linux system. To do this, you can use the mkswap command to prepare the swap partition and the swapon command to activate it. To prepare
and activate the /dev/sda6 partition that we created earlier as virtual memory, you can use
the following commands:
[[email protected] ~]# mkswap /dev/sda6
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=f7999b84-f9e4-48ff-879b-35b3bc941fab
[[email protected] ~]# swapon /dev/sda6
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
218
Chapter 5
Linux Filesystem Administration
You can also use the swapoff command to deactivate a swap
partition.
Next, you can edit the /etc/fstab file to ensure that the new /dev/sda6 partition is activated as
virtual memory, as shown here:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Sep 24 15:33:17 2015
#
# Accessible filesystems, by reference, are maintained under’/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for info
#
/dev/sda2
/
ext4
defaults
11
/dev/sda1
swap
swap
defaults
00
/dev/sda5
/data
ext4
defaults
00
/dev/sda6
swap
swap
defaults
00
[[email protected] ~]#_
If your hard disk uses a GPT instead of a MBR, you can’t use the fdisk or cfdisk commands to create and modify partitions before you format them with a filesystem or prepare
them for use as Linux swap memory. Instead, you can use the gdisk (GPT fdisk) command
to create and work with up to 128 primary partitions on the GPT hard disk using an interface that is nearly identical to fdisk.
Many Linux distributions also contain the parted (GNU Parted) command, which can be
used to create and modify partitions on both MBR and GPT hard disks. To work with partitions on /dev/sda, you can run the parted command with the device file as an argument.
Once in the GNU Parted utility, you can type help to obtain a list of valid commands, or
print to print the existing partitions, as shown here:
[[email protected] ~]# parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) print
Model: ATA INTEL SSDSA2CW08 (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number
Start
1
1049kB
2
2149MB
(parted)_
End
2149MB
15.7GB
Size
2147MB
13.6GB
Type
primary
primary
File system
Flags
linux-swap(v1)
ext4
boot
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
219
Note from the previous output that the Intel SSD uses a GPT partition table and contains
two partitions: a 2147MB Linux swap partition and a 13.6GB partition that contains the
ext4 filesystem (the root filesystem). In the following example, two additional partitions are
created using the free space on our hard disk within the GNU parted utility: a 3GB partition
with the label “data” (/dev/sda3) and a 2GB partition with the label “logs” (/dev/sda4). During the creation process, you are prompted for the file system type. If you plan on formatting
the partition with a Linux filesystem afterwards, choosing the default partition type of ext2
will suffice. However, if you plan on using the partition for swap, you should specify
linux-swap when prompted for the file system type.
(parted) mkpart
Partition name? []? data
File system type? [ext2]? ext2
Start? 15.7GB
End? 18.7GB
(parted) print
Model: ATA INTEL SSDSA2CW08 (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number
1
2
3
Start
1049kB
2149MB
15.7GB
End
2149MB
15.7GB
18.7GB
Size
2147MB
13.6GB
2989MB
File system
linux-swap(v1)
ext4
5
Name
Flags
ext4
data
boot
Name
Flags
ext4
data
logs
boot
(parted) mkpart
Partition name? []? logs
File system type? [ext2]? ext2
Start? 18.7GB
End? 20.7GB
(parted) print
Model: ATA INTEL SSDSA2CW08 (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number
1
2
3
4
Start
1049kB
2149MB
15.7GB
18.7GB
End
2149MB
15.7GB
18.7GB
20.7GB
Size
2147MB
13.6GB
2989MB
1986MB
File system
linux-swap(v1)
ext4
(parted) quit
Information: You may need to update /etc/fstab.
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
220
Chapter 5
Linux Filesystem Administration
After creating GPT partitions that should contain a filesystem, you can proceed to format
those partitions with a filesystem using mkfs, mount them to the directory tree with the
mount command, and update the /etc/fstab file to mount them automatically, like we did
earlier after creating partitions with fdisk. Alternatively, if you’ve created a Linux swap
partition on a GPT hard disk, you can proceed to prepare that partition using mkswap and
activate it using the swapon command, as we did earlier after creating partitions with
fdisk.
Recall that parted can also work with disks that contain an MBR.
If you create partitions on an MBR hard disk, parted will additionally prompt you for the partition type (primary/extended/logical).
To remove the two GPT partitions that were added in the previous example, you can use the
rm command within the parted utility and specify the partition number:
[[email protected] ~]# parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) print
Model: ATA INTEL SSDSA2CW08 (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number
1
2
3
4
Start
1049kB
2149MB
15.7GB
18.7GB
End
2149MB
15.7GB
18.7GB
20.7GB
Size
2147MB
13.6GB
2989MB
1986MB
File system
linux-swap(v1)
ext4
Name
Flags
ext4
data
logs
boot
(parted) rm 4
(parted) rm 3
(parted) print
Model: ATA INTEL SSDSA2CW08 (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number
1
2
Start
1049kB
2149MB
End
2149MB
15.7GB
Size
2147MB
13.6GB
Type
primary
primary
File system
linux-swap(v1)
ext4
Flags
boot
(parted) quit
Information: You may need to update /etc/fstab.
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
221
Working with the LVM
In the previous section, you learned how to create standard hard disk partitions on a MBR
or GPT hard disk. You also learned how to create filesystems on those partitions and mount
the filesystems to a directory within the Linux filesystem hierarchy.
Instead of creating and mounting filesystems that reside on standard partitions, you can use
the Logical Volume Manager (LVM) to create volumes. These volumes can contain a filesystem and be mounted to directories within the Linux filesystem hierarchy. Using volumes to
host filesystems is far more flexible than using standard partitions since it allows you to select
free space from unused partitions across multiple hard disks in your computer. This free
space is then pooled together into a single group from which volumes can be created. These
volumes can then be formatted with a filesystem and mounted to a directory on the Linux
filesystem hierarchy. Furthermore, additional hard disks can easily be added to the LVM,
where existing volumes can take advantage of the additional storage space.
The LVM consists of several different components:
•
Physical Volumes (PVs) are unused partitions on hard disks that the LVM can use to
store information.
•
Volume Groups (VGs) contain one or more PVs. They represent the pools of hard disk
storage space that are available to the LVM for creating logical volumes. Additional
PVs can easily be added to a VG after creation.
•
Logical Volumes (LVs) are the usable volumes that are created by the LVM from the
available storage space within a VG. LVs contain a filesystem and are mounted to a
directory in the Linux filesystem hierarchy. In addition, LVs can be resized easily by
the LVM to use more or less storage space.
The LVM subsystem in Linux manages the storage of all data that is saved to LVs. The physical location of the data is transparent to the user. Furthermore, the LVM has error correction abilities that minimize the chance that data will become corrupted or lost. Figure 5-8
illustrates the relationships among LVM components in a sample LVM configuration that
creates four different PVs from the standard partitions on three different hard disks. These
PVs are added to a VG that is divided into three LVs that are each mounted to a directory
on the Linux filesystem hierarchy (/directory1, /directory2, and /directory3).
To configure the LVM, you must first create one or more PVs that reference an unused partition on a hard disk in your computer. Say, for example, that you recently created a new partition called /dev/sda4. Rather than placing a filesystem on /dev/sda4, you could instead allow
the LVM to use the /dev/sda4 partition using the pvcreate command, as shown here:
[email protected] ~]# pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created
[[email protected] ~]#_
You should use the pvcreate command to create PVs for each
unused partition that you wish to be used by the LVM. For simplicity
in code output, we will only create a single PV in this section to
demonstrate the configuration of LVM.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
222
Chapter 5
Linux Filesystem Administration
/directory1
/directory2
/directory3
Logical
Volume (LV)
Logical
Volume (LV)
Logical
Volume (LV)
Volume Group (VG)
Physical Volume (PV)
/dev/hda1
Physical Volume (PV)
/dev/sda1
Physical Volume (PV)
/dev/sdb2
Physical Volume (PV)
/dev/sda4
Physical Hard Disk
/dev/hda
Physical Hard Disk
/dev/sda
Physical Hard Disk
/dev/sdb
Figure 5-8 A sample LVM configuration
The pvdisplay command can be used to display detailed information about each PV. The
following pvdisplay command indicates that /dev/sda4 has 32 GB of available space:
[[email protected] ~]# pvdisplay
"/dev/sda4" is a new physical volume of "32.00 GiB"
--- NEW Physical volume --PV Name
/dev/sda4
VG Name
PV Size
32.00 GiB
Allocatable
NO
PE Size
0
Total PE
0
Free PE
0
Allocated PE
0
PV UUID
YZDmIG-0qFU-54AT-ioB6-bjKa-sHcC-i8IJo0
[[email protected] ~]#_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
223
Once you have created PVs, you can create a VG that uses the space in the PVs using the
vgcreate command. For example, to create a VG called vg00 that uses the /dev/sda4 PV,
you could use the following vgcreate command:
[[email protected] ~]# vgcreate vg00 /dev/sda4
Volume group "vg00" successfully created
[[email protected] ~]#_
To create a VG that uses multiple PVs, simply add multiple device arguments to the
vgcreate command. For example, the vgcreate vg00 /dev/sda5 /dev/sdb1 /dev/
sdc3 command would create a VG called vg00 that uses three PVs (/dev/sda5, /dev/sdb1,
and /dev/sdc3).
When creating a VG, it is important to choose the block size for saving data since it cannot
be safely changed later. This is called the physical extent (PE) size of the VG. A large PE size
results in larger write operations and a larger maximum filesystem size for LVs. For example,
a PE size of 32MB will allow for a maximum LV size of 2TB.
By default, the vgcreate command chooses an appropriate PE size according to the current
sizes of the PVs that are associated with the VG, but you can use the –s or
--physicalextentsize options to the vgcreate command to select a different PE size
during VG creation.
The vgdisplay command can be used to display detailed information about each VG. The
following vgdisplay command indicates that /dev/sda4 has over 31 GB of available space:
[[email protected] ~]# vgdisplay
--- Volume group --VG Name
vg00
System ID
Format
lvm2
Metadata Areas
1
Metadata Sequence No
1
VG Access
read/write
VG Status
resizable
MAX LV
0
Cur LV
0
Open LV
0
Max PV
0
Cur PV
1
Act PV
1
VG Size
32.00 GiB
PE Size
4.00 MiB
Total PE
8192
Alloc PE / Size
0/0
Free PE / Size
8192 / 32.00 GiB
VG UUID
C2jrUC-LZmR-0tSX-bLqj-43S0-8U7i-0ajUgP
[[email protected] ~]#_
Next, you can create LVs from the available space in your VG using the lvcreate
command and view your results using the lvdisplay command. The following commands
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
224
Chapter 5
Linux Filesystem Administration
create an LV called “data” that uses 15GB of space from vg00 as well as an LV called
“extras” that uses 16GB of space from vg00, and it displays the results.
[[email protected] ~]# lvcreate -L 15GB -n data vg00
Logical volume "data" created
[[email protected] ~]# lvcreate -L 16GB -n extras vg00
Logical volume "extras" created
[[email protected] ~]# lvdisplay
--- Logical volume --LV Path
/dev/vg00/data
LV Name
data
VG Name
vg00
LV UUID
hMCr4Z-JNrO-lVcf-5SZk-8UJg-B0ri-Am3siP
LV Write Access
read/write
LV Creation host, time
localhost.localdomain, 2015-11-25 07:42:30
LV Status
available
# open
0
LV Size
15.00 GiB
Current LE
3840
Segments
1
Allocation
inherit
Read ahead sectors
auto
- currently set to
256
Block device
253:0
--- Logical volume --LV Path
LV Name
VG Name
LV UUID
LV Write Access
LV Creation host, time
LV Status
# open
LV Size
Current LE
Segments
Allocation
Read ahead sectors
- currently set to
Block device
[[email protected] ~]#_
/dev/vg00/extras
extras
vg00
yUH5xW-U81b-CD3t-N22K-70gD-xHpm-TTHQca
read/write
localhost.localdomain, 2015-11-25 07:42:44
available
0
16.00 GiB
4096
1
inherit
auto
256
253:1
Notice from the preceding output that the new VGs can be accessed using the device files
/dev/vg00/data and /dev/vg00/extras. You can also refer to your new VGs using the device
files /dev/mapper/vg00-data and /dev/mapper/vg00-extras, which are typically used by the
system when accessing the filesystems on your VGs.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Working with Hard Disks and SSDs
225
You can work with these files as you would normally work with any other hard disk partition device file. For example, to create an ext4 filesystem on these devices and mount them
to the appropriate directories on the filesystem, you could use the following commands:
[[email protected] ~]# mke2fs –t ext4 /dev/vg00/data
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3932160 blocks
196608 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4026531840
120 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]# mke2fs –t ext4 /dev/vg00/extras
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1048576 inodes, 4194304 blocks
209715 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
128 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]# mkdir /data
[[email protected] ~]# mkdir /extras
[[email protected] ~]# mount /dev/vg00/data /data
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
226
Chapter 5
Linux Filesystem Administration
[[email protected] ~]# mount /dev/vg00/extras /extras
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/vg00-data on /data type ext4 (rw)
/dev/mapper/vg00-extras on /extras type ext4 (rw)
[[email protected] ~]#_
Next, you can edit the /etc/fstab file to ensure that your new logical volumes are automatically mounted at system startup, as shown here:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Aug 2 09:34:19 2015
#
# Accessible filesystems, by reference, are maintained under’/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for info
#
/dev/sda1
/
ext4
defaults
11
/dev/sda2
swap
swap
defaults
00
/dev/vg00/data
/data
ext4
defaults
00
/dev/vg00/extras
/extras
ext4
defaults
00
[[email protected] ~]#_
Three other useful commands that can display information about the PVs, VGs, and LVs that
are configured on your system are the pvscan command, the vgscan command, and
lvscan command, respectively. Also, you can add additional storage space to your LVs
after configuration. More specifically, you can add another PV using the pvcreate command, add this PV to your existing VG using the vgextend command, and then increase
the size of your LVs to use the additional space using the lvextend command. Before
using these commands, always consult the manual pages to ensure that you are using the
command options and arguments that will perform the desired actions.
Working with USB and Firewire-based Storage Devices
Because of their large capacity and portability, removable storage devices are commonly used
today to store backups of data, pictures, music, movies, programs, and documents. These
devices, which typically connect to your PC via high speed USB or Firewire cables, include:
• Flash memory drives
• External hard drives
• Digital cameras (that contain flash memory cards)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Monitoring Filesystems
•
Media players (such as Apple iPods)
•
Smartphones
•
Tablets
227
Nearly all removable storage device manufacturers emulate the SCSI protocol in the firmware
of the device itself. If a GUI environment is loaded, Fedora 20 recognizes removable media
devices that connect to your PC via USB or Firewire cables as SCSI devices and automatically
mounts them to a new directory under the /run/media/username directory named for the label
on the device, much like CDs and DVDs. Additionally, you will be able to access or safely
eject your device using the associated icon in the Files application within the GUI environment, as shown with the Cafe_Scientifique DVD in Figure 5-3.
Since it is common to work with removable media devices within a GUI environment, understanding the device names and mount point directories used by the devices themselves is often
irrelevant. You can work with the files on removable media devices by accessing the appropriate icons in the Files application that represent the devices in the GUI environment. However,
if you are working in a command-line terminal (perhaps on a Linux server that does not have
a GUI environment), you need to keep track of when a USB or Firewire storage device is
plugged into the computer in order to understand which device file the Linux operating system will assign to it.
For example, if your Linux system has two hard disks (/dev/sda and /dev/sdb) and a PATA
DVD drive (/dev/hda), the next available device file for a newly inserted USB flash drive
would be /dev/sdc and the first partition on that device would be referred to as /dev/sdc1. As
a result, you could create a mount point to the directory for the USB flash drive under the /
media directory and mount the device to this directory, as shown here (omitting the filesystem
type allows the mount command to auto-detect the filesystem type on the device):
[[email protected] ~]# mkdir /media/FlashDrive
[[email protected] ~]# mount /dev/sdc1 /media/FlashDrive
[[email protected] ~]#_
Monitoring Filesystems
After filesystems are created on devices and those devices are mounted to the directory tree,
they should be checked periodically for errors, disk space usage, and inode usage. This minimizes the problems that can occur as a result of a damaged filesystem and reduces the likelihood that a file cannot be saved due to insufficient disk space.
Disk Usage
Several filesystems can be mounted to the directory tree. As mentioned earlier, the more filesystems that are used, the less likely a corrupted filesystem will interfere with normal system
operations. Conversely, more filesystems typically results in less hard disk space per filesystem and might result in system errors if certain filesystems fill up with data. Many Linux
administrators create filesystems for the /, /usr, and /var directories during installation. The
/usr directory contains most utilities and installed programs on a Linux system, and it should
have enough space for future software installations. The /var directory grows in size continuously as it stores log files. Old log files should be removed periodically to leave room for new
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
228
Chapter 5
Linux Filesystem Administration
ones. The / filesystem is the most vital of these; it should always contain a great deal of free
space used as working space for the operating system. As a result, the / filesystem should be
monitored frequently. If free space on the / filesystem falls below 10 percent, the system
might suffer from poorer performance or cease to operate.
The easiest method for monitoring free space by mounted filesystems is to use the df (disk
free space) command, as shown in the following output:
[[email protected] ~]# df
Filesystem
/dev/sda1
devtmpfs
tmpfs
/dev/mapper/vg00-data
/dev/mapper/vg00-extras
[[email protected] ~]#_
1K-blocks
Used
30237648 7227280
422820
80
480640
272
15481840 169456
16513960 176108
Available
21474368
422760
480368
14525952
15498992
Use%
26%
1%
1%
2%
2%
Mounted on
/
/dev
/dev/shm
/data
/extras
From the preceding output, the only filesystems used are the / filesystem, the /data filesystem,
and the /extras filesystem; the /usr and /var directories are simply directories on the / filesystem, which increases the importance of monitoring the / filesystem. Because the / filesystem is
26 percent used in the preceding output, there is no immediate concern. However, log files
and software installed in the future will increase this number and might warrant the purchase
of an additional hard disk for data to reside on.
Alternatively, you can view the output of the df command in a more user-friendly (or
human-readable) format by using the –h option, which prints sizes in the most convenient
format (G ¼ gigabyte, M ¼ megabyte), as shown here:
[[email protected] ~]# df -h
Filesystem
/dev/sda1
devtmpfs
tmpfs
/dev/mapper/vg00-data
/dev/mapper/vg00-extras
[[email protected] ~]#_
Size
29G
413M
470M
15G
16G
Used
6.9G
80K
272K
166M
172M
Avail
21G
413M
470M
14G
15G
Use%
26%
1%
1%
2%
2%
Mounted on
/
/dev
/dev/shm
/data
/extras
It is important to remember that the df command only views mounted filesystems; thus, to
get disk free space statistics for a floppy filesystem, you should mount it prior to viewing the
output of the df command:
[[email protected] ~]# mount /dev/fd0
[[email protected] ~]# df
Filesystem
1K-blocks
Used
/dev/sda1
30237648 7227280
devtmpfs
422820
80
tmpfs
480640
272
/dev/mapper/vg00-data
15481840 169456
/dev/mapper/vg00-extras 16513960 176108
/dev/fd0
1412
17
floppy
Available
21474368
422760
480368
14525952
15498992
1323
Use%
26%
1%
1%
2%
2%
2%
Mounted on
/
/dev
/dev/shm
/data
/extras
/mnt/
[[email protected] ~]# umount /dev/fd0
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Monitoring Filesystems
[[email protected] ~]# df
Filesystem
/dev/sda1
devtmpfs
tmpfs
/dev/mapper/vg00-data
/dev/mapper/vg00-extras
[[email protected] ~]#_
1K-blocks
Used
30237648 7227280
422820
80
480640
272
15481840 169456
16513960 176108
Available
21474368
422760
480368
14525952
15498992
Use%
26%
1%
1%
2%
2%
229
Mounted on
/
/dev
/dev/shm
/data
/extras
If a filesystem is approaching full capacity, it might be useful to examine which directories on
that filesystem are taking up the most disk space such that you can remove or move files
from that directory to another filesystem that has sufficient space. To view the size of a directory and its contents in kilobytes, you can use the du (directory usage) command. If the
directory is large, you should use either the more or less command to view the output
page-by-page, as shown with the following /usr directory:
[[email protected] ~]# du /usr |more
56048
/usr/sbin
4
/usr/include/telepathy-1.0
48
/usr/include/python3.3m
40
/usr/include/python2.7
156
/usr/include
8
/usr/libexec/gedit
24
/usr/libexec/spice-gtk-x86_64
20
/usr/libexec/awk
2720
/usr/libexec/ipsec
28
/usr/libexec/plymouth
32
/usr/libexec/shotwell
8
/usr/libexec/totem
12
/usr/libexec/gstreamer-1.0
16
/usr/libexec/utempter
8
/usr/libexec/psacct
12
/usr/libexec/coreutils
1592
/usr/libexec/sssd
20
/usr/libexec/file-roller
36
/usr/libexec/man-db
12
/usr/libexec/gstreamer-0.10
432
/usr/libexec/sudo
1168
/usr/libexec/bluetooth
528
/usr/libexec/openssh
--More-To view only a summary of the total size of a directory, simply use the –s switch to the du
command, as shown in the following example with the /usr directory:
[[email protected] ~]# du –s /usr
5719324 /usr
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
230
Chapter 5
Linux Filesystem Administration
As with the df command, the du command also accepts the –h option to make the view more
human readable; the following indicates that the total size of the /usr directory is 5.5GB:
[[email protected] ~]# du –hs /usr
5.5G /usr
[[email protected] ~]# _
Recall that every filesystem has an inode table that contains the inodes for the files and
directories on the filesystem; this inode table is made during filesystem creation and is usually
proportionate to the size of the filesystem. Each file and directory uses one inode; thus, a filesystem with several small files might use up all of the inodes in the inode table and prevent
new files from being created on the filesystem. To view the total number of inodes and free
inodes for an ext2, ext3, or ext4 filesystem, you can use the dumpe2fs command with the
–h switch, as shown in the following output:
[[email protected] ~]# df
Filesystem
1K-blocks
Used Available Use% Mounted on
/dev/sda1
30237648 7227280
21474368
26% /
devtmpfs
422820
80
422760
1% /dev
tmpfs
480640
272
480368
1% /dev/shm
/dev/mapper/vg00-data
15481840 169456
14525952
2% /data
/dev/mapper/vg00-extras 16513960 176108
15498992
2% /extras
[[email protected] ~]# dumpe2fs -h /dev/sda1
dumpe2fs 1.42.8 (20-Jun-2013)
Filesystem volume name:
<none>
Last mounted on:
/
Filesystem UUID:
db545f1d-c1ee-4b70-acbe-3dc61b41db20
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
has_journal ext_attr resize_inode dir_index
filetype needs_recovery extent flex_bg sparse_super large_file huge_file
uninit_bg dir_nlink extra_isize
Filesystem flags:
signed_directory_hash
Default mount options:
user_xattr acl
Filesystem state:
clean
Errors behavior:
Continue
Filesystem OS type:
Linux
Inode count:
1921360
Block count:
7680000
Reserved block count:
384000
Free blocks:
5752659
Free inodes:
1663809
First block:
0
Block size:
4096
Fragment size:
4096
Reserved GDT blocks:
1022
Blocks per group:
32768
Fragments per group:
32768
Inodes per group:
8176
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Monitoring Filesystems
Inode blocks per group:
Flex block group size:
Filesystem created:
Last mount time:
Last write time:
Mount count:
Maximum mount count:
Last checked:
Check interval:
Lifetime writes:
Reserved blocks uid:
Reserved blocks gid:
First inode:
Inode size:
Required extra isize:
Desired extra isize:
Journal inode:
First orphan inode:
Default directory hash:
Directory Hash Seed:
Journal backup:
Journal features:
Journal size:
Journal length:
Journal sequence:
Journal start:
[[email protected] ~]# _
231
511
16
Mon Aug 2 09:27:13 2015
Thu Aug 12 10:55:03 2015
Mon Aug 2 10:24:23 2015
20
-1
Mon Aug 2 09:27:13 2015
0 (<none>)
13 GB
0 (user root)
0 (group root)
11
256
28
28
8
1705241
half_md4
cab24941-82b1-43a5-8b3f-18a93adbfd94
inode blocks
journal_incompat_revoke
128M
32768
0x000031fe
1
In the preceding output, you can see that there are 1921360 inodes in the inode table and
that 1663809 of them are free to use when creating new files and directories.
Checking Filesystems for Errors
Filesystems themselves can accumulate errors over time. These errors are often referred to as
filesystem corruption and are common on most filesystems. Those filesystems that are
accessed frequently are more prone to corruption than those that are not. As a result, such
filesystems should be checked regularly for errors.
The most common filesystem corruption occurs because a system was not shut down properly using the shutdown, poweroff, halt, or reboot commands. Data is stored in memory for a short period of time before it is written to a file on the hard disk. This process of
saving data to the hard disk is called syncing. If the computer’s power is turned off, data in
memory might not be synced properly to the hard disk and corruption might occur.
Filesystem corruption can also occur if the hard disks are used frequently for time-intensive
tasks such as database access. As the usage of any system increases, so does the possibility
for operating system errors when writing to the hard disks. Along the same lines, the physical
hard disks themselves are mechanical in nature and can wear during time. Some areas of the
hard disk platter might become unusable if they cannot hold a magnetic charge; these areas
are known as bad blocks. When the operating system finds a bad block, it puts a reference
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
232
Chapter 5
Linux Filesystem Administration
to that bad block in the bad blocks table on the filesystem. Any entries in the bad blocks
table are not used for any future disk storage.
To check a filesystem for errors, you can use the fsck (filesystem check) command, which
can check filesystems of many different types. The fsck command takes an option specifying
the filesystem type and an argument specifying the device to check; if the filesystem type is
not specified, the filesystem is automatically detected. It is also important to note that the filesystem being checked must be unmounted beforehand for the fsck command to work properly, as shown next:
[[email protected] ~]# fsck /dev/vg00/data
fsck from util-linux 2.24
e2fsck 1.42.8 (20-Jun-2013)
/dev/mapper/vg00-data is mounted.
e2fsck: Cannot continue, aborting.
[[email protected] ~]# umount /dev/vg00/data
[[email protected] ~]# fsck /dev/vg00/data
fsck from util-linux 2.24
e2fsck 1.42.8 (20-Jun-2013)
/dev/mapper/vg00-data: clean, 11/983040 files, 104064/3932160 blocks
[[email protected] ~]# _
Because the / filesystem cannot be unmounted, you should only run the
fsck command on the / filesystem from single-user mode (discussed
in Chapter 8) or from live installation media (discussed in Chapter 6).
Notice from the preceding output that the fsck command does not give lengthy output on
the terminal screen when checking the filesystem; this is because the fsck command only
performs a quick check for errors unless the –f option is used to perform a full check, as
shown in the following example:
[[email protected] ~]# fsck -f /dev/vg00/data
fsck from util-linux 2.24
e2fsck 1.42.8 (20-Jun-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg00-data: 11/983040 files (0.0% non-contiguous),
104064/3932160 blocks
[[email protected] ~]# _
Table 5-6 displays a list of common options used with the fsck command.
If the fsck command finds a corrupted file, it displays a message to the user asking whether
to fix the error; to avoid these messages, you may use the –a or –y options listed in Table 5-6
to specify that the fsck command should automatically repair any corruption. If there are
files that the fsck command cannot repair, it places them in the lost+found directory on
that filesystem and renames the file to the inode number.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Monitoring Filesystems
Option
Description
-f
Performs a full filesystem check
-a or -y
Allows
-A
Checks all filesystems in /etc/fstab that have a 1 or 2 in the sixth field
-Cf
Performs a full filesystem check and displays a progress line
-AR
Checks all filesystems in /etc/fstab that have a 1 or 2 in the sixth field but skips
the / filesystem
-V
Displays verbose output
233
fsck to automatically repair any errors
Table 5-6 Common options to the fsck Command
To view the contents of the lostþfound directory, simply mount the device and view the contents of the lostþfound directory immediately underneath the mount point. Because it is
difficult to identify lost files by their inode number, most users delete the contents of this
directory periodically. Recall that the lostþfound directory is automatically created when an
ext2, ext3 or ext4 filesystem is created.
Just as you can use the mke2fs command to make an ext2, ext3, or ext4 filesystem, you can
use the e2fsck command to check an ext2, ext3, or ext4 filesystem. The e2fsck command
accepts more options and can check a filesytem more thoroughly than fsck. For example,
by using the –c option to the e2fsck command, you can check for bad blocks on the hard
disk and add them to a bad block table on the filesystem so that they are not used in the
future, as shown in the following example:
[[email protected] ~]# e2fsck -c /dev/vg00/data
e2fsck 1.42.8 (20-Jun-2013)
Checking for bad blocks (read-only test): done
/dev/vg00/data: Updating bad block inode.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg00/data: ***** FILE SYSTEM WAS MODIFIED *****
/dev/vg00/data: 11/983040 files (0.0% non-contiguous), 104064/3932160 blocks
[[email protected] ~]# _
The badblocks command can be used to perform the same function as the e2fsck command with the –c option.
You cannot use the fsck command to check a btrfs filesystem.
Instead, you must use the btrfs command to check a btrfs filesystem after unmounting it. For example, the command btrfs check
/dev/sda5 will check the unmounted btrfs filesystem on /dev/sda5.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
234
Chapter 5
Linux Filesystem Administration
Recall from earlier in this chapter that the fsck command is run at boot time when filesystems are mounted from entries in the /etc/fstab file. Any entries in /etc/fstab that have a 1 in
the sixth field are checked first, followed by entries that have a 2 in the sixth field. However,
on many Linux systems a full filesystem check is forced periodically each time an ext2, ext3,
or ext4 filesystem is mounted. This might delay booting for several minutes or even hours,
depending on the size of the filesystems being checked. To change this interval, you can use
the –i option to the tune2fs command, as shown next:
[[email protected] ~]# tune2fs -i 0 /dev/vg00/data
tune2fs 1.42.8 (20-Jun-2013)
Setting interval between check 0 seconds
[[email protected] ~]# _
The tune2fs command can be used to change or “tune” filesystem parameters after a filesystem has been created. Changing the interval between checks to 0 seconds, as shown in the
preceding example, disables filesystem checks.
Hard Disk Quotas
If there are several users on a Linux system, there must be enough hard disk space to support the files that each user is expected to store on the hard disk. However, if hard disk
space is limited or company policy limits disk usage, you should impose limits on filesystem usage. These restrictions, called hard disk quotas, can be applied to users or groups
of users. Furthermore, quotas can restrict how many files and directories a user can create
(that is, restrict the number of inodes created) on a particular filesystem or the total size of
all files that a user can own on a filesystem. Two types of quota limits are available: soft
limits and hard limits. Soft limits are hard disk quotas that the user can exceed for a certain period of time (seven days by default), whereas hard limits are rigid quotas that the
user cannot exceed. Quotas are typically enabled at boot time if there are quota entries in
/etc/fstab, but they can also be turned on and off afterward by using the quotaon and
quotaoff commands, respectively.
By default, quota support is not installed on Fedora 20. To obtain quota support, you must
run the yum install quota command to download and install the quota package from an
Internet software repository. Following this, you can carry out the following steps to set up
quotas for the /data filesystem and restrict the user user1:
1. Edit the /etc/fstab file to add the usrquota and grpquota mount options for the /data
filesystem. The resulting /etc/fstab file should look like the following:
[[email protected] ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Aug 2 09:34:19 2015
#
# Accessible filesystems, by reference, are maintained under’/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for info
#
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hard Disk Quotas
/dev/sda1
/
/dev/sda2
swap
/dev/vg00/data
/data
[[email protected] ~]# _
ext4
swap
ext4
defaults
defaults
defaults,usrquota,grpquota
235
11
00
00
You can also use journaled quotas on modern Linux kernels, which
protects quota data during an unexpected shutdown. To use journaled quotas, simply replace the mount options of defaults,
usrquota,grpquota in Step 1 with: defaults,usrjquota
=aquota.user,grpjquota=aquota.group,jqfmt=
vfsv0 .
2. Remount the /data filesystem as read-write to update the system with the new options
from /etc/fstab. The command to do this is:
[[email protected] ~]# mount /data -o remount,rw
[[email protected] ~]# _
3. Run the quotacheck –mavugf –F vfsv0 command, which looks on the system
for file ownership and creates the quota database (-f) using the default quota format
(-F vfsv0) for all filesystems with quota options listed in /etc/fstab (-a), giving
verbose output (-v) for all users and groups (-u and -g) even if the filesystem is used
by other processes (-m). This creates and places information in the /data/aquota.user
and /data/aquota.group files. Sample output from the quotacheck command is
shown here:
[[email protected] ~]# quotacheck –mavugf –F vfsv0
quotacheck: Your kernel probably supports journaled quota but you are
not using it. Consider switching to journaled quota to avoid running
quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/sda2 [/] done
quotacheck: Checked 8983 directories and 109416 files
[[email protected] ~]# _
If you receive any warnings at the beginning of the quotacheck
output at this stage, you can safely ignore them since they are the
result of newly created aquota.user and aquota.group files that have
not been used yet.
4. Turn user and group quotas on for all filesystems that have quotas configured using the
quotaon -avug command:
[[email protected] ~]# quotaon -avug
/dev/mapper/vg00-data [/data]: group quotas turned on
/dev/mapper/vg00-data [/data]: user quotas turned on
[[email protected] ~]# _
You can also enable and disable quotas for individual filesystems. For
example, you can use the quotaon /data command to enable
quotas for the /data filesystem and the quotaoff /data command to disable them.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
236
Chapter 5
Linux Filesystem Administration
5. Edit the quotas for certain users by using the edquota command as follows: edquota
–u <username>. This brings up the vi editor and allows you to set soft and hard quotas for the number of blocks a user can own on the filesystem (typically, 1 block ¼ 1
kilobyte) and the total number of inodes (files and directories) that a user can own on
the filesystem. A soft limit and hard limit of zero (0) indicates that there is no limit. To
set a hard limit of 20MB (¼20480KB) and 1000 inodes, as well as a soft limit of 18MB
(¼18432KB) and 900 inodes, you can do the following:
[[email protected] ~]# edquota –u user1
Disk quotas for user user1 (uid 500):
Filesystem
blocks
soft
/dev/mapper/vg00-data
1188
0
~
~
~
~
"/tmp//EdP.aclpslv" 3L, 216C
hard
0
inodes
326
soft
0
hard
0
Next, place the appropriate values in the columns provided and then save and quit the vi
editor:
Disk quotas for user user1 (uid 500):
Filesystem
blocks
soft
/dev/mapper/vg00-data
1188 18432
~
~
~
~
:wq
"/tmp/EdP.aclpslv" 3L, 216C written
[[email protected] ~]# _
hard
20480
inodes
326
soft
900
hard
1000
6. Edit the time limit for which users can go beyond soft quotas by using the edquota –u
–t command. The default time limit for soft quotas is seven days, but it can be changed
as follows:
[[email protected] ~]# edquota –u -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem
Block grace period
Inode grace period
/dev/mapper/vg00-data
7days
7days
~
~
~
"/tmp//EdP.a1vzfSy" 4L, 233C
7. Ensure that quotas were updated properly by gathering a report for quotas by user on
the /data filesystem using the repquota command, as shown in the following output:
[[email protected] ~]# repquota /data
*** Report for user quotas on device /dev/mapper/vg00-data
Block grace time: 7days; Inode grace time: 7days
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter Summary
237
Block limits
File limits
User
used
soft
hard grace
used
soft hard grace
-----------------------------------------------––––––––- root
-- 6420573
0
0
376520
0
0
daemon
-8
0
0
3
0
0
lp
-4
0
0
1
0
0
polkitd -8
0
0
2
0
0
abrt
-24
0
0
4
0
0
colord
-20
0
0
4
0
0
geoclue -4
0
0
1
0
0
chrony
-12
0
0
3
0
0
tss
-12
0
0
2
0
0
unbound -8
0
0
2
0
0
pulse
-4
0
0
1
0
0
gdm
-128
0
0
32
0
0
user1
-1188 18432 20480
326
900 1000
[[email protected] ~]# _
Note that most users in the preceding output are system users and do not have quotas
applied to them. These users are discussed in Chapter 10.
The aforementioned commands are only available to the root user; however, regular users can
view their own quota using the quota command. The root user can also use the quota command but can also use it to view quotas of other users:
[[email protected] ~]# quota
Disk quotas for user root (uid 0): none
[[email protected] ~]# quota -u user1
Disk quotas for user user1 (uid 500):
Filesystem
blocks quota limit grace
/dev/mapper/vg00-data
1188 18432 20480
[[email protected] ~]# _
files quota limit grace
326
900
1000
Chapter Summary
■ Disk devices are represented by device files that reside in the /dev directory. These
device files specify the type of data transfer, the major number of the device driver in
the Linux kernel, and the minor number of the specific device.
■ Each disk device must contain a filesystem, which is then mounted to the Linux
directory tree for usage using the mount command. The filesystem can later be
unmounted using the umount command. The directory used to mount the device
must not be in use by any logged-in users for mounting and unmounting to take
place.
■ Hard disks must be partitioned into distinct sections before filesystems are created on
those partitions. To partition a hard disk with an MBR, you can use the fdisk,
cfdisk, or parted command. To partition a hard disk with a GPT, you can use the
gdisk or parted command.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
238
Chapter 5
Linux Filesystem Administration
■ Many different filesystems are available to Linux; each filesystem is specialized for a
certain purpose, and several different filesystems can be mounted to different mount
points on the directory tree. You can create a filesystem on a device using the mkfs
command and its variants.
■ The LVM can be used to create logical volumes from the free space within multiple
partitions on the various hard disks within your system. Like standard hard disk
partitions, logical volumes can contain a filesystem and be mounted to the Linux
directory tree. They allow for the easy expansion and reconfiguration of storage.
■ USB and Firewire storage devices are recognized as SCSI disks by the Linux system.
■ It is important to monitor disk usage using the df, du, and dumpe2fs commands to
avoid running out of storage space. Similarly, it is important to check disks for errors
using the fsck command and its variants.
■ If hard disk space is limited, you can use hard disk quotas to limit the space that each
user has on filesystems.
Key Terms
/dev directory
The directory off the root where device files are typically stored.
/etc/fstab
A file used to specify which filesystems to mount automatically at boot time and
queried by the mount command if an insufficient number of arguments are specified.
/etc/mtab
A file that stores a list of currently mounted filesystems.
/proc/devices
bad blocks
A file that contains currently used device information.
The areas of a storage medium unable to store data properly.
block
The unit of data commonly used by filesystem commands; a block can contain
several sectors.
block devices The storage devices that transfer data to and from the system in chunks of
many data bits by caching the information in RAM; they are represented by block device
files.
btrfs command
A command used to configure btrfs filesystem options and check btrfs
filesystems for errors.
cfdisk command A command used to partition hard disks; displays a graphical interface
in which the user can select partitioning options.
character devices The storage devices that transfer data to and from the system one data
bit at a time; they are represented by character device files.
cylinder
A series of tracks on a hard disk that are written to simultaneously by the
magnetic heads in a hard disk drive.
device file
A file used by Linux commands that represents a specific device on the system;
these files do not have a data section and use major and minor numbers to reference the
proper driver and specific device on the system, respectively.
df (disk free space) command
du (directory usage) command
A command that displays disk free space by filesystem.
A command that displays directory usage.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Key Terms
edquota command
fdisk command
239
A command used to specify quota limits for users and groups.
A command used to create, delete, and manipulate partitions on hard
disks.
filesystem
The organization imposed on a physical storage medium that is used to manage
the storage and retrieval of data.
filesystem corruption
The errors in a filesystem structure that prevent the retrieval of
stored data.
formatting
The process in which a filesystem is placed on a disk device.
fsck (filesystem check) command
A command used to check the integrity of a filesystem
and repair damaged files.
fuser command
A command used to identify any users or processes using a particular file
or directory.
gdisk (GPT fdisk) command A command used to create partitions on a GPT hard disk. It
uses an interface that is very similar to fdisk.
hard disk quotas The limits on the number of files, or total storage space on a hard disk
drive, available to a user.
hard limit
A hard disk quota that the user cannot be exceed.
Logical Volume (LV)
A volume that is managed by the LVM and comprised of free space
within a VG.
Logical Volume Manager (LVM)
A set of software components within Linux that can be
used to manage the storage of information across several different hard disks on a Linux
system.
lvcreate command
A command used to create LVM logical volumes.
lvdisplay command
A command used to view LVM logical volumes.
lvextend command
A command used to add additional space from VGs to existing LVM
logical volumes.
lvscan command
A command used to view LVM logical volumes.
major number
The number used by the kernel to identify which device driver to call to
interact properly with a given category of hardware; hard disk drives, CDs, and video cards
are all categories of hardware; similar devices share a common major number.
minor number The number used by the kernel to identify which specific hardware device,
within a given category, to use a driver to communicate with. See also major number.
mkfs (make filesystem) command
mkisofs command
A command used to format or create filesystems.
A command used to create an ISO image from one or more files on the
filesystem.
mknod command
A command used to re-create a device file, provided the major number,
minor number, and type (character or bock) are known.
mkswap command
A command used to prepare newly created swap partitions for use by
the Linux system.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
240
Chapter 5
Linux Filesystem Administration
mount command
A command used to mount filesystems on devices to mount point
directories.
mount point
The directory in a file structure to which something is mounted.
mounting
A process used to associate a device with a directory in the logical directory tree
such that users can store data on that device.
parted (GNU Parted) command
A command used to create partitions on a GPT or MBR
hard disk.
partition
A physical division of a hard disk drive.
physical extent (PE) size
The block size used by the LVM when storing data on a volume
group.
Physical Volumes (PVs)
A hard disk partition that is used by the LVM.
pvcreate command
A command used to create LVM physical volumes.
pvdisplay command
A command used to view LVM physical volumes.
pvscan command
quota command
A command used to view LVM physical volumes.
A command used to view disk quotas imposed on a user.
quotaoff command
quotaon command
quotas
A command used to deactivate disk quotas.
A command used to activate disk quotas.
The limits that can be imposed on users and groups for filesystem usage.
repquota command
A command used to produce a report on quotas for a particular
filesystem.
root filesystem
The filesystem that contains most files that make up the operating system;
it should have enough free space to prevent errors and slow performance.
sector The smallest unit of data storage on a hard disk; sectors are arranged into
concentric circles called tracks and can be grouped into blocks for use by the system.
soft limit
A hard disk quota that the user can exceed for a certain period of time.
swapoff command
A command used to disable a partition for use as virtual memory on
the Linux system.
swapon command
A command used to enable a partition for use as virtual memory on the
Linux system.
syncing
track
The process of writing data to the hard disk drive that was stored in RAM.
The area on a hard disk that forms a concentric circle of sectors.
tune2fs command
A command used to modify ext2 and ext3 filesystem parameters.
umount command A command used to break the association between a device and a
directory in the logical directory tree.
vgcreate command
A command used to create LVM VG.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
vgdisplay command
vgextend command
vgscan command
Volume Group (VG)
241
A command used to view LVM VG.
A command used to add additional physical volumes to an LVM VG.
A command used to view LVM VGs.
A group of PVs that are used by the LVM.
Review Questions
1. Which of the following commands can be used to create partitions on either a MBR or
GPT hard disk?
a.
gdisk
b. cfsck
c.
fdisk
d. parted
2. After a partition on a hard disk drive is formatted with a filesystem, all partitions on
that hard disk drive must use the same filesystem. True or False?
3. You want to see the filesystems that are presently in use on the system. What command
could you use?
a.
cat /etc/fstab
b. ls -l /etc/fstab
c.
cat /etc/mtab
d. ls -l /etc/fstab
4. Jim has just purchased two new SCSI hard disk drives and a controller card for them.
He properly installs the hardware in his machine. Before he can use them for data storage and retrieval, what must he do? (Choose all that apply.)
a.
Mount the two hard drives so they are accessible by the operating system.
b. Mount a filesystem to each of the hard disk drives.
c.
Create one or more partitions on each of the hard disk drives.
d. Use the vi editor to edit /etc/mtab and create an entry for the controller card and the
hard disk drives.
e.
Mount any partitions created on the two hard drives such that they are accessible
by the operating system.
f.
Format any partitions created with a valid filesystem recognized by Linux.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
242
Chapter 5
Linux Filesystem Administration
5. Given the following output from /etc/fstab, which filesystems will be automatically
checked on boot by the fsck command?
/dev/sda1
none
none
none
/dev/sdc2
/dev/dvd
/dev/fd0
a.
/
/dev/pts
/proc
/dev/shm
swap
/media/dvd
/media/floppy
ext4
devpts
proc
tmpfs
swap
iso9660
auto
defaults
gid=5,mode=620
defaults
defaults
defaults
noauto,ro
noauto
11
10
01
10
01
00
00
none, as fsck must be run manually for each filesystem
b. /, /dev/pts, and /dev/shm
c.
/, /proc, and swap
d. all of them, as fsck is run automatically at boot for all filesystems
6. A user mounts a device to a mount point directory and realizes afterward there are files
previously found within the mount point directory that are needed. What should this
user do?
a.
Nothing; the files are lost and cannot ever be accessed.
b. Nothing; the files could not have been there because you can only mount to empty
directories.
c.
Unmount the device from the directory.
d. Run the fsck command to recover the file.
e.
Look in the lostþfound directory for the file.
7. Which command is used to display the amount of free space that exists on a filesystem?
a.
fsck
b. quota
c.
du
d. df
8. What must you do to successfully run the fsck command on a filesystem?
a.
Run the fsck command with the -u option to automatically unmount the filesystem first.
b. Choose yes when warned that running fsck on a mounted filesystem can cause
damage.
c.
Unmount the filesystem.
d. Ensure that the filesystem is mounted.
9. Character devices typically transfer data more quickly than block devices. True or False?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
243
10. What does the du /var command do?
a.
shows the users connected to the /var directory
b. shows the size of all directories within the /var directory
c.
dumps the /var directory
d. displays the amount of free space in the /var directory
11. What does the command dumpe2fs –h do?
a.
backs up an ext2 filesystem
b. displays the number of inodes used and available in an ext2 filesystem
c.
dumps an ext2 filesystem
5
d. is not a valid command
12. The first floppy drive on the system is not responding. You enter the file /dev/fd0
command and receive the following output. What is the problem?
[[email protected] root]# file /dev/fd0
/dev/fd0: ASCII text
[[email protected] root]#
a.
The floppy drive cable has come loose.
b. There is no floppy disk in the drive.
c.
The device file has become corrupt.
d. The floppy drive is seen as a character device.
13. Which of the following statements are true? (Choose all that apply.)
a.
Quotas can only limit user space.
b. Quotas can only limit the number of files a user can own.
c.
Quotas can limit both user space and the number of files a user can own.
d. Hard limits can never be exceeded.
e.
Hard limits allow a user to exceed them for a certain period of time.
f.
Soft limits can never be exceeded.
g.
Soft limits allow a user to exceed them for a certain period of time.
h. Either a hard limit or a soft limit can be set, but not both concurrently.
14. A device file
a.
. (Choose all that apply.)
has no inode section
b. has no data section
c.
has no size
d. displays a major and minor number in place of a file size
e.
has a fixed size of 300 kilobytes
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
244
Chapter 5
Linux Filesystem Administration
15. Which of the following statements regarding LVM structure is correct?
a.
PVs are collections of VGs.
b. LVs are created from the free space available within PVs.
c.
VGs are comprised of one or more PVs.
d. PVs use the space within LVs to create VGs.
16. The lvextend command can be used to add additional unused space within a volume
group to an existing logical volume. True or False?
17. You plug a USB flash memory drive into a system that has two SATA hard disks. How
will the partition on this USB flash memory drive be identified by Linux?
a.
/dev/sda1
b. /dev/sda2
c.
/dev/sdb1
d. /dev/sdc1
18. Which command mounts all existing filesystems in /etc/fstab?
a.
mount -f
b. mount -a
c.
mount /etc/fstab
d. mount /etc/mtab
19. A user runs the fsck command with the -a option on a filesystem that is showing signs
of corruption. How would that user locate any files the system was unable to repair?
a.
Look in the root of the filesystem.
b. The system prompts the user for a target location when it comes across a file it
cannot repair.
c.
Mount the filesystem and check the lost+found directory underneath the mount
point.
d. View the contents of the directory /lostþfound.
20. Which command is used to format a partition on a hard disk drive with the ext4
filesystem?
a.
format_ext4 device
b. ext4mkfs device
c.
e2mkfs –t ext4 device
d. makeext4FS device
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
245
Hands-on Projects
These projects should be completed in the order given. The hands-on projects
presented in this chapter should take a total of three hours to complete. The
requirements for this lab include:
•
A computer with Fedora 20 installed according to Hands-on Project 2-1
Project 5-1
In this hands-on project, you view and create device files.
1. Boot your Fedora Linux virtual machine. After your Linux system has loaded, switch to
a command-line terminal (tty2) by pressing Ctrl+Alt+F2. Log in to the terminal using
the user name of root and the password of LNXrocks!.
2. At the command prompt, type ls –l /dev/tty6 and press Enter. What device does
/dev/tty6 represent? Is this file a block or character device file? Why? What are the
major and minor numbers for this file?
3. At the command prompt, type rm –f /dev/tty6 and press Enter. Next, type ls –l
/dev/tty6 at the command prompt and press Enter. Was the file removed successfully?
4. Switch to the command-line terminal (tty6) by pressing Ctrl+Alt+F6 and attempt to log
in to the terminal using the user name of root and the password of LNXrocks!. Were
you successful?
5. Switch back to the command-line terminal (tty2) by pressing Ctrl+Alt+F2, type the command mknod /dev/tty6 c 4 6 at the command prompt, and press Enter. What did
this command do? Next, type ls –l /dev/tty6 at the command prompt and press
Enter. Was the file re-created successfully?
6. At the command prompt, type reboot and press Enter. After your Linux system has
loaded, switch to a command-line terminal (tty6) by pressing Ctrl+Alt+F6 and log in to
the terminal using the user name of root and the password of LNXrocks!. Why were
you successful?
7. At the command prompt, type ls –l /dev/tty? and press Enter. What is similar
about all of these files? Is the major number different for each file? Is the minor number
different for each file? Why?
8. At the command prompt, type find /dev and press Enter to list all of the filenames
underneath the /dev directory. Are there many files? Next, type du –s /dev at the command prompt and press Enter. How large in kilobytes are all files within the /dev directory? Why?
9. At the command prompt, type cat /proc/devices | more and press Enter. Which
devices and major numbers are present on your system? What character devices have a
major number of 4? How does this compare with what you observed in Step 2?
10. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
246
Chapter 5
Linux Filesystem Administration
Project 5-2
In this hands-on project, you create a filesystem on a floppy disk and practice mounting it to
the directory tree.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2. Log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type poweroff and press Enter to shut down your virtual
machine.
3. In your virtualization software, create a new virtual floppy disk and attach it to your
virtual machine. Next, boot your virtual machine. After your Linux system has loaded,
switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2. Log in to the terminal using the user name of root and the password of LNXrocks!.
4. At the command prompt, type modprobe floppy and press Enter to load the floppy
driver into the Linux kernel.
5. At the command prompt, type mkdir /mymount and press Enter to create a new
mount point directory. Next, type ls –F /mymount at the command prompt and press
Enter. Are there any files in the /mymount directory? Next, type cp /etc/hosts /
mymount at the command prompt and press Enter. Next, type ls –F /mymount at the
command prompt and press Enter to verify that the hosts file was copied successfully.
6. At the command prompt, type mkfs –t ext2 /dev/fd0 and press Enter.
7. At the command prompt, type mount –t ext2 /dev/fd0 /mymount and press Enter.
Next, type mount at the command prompt and press Enter. Was your device successfully mounted to the /mymount directory?
8. At the command prompt, type ls -F /mymount and press Enter. What files do you
see? Why? What happened to the hosts file? Next, type cp /etc/inittab /mymount
at the command prompt and press Enter. At the command prompt, type ls -F
/mymount and press Enter to verify that the file was copied to your device.
9. At the command prompt, type umount /mymount and press Enter. Next, type mount
at the command prompt and press Enter. Was the device successfully unmounted from
the /mymount directory?
10. At the command prompt, type ls -F /mymount and press Enter. What files do you see?
Why? What happened to the inittab file and lost+found directory? Is the hosts file present?
11. At the command prompt, type mount –t ext2 /dev/fd0 /mymount and press Enter.
12. At the command prompt, type cd /mymount and press Enter. Next, type ls –F and
press Enter. Are the inittab file and lost+found directory available again?
13. At the command prompt, type umount /mymount and press Enter. What error message
did you receive? Next, type fuser –u /mymount and press Enter. Who is using the
/mymount directory?
14. At the command prompt, type cd and press Enter to return to your home directory.
Next, type umount /mymount at the command prompt and press Enter. Did you
receive an error message? Type mount at the command prompt and press Enter to verify that the device was successfully unmounted from the /mymount directory.
15. Type exit and press Enter to log out of your shell.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
247
Project 5-3
In this hands-on project, you mount DVDs to the directory tree and view their contents.
1. Switch to a graphical terminal (tty1) by pressing Ctrl+Alt+F1 and log in to the GNOME
desktop using the user name of user1 and the password of LNXrocks!.
2. Click the Activities menu and select the Files icon to open the Files application.
3. In your virtualization software, attach the DVD ISO image for Fedora Linux
(Fedora-Live-Desktop-x86_64-20-1.iso) to the DVD drive for the virtual machine. After
you have completed this action, view your Files application. Is there an icon that represents
your Fedora Live Desktop DVD in the left pane? Place your mouse over this icon to view the
mount point directory. Which directory was your DVD automatically mounted to?
4. Take a few moments to explore the contents of the DVD within the Files application.
When finished, click the eject button next to the Fedora Live Desktop DVD icon to
eject the DVD.
5. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the
GNOME desktop using the user name of root and the password of LNXrocks!.
6. In your virtualization software, attach the DVD ISO image for Fedora Linux
(Fedora-Live-Desktop-x86_64-20-1.iso) to the DVD drive for the virtual machine. After you
have completed this action, type mount and press Enter. Is the DVD automatically mounted?
7. Next, type mount /dev/cdrom /mymount at the command prompt, and press Enter. What
warning did you receive? Was the DVD successfully mounted to the /mymount directory?
8. At the command prompt, type ls -F /mymount and press Enter. Do you see the same
files as you did in Step 4? Why?
9. At the command prompt, type ls –l /dev/cdrom and press Enter. What target file
does the /dev/cdrom symbolic link point to?
10. At the command prompt, type mount and press Enter. Note the line at the bottom that
lists your mounted DVD. What filesystem was automatically detected earlier when you
mounted your DVD? Is the device file identical to the target file shown in Step 9?
11. Next, type umount /mymount at the command prompt and press Enter. Was the DVD
successfully unmounted from the /mymount directory? Type the mount command at a
command prompt and press Enter to verify this.
12. In your virtualization software, deattach the DVD ISO image for Fedora Linux
(Fedora-Live-Desktop-x86_64-20-1.iso) to the DVD drive for the virtual machine. This
performs the same action as ejecting the DVD from the physical DVD drive.
13. Type exit and press Enter to log out of your shell.
Project 5-4
In this hands-on project, you work with standard hard disk partitions. You will first create a
hard disk partition using the fdisk utility. Next, you create an ext4 filesystem on the partition and mount it to the directory tree. Finally, you use the /etc/fstab file to automatically
mount the partition at boot time.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
248
Chapter 5
Linux Filesystem Administration
2. At the command prompt, type fdisk device_file and press Enter, where
device_file is the device file for your first hard disk (/dev/sda if you have a SCSI or
SATA hard disk, or /dev/hda if you have a PATA hard disk). At the fdisk prompt,
type m and press Enter to view the various fdisk commands.
3. At the fdisk prompt, type p and press Enter to view the partition table on your hard
disk. Which three partitions are present? When were they created? What are their
types?
4. At the fdisk prompt, type n and press Enter to create a new partition. Next, type e to
select a primary partition and press Enter. When prompted for the start sector, observe
the valid range within the brackets and press Enter to select the default (the first available sector). When prompted for the end cylinder, observe the valid range within the
brackets and press Enter to select the default (the last available sector).
5. At the fdisk prompt, type p and press Enter to view the partition table on your hard
disk. How many partitions are present? What type of partition is /dev/hda4 or /dev/
sda4?
6. At the fdisk prompt, type n and press Enter to create a new partition. Note that logical drive is automatically selected as the partition type since all available primary partitions have been used and there is already an extended partition. When prompted for the
start sector, observe the valid range within the brackets and press Enter to select the
default (the first available sector). When prompted for the end cylinder, type +1GB and
press Enter.
7. At the fdisk prompt, type p and press Enter to view the partition table on your hard
disk. How many partitions are present? What type of partition is /dev/hda5 or /dev/
sda5?
8. At the fdisk prompt, type l and press Enter to view the different partition types. What
type is used for Linux swap? Which character would you type at the fdisk prompt to
change the type of partition?
9. At the fdisk prompt, type w and press Enter to save the changes to the hard disk and
exit the fdisk utility.
10. At the command prompt, type reboot and press Enter to reboot your machine and
ensure that the partition table was read into memory correctly. After your Linux
system has been loaded, switch to a command-line terminal (tty2) by pressing
Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password
of LNXrocks!.
11. At the command prompt, type mkfs –t ext4 device_file and press Enter, where
device_file is the device file for the first logical drive on your first hard disk (/dev/
sda5 if you have a SCSI or SATA hard disk, or /dev/hda5 if you have a PATA hard
disk).
12. At the command prompt, type mkdir /newmount and press Enter to create a mount
point directory underneath the / directory for mounting the third partition on your first
hard disk.
13. At the command prompt, type mount –t ext4 device_file /newmount and press
Enter, where device_file is the device file for the first logical drive on your first
hard disk (/dev/sda5 if you have a SCSI or SATA hard disk, or /dev/hda5 if you have a
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
249
PATA hard disk). This will mount the filesystem on your first logical drive in the
extended partition to the /newmount directory. Next, type the mount command and
press Enter to verify that the filesystem was mounted correctly.
14. At the command prompt, type ls –F /newmount and press Enter. Is the lost+found
directory present? Next, type cp /etc/hosts /newmount at the command prompt
and press Enter to copy the hosts file to the new partition. Verify that the copy was successful by typing the ls –F /newmount command at the command prompt again, and
press Enter.
15. At the command prompt, type umount /newmount and press Enter. Next, type the
mount command and press Enter to verify that the filesystem was unmounted
correctly.
16. At the command prompt, type vi /etc/fstab and press Enter. Observe the contents
of the file. Add a line to the bottom of the file as shown below, where device_file
is the device file for the first logical drive on your first hard disk (/dev/sda5 if you have
a SCSI or SATA hard disk, or /dev/hda5 if you have a PATA hard disk).
device_file
/newmount
ext4
defaults
0 0
17. Save your changes and quit the vi editor.
18. At the command prompt, type reboot and press Enter. After your Linux system has
been loaded, switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log
in to the terminal using the user name of root and the password of LNXrocks!.
19. At the command prompt, type mount and press Enter. Is the third partition on your
hard disk mounted? Why?
20. At the command prompt, type umount /newmount and press Enter. Next, type the
mount command to verify that the filesystem was unmounted correctly.
21. At the command prompt, type mount -a and press Enter. Next, type the mount command and press Enter. Is the third partition on your hard disk mounted? Why?
22. Type exit and press Enter to log out of your shell.
Project 5-5
In this hands-on project, you create a new partition using the GNU Parted utility, and configure the LVM to host two logical volumes using the space within. Next, you will format
these logical volumes and mount them to the directory tree, as well as edit the /etc/fstab file
to ensure that they are mounted at boot time.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type parted device_file and press Enter, where
device_file is the device file for your first hard disk (/dev/sda if you have a SCSI or
SATA hard disk, or /dev/hda if you have a PATA hard disk). At the parted prompt, type
help and press Enter to view the available commands.
3. At the parted prompt, type print and press Enter. Write down the End value for your
(A). Next, write down the End value
first logical drive (/dev/sda5 or /dev/hda5):
(B). These two values reprefor your extended partition (/dev/sda4 or /dev/hda4):
sent the start and end of the remainder of the free space on your virtual hard disk.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
250
Chapter 5
Linux Filesystem Administration
4. At the parted prompt, type mkpart and press Enter to accept the default of logical
drive. Press Enter again to accept the default partition type (Linux ext2). When
prompted for the Start of the new partition, enter the (A) value you recorded in Step 3
and press Enter. When prompted for the End of the new partition, enter the (B) value
you recorded in Step 3 and press Enter.
5. At the parted prompt, type p and press Enter to view the partition table on your hard
disk. How many partitions are present? What type of partition is /dev/sda6 or /dev/
hda6?
6. At the parted prompt, type quit and press Enter to save the changes to the hard disk
and exit the GNU Parted utility.
7. At the command prompt, type reboot and press Enter to reboot your machine and
ensure that the partition table was read into memory correctly. After your Linux system
has been loaded, switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and
log in to the terminal using the user name of root and the password of LNXrocks!.
8. At the command prompt, type pvcreate device_file and press Enter, where
device_file is the device file for your new partition (/dev/sda6 if you have a SCSI
or SATA hard disk, or /dev/hda6 if you have a PATA hard disk). What does this
command do?
9. At the command prompt, type vgcreate vg00 device_file and press Enter, where
device_file is the device file for your new partition (/dev/sda6 if you have a SCSI
or SATA hard disk, or /dev/hda6 if you have a PATA hard disk). What does this
command do?
10. At the command prompt, type lvcreate –L 2GB –n volume1 vg00 and press Enter
to create a 2GB logical volume called volume1 from the vg00 volume group.
11. At the command prompt, type lvcreate –L 2.5GB –n volume2 vg00 and press
Enter to create a 2.5GB logical volume called volume2 from the vg00 volume group.
12. At the command prompt, type mkfs –t ext4 /dev/vg00/volume1 and press Enter
to format the volume1 logical volume using the ext4 filesystem. Next, type mkfs –t
ext4 /dev/vg00/volume2 and press Enter to format the volume2 logical volume
using the ext4 filesystem.
13. At the command prompt, type mkdir /volume1 and press Enter to create a mount
point for the volume1 logical volume. Next, type mkdir /volume2 and press Enter to
create a mount point for the volume2 logical volume.
14. At the command prompt, type mount –t ext4 /dev/vg00/volume1 /volume1 and
press Enter to mount the volume1 logical volume to the /volume1 directory. Next, type
mount –t ext4 /dev/vg00/volume2 /volume2 and press Enter to mount the volume2 logical volume to the /volume2 directory. When finished, type mount and press
Enter to verify that both filesystems are mounted.
15. At the command prompt, type ls –F /volume1 /volume2 and press Enter. Is there a
lostþfound directory underneath each directory? Why?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
251
16. At the command prompt, type vi /etc/fstab and press Enter. Add the following
lines to the bottom of the file, as shown below, to ensure that the volume1 and volume2
logical volumes are mounted at boot time:
/dev/vg00/volume1
/dev/vg00/volume2
/volume1
/volume2
ext4
ext4
defaults
defaults
00
00
17. Save your changes and quit the vi editor.
18. At the command prompt, type reboot and press Enter. After your Linux system has
been loaded, switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log
in to the terminal using the user name of root and the password of LNXrocks!.
19. At the command prompt, type mount and press Enter. Are volume1 and volume2
mounted? Why? What device file is listed for volume1 and volume2?
20. At the command prompt, type the following commands in turn (pressing Enter after
each one) and view the information displayed regarding your physical volumes volume
group, and logical volumes:
pvdisplay
pvscan
vgdisplay
vgscan
lvdisplay
lvscan
21. Type exit and press Enter to log out of your shell.
Project 5-6
In this hands-on project, you view disk usage and check filesystems for errors.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type df and press Enter. What filesystems are displayed? Can
you see the swap partition? Why?
3. At the command prompt, type dumpe2fs –h /dev/vg00/volume1 and press Enter.
How many inodes are available to this filesystem? How many inodes are free to be
used? Why?
4. At the command prompt, type fsck /dev/vg00/volume1 and press Enter. What
error message do you receive and why?
5. At the command prompt, type umount /volume1 and press Enter. Next, type the
mount command and press Enter to verify that the filesystem was unmounted
correctly.
6. At the command prompt, type fsck /dev/vg00/volume1 and press Enter. How long
did the filesystem check take and why?
7. At the command prompt, type fsck –f /dev/vg00/volume1 and press Enter. How
long did the filesystem check take and why?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
252
Chapter 5
Linux Filesystem Administration
8. At the command prompt, type e2fsck –c /dev/vg00/volume1 and press Enter.
What does this command do?
9. At the command prompt, type tune2fs –i 0 /dev/vg00/volume1 and press Enter
to change the interval for forced checks such that they are avoided. Is this a good idea
for the ext4 filesystem? Why?
10. At the command prompt, type mount /dev/vg00/volume1 and press Enter. Next,
type the mount command and press Enter to verify that the filesystem was mounted
correctly. Why did the mount command work even though you didn’t specify the
mount point directory?
11. Type exit and press Enter to log out of your shell.
Project 5-7
In this hands-on project, you enable, set, and view disk quotas for the /newmount filesystem
created earlier.
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type chmod 777 /newmount to give all users the ability to
create files within the /newmount directory.
3. Switch to a command-line terminal (tty3) by pressing Ctrl+Alt+F3 and log in to the terminal using the user name of user1 and the password of LNXrocks!.
4. At the command prompt, type touch /newmount/samplefile to create a file
in /newmount that is owned by the user user1.
5. Type exit and press Enter to log out of your shell.
6. Switch back to tty2 by pressing Ctrl+Alt+F2 and note that you are still logged in as the
root user on this terminal.
7. At the command prompt, type vi /etc/fstab and press Enter. Observe the options
for the /newmount filesystem. If your system uses a SATA or SCSI hard disk, change
the line that mounts /dev/sda5 to the following:
/dev/sda5 /newmount ext4 defaults,usrquota,grpquota
0 0
If your system uses a PATA hard disk, change the line that mounts /dev/hda5 to the
following:
/dev/hda5 /newmount ext4 defaults,usrquota,grpquota
0 0
8. Save your changes and quit the vi editor.
9. Remount the filesystem as read-write by typing the command mount /newmount –o
remount,rw and press Enter.
10. At the command prompt, type yum install quota and press Enter to begin the installation process of the quota package from an Internet repository. Accept the download
when prompted by typing y and press Enter.
11. At the command prompt, type quotacheck –mavugf –F vfsv0 and press Enter.
Ignore any warnings that appear. What does this command do? Next, type ls –l
/newmount and press Enter. What are the sizes of the aquota.user and aquota.group
files? What are these files used for?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
253
12. At the command prompt, type quotaon -avug and press Enter to activate quotas for
all partitions that have quota options defined within /etc/fstab.
13. At the command prompt, type edquota –u user1 and press Enter. Are there any
quota limits applied to the user user1 by default? Change the value of the soft quota
for blocks to 50000 and the value of the hard quota for blocks to 60000. Similarly,
change the value of the soft quota for inodes to 300 and the value of the hard quota
for inodes to 400. How many files and directories can user1 create on this partition?
How much space can user1 use in total on this partition?
14. Save your changes and quit the vi editor.
15. At the command prompt, type edquota –u -t and press Enter. Change the time limit
for users who extend the soft limit to 5 days for both inodes and blocks.
16. Save your changes and quit the vi editor.
17. At the command prompt, type repquota /newmount and press Enter. Are the quota
changes you made for the user user1 visible? How many files has user1 stored on this
volume so far? What is the total size of those files (in 1KB blocks) and why?
18. At the command prompt, type quota –u user1 and press Enter. How do the values
compare with those from the previous step?
19. Type exit and press Enter to log out of your shell.
Discovery Exercises
1. Answer the following questions regarding your system by using the commands listed in
this chapter. For each question, write the command you used to obtain the answer.
a.
What are the total number of inodes in the root filesystem? How many are currently utilized? How many are available for use?
b. What filesystems are currently mounted on your system?
c.
What filesystems are available to be mounted on your system?
d. What filesystems will be automatically mounted at boot time?
2. List the major numbers for the following devices (if they are present on your system):
a.
fd0
b. fd1
c.
fd3
d. hda1
e.
hda2
f.
hda3
g.
sda1
h. sda2
i.
sda3
How do they compare? Is there a pattern? Why or why not?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
5
254
Chapter 5
Linux Filesystem Administration
3. Use the Internet to gather information on four filesystems compatible with Linux. For
each filesystem, list the situations for which the filesystem was designed and the key
features that the filesystem provides.
4. You have a Linux system that has a 1000GB hard disk drive, which has a 90GB
partition containing an ext4 filesystem mounted to the / directory and a 4GB swap
partition. Currently, this Linux system is only used by a few users for storing small files;
however, the department manager wants to upgrade this system and use it to run a
database application that will be used by 100 users. The database application and the
associated data will take up over 200GB of hard disk space. In addition, these 100 users
will store their personal files on the hard disk of the system. Each user must have a
maximum of 5GB of storage space. The department manager has made it very clear that
this system must not exhibit any downtime as a result of hard disk errors. How much
hard disk space will you require, and what partitions would you need to ensure that the
system will perform as needed? Where would these partitions be mounted? What quotas
would you implement? What commands would you need to run and what entries to /etc/
fstab would you need to create? Justify your answers.
5. You have several filesystems on your hard disk that are mounted to separate directories
on the Linux directory tree. The /dev/sdc6 filesystem was unable to be mounted at boot
time. What could have caused this? What commands could you use to find more
information about the nature of the problem?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
6
Linux Server Deployment
After completing this chapter, you will be able to:
•
•
•
•
•
•
•
Identify the types of hardware present in most server systems
Describe the configuration of SCSI devices
Explain the different levels of RAID and types of RAID configurations
Configure the ZFS filesystem
Install a Linux server distribution
Troubleshoot the Linux server installation process
Access an installed system using system rescue
255
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
256
Chapter 6
Linux Server Deployment
In Chapter 2, you examined a standard Linux installation process using common
hardware components and practices. This chapter examines the specialized hardware and software configuration that will affect your choices during the installation of a Linux server distribution. In addition, you install the Ubuntu Server Linux distribution as well as learn how to
deal with common installation problems. Finally, this chapter discusses how to access and
use system rescue.
Understanding Server Hardware
Recall from Chapter 2 that the minimum hardware requirements for Fedora 20 include a
meager 1GHz CPU, 1GB of RAM, and 10GB of disk space, which is far lower than most
modern operating systems. However, Linux is incredibly scalable and often configured to
work with far more hardware to perform a specialized set of tasks. For example, a Linux
computer used as a desktop workstation will usually require enough RAM to run GNOME
and desktop applications smoothly, as well as a modern CPU and plenty of disk space to
store files, pictures, movies, and so on. An Intel Core i7 system with 8GB of RAM and a
1TB hard disk drive is closer to the type of hardware that you may find on a typical Linux
desktop workstation. If that workstation is for personal use or gaming, expect to add a highend graphics card to the mix that is supported by game platforms, such as Steam. For server
computers, the amount of hardware should adequately support its intended use. A Linux Web
server that handles e-commerce and a database engine will likely require at least 64 GB of
RAM, multiple CPUs, and high-capacity SSDs to host the operating system, Web server software, and database engine.
Nearly all standard server hardware is supported by Linux. If you have recently purchased
modern server hardware, there is a high likelihood that your Linux distribution will have all
of the drivers that you need. However, if your system has specialized hardware (e.g., a specific
Fibrechannel SAN controller), it is important to first verify with the hardware vendor that it
has an adequate driver included for your Linux distribution (or available for download).
Moreover, server hardware has a different form factor compared to other computers. Nearly
all servers within an organization are housed within a rackmount case that is mounted alongside other servers on a vertical server storage rack. Consequently, we call these servers rackmount servers.
Rackmount servers are sometimes called blade servers.
Each rackmount server in the rack may contain a different operating system (or multiple operating systems if virtualization software is used) and will connect to a shared monitor/keyboard/mouse. This shared monitor/keyboard/mouse often folds away into the rack for storage,
and it is necessary for initial configuration tasks such as server installation. All other server
administration is performed remotely using the remote administration methods discussed in
Chapter 12.
Most racks also contain one or more Storage Area Network (SAN) devices that provide a
large amount of storage for the servers within the rack as well as one or more uninterruptible
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Understanding Server Hardware
257
power supply (UPS) devices that provide backup battery power to servers and SANs within
the rack in the event of a power loss.
The minimum height of a rackmount server is 1.75 inches; this is called a 1U server. Most 1U
servers have up to two hard drives (or SSDs) and up to two CPUs. Other rackmount servers
take up more than one spot on the rack and have a height that is a multiple of a 1U server.
For example, a 2U server is twice as high as a 1U server and often contains up to four CPUs
and eight hard disks (or SSDs). Rackmount servers rarely exceed 4U.
Figure 6-1 shows a sample server rack configuration that hosts three 1U servers (Web server,
file server and firewall server), two 2U servers (database server and email server), a 2U UPS, a
4U SAN, and a management station with a shared monitor/keyboard/mouse.
Virtualization software is commonly used within server environments
today. Consequently, each rackmount server shown in Figure 6-1
could host multiple server operating systems concurrently to better
utilize the hardware installed on the rack.
Web server
File server
Firewall server
Database server
Email server
UPS
SAN
Management station
(shared monitor/
keyboard/mouse)
Rack
Figure 6-1 A sample server rack
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
258
Chapter 6
Linux Server Deployment
Configuring Server Storage
During the Fedora 20 installation process, described in Chapter 2, one of the most important
configuration tasks involves configuring the permanent storage devices that will host the
Linux operating system. This involves selecting the storage devices that will be used as well
as creating partitions and filesystems. The storage that you configure during installation
depends on your specific storage technologies and the space needs of your Linux system. In
this section, we examine the configuration of advanced storage technologies that are commonly used on Linux servers, including SCSI, RAID, and ZFS.
You can choose from many different advanced storage technologies.
However, because many of these technologies involve proprietary
hardware and are used primarily on specialized systems, we will limit
our discussion to the general-use technologies discussed in this section. To learn more about configuring other advanced storage technologies during a Linux server installation, consult the installation
guide available on the Linux distribution’s Web site.
SCSI Hard Disk Configuration
The Small Computer System Interface (SCSI) was designed as a way to connect multiple peripherals to the system in a scalable, high-speed manner. In most systems, a SCSI device is
connected to a controller card, which, in turn, connects all devices attached to it to the system. However, many other types of SCSI disk configurations and technologies are available,
including Parallel SCSI, Serial Attached SCSI, and iSCSI.
Parallel SCSI Configuration Parallel SCSI, which is the traditional SCSI technology,
relies on ribbon cables to transmit information between the hard disk and SCSI controller.
Disk devices can attach to the SCSI controller card via one cable, with several connectors
for the devices to plug in to. Information is then sent from device to device along this cable
in a daisy-chain fashion. To prevent signals from bouncing back and forth on the cable,
each end of the cable must be terminated with a device that stops signals from being perpetuated. This device is called a terminator. Typically, one terminator is on the controller card
itself, as shown in the top half of Figure 6-2. Some systems that have several hard drives
attached to one controller, however, typically place the controller in the middle of the daisy
chain, as shown in the bottom half of Figure 6-2.
SCSI disk drives must be configured such that each hard disk drive can be uniquely identified by the system; this is accomplished by assigning a unique ID number known as a SCSI
ID or target ID to each device. Most Parallel SCSI controllers support up to 15 devices and
identify these devices with the numbers 0–15 (one number must be reserved for the controller card itself). This SCSI ID also gives priority to the device. The highest priority device is
given the number 7, followed by 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, and 8.
The SCSI ID of a SCSI hard disk can be configured using software on
the SCSI controller or using jumper switches on the physical hard
drive itself.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
SCSI ribbon cable
259
Terminator
Terminator
Device 1
Device 2
Device 3
Controller card
SCSI ribbon cable
Terminator
Terminator
6
Device 1
Device 2
Device 3
Controller card
Figure 6-2 Connecting Parallel SCSI devices
Some SCSI devices act as a gateway to other devices; if this is the case,
each device is associated with a unique Logical Unit Number (LUN).
Parallel SCSI technology has evolved over time; it was initially adopted as an industrydefined standard in 1986. At that time, SCSI used an 8-bit-wide data path on a controller
card that held up to seven devices and had a data transfer speed of 5MB per second. This
was commonly referred to as SCSI-1 (SCSI Standard 1). By 1994, it had evolved to a standard that used a 16-bit-wide data path on a controller card that could hold up to 15 devices
and had a transfer speed of 20MB per second. This advance was referred to as SCSI-2 (SCSI
Standard 2). SCSI-3 was introduced a short time later and provided speeds of over 160MB
per second. Table 6-1 describes various SCSI technologies.
Before you install a Linux system, you must first ensure that you configure your Parallel SCSI
devices properly. First, verify that all of your SCSI components (SCSI controller, cables,
connectors, hard disks, terminators) support the same technology as listed in Table 6-1.
Next, ensure that your SCSI components are connected properly, as shown in Figure 6-1.
Finally, make sure your system recognizes the hard drives at system startup. To do this, examine the system BIOS or enter the SCSI BIOS on your SCSI controller card. When you start the
Linux installation, your SCSI hard disks will be detected automatically as /dev/sda, /dev/sdb,
and so on.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
260
Chapter 6
SCSI Type
Linux Server Deployment
Speed (MB/s)
Bus Width (Bits)
Connector
Number of Devices
Supported
5
8
50-pin Centronics or 50-pin
LPT (Line Port Terminal)
type
7
SCSI-2 (Fast)
10
8
50-pin LPT type
7
SCSI-2 (Wide)
20
16
68-pin LPT type
15
40
80
160
320
640
16
68-pin LPT type or 80-pin
SCA (Single Connector
Adapter) type
15
SCSI-1 (Narrow/Slow)
SCSI-3
SCSI-3
SCSI-3
SCSI-3
SCSI-3
(Ultra)
(Ultra2 Wide)
(Ultra3 Wide)
(Ultra320)
(Ultra640)
Table 6-1 Common SCSI standards
Most SCSI controllers add a second BIOS to your system that is
started after the system BIOS. You can interact with this SCSI BIOS
at system startup by pressing a key combination that is unique to
your SCSI controller manufacturer. For example, Adaptec SCSI controllers allow you to enter the SCSI BIOS by pressing the Ctrl+A key
combination at system startup.
Parallel SCSI hard disks are rarely found on modern Linux servers.
However, because Linux performs well with limited hardware, many
organizations install Linux on legacy servers to extend their lifetime
within the organization and save costs. These legacy servers often
use parallel SCSI hard disks.
Serial Attached SCSI Configuration Serial Attached SCSI is a newer SCSI technology that can transfer data at up to 768MB/s. Up to 65,535 Serial Attached SCSI hard disks
can be connected to a single SCSI controller via serial cables with small serial connectors
(between 7 and 36 pins).
Before you install Linux on a system that includes Serial Attached SCSI hard disks, you must
first connect the hard disks to the SCSI controller via the correct serial cable. Then, you
must ensure that the hard disks are detected properly by the system or SCSI BIOS. All
other Serial Attached SCSI configuration (SCSI ID, LUN, etc.) is performed automatically
by the SCSI controller but can be changed manually if you access the SCSI BIOS.
As with Parallel SCSI disks, your Linux installation will detect your Serial Attached SCSI
hard disks using /dev/sda, /dev/sdb, and so on.
FireWire (IEEE 1394) is also a serial-based SCSI transfer protocol. It
can be used to transfer data to an external hard disk at speeds of
over 800MB/s. Unfortunately, FireWire hard disks are not supported
for hosting the Linux operating system during installation. Like USB
hard disks, they are only intended to be used as removable media
and are treated like USB hard disks by the Linux OS.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
261
iSCSI Configuration Internet SCSI (iSCSI) is a recent SCSI technology that uses network
cables to transfer data to and from remote hard disks that reside within a SAN or remote system,
either on the local network or across the Internet. The computer connected to a remote hard disk
via iSCSI is referred to as an iSCSI initiator, and the remote hard disk is called the iSCSI target.
A iSCSI initiator can consist of a software component that is part of the Linux OS, or it can consist of a hardware component that is part of the iSCSI-compliant network card. Typically, iSCSI
targets are contained within external network-attached SAN devices that contain multiple hard
disks or SSDs (some SAN devices contain over 50 hard disks or SSDs).
A single iSCSI target can be used by multiple computers or iSCSI
initiators across a network. The computers that use the same iSCSI
target are said to be part of the same SAN. As a result, iSCSI is
often referred to as a SAN technology.
The configuration settings for remote iSCSI devices vary by manufacturer. To connect the
Linux computer to the remote iSCSI device, you must specify the configuration settings
while installing Linux. This means you must understand the configuration settings used by
your iSCSI device (name, authentication information, etc.) and ensure that your computer
has an iSCSI-compliant network card before starting the installation process. During Linux
installation, you need to provide the details regarding your iSCSI target if you want to
install Linux on an iSCSI device. For Fedora 20, you will need to select Add a disk, as
shown in Figure 2-10. This will allow you to select advanced storage options, as shown in
Figure 6-3. You can then click the Add iSCSI Target button shown in Figure 6-3 and supply
the TCP/IP configuration of your computer’s network card as well as the configuration settings needed to connect to the remote iSCSI device. Following this, the installation will proceed and allow you to create Linux filesystems (e.g., /dev/sda) on your iSCSI device.
Figure 6-3 Configuring advanced storage options during a Fedora 20 installation
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
262
Chapter 6
Linux Server Deployment
RAID Configuration
Recall that you typically create several partitions during installation to decrease the likelihood
that the failure of a filesystem on one partition will affect the rest of the system. These partitions should be spread across several different hard disks to minimize the impact of a hard
disk failure; if one hard disk fails, the data on the other hard disks is unaffected.
If a hard disk failure occurs, you must power down the computer, replace the failed hard
disk drive, power on the computer, and restore the data that was originally on the hard disk
drive from a back-up source, such as a tape device. The whole process can take several
hours. However, with some systems, such as database servers, no amount of downtime is
acceptable. In such situations, hard disk configurations that minimize the time required to
recover from a hard disk failure are required. Such fault tolerant configurations are typically
implemented by a Redundant Array of Independent Disks (RAID). Note that RAID has
other uses besides creating a fault tolerant system. It can be used to speed up access to hard
disks or combine multiple hard disks into a single volume.
Most Linux servers and SAN devices use RAID internally to provide
for data fault tolerance.
Currently, seven basic RAID configurations, ranging from level 0 to level 6, are available. RAID
level 0 configurations are not fault tolerant. One type of RAID level 0, known as spanning,
consists of two hard disks that the system sees as one large volume. Using this technology, you
could, for example, combine two 1TB hard disks into one 2TB partition. Spanning is useful
when you need a large amount of storage space in a single volume without fault tolerance.
In another type of RAID level 0, called disk striping, an individual file is divided into sections
and saved concurrently on multiple disks, one section per disk. For example, suppose you
have a disk striping configuration made up of three disks. In that case, when you save a file,
it is divided into three sections, with each section written to separate hard disk devices concurrently, in a third of the amount of time it would take to save the entire file on one hard
disk device. Note that the system can also read the same file in one-third the amount of time
it would take if the file were stored on a single hard drive. Disk striping is useful when you
need to speed up disk access, but it is not fault tolerant. If one hard disk fails in a RAID
level 0 configuration, all data is lost.
RAID level 1, which is often referred to as disk mirroring, provides fault tolerance in the case of a
hard disk failure. In this RAID configuration, the same data is written to two separate hard disks
at the same time. This results in two hard disks with identical information. If one fails, the copy
can replace the failed hard disk in a short period of time. The only drawback to RAID level 1 is
the cost, because you need to purchase twice the hard disk space needed for a given computer.
RAID level 2 is no longer used and was a variant of RAID 0 that allowed for error and
integrity checking on hard disk drives. Modern hard disk drives do this intrinsically.
RAID level 3 is disk striping with a parity bit, or marker, which indicates what data is where.
It requires a minimum of three hard disk drives to function, with one of the hard disks used
to store the parity information. Should one of the hard disks that contain data fail, you can
replace the hard disk drive and regenerate the data using the parity information stored on the
parity disk. If the parity disk fails, the system must be restored from a back-up device.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
263
RAID level 4 is only a slight variant on RAID level 3. RAID level 4 offers greater access
speed than RAID level 3, because it can store data in blocks and, thus, does not need to
access all disks in the array at once to read data.
RAID level 5 replaces RAID levels 3 and 4; it is the most common RAID configuration as of
this writing. It is commonly referred to as disk striping with parity. As with RAID levels 3
and 4, it requires a minimum of three hard disk drives for implementation; however, the parity information is not stored on a separate drive, but is intermixed with data on the drives
that make up the set. This offers better performance and fault tolerance; if any drive in the
RAID configuration fails, the information on the other drives can be used to regenerate the
lost information after the failed hard disk has been replaced. If two hard disks fail, the system
must be restored from a back-up device. Figure 6-4 shows how a RAID level 5 configuration
can be restored using parity information. The parity bits shown in Figure 6-4 are a sum of
the information on the other two disks (22 þ 12 ¼ 34). If the third hard disk fails, the information can be regenerated because only one element is missing from each equation:
22 þ 12 ¼ 34
68 65 ¼ 3
13 9 ¼ 4
Parity information
22
12
34
65
68
3
13
9
4
Hard disk #1
Hard disk #2
Hard disk #3
Figure 6-4 Organization of data on RAID level 5
RAID level 6 is basically the same as RAID level 5, but it adds a second set of parity bits for
added fault tolerance and allows up to two simultaneous hard disk drive failures while
remaining fault tolerant.
RAID levels are often combined; RAID level 15 refers to a Stripe Set
with Parity (RAID level 5) that is mirrored (RAID level 1) to another
Stripe Set with Parity.
RAID configurations can be handled by software running on an operating system (called
software RAID) but are more commonly handled by the hardware contained within a SCSI
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
264
Chapter 6
Linux Server Deployment
or SATA hard disk controller (called hardware RAID), or by the system BIOS (called firmware RAID).
Most firmware RAID devices only support RAID level 0 and 1.
To configure hardware RAID, you must use the RAID setup utility for your specific SCSI
or SATA hard disk controller. You can access this setup utility by entering the system or
SCSI BIOS at system startup, or by using a manufacturer-supplied boot CD or DVD.
After you have configured your hardware RAID volumes within the setup utility, they
will automatically appear as standard hard disk volumes to the Linux installation program. For example, if you configure three hard disks in a RAID level 5 volume using the
RAID setup utility, they will appear as a single volume (/dev/sda1) to the Linux installation program. You can then place a filesystem on /dev/sda1 as you would any other physical hard disk.
To configure firmware RAID, you must first configure a RAID volume using the RAID setup
utility within the system BIOS. Next, you can start the Linux installation and select the firmware RAID volume. For a Fedora 20 installation, select Add a disk, as shown in Figure 2-10,
highlight the Firmware RAID tab shown Figure 6-3, and select the firmware RAID volumes
that you would like to use for Linux installation.
Unlike hardware or firmware RAID, software RAID is configured entirely during the installation process (provided that your system has more than one hard disk). To configure software
RAID during a Fedora 20 installation, ensure that multiple disks are present and selected on
the screen shown in Figure 2-10. Next, select a partitioning strategy as well as the I want to
review/modify my disk partitions before continuing option shown in Figure 2-11. Following
this, you can create partitions to host your Linux filesystems, select RAID as the device type,
and choose the appropriate RAID level and filesystem. The / (root) filesystem shown in
Figure 6-5 uses RAID 1 spread across two 20GB hard disks; 15GB from each disk will be
used to store the / (root) filesystem.
Your first RAID volume will use a multiple disk (md) device file that starts with number 127
(/dev/md127). Your second RAID volume will use the device file /dev/md128, and so on. For
example, the / (root) filesystem configured in Figure 6-5 will show up as /dev/md127 in the
output of the df command, as shown here:
[[email protected] ~]# df
Filesystem 1K-blocks
/dev/md127
14987656
devtmpfs
1014932
tmpfs
1022260
tmpfs
1022260
tmpfs
1022260
tmpfs
1022260
/dev/sda1
487652
[[email protected] ~]# _
Used
3242408
0
80
684
0
16
90207
Available
10960864
1014932
1022180
1021576
1022260
1022244
367749
Use%
23%
0%
1%
1%
0%
1%
20%
Mounted on
/
/dev
/dev/shm
/run
/sys/fs/cgroup
/tmp
/boot
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
265
6
Figure 6-5 Configuring software RAID during a Fedora 20 installation
You can manage your software RAID configuration after installation using the mdadm command, as well as obtain details regarding configured software RAID devices by viewing the
contents of the /etc/mdadm.conf and /proc/mdstat files, as shown here:
[[email protected] ~]# cat /etc/mdadm.conf
# mdadm.conf written out by anaconda
MAILADDR root
AUTO +imsm +1.x -all
ARRAY /dev/md/root level=raid1 num-devices=2 UUID=77d3f6c5:fbc71aa5:426b55eb:1e0d9860
[[email protected] ~]#_
[[email protected] ~]# cat /proc/mdstat
Personalities : [raid1]
md127 : active raid1 sda2[0] sdb1[1]
15360000 blocks super 1.2 [2/2] [UU]
bitmap: 0/1 pages [0KB], 65536KB chunk
unused devices: <none>
[[email protected] ~]#_
ZFS Configuration
Most Linux servers store the Linux operating system and any additional programs within a
filesystem on standard partitions, LVM volumes, or RAID volumes. For Linux servers that
require a large amount of storage for additional data (e.g., a file server or database server),
filesystems are often created on SAN storage devices (e.g., an iSCSI SAN on the same rack
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
266
Chapter 6
Linux Server Deployment
as the server) and mounted to a directory on the Linux system that will be used to store the
additional data.
The Zettabyte File System (ZFS) is a high-performance filesystem and volume management
software that was designed for large-scale Linux systems that need to store data on multiple
disks, SANs, and remote systems. You can create RAID-like ZFS volumes that span thousands of different local and network storage devices, such as local hard disks, SSDs, SANs,
and remote file shares, as well as resize volumes while the filesystem is mounted. Moreover,
the ZFS filesystem detects and repairs data errors automatically as data is read and written,
as well as protects against problems that are commonly seen on large systems that write
large amounts of data to a non-ZFS filesystem, including:
• Silent data corruption
• Bit rot
• Disk firmware bugs
• Phantom writes
• Misdirected writes
• Driver and kernel buffer errors
• Accidental driver overwrites
ZFS also supports new storage technologies such as PCIe SSDs and battery-backed RAM
disk devices with ultra-low latency. Additionally, ZFS caches frequently accessed information
in RAM and on faster storage devices such as SSDs to provide ultra-fast performance. ZFS
also supports nearly all advanced filesystem features, including deduplication (storing one
copy of a file that is located in multiple directories until one of the files change), snapshots,
cloning, compression, encryption, NFSv4, volume management, and more.
ZFS was initially created by Sun Microsystems in 2001 and is often
used by the largest Linux and UNIX systems in the world. It is available for Solaris UNIX, Mac OSX, Linux, FreeBSD, FreeNAS, and
more. Since ZFS does not currently have a GPL-compatible license,
it cannot be bundled within a Linux distribution, but it can be
easily added afterwards. On Linux, ZFS support is maintained by
http://zfsonlinux.org.
Although ZFS is primarily used on large Linux systems that may have hundreds or thousands
of storage devices, it is also used within small and medium-sized organizations that require
flexible volume management for data that is easy to configure.
ZFS pools are groups of physical disks that ZFS can manage (local disks, SANs, shared
devices, large raw files, remote shares, etc.), and ZFS volumes are simply ZFS-managed filesystems that are created from ZFS pools.
Take, for example, a system that hosts several SATA hard disks, with the Linux OS installed
on /dev/sda. To create a simple ZFS pool called datastorage1 from the second SATA hard
disk, you could use the following zpool command:
[[email protected] ~]# zpool create datastorage1 /dev/sdb
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
267
This will also create a new ZFS volume called datastorage1 and mount it to /datastorage1, as
shown here in the last line of the mount command output:
[[email protected] ~]# mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
systemd on /sys/fs/cgroup/systemd type cgroup
(rw,noexec,nosuid,nodev,none,name=systemd)
datastorage1 on /datastorage1 type zfs (rw,xattr)
[[email protected] ~]# _
You can also use the zpool command to view the details for your datastorage1 volume:
[[email protected] ~]# zpool list
NAME SIZE ALLOC FREE CAP DEDUP HEALTH ALTROOT
datastorage1 816M 114K 816M 0% 1.00x ONLINE [[email protected] ~]# _
You can remove a ZFS volume using the zpool remove command. For example, zpool remove datastorage1 would
remove the ZFS volume and pool that we created in the previous
output.
If you specify multiple devices when creating a simple ZFS volume, then both devices are
automatically added to the ZFS pool and a ZFS volume created from their contents. For
example, if you run the following command, the /dev/sdc and /dev/sdd hard disks would be
added to the datastorage2 pool, and a ZFS volume called datastorage2 would be created
(with a capacity of both hard disks combined) and mounted to the /datastorage2 directory.
This would act as the equivalent of a RAID 0 volume, but with the file corruption prevention
benefits of ZFS.
[[email protected] ~]# zpool create datastorage2 /dev/sdc /dev/sdd
[[email protected] ~]# _
To create a ZFS pool and mirrored ZFS volume called datastorage3 from the /dev/sde and /
dev/sdf hard disks and mount the volume to /datastorage3, you simply need to specify the
mirror keyword during creation, as shown here:
[[email protected] ~]# zpool create datastorage3 mirror /dev/sde /dev/sdf
[[email protected] ~]# _
The datastorage3 volume is the equivalent of RAID 1 but resize-able under ZFS, and the
total size of the volume is identical to the size of one of the hard disks.
To create a ZFS mirrored volume, you need to specify a minimum of
two hard disk devices to protect against a single disk failure.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
268
Chapter 6
Linux Server Deployment
To view the status of the disks that are part of the datastorage3 pool, you can use the following command:
[[email protected] ~]# zpool status datastorage3
pool: datastorage3
state: DEGRADED
status: One or more devices could not be used because the label is missing or
invalid. Sufficient replicas exist for the pool to continue functioning in a degraded state.
action: Replace the device using ’zpool replace’.
see: http://zfsonlinux.org/msg/ZFS-8000-4J
scan: scrub repaired 0 in 0h0m with 0 errors on Tue Sep 30 13:33:11 2014
config:
NAME
STATE
READ
WRITE
CKSUM
datastorage3
DEGRADED
0
0
0
mirror-0
DEGRADED
0
0
0
/dev/sde
UNAVAIL
0
0
0 corrupted data
/dev/sdf
ONLINE
0
0
0
errors: No known data errors
[[email protected] ~]# _
Notice from the preceding example that one of the disks in the mirror has failed (/dev/sde). In
this case, you should detach the device from the pool using the zpool detach datastorage3 /dev/sde command, replace the failed hard disk, and reattach the disk to the mirror using the zpool attach datastorage3 /dev/sdf /dev/sde command. During this process, the filesystem is still available to users for reading and writing data. After
reattaching the new hard disk, the output of the zpool status datastorage3 command should resemble the following output:
[[email protected] ~]# zpool status datastorage3
pool: datastorage3
state: ONLINE
scan: none requested
config:
NAME
STATE
READ
WRITE
datastorage3 ONLINE
0
0
mirror-0
ONLINE
0
0
/dev/sde
ONLINE
0
0
/dev/sdf
ONLINE
0
0
errors: No known data errors
[[email protected] ~]# _
CKSUM
0
0
0
0
To create ZFS pool and RAID-Z volume (the equivalent of a RAID-5 volume with a
variable-sized stripe) called datastorage4 from the /dev/sdg, /dev/sdh, and /dev/sdi hard disks
and mount it to /datastorage4, you simply need to specify the raidz keyword during creation,
as shown here:
[[email protected] ~]# zpool create datastorage4 raidz /dev/sdg /dev/sdh /dev/
sdi
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Configuring Server Storage
269
A RAID-Z volume needs a minimum of three hard disks to protect
against a single disk failure and a minimum of seven hard disks to
protect against a multi-disk failure.
You can also use raidz2 (double parity like RAID-6) and raidz3
(triple parity) in place of raidz in the previous command. You need
a minimum of four devices for raidz2 and a minimum of five
devices for raidz3.
Because ZFS writes parity info with a variable-sized stripe, performance is maximized, and there is virtually no chance of the infamous
“RAID 5 Hole” (data loss in the event of a power failure) that
plagues traditional RAID systems.
Following this, you can view the state and performance of the RAID-Z volume using the
zpool status and zpool iostat –v commands:
[[email protected] ~]# zpool status datastorage4
pool: datastorage4
state: ONLINE
scan: none requested
config:
NAME
STATE
READ
WRITE
CKSUM
datastorage4 ONLINE
0
0
0
raidz1-0
ONLINE
0
0
0
/dev/sdg
ONLINE
0
0
0
/dev/sdh
ONLINE
0
0
0
/dev/sdi
ONLINE
0
0
0
errors: No known data errors
[email protected]:~# zpool iostat -v datastorage4
capacity
operations
bandwidth
pool
alloc free read write read
write
------------ ----- ---- ---- ----- ----- ----datastorage4
220K
970M
0
11
701 10.5K
raidz1
220K
970M
0
11
701 10.5K
/dev/sdg
0
10
25.2K 83.6K
/dev/sdh
0
10
25.1K 83.6K
/dev/sdi
0
10
1.48K 83.6K
------------ ----- ---- ---- ----- ----- ----[[email protected] ~]# _
For more granular management of ZFS, you can use the zfs command to manage the specific features of the ZFS filesystem stored within ZFS volumes. The zfs command allows
you to set a wide variety of ZFS-specific functionality, including directory size quotas and
file- and directory-specific features and performance options. Moreover, you can identify specific subdirectories on a ZFS filesystem to tag for ZFS management; these subdirectories are
often called ZFS subfilesystems. For example, if you create a directory under /datastorage4
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
270
Chapter 6
Linux Server Deployment
called research, you can run the following command to specify that the research subdirectory
should be treated as a ZFS subfilesystem:
[[email protected] ~]# zfs create datastorage4/research
[[email protected] ~]# _
To see a list of ZFS filesystems and subfilesystems that are available for ZFS management,
you can run the zfs list command:
[[email protected] ~]# zfs list
NAME
USED
datastorage4
286K
datastorage4/research 38.6K
[[email protected] ~]# _
AVAIL
970M
970M
REFER
42.6K
38.6K
MOUNTPOINT
/datastorage4
/datastorage4/research
To list the specific configuration parameters that you can modify for the research subfilesystem, you can use the zfs get all command:
[[email protected] ~]# zfs get all datastorage4/research | less
NAME
PROPERTY
VALUE
datastorage4/research type
filesystem
datastorage4/research creation
Tue Sep 30 13:41 2015
datastorage4/research used
38.6K datastorage4/research available
214M
datastorage4/research referenced
38.6K
datastorage4/research compressratio 1.00x
datastorage4/research mounted
yes
datastorage4/research quota
none
datastorage4/research reservation
none
datastorage4/research recordsize
128K
datastorage4/research mountpoint
/datastorage4/research
datastorage4/research sharenfs
off
datastorage4/research checksum
on
datastorage4/research compression
off
datastorage4/research atime
on
datastorage4/research atime
on
datastorage4/research devices
on
datastorage4/research exec
on
datastorage4/research setuid
on
datastorage4/research readonly
off
:
SOURCE
default
default
default
default
default
default
default
default
default
default
default
default
default
You can modify the settings for the research subfilesystem to suit your needs. For example, to
limit the total size of files within the research subfilesystem to 10GB, you could use the zfs
set quota=10G datastorage4/research command, or to ensure that the contents of
the research subfilesystem are read-only, you could run the zfs set readonly=on
datastorage4/research command.
On Linux systems, ZFS volumes are mounted by the ZFS system and not via entries within
the /etc/fstab file by default. As a result, to ensure that the datastorage1, datastorage2, datastorage3, and datastorage4 volumes created during the examples in this section are mounted
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing a Linux Server Distribution
271
at boot time, you simply need to add the line zfs mount datastorage1 datastorage2 datastorage3 datastorage4 to a system startup script. System startup scripts
are discussed in Chapter 8.
Installing a Linux Server Distribution
Any Linux distribution can be used as a server if the necessary packages are installed that provide
for server services on the network. However, many Linux distributions also provide a version that
is geared for Linux servers. These Linux server distributions do not have a GUI environment
installed by default, since administration of Linux installed on a rackmount server is often performed using commands within a BASH terminal on a remote computer across the network.
Instead, most Linux server distributions ship with a set of packages that are commonly used on
Linux servers, including Web server, database server, file server and email server software.
Remote administration methods are discussed in Chapter 12.
The configuration of common server software packages is covered in
Chapter 13.
The installation process for a Linux server distribution differs from a standard or live Linux
installation process. No GUI environment is loaded during the installation process, and the
installation program often prompts you for additional information that is necessary for a
server, such that you don’t need to configure that same information afterwards, including:
•
The host name and IP configuration of the server—For a desktop system, the default
hostname (localhost) will often suffice. However, for a server system, the host name is
often used by the configuration of server services and should match the name that
other computers on the network will use when contacting the server. Additionally,
many Linux administrators prefer to configure a static IP address on server systems
rather than obtain an IP address automatically. This ensures that the IP address of the
server cannot change over time.
•
Whether to allow for automatic updating—Most Linux systems are configured by default
to download important OS and application updates periodically. However, for a Linux
server, an update could potentially cause problems with software that is currently running
on the server. As a result, most Linux administrators prefer to manually update software
after testing the update on a non-production computer that contains the same software as
the server. Application updates will be discussed in more depth in Chapter 11.
•
Package selection—While most Linux distributions automatically install a predefined
set of software packages during installation without user input, Linux server
distributions often prompt you to select the specific software packages that will be
needed on the Linux server. This prevents unnecessary packages from being installed
and space from being wasted on the server filesystems.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
272
Chapter 6
Linux Server Deployment
• Server service configuration—Some server service packages that are installed require
additional configuration in order to provide functionality. The installation program for
a Linux server distribution may prompt you for that information during the
installation so that you don’t need to configure it afterwards.
• Boot loader configuration—During the Fedora installation program, you are not prompted
to supply information regarding the boot loader that will be used to boot the Linux kernel.
However, most Linux server distributions prompt you for the location of the Linux boot
loader during the installation. GRUB (GRand Unified Boot loader) is the standard boot
loader used on Linux systems. By default, most Linux distributions install the first part of
GRUB on the MBR/GPT (e.g., /dev/sda). However, you can also choose to install the first
part of GRUB on the first sector of the Linux partition that holds the kernel (e.g., /dev/
sda1) if you have one or more operating systems already installed on the server system in a
multi-boot configuration. Boot loaders will be discussed in more depth in Chapter 8.
In Hands-on Project 6-1, you will install the Ubuntu Server 14.04
Linux distribution. In addition to providing the opportunity to install
a Linux server distribution, this project will allow you to apply concepts in later chapters to more than one Linux distribution, as well
as configure Linux components that are not found in Fedora Linux,
such as the Debian Package Manager, the SysV initialization system,
and the system log daemon.
Since servers provide a critical role within the organization, it is important to verify that the
installation process has completed successfully and that all hardware components are working
properly with the OS after installation.
Dealing with Problems During Installation
Most problems that occur during a Linux installation are related to faulty hardware or an
incorrect device driver for a hardware component. In both of these cases, the installation may
end abnormally and a “fatal signal 11” error message may appear on the screen. This indicates
an error, known as a segmentation fault, in which a program accesses a certain area of RAM
that was not assigned. If a segmentation fault occurs when you are installing Linux, first check
the RAM for errors by running the memtest86 utility shown in Figure 2-6. The memtest86 utility is included on nearly all Linux installation media and can be run from the welcome screen
that first appears when you boot the computer from the installation media.
If you experience a segmentation fault during installation and the memtest86 utility finds no
RAM errors, first check with the hardware manufacturer of the system (as well as the hardware manufacturer of any additional hardware devices added to the system prior to installation) to determine if an updated driver is available for your distribution of Linux. If one is
available that is needed during the installation process (e.g., for a new disk controller card),
the manufacturer will often provide instructions that allow you to specify the new driver during the installation process. If you are installing from live media, this is often performed by
activating the driver after the live Linux system has loaded. Alternatively, if you are installing
from standard installation media, the location of the driver is often specified by modifying
the default installation boot options at the welcome screen when you boot the Linux installation media. To access the default installation boot options for Fedora 20, you can press the
Tab key at the welcome screen shown in Figure 2-4.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing a Linux Server Distribution
273
If you experience a segmentation fault during installation and both your RAM and device
drivers appear functional, you can assume that some hardware component is faulty. Zeroing
in on the right piece of hardware can be tricky. Often, you can fix segmentation faults by
turning off the CPU cache memory or by increasing the number of memory wait states in
the BIOS. On some computers, the BIOS allows the user to change the voltage for the RAM
and CPU; incorrect values could be the source of a segmentation fault. Other causes include
power management conflicts and overclocked CPUs.
An overclocked CPU is a CPU that is faster than the speed for
which the processor was designed. Although this might lead to
increased performance, it also makes the processor hotter and can
result in intermittent computer crashes.
If the installation fails with an error other than fatal signal 11, consult the support documentation for your distribution of Linux, or
check Internet newsgroups and forums.
In some cases, the installation process fails to place a boot loader on the hard disk properly;
this often occurs with older generation hard disk drives that have over 1024 cylinders. To
avoid this problem, ensure that the / partition starts before the 1024th cylinder (the 8GB
mark on most hard disks) or create a partition for the /boot directory that starts before the
1024th cylinder.
Dealing with Problems After Installation
Even after a successful Linux installation, problems might arise if the installation program
failed to detect the computer’s hardware properly or if certain programs were not installed
as expected. To ensure no such problems occurred, check the installation log files after installation and then verify system settings to ensure that all hardware was detected with the correct values.
The installation log files are files that are created by the installation program that record the
events that occurred during the installation process, including errors. For Fedora Linux, these
log files are stored in the /var/log/anaconda directory; and for Ubuntu Server Linux, these log
files are stored in the /var/log/installer directory. To search these files for errors and warnings,
you should use the grep command, as their contents are often quite lengthy. For example,
the egrep –i “(error|warn)” /var/log/anaconda/* command can be used to
search for any errors or warnings within the log files in the /var/log/anaconda directory.
To verify hardware settings, you can examine the content of the /proc directory or boot-up
log files. The /proc directory is mounted to a special filesystem contained within RAM that
lists system information made available by the Linux kernel; because this is an administrative
filesystem, all files within are readable only by the root user. The following sample listing of
the /proc directory shows many file and subdirectory contents:
[[email protected] ~]# ls -F /proc
1/
1171/ 171/ 3/
415/ 48/ 801/
10/
12/
172/ 300/ 417/ 484/ 9/
kallsyms
kcore
softirqs
stat
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
274
Chapter 6
Linux Server Deployment
1018/ 1224/ 173/ 334/
1020/ 1239/ 174/ 361/
1024/ 1257/ 175/ 362/
1028/ 1266/ 176/ 364/
1038/ 1282/ 177/ 365/
1068/ 1284/ 178/ 367/
1074/ 1288/ 179/ 368/
1077/ 13/
18/ 369/
1084/ 1317/ 180/ 371/
1090/ 14/
187/ 372/
11/
15/
188/ 373/
1106/ 16/
19/ 383/
1109/ 163/ 190/ 384/
1112/ 165/ 2/
385/
1117/ 166/ 20/ 392/
1137/ 167/ 203/ 393/
1141/ 168/ 204/ 394/
1159/ 169/ 205/ 399/
1165/ 17/
287/ 410/
1168/ 170/ 297/ 413/
[[email protected] ~]# _
418/
419/
422/
424/
425/
426/
428/
434/
436/
437/
449/
45/
455/
456/
459/
46/
460/
462/
47/
478/
485/
49/
5/
548/
550/
58/
59/
6/
60/
62/
624/
64/
7/
72/
748/
751/
752/
755/
758/
8/
acpi/
buddyinfo
bus/
cgroups
cmdline
consoles
cpuinfo
crypto
devices
diskstats
dma
driver/
execdomains
fb
filesystems
fs/
interrupts
iomem
ioports
irq/
keys
key-users
kmsg
kpagecount
kpageflags
loadavg
locks
mdstat
meminfo
misc
modules
[email protected]
mtrr
[email protected]
pagetypeinfo
partitions
sched_debug
scsi/
[email protected]
slabinfo
swaps
sys/
sysrq
sysvipc/
timer
timer_s
tty/
uptime
version
vmallocin
vmstat
zoneinfo
The subdirectories that start with a number in the preceding output are used to display process information; other directories can contain kernel parameters. The files listed in the preceding output are text representations of various parts of the Linux system; they are updated
regularly by the Linux kernel and can be viewed using standard text commands, such as cat
or less.
To view the information that Linux has detected regarding a computer’s CPUs, view the contents of the cpuinfo file in the /proc directory:
[[email protected] ~]# cat /proc/cpuinfo
processor
:0
vendor_id
: GenuineIntel
cpu family
:6
model
: 58
model name
: Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
stepping
:9
microcode
: 0xffffffff
cpu MHz
: 2394.550
cache size
: 6144 KB
fpu
: yes
fpu_exception : yes
cpuid level
: 13
wp
: yes
flags
: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ss syscall nx lm constant_tsc rep_good
nopl eagerfpu pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes xsave avx
f16c rdrand hypervisor lahf_lm xsaveopt fsgsbase smep erms
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing a Linux Server Distribution
275
bogomips
: 4789.10
clflush size
: 64
cache_alignment : 64
address sizes
: 36 bits physical, 48 bits virtual
power management :
[[email protected] ~]# _
The preceding output is from a computer with one processor that runs at 2.4GHz (processor 0),
with 6MB of processor cache. If this information is incorrect because Linux failed to detect
the processor information properly, you might need to change a setting in BIOS or research
a solution to the problem on the mainboard or processor manufacturer’s Web site.
After installation, you also need to ensure that Linux has detected the correct amount of
RAM. To do this, you can view the contents of the /proc/meminfo file, as shown in the following output:
[[email protected] ~]# cat /proc/meminfo
MemTotal:
2044524 kB
MemFree:
821612 kB
Buffers:
88012 kB
Cached:
274340 kB
SwapCached:
0 kB
Active:
335964 kB
Inactive:
246028 kB
Active(anon):
220176 kB
Inactive(anon):
212 kB
Active(file):
115788 kB
Inactive(file):
245816 kB
Unevictable:
0 kB
Mlocked:
0 kB
SwapTotal:
2097148 kB
SwapFree:
2097148 kB
Dirty:
0 kB
Writeback:
0 kB
AnonPages:
219644 kB
Mapped:
71760 kB
Shmem:
764 kB
Slab:
95344 kB
SReclaimable:
65104 kB
SUnreclaim:
30240 kB
KernelStack:
2080 kB
PageTables:
10916 kB
NFS_Unstable:
0 kB
Bounce:
0 kB
WritebackTmp:
0 kB
CommitLimit:
3119408 kB
Committed_AS:
963836 kB
VmallocTotal:
34359738367 kB
VmallocUsed:
30444 kB
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
276
Chapter 6
Linux Server Deployment
VmallocChunk:
HardwareCorrupted:
AnonHugePages:
HugePages_Total:
HugePages_Free:
HugePages_Rsvd:
HugePages_Surp:
Hugepagesize:
DirectMap4k:
DirectMap2M:
[[email protected] ~]# _
34359698216 kB
0 kB
100352 kB
0
0
0
0
2048 kB
57280 kB
2039808 kB
In the preceding output, the total amount of memory (MemTotal) is 2044524KB, or approximately 2GB. If this value is incorrect, you may need to change a setting in BIOS or research
a solution to the problem on the mainboard or RAM manufacturer’s Web site.
In some cases, it’s necessary to insert a device’s driver into the Linux kernel as a module. This
is typically true of specialized server hardware, such as SAN controller cards. To see a list of
modules currently inserted into the Linux kernel, view the /proc/modules file, as shown in the
following output:
[[email protected] ~]# less /proc/modules
binfmt_misc 17431 1 - Live 0xffffffffa0240000
nf_conntrack_netbios_ns 12665 0 - Live 0xffffffffa023b000
nf_conntrack_broadcast 12527 1 nf_conntrack_netbios_ns, Live
0xffffffffa0236000
ipt_MASQUERADE 12880 1 - Live 0xffffffffa0231000
bnep 19704 2 - Live 0xffffffffa0249000
bluetooth 361772 5 bnep, Live 0xffffffffa01d7000
ip6t_REJECT 12939 2 - Live 0xffffffffa0160000
cfg80211 460310 0 - Live 0xffffffffa0165000
rfkill 21694 4 bluetooth,cfg80211, Live 0xffffffffa0159000
xt_conntrack 12760 42 - Live 0xffffffffa014f000
ebtable_nat 12807 0 - Live 0xffffffffa014a000
ebtable_broute 12731 0 - Live 0xffffffffa0154000
bridge 110624 1 ebtable_broute, Live 0xffffffffa012d000
stp 12868 1 bridge, Live 0xffffffffa0128000
llc 14045 2 bridge,stp, Live 0xffffffffa0123000
ebtable_filter 12827 0 - Live 0xffffffffa011e000
ebtables 30758 3 ebtable_nat,ebtable_broute,ebtable_filter, Live
0xffffffffa0111000
ip6table_nat 13015 1 - Live 0xffffffffa010c000
:
If a hardware device is not working with Linux, first consult the hardware vendor’s Web site
to determine the name of the kernel module. You can then search for this module in the /
proc/modules file; if the module isn’t present, the driver for that hardware is not loaded into
the Linux kernel, and you proceed to add the module manually, as discussed in Chapter 12.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing a Linux Server Distribution
277
The /proc directory contains many more files than have been presented so far in this
chapter. Table 6-2 describes some files that you will find useful when examining a system after
installation.
Hardware is detected by the Linux kernel at system startup. However, this information is displayed too quickly to read or hidden by a graphical startup screen. Fortunately, the system
logs all information regarding hardware detection and the startup of system processes in log
files that can be viewed at a later time.
Filename
Contents
cmdline
Current location of the Linux kernel
cpuinfo
Information regarding the processors in the computer
devices
List of the character and block devices that are currently in use by the Linux kernel
execdomains
List of execution domains for processes on the system; execution domains allow a process to
execute in a specific manner
fb
List of framebuffer devices in use on the Linux system; typically, these include video adapter card
devices
filesystems
List of filesystems supported by the Linux kernel
interrupts
List of IRQs in use on the system
iomem
List of memory addresses currently used
ioports
List of memory address ranges reserved for device use
kcore
A representation of the physical memory inside the computer; this file should not be viewed
kmsg
Temporary storage location for messages from the kernel
loadavg
Statistics on the performance of the processor
locks
List of files currently locked by the kernel
mdstat
Configuration of multiple-disk RAID hardware
meminfo
Information regarding physical and virtual memory on the Linux system
misc
List of miscellaneous devices (major number=10)
modules
List of currently loaded modules in the Linux kernel
mounts
List of currently mounted filesystems
partitions
Information regarding partition tables loaded in memory on the system
swaps
Information on virtual memory utilization
version
Version information for the Linux kernel and libraries
Table 6-2 Files commonly found in the /proc directory
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
278
Chapter 6
Linux Server Deployment
To view the hardware detected during boot time, you can use the dmesg command after system startup, as shown in the following output:
[[email protected] ~]# dmesg | less
[0.000000] Initializing cgroup subsys cpuset
[0.000000] Initializing cgroup subsys cpu
[0.000000] Initializing cgroup subsys cpuacct
[0.000000] Linux version 3.11.10-301.fc20.x86_64 ([email protected]
phx2.fedoraproject.org) (gcc version 4.8.2 20131017 (Red Hat 4.8.2-1)
(GCC) ) #1 SMP Thu Dec 5 14:01:17 UTC 2013
[0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.11.10-301.fc20.x86_64
root=/dev/sda3 ro vconsole.font=latarcyrheb-sun16 rhgb quiet LANG=en_US.
UTF-8
[0.000000] e820: BIOS-provided physical RAM map:
[0.000000] BIOS-e820: [mem 0x000000000000-0x000000000009fbff] usable
[0.000000] BIOS-e820: [mem 0x00000009fc00-0x000000009ffff] reserved
[0.000000] BIOS-e820: [mem 0x0000000e0000-0x0000000fffff] reserved
[0.000000] BIOS-e820: [mem 0x000000100000-0x000000007ffeffff] usable
[0.000000] BIOS-e820: [mem 0x00007fff0000-0x000000007fffefff] ACPI data
[0.000000] BIOS-e820: [mem 0x00007ffff000-0x000000007fffffff] ACPI NVS
[0.000000] NX (Execute Disable) protection: active
[0.000000] SMBIOS 2.3 present.
[0.000000] DMI: Microsoft Corporation Virtual Machine/Virtual Machine,
BIOS
090006 05/23/2012
[0.000000] Hypervisor detected: Microsoft HyperV
[0.000000] HyperV: features 0xe7f, hints 0x2c
[0.000000] e820: update [mem 0x00000000-0x00000fff] usable ==> reserved
[0.000000] e820: remove [mem 0x000a0000-0x000fffff] usable
:
You can also view the system processes that started successfully or unsuccessfully during
boot time by viewing the contents of the /var/log/boot.log file on most Linux distributions.
A sample boot.log file from an Ubuntu Server system is shown in the following output:
[email protected]:~# tail /var/log/boot.log
* Starting MySQL Server
[ OK ]
* Starting PostgreSQL 9.3 database server
[ OK ]
* Starting configure network device security
[ OK ]
* Starting configure network device
[ OK ]
* Starting Postfix Mail Transport Agent postfix
[ OK ]
* Restoring resolver state...
[ OK ]
AH00558: apache2: Could not reliably determine the server’s fully qualified
domain name, using 127.0.1.1. Set the ’ServerName’ directive globally to
suppress this message
*
* Starting Tomcat servlet engine tomcat7
[ OK ]
* Stopping System V runlevel compatibility
[ OK ]
[email protected]:~# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Installing a Linux Server Distribution
279
For more detailed information regarding system processes during the startup process and
afterwards, you can view the contents of the /var/log/messages or /var/log/syslog files on
most Linux distributions. A sample syslog file from an Ubuntu Server system is shown in
the following output:
[email protected]:~# tail /var/log/syslog
Sep 30 09:56:48 server1 kernel: [ 38.357431] cgroup: systemd-logind (619)
created nested cgroup for controller "memory" which has incomplete
hierarchy support. Nested cgroups may change behavior in the future.
Sep 30 09:56:48 server1 kernel: [ 38.357435] cgroup: "memory" requires
setting use_hierarchy to 1 on the root.
Sep 30 09:57:07 server1 ntpdate[2031]: step time server 91.189.89.199
offset17.350701 sec
Sep 30 09:56:57 server1 kernel: [ 46.667766] audit_printk_skb: 48 callbacks
suppressed
Sep 30 09:56:57 server1 kernel: [ 46.667770] type=1400 audit
(1412085417.259:28):
apparmor="STATUS"
operation="profile_replace"
profile="unconfined"name="/usr/lib/cups/backend/cups-pdf" pid=2146 comm="apparmor_parser"
Sep 30 09:56:57 server1 kernel: [ 46.667776] type=1400 audit
(1412085417.259:29): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/cupsd" pid=2146 comm="apparmor_parser"
Sep 30 09:56:57 server1 kernel: [ 46.668185] type=1400 audit
(1412085417.259:30): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/cupsd" pid=2146 comm="apparmor_parser"
Sep 30 09:57:13 server1 kernel: [ 63.274963] hv_balloon: Received INFO_
TYPE_MAX_PAGE_CNT
Sep 30 09:57:13 server1 kernel: [ 63.274970] hv_balloon: Data Size is 8
Sep 30 10:09:01 server1 CRON[2290]: (root) CMD ( [ -x /usr/lib/php5/
maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/
php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/
maxlifetime))
[email protected]:~# _
In Fedora 20, the logging system used to record the messages normally stored within /var/log/
boot.log, /var/log/messages, or /var/log/syslog has been replaced by a journaling database
system called journald. You can use the journalctl command on a Fedora 20 system
with the –b option to view the same information that would normally be stored within /var/
log/boot.log, /var/log/messages, or /var/log/syslog files. Since the journalctl command will
display entries for each boot process, it is important to narrow down the time frame. The following command only displays boot information from the current day:
[[email protected] ~]# journalctl –b ––since=today | less
– Logs begin at Tue 2015-09-16 21:56:29 EDT, end at Tue 2015-09-30 10:20:47
EDT. –
Sep 30 11:02:08 localhost.localdomain systemd-journal[71]: Runtime journal
is using 8.0M (max 99.8M, leaving 149.7M of free 990.2M, current limit 99.8M).
Sep 30 11:02:08 localhost.localdomain systemd-journal[71]: Runtime journal
is using 8.0M (max 99.8M, leaving 149.7M of free 990.2M, current limit 99.8M).
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
280
Chapter 6
Linux Server Deployment
Sep 30 11:02:08 localhost.localdomain kernel: Initializing cgroup subsys
cpuset
Sep 30 11:02:08 localhost.localdomain kernel: Initializing cgroup subsys cpu
Sep 30 11:02:08 localhost.localdomain kernel: Initializing cgroup subsys
cpuacct
Sep 30 11:02:08 localhost.localdomain kernel: Linux version 3.11.10-301.
fc20.x86_64 ([email protected]) (gcc version
4.8.2 20131017 (Red Hat 4.8.2-1) (GCC) ) #1 SMP Thu Dec 5 14:01:17 UTC 2013
Sep 30 11:02:08 localhost.localdomain kernel: Command line: BOOT_IMAGE=/
vmlinuz-3.11.10-301.fc20.x86_64 root=/dev/sda3 ro vconsole.font=latarcyrhebsun16 rhgb quiet LANG=en_US.UTF-8
Sep 30 11:02:08 localhost.localdomain kernel: e820: BIOS-provided physical
RAM map:
Sep 30 11:02:08 localhost.localdomain kernel: BIOS-e820: [mem
0x0000000000000000-0x000000000009fbff] usable
Sep 30 11:02:08 localhost.localdomain kernel: BIOS-e820: [mem
0x000000000009fc00-0x000000000009ffff] reserved
Sep 30 11:02:08 localhost.localdomain kernel: BIOS-e820: [mem
0x00000000000e0000-0x00000000000fffff] reserved
–More—
System Rescue
Booting to a live Linux OS from live installation media offers several benefits to Linux administrators when repairing a damaged Linux system. The live OS contains a small bootable
Linux kernel and virtual filesystem that are loaded into RAM and have access to the filesystems on the underlying hard disk. As a result, you can use the utilities on the live OS to fix
problems within a Linux installation on the hard disk that are preventing the Linux installation from booting, such as:
• Boot loader problems
• Filesystem/partition problems
• Configuration file problems
• Driver problems
All live OS systems contain several utilities that are useful for fixing system problems, including fdisk, parted, e2mkfs, fsck, and several more that will be introduced in later chapters.
Recall from the previous chapter that the fsck command can only be run on an unmounted
filesystem. If the / (root) filesystem becomes corrupted, you can boot your system using the live
installation media for your Linux distribution and use the fsck command in the live OS to
check the / (root) filesystem on the hard disk since it is not mounted by the live OS by default.
The process of using a live OS to repair problems on another Linux
system installed on the hard disk is commonly called system rescue.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Chapter Summary
281
You can also configure the Linux kernel loaded by the live OS to use the / (root) filesystem on
the hard disk. To do this, simply mount the / (root) filesystem on your local disk under an
empty directory in the live OS (e.g., /mnt) and then type chroot /mnt at the command
prompt. The chroot command will then change the root of the live OS to the /mnt directory,
which is actually the / (root) filesystem on the local disk. From this point onwards, you will
have root user access to any commands on your / (root) filesystem, as you normally would if
you had booted your Linux system on the hard disk. For example, if you run the passwd
command (used for changing user passwords), you’ll actually be changing the password for
the root user on the Linux system installed on the hard drive. This is often used to recover a
locally accessible Linux system if the root password has been lost or forgotten.
It is also important to note that this feature is not limited to live installation media. If you
install Linux from standard installation media, the welcome screen will often provide a system
rescue option that will allow you to boot a small live Linux system that does not contain a
GUI environment. This small live Linux system contains the same recovery utilities (fdisk,
parted, e2mkfs, fsck, etc.) and will allow you to mount the / (root) filesystem on your hard
disk as well as run the chroot command to switch your live Linux system to it.
Chapter Summary
■ Linux servers typically use far more hardware that desktop systems. This hardware is
installed in a rack using a rackmount form factor.
■ Many different SCSI standards have been developed since 1986. Parallel SCSI hard
disks are uniquely identified by a SCSI ID and attach to a controller via a terminated
cable. SAS is a newer SCSI technology that transfers information to hard disks using a
serial cable. iSCSI is a SAN technology used to transfer information from iSCSI
initiators to iSCSI targets across a TCP/IP network.
■ RAID is often used within Linux servers to combine several hard disks into a single
volume for speed or fault tolerance. It can be implemented by software that runs
within the OS, hardware on a hard disk controller card, or by the system BIOS.
■ The different levels of RAID determine how disks are combined and written to. RAID
level 0 is not fault tolerant, whereas RAID levels 1 through 6 provide fault tolerance
for data in the event of hard disk failure.
■ ZFS is a high-performance, fault tolerant filesystem that is commonly installed on
Linux servers. ZFS provides much of the same functionality as RAID, using storage
space that spans a wide range of different storage devices, including local disks, SANs,
shared devices, large raw files, and remote shares.
■ Linux server distributions do not contain a GUI environment and are often
administered remotely. They have an installation process that requires additional
information compared to other Linux distributions.
■ Unsupported hardware, defective RAM, overclocked CPUs, and improper RAM
settings can cause a Linux installation to fail.
■ The /proc directory contains information regarding detected hardware on the system
and is useful when verifying whether an installation was successful.
■ You can use the bootable OS found on standard and live Linux installation media to
access and repair a damaged Linux installation.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
282
Chapter 6
Linux Server Deployment
Key Terms
1U server
A rackmount server that has a standard height of 1.75 inches.
blade server
See rackmount server.
chroot command A Linux command that can be used to change the root of one Linux
system to another.
disk mirroring A RAID configuration consisting of two identical hard disks to which
identical data are written in parallel, thus ensuring fault tolerance. Also known as RAID 1.
disk striping A RAID configuration in which a single file is divided into sections, which
are then written to different hard disks concurrently to speed up access time; this type of
RAID is not fault tolerant. Also known as RAID 0.
disk striping with parity
A RAID configuration that incorporates disk striping for faster
file access, as well as parity information to ensure fault tolerance. Also known as RAID 5.
fault tolerant Term used to describe a device that exhibits a minimum of downtime in the
event of a failure.
firmware RAID
A RAID system controlled by the computer’s BIOS.
GRUB (GRand Unified Boot loader) The most common boot loader used on Linux systems.
It loads and executes the Linux kernel at boot time.
hardware RAID
A RAID system controlled by hardware located on a disk controller card
within the computer.
installation log files The files created at installation to record actions that occurred or
failed during the installation process.
Internet SCSI (iSCSI)
iSCSI initiator
A SCSI technology that transfers data via TCP/IP networks.
A term that refers to the computer that connects to the iSCSI target within
an iSCSI SAN.
iSCSI target
A term that refers to a storage device within an iSCSI SAN.
journalctl command A command used to configure journald, as well as to view log
entries in the journald database.
journald A system used to record (or journal) system events on some Linux distributions,
such as Fedora 20.
Linux server distribution
A Linux distribution that contains packages that are useful for
Linux servers.
Logical Unit Number (LUN)
A unique identifier for each device attached to any given node
in a SCSI chain.
mdadm command
A command used to configure software RAID on a Linux system.
overclocked Term used to describe a CPU that runs faster than the clock speed for which
it has been rated.
Parallel SCSI
The traditional SCSI technology that transfers data across parallel cables.
rackmount server
A thin form factor used to house server hardware that is installed in a
server rack.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
283
RAID-Z An implementation of RAID level 5 using ZFS, which uses a variable stripe that
provides for better performance and fault tolerance.
Redundant Array of Independent Disks (RAID) The process of combining the storage space
of several hard disk drives into one larger, logical storage unit.
SCSI ID
A number that uniquely identifies and prioritizes devices attached to a SCSI controller.
segmentation fault An error that software encounters when it cannot locate the
information needed to complete its task.
Serial Attached SCSI (SAS) A SCSI technology that transfers information in serial mode
rather than the traditional parallel mode.
software RAID
A RAID system that is controlled by software running within the operating
system.
spanning
A type of RAID level 0 that allows two or more devices to be represented as a
single large volume.
Storage Area Network (SAN)
A group of computers that access the same storage device
across a fast network.
system rescue
The process of using a live Linux OS to access and repair a damaged Linux
installation.
target ID
See SCSI ID.
terminator A device used to terminate an electrical conduction medium to absorb the
transmitted signal and prevent signal bounce.
uninterruptible power supply (UPS)
A device that contains battery storage and is used to
supply power to computers in the event of a power outage.
Zettabyte File System (ZFS)
A high-performance filesystem and volume management software
that is often used to create volumes from multiple storage devices on Linux and UNIX systems.
zfs command
ZFS pool
A command used to configure ZFS filesystem features.
A series of storage devices that are managed by ZFS.
ZFS subfilesystem
A subdirectory on a ZFS volume that can be managed as a separate
unit by ZFS.
ZFS volume
A volume created from space within a ZFS pool that contains a ZFS filesystem.
zpool command
A command used to configure ZFS pools and volumes.
Review Questions
1. SAS transfers data to SCSI disks via parallel cables. True or False?
2. Which of the following is used to describe a computer that is used to access an iSCSI
hard disk across the network?
a.
iSCSI target
b. iSCSI requestor
c.
iSCSI initiator
d. iSCSI terminator
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
284
Chapter 6
Linux Server Deployment
3. You want to view log files to get information about a problem that occurred during a
Linux installation. In which directory will you likely find the log files?
a.
/root/log
b. /sys/log
c.
/var/log
d. /etc/log
4. Which of the following RAID levels is not fault tolerant?
a.
RAID 0
b. RAID 1
c.
RAID 4
d. RAID 5
5. Which command can you use during a system rescue to switch from the root of the live
OS to the root of the Linux system installed on the hard disk?
a.
mount
b. sysimage
c.
chroot
d. rescue
6. Which of the following is not a type of RAID?
a.
hardware RAID
b. software RAID
c.
firmware RAID
d. serial RAID
7. Where is the /proc filesystem stored?
a.
in RAM
b. on the hard disk drive in the / directory
c.
on the hard disk drive in the /etc directory
d. on the hard disk drive in the /var directory
8. Which RAID level uses striping with parity?
a.
2
b. 4
c.
1
d. 5
9. RAID-Z is functionally equivalent to RAID level 1. True or False?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Review Questions
285
10. SCSI devices that use an 8-bit-wide data path use ________________.
a.
an 8-pin connector
b. a 15-pin connector
c.
a 50-pin connector
d. a 68-pin connector
11. Which type of RAID is entirely configured during the Linux installation process?
a.
hardware RAID
b. software RAID
c.
firmware RAID
d. serial RAID
12. What command can be used to create a ZFS volume called test from the space on /dev/
sdb and /dev/sdc that functions like RAID level 1?
a.
zpool create test /dev/sdb /dev/sdc
b. zpool create test mirror /dev/sdb /dev/sdc
c.
zpool create test raidz /dev/sdb /dev/sdc
d. zpool create test raidz2 /dev/sdb /dev/sdc
13. To which directory will the test ZFS volume from the previous question be mounted by
the ZFS system?
a.
/mnt/test
b. /media/test
c.
/zfs/test
d. /test
14. Linux servers are typically installed in a rack using rackmount server hardware. Which
of the following is used to describe the minimum height of a rackmount server?
a.
1U
b. Series A
c.
Type A
d. Level 5
15. Which RAID level is also referred to as mirroring?
a.
0
b. 1
c.
4
d. 5
16. ZFS volumes are mounted at boot time from entries within /etc/fstab by default. True or
False?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
286
Chapter 6
Linux Server Deployment
17. Which of the following could result in a segmentation fault (fatal signal 11) during a
Fedora installation?
a.
RAM problems
b. overclocked CPU
c.
improper device driver
d. all of the above
18. SCSI-1 is also referred to as ________________.
a.
fast and wide
b. slow and wide
c.
slow and narrow
d. fast and narrow
19. Which of the following items are you typically required to configure during a Linux
server installation? (Choose all that apply.)
a.
package selection
b. boot loader configuration
c.
system host name
d. update configuration
20. Which of the following commands can be used on a Fedora 20 system to view hardware
and service startup information during the boot process?
a.
less /var/log/boot.log
b. less /var/log/messages
c.
less /var/log/syslog
d. journalctl -b
Hands-on Projects
These projects should be completed in the order given. The hands-on projects
presented in this chapter should take a total of three hours to complete. The
requirements for this lab include:
• A computer with Fedora 20 installed according to Hands-on Project 2-1
• The ISO image for Ubuntu Server 14.04 installation media (ubuntu-14.04.1-serveramd64.iso)
Project 6-1
In this hands-on project, you install Ubuntu Server 14.04 Linux within a virtual machine on
a Windows computer and examine the LVM partition configuration afterwards.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
287
1. In your virtualization software, create a new virtual machine called Ubuntu Server
Linux that has the following characteristics:
a.
2GB of memory
b. An Internet connection via your PC’s network card (preferably using bridged mode)
c.
A 20GB virtual hard disk
d. The virtual machine DVD drive attached to the ISO file for the Ubuntu Server
14.04 installation media (ubuntu-14.04.1-server-amd64.iso)
2. Start and then connect to your Ubuntu Server Linux virtual machine using your virtualization software.
The Ubuntu Server Linux installation program does not use a graphical desktop. As a result, you must switch between buttons shown on
the screen using the Tab key and press Enter to make your
selections.
3. At the Language screen, ensure that English is selected and press Enter.
4. At the Ubuntu welcome screen, examine the options. Are there options that allow you
to check the installation media for defects as well as test RAM for defects (using memtest86)? Ensure that Install Ubuntu Server is selected and press Enter.
5. At the Select a language page, ensure that English is selected and press Enter.
6. At the Select your location screen, ensure that United States is selected and press Enter.
7. At the Configure the keyboard screen, select Yes and press Enter. Follow the prompts to
complete the detection. When the detection process finds a “us” keyboard layout, select
Continue and press Enter.
8. At the Configure the network screen, type a hostname of UbuntuXX (where XX is the
unique number assigned to you by your instructor during course setup), select Continue,
and press Enter.
9. At the Set up users and passwords page, type user1 as the full user name, select Continue, and press Enter. When prompted for the simple user name, ensure that user1 is
displayed, select Continue, and press Enter. When prompted for a password for user1,
type LNXrocks!, select Continue, and press Enter. When prompted to repeat the password, enter LNXrocks! again, select Continue, and press Enter. When prompted to
encrypt user1’s home directory, ensure that No is selected and press Enter.
10. At the Configure the clock screen, ensure that the correct time zone is displayed, select
Yes and press Enter. Alternatively, if an incorrect time zone is displayed, select No,
press Enter, select your time zone, and press Enter.
11. At the Partition disks screen, note that the default selection is Guided – use entire disk
and set up LVM and press Enter. Press Enter again to select your disk (sda or hda), and
then select Yes and press Enter to write your partition changes to the disk. Note that the
full size (approx. 20GB) is used for the volume group, select Continue, and press Enter.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
288
Chapter 6
Linux Server Deployment
12. At the summary screen, note that Ubuntu will create an LV for the root filesystem with
an ext4 filesystem and an LV for swap as well. Select Yes and press Enter to write the
changes to the disk and start the installation of the core system packages.
13. At the Configure the package manager screen, optionally supply the URL address of
your classroom proxy (if your classroom uses one), select Continue, and press Enter.
14. At the Configuring tasksel, ensure that No automatic updates is selected and press
Enter. Why is automatic updating often disabled on production servers?
15. At the Software selection screen, use the spacebar to individually choose all packages
(except for Manual package selection), select Continue, and press Enter.
16. At the Configuring mysql-server-5.5 screen, supply a password of LNXrocks!, select
Continue, and press Enter. When prompted to confirm the password, type LNXrocks!,
select Continue, and press Enter.
17. At the Postfix Configuration screen, ensure that Internet Site is selected and press Enter.
When prompted for a system mail name, ensure that UbuntuXX is listed (where XX is
the unique number assigned to you by your instructor during course setup), select Continue, and press Enter.
18. At the configuring dovecot-core screen, ensure that Yes is selected and press Enter to
create an SSL encryption certificate. When prompted for the host name used in the certificate, type UbuntuXX in place of localhost (where XX is the unique number assigned
to you by your instructor during course setup), select Continue, and press Enter.
19. At the Install the GRUB boot loader on a hard disk screen, ensure that Yes is selected
and press Enter.
20. At the Finish the installation screen, ensure that Continue is selected and press Enter to
reboot into the new system.
21. After the system has booted, note that a graphical login is not available. Log in as user1
with the password LNXrocks!. At the command prompt, type who and press Enter.
What terminal are you using?
22. The root user password is not set during an Ubuntu Server installation. Instead, the user
configured during the installation process (user1) has the ability to configure the root
user password following installation. At the command prompt, type sudo passwd
root and press Enter. Supply user1’s password of LNXrocks! when prompted and
press Enter. Supply a password of LNXrocks! for the root user when prompted and
press Enter. Confirm the root user password by typing LNXrocks! and press Enter.
23. At the command prompt, type su - root and supply a password of LNXrocks! to
switch to the root user.
24. At the command prompt, type df and press Enter. Examine the output of the df command. Why was a /boot partition created on your hard disk? Is the / (root) filesystem a
volume managed by the LVM?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
289
25. At the command prompt, type fdisk -l and press Enter. Note that the remainder of
the drive is an extended partition (/dev/sda2 or /dev/hda2) that contains a single logical
drive (/dev/sda5 or /dev/hda5) used exclusively by the LVM.
26. At the command prompt, type lvdisplay and press Enter. Note the logical volumes
created for the root filesystem and swap partition within the Ubuntu-XX-vg volume
group.
27. Type exit and press Enter to log out of your shell.
Project 6-2
In this hands-on project, you examine installation log files and system information on
Fedora Linux and Ubuntu Server Linux.
1. On your Ubuntu Server Linux virtual machine, log into the command-line terminal
(tty1) using the user name of root and the password of LNXrocks!.
2. At the command prompt, type ls /var/log/installer and press Enter. What regular log files are present in this directory? From their names, which of these files likely
contains system information, and which of these files contains events that occurred during the installation process?
3. At the command prompt, type egrep –i “(error|warn)” /var/log/
installer/syslog and press Enter, View any warnings or errors that occurred during your installation.
4. At the command prompt, type ls –F /proc and press Enter to view the file and
directory contents of the proc filesystem.
5. At the command prompt, type less /proc/cpuinfo and press Enter. Did the
installation detect your CPU correctly? Type q to quit the less utility.
6. At the command prompt, type less /proc/modules and press Enter. What is displayed? What does each entry represent? Type q to quit the less utility.
7. At the command prompt, type less /proc/meminfo and press Enter. Does your
Linux system recognize all the memory in your computer? If not, what could you do?
Type q to quit the less utility.
8. At the command prompt, type dmesg | less and press Enter. Observe the entries.
How do they correspond with the hardware information that you saw in the previous
three steps? Type q to quit the less utility.
9. At the command prompt, type less /var/log/boot.log and press Enter. What
does each entry represent? Type q to quit the less utility.
10. At the command prompt, type less /var/log/syslog and press Enter. What does
each entry represent? How do these lines correspond to the information displayed in the
previous two steps? Type q to quit the less utility.
11. At the command prompt, type poweroff and press Enter to shut down your Ubuntu
Server Linux virtual machine.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
290
Chapter 6
Linux Server Deployment
12. Boot your Fedora Linux virtual machine. After your Linux system has loaded, switch to
a command-line terminal (tty2) by pressing Ctrl+Alt+F2. Log in to the terminal using
the user name of root and the password of LNXrocks!.
13. At the command prompt, type ls /var/log/anaconda and press Enter. What regular log files are present in this directory? From their names, which of these files likely
contains all key events that occurred during the installation process? Which one likely
contains information about the programs installed during installation?
14. At the command prompt, type egrep –i “(error|warn)” /var/log/anaconda/* and press Enter. View any warnings or errors that occurred during your
installation.
15. At the command prompt, type ls –F /proc and press Enter to view the file and
directory contents of the proc filesystem. Do the contents look similar to those from
Ubuntu Server Linux? Next, type less /proc/cpuinfo and press Enter. Do the contents of this file look similar to the contents you saw in Step 5? Why? Type q to quit the
less utility.
16. At the command prompt, type dmesg | less and press Enter. Observe the entries.
Type q to quit the less utility.
17. At the command prompt, type journalctl –b | less and press Enter. What does
each entry represent? Type q to quit the less utility.
18. At the command prompt, type poweroff and press Enter to shut down your Fedora
Linux virtual machine.
Project 6-3
In this hands-on project, you install ZFS support on Ubuntu Server and configure the ZFS
filesystem.
1. Boot your Ubuntu Server Linux virtual machine. After your Linux system has loaded,
log into the command-line terminal (tty1) using the user name of root and the password
of LNXrocks!.
2. At the command prompt, type apt-get -y install build-essential gawk
zlib1g-dev uuid-dev vim-nox python-software-properties and press
Enter to download and install ZFS prerequisite packages.
Ubuntu Server Linux uses a different package manager than Fedora
Linux. The apt-get command is functionally equivalent to the
yum command that you used in previous chapters to download and
install software from Internet repositories. Both apt-get and yum
are discussed in Chapter 11.
3. At the command prompt, type add-apt-repository ppa:zfs-native/stable,
press Enter, and then press Enter again to add the location of the online ZFS software
repository on the Internet to the Ubuntu installer. Next, type apt-get update and
press Enter to update the Ubuntu installer with the new repository information.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
291
4. At the command prompt, type apt-get install ubuntu-zfs and press Enter to
install ZFS on your Ubuntu Server Linux system.
5. Edit the /etc/modules file (vi /etc/modules) and add the following lines to the end
of the file. When finished, save your changes and quit the vi editor.
spl
zavl
znvpair
zunicode
zcommon
zfs
6. At the command prompt, type update-initramfs –u and press Enter to update the
disk and module support for the image used to load kernel modules at boot time.
7. Although ZFS normally works with device files for additional disk devices (/dev/sdb1, /
dev/sdc1, /dev/sdd1, and so on), ZFS can work with nearly any device or file that can be
used to store data. As a result, we’ll create four empty files using the dd command that
we’ll treat as four separate disks to practice ZFS commands. Run the following commands to create four 128MB files called /disk1, /disk2, /disk3, and /disk4:
dd
dd
dd
dd
if=/dev/zero
if=/dev/zero
if=/dev/zero
if=/dev/zero
of=/disk1
of=/disk2
of=/disk3
of=/disk4
bs=1024k
bs=1024k
bs=1024k
bs=1024k
count=128
count=128
count=128
count=128
8. At the command prompt, type zpool create webstorage /disk1 and press
Enter to create a simple ZFS volume called webstorage from the space on /disk1. Next,
type zpool list at the command prompt and press Enter to view your configuration.
9. At the command prompt, type cp /etc/hosts /webstorage and press Enter to
copy the /etc/hosts file to the new ZFS filesystem . Next, type ls -l /webstorage
at the command prompt and press Enter to view the contents of the filesystem. Was
the /etc/hosts file copied successfully?
10. At the command prompt, type zpool destroy webstorage and press Enter to
remove the webstorage volume.
11. At the command prompt, type zpool create webstorage mirror /disk1
/disk2 and press Enter to create a mirrored ZFS volume called webstorage from the
space on /disk1 and /disk2. Next, type zpool list at the command prompt and
press Enter to view your configuration. Following this, type zpool status webstorage at the command prompt and press Enter. Does you mirror have any problems
listed?
12. At the command prompt, type dd if=/dev/random of=/disk1 bs=512
count=1 and press Enter to overwrite a portion of /disk1 using the dd command, simulating disk corruption. Next, type zpool scrub webstorage at the command
prompt and press Enter to update the status of the ZFS filesystem, and then type
zpool status webstorage at the command prompt and press Enter. Does you
mirror have any problems listed?
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
292
Chapter 6
Linux Server Deployment
13. At the command prompt, type zpool detach webstorage /disk1 and press
Enter to remove the bad disk (/disk1) from the mirror, and then type zpool status
webstorage at the command prompt and press Enter to verify that it is no longer
listed.
14. At the command prompt, type zpool attach webstorage /disk2 /disk3 and
press Enter to mirror the data on /disk2 to a new disk (/disk3), and then type zpool
status webstorage at the command prompt and press Enter. Is the mirror fully
functional using /disk2 and /disk3?
15. At the command prompt, type zpool iostat -v webstorage and press Enter to
view the input and output statistics for your mirror. Next, type zpool destroy
webstorage and press Enter to remove the webstorage mirror.
16. At the command prompt, type zpool create webstorage raidz /disk2
/disk3 /disk4 and press Enter to create a RAID-Z volume called webstorage
using /disk2, /disk3 and /disk4, and then type zpool status webstorage at
the command prompt and press Enter to verify the results. Next, type zpool
iostat -v webstorage and press Enter to view the input and output statistics
for your RAID-Z volume.
17. At the command prompt, type each of the following commands in turn and press Enter
to create three subdirectories under /webstorage:
mkdir /webstorage/site1
mkdir /webstorage/site2
mkdir /webstorage/site3
18. At the command prompt, type zfs list and press Enter. Are the site1, site2, and
site3 directories listed? Why? Next, type each of the following commands in turn and
press Enter to create three subfilesystems for site1, site2, and site3:
zfs create webstorage/site1
zfs create webstorage/site2
zfs create webstorage/site3
19. At the command prompt, type zfs list and press Enter. Are the site1, site2, and
site3 directories listed? Why?
20. At the command prompt, type zfs get all webstorage/site1 and press Enter
to view the available options for the site1 subfilesystem. Next, type zfs set quota=10G webstorage/site1 at the command prompt and press Enter to set a
quota of 10GB for the subfilesystem. Next, type zfs get all webstorage/
site1 and press Enter. Was your quota listed? Why will this quota never be applied
to the webstorage/site1 subfilesystem? (Hint: Think of the total size of the webstorage
volume)
21. At the command prompt, type poweroff and press Enter to shut down your Ubuntu
Server Linux virtual machine.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Hands-on Projects
293
Project 6-4
In this hands-on project, you use system rescue on both Fedora Linux and Ubuntu Server
Linux to check your root filesystem for errors and change the root user’s password.
1. In your virtualization software, ensure that the DVD for your Fedora Linux virtual
machine is attached to the ISO image for Fedora 20 Live (Fedora-Live-Desktop-x86_6420-1.iso). Next, start and then connect to your Fedora Linux virtual machine using your
virtualization software.
2. At the Fedora Live welcome screen, press Enter to boot Fedora Live.
3. Once the graphical desktop and Welcome to Fedora screen have loaded, select the
option Try Fedora and click Close when prompted.
4. Navigate to Activities, Show Applications, Utilities, Terminal within the GNOME desktop to open a BASH terminal.
5. At the command prompt, type su - root and press Enter to switch to the root user.
6. At the command prompt, type df and press Enter to view the mounted filesystems. Is
the root filesystem on your hard disk mounted?
7. At the command prompt, type fsck –f /dev/sda3 (or fsck -f /dev/hda3)
and press Enter to check your (/) root filesystem for errors.
8. At the command prompt, type mount /dev/sda3 /mnt (or mount /dev/hda3
/mnt) and press Enter to mount the (/) root filesystem on your hard disk to the
/mnt directory on the Fedora live system. Next, type mount /dev/sda1 /mnt
/boot (or mount /dev/hda1 /mnt/boot) and press Enter to mount the /boot
filesystem on your hard disk to the /mnt/boot directory on the Fedora live system.
9. At the command prompt, type chroot /mnt and press Enter to switch from the root
filesystem on your Live Fedora system to the root filesystem on your hard disk. Next,
type ls /root and press Enter. Do you recognize the files?
10. At the command prompt, type passwd root and press Enter. Supply a new root user
password of Secret123 and press Enter when prompted (twice).
11. Click the power icon in the upper-right corner, select the power icon that appears, and
click Power Off to shut down your Fedora Live installation image.
12. In the Settings for your virtual machine in your virtualization software, ensure that the
DVD drive is no longer attached to the Fedora ISO image.
13. Finally, start your Fedora Linux virtual machine using your virtualization software.
After the system has loaded, switch to a command-line terminal (tty2) by pressing
Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of
Secret123. Were you successful?
14. At the command prompt, type passwd root and press Enter. Supply a new root user
password of LNXrocks! and press Enter when prompted (twice).
15. At the command prompt, type poweroff and press Enter to shut down your Fedora
Linux virtual machine.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
294
Chapter 6
Linux Server Deployment
16. In your virtualization software, ensure that the DVD for your Ubuntu Server Linux virtual machine is attached to the ISO image for Ubuntu Server 14.04 (ubuntu-14.04.1server-amd64.iso). Next, start and then connect to your Ubuntu Server Linux virtual
machine using your virtualization software.
17. At the language screen, ensure that English is selected and press Enter.
18. At the Ubuntu welcome screen, navigate to the Rescue a broken system option and
press Enter.
19. At the Select a language page, ensure that English is selected and press Enter.
20. At the Select your location screen, ensure that United States is selected and press Enter.
21. At the Configure the keyboard screen, select Yes and press Enter. Follow the prompts to
complete the detection. When the detection process finds a “us” keyboard layout, select
Continue and press Enter.
22. At the Configure the network screen, view the default hostname supplied, select Continue, and press Enter.
23. At the Configure the clock screen, ensure that the correct time zone is displayed, select
Yes, and press Enter. Alternatively, if an incorrect time zone is displayed, select No,
press Enter, select your time zone, and press Enter.
24. At the Enter rescue mode screen, select /dev/UbuntuXX-vg/root (where XX is the unique
number assigned to you by your instructor during course setup) and press Enter. When
prompted to also mount the /boot partition, ensure that Yes is selected and press Enter.
25. At the Enter rescue mode screen, view the options. Select Execute a shell in /dev/UbuntuXX-vg/root (where XX is the unique number assigned to you by your instructor during course setup) and press Enter. Ensure that Continue is selected and press Enter when
prompted. This will change the root to the mounted / (root) filesystem on your hard
disk with the chroot command.
26. At the command prompt, type df and press Enter to view the mounted filesystems. Is
the root filesystem on your hard disk mounted?
27. At the command prompt, type passwd root and press Enter. Supply a new root user
password of Secret123 and press Enter when prompted (twice).
28. At the command prompt, type exit and press Enter.
29. At the Enter rescue mode screen, select Reboot the system and press Enter.
30. In the Settings for your virtual machine in your virtualization software, ensure that the
DVD drive is no longer attached to the Ubuntu Server ISO image.
31. After the system has loaded, log into the command-line terminal (tty1) using the user
name of root and the password of Secret123.
32. At the command prompt, type passwd root and press Enter. Supply a new root user
password of LNXrocks! and press Enter when prompted (twice).
33. At the command prompt, type poweroff and press Enter to shut down your Ubuntu
Server Linux virtual machine.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovery Exercises
295
Discovery Exercises
1. Determine the following about your system by viewing the appropriate files within the /
proc directory:
a.
Where is the Linux kernel located?
b. What version is your Linux kernel
c.
What filesystems are supported by your Linux kernel?
d. What modules are loaded into the kernel?
e.
Gather information about the physical memory on your system.
2. Use the Internet to gather information on three different commercial iSCSI SAN enclosures that can be used to host Linux, and write a short report that compares and contrasts their features and cost. In what situations would you use each one?
3. Use the Internet to gather information on three different commercial hardware RAID
controllers that perform RAID level 5, and write a short report that compares and contrasts their features and cost. In what situations would you use each one?
4. Search the Internet for information about five Linux installation problems that are different than those described in this chapter. How have other people solved these problems? If you had similar difficulties during installation, how could you get help?
5. Linux servers are typically stored in a locked server closet to prevent physical access by
unauthorized persons. Given the steps that you performed in Project 6-4, describe why
these physical restrictions are warranted.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
chapter
7
Working with the BASH Shell
After completing this chapter, you will be able to:
•
•
•
•
•
•
•
Redirect the input and output of a command
Identify and manipulate common shell environment variables
Create and export new shell variables
Edit environment files to create variables upon shell startup
Describe the purpose and nature of shell scripts
Create and execute basic shell scripts
Effectively use common decision constructs in shell scripts
297
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
298
Chapter 7
Working with the BASH Shell
A solid understanding of shell features is vital to both administrators and users, who
must interact with the shell on a daily basis. The first part of this chapter describes how the
shell can manipulate command input and output using redirection and pipe shell metacharacters. Next, you explore the different types of variables present in a BASH shell after login, as
well as their purpose and usage. Finally, this chapter ends with an introduction to creating
and executing BASH shell scripts.
Command Input and Output
The BASH shell is responsible for providing a user interface and interpreting commands entered
on the command line. In addition, the BASH shell can manipulate command input and output,
provided the user specifies certain shell metacharacters on the command line alongside the command. Command input and output are represented by labels known as file descriptors. For
each command that can be manipulated by the BASH shell, there are three file descriptors:
• Standard Input (stdin)
• Standard Output (stdout)
• Standard Error (stderr)
Standard Input (stdin) refers to the information processed by the command during execution;
this often takes the form of user input typed on the keyboard. Standard Output (stdout) refers
to the normal output of a command, whereas Standard Error (stderr) refers to any error messages generated by the command. Both Standard Output and Standard Error are displayed on
the terminal screen by default. All three components are depicted in Figure 7-1.
As shown in Figure 7-1, each file descriptor is represented by a number, with stdin represented
by the number 0, stdout represented by the number 1, and stderr represented by the number 2.
Although all three descriptors are available to any command, not all commands use every
descriptor. The ls /etc/hosts /etc/h command used in Figure 7-1 gives Standard
stdin
0
stdout
1
stderr
2
Figure 7-1 The three common file descriptors
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
299
Output (the listing of the /etc/hosts file) and Standard Error (an error message indicating that
the /etc/h file does not exist) to the terminal screen, as shown in the following output:
[[email protected]1 ~]# ls /etc/hosts /etc/h
ls: cannot access /etc/h: No such file or directory
/etc/hosts
[[email protected] ~]# _
Redirection
You can use the BASH shell to redirect Standard Output and Standard Error from the terminal screen to a file on the filesystem. To do this, include the > shell metacharacter followed
by the absolute or relative pathname of the file. For example, to redirect only the Standard
Output to a file called goodoutput for the command used in Figure 7-1, you append the
number of the file descriptor (1) followed by the redirection symbol > and the file to redirect
the Standard Output to (goodoutput), as shown in the following output:
[[email protected] ~]# ls /etc/hosts /etc/h 1>goodoutput
ls: cannot access /etc/h: No such file or directory
[[email protected] ~]# _
You can include a space character after the > metacharacter, but it is
not necessary.
In the preceding output, the Standard Error is still displayed to the terminal screen because it
was not redirected to a file. The listing of /etc/hosts was not displayed, however; instead, it
was redirected to a file called goodoutput in the current directory. If the goodoutput file did
not exist prior to running the command in the preceding output, it is created automatically.
However, if the goodoutput file did exist prior to the redirection, the BASH shell clears its
contents before executing the command. To see that the Standard Output was redirected to
the goodoutput file, you can run the following commands:
[[email protected] ~]# ls –F
Desktop/ goodoutput
[[email protected] ~]# cat goodoutput
/etc/hosts
[[email protected] ~]# _
Similarly, you can redirect the Standard Error of a command to a file; simply specify file
descriptor number 2, as shown in the following output:
[[email protected] ~]#
/etc/hosts
[[email protected] ~]#
ls: cannot access
[[email protected] ~]#
ls /etc/hosts /etc/h 2>badoutput
cat badoutput
/etc/h: No such file or directory
_
In the preceding output, only the Standard Error was redirected to a file called badoutput;
thus, the Standard Output (a listing of /etc/hosts) was displayed on the terminal screen.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
300
Chapter 7
Working with the BASH Shell
Because redirecting the Standard Output to a file for later use is more common than redirecting the Standard Error to a file, the BASH shell assumes Standard Output in the absence of a
numeric file descriptor:
[[email protected] ~]#
ls: cannot access
[[email protected] ~]#
/etc/hosts
[[email protected] ~]#
ls /etc/hosts /etc/h >goodoutput
/etc/h: No such file or directory
cat goodoutput
_
In addition, you can redirect both Standard Output and Standard Error to separate files at
the same time, as shown in the following output:
[[email protected] ~]#
[[email protected] ~]#
/etc/hosts
[[email protected] ~]#
ls: cannot access
[[email protected] ~]#
ls /etc/hosts /etc/h >goodoutput 2>badoutput
cat goodoutput
cat badoutput
/etc/h: No such file or directory
_
The order of redirection on the command line does not matter;
the command ls /etc/hosts /etc/h >goodoutput
2>badoutput is the same as ls /etc/hosts /etc/h
2>badoutput >goodoutput.
It is important to use separate filenames to hold the contents of Standard Output and Standard Error; using the same filename for both results in a loss of data because the system
attempts to write both contents to the file at the same time:
[[email protected] ~]#
[[email protected] ~]#
/etc/hosts
access /etc/h: No
[[email protected] ~]#
ls /etc/hosts /etc/h >goodoutput 2>goodoutput
cat goodoutput
such file or directory
_
To redirect both Standard Output and Standard Error to the same file without any loss of data,
you must use special notation. To specify that Standard Output be sent to the file goodoutput
and Standard Error be sent to the same place as Standard Output, you can do the following:
[[email protected] ~]#
[[email protected] ~]#
ls: cannot access
/etc/hosts
[[email protected] ~]#
ls /etc/hosts /etc/h >goodoutput 2>&1
cat goodoutput
/etc/h: No such file or directory
_
Alternatively, you can specify that the Standard Error be sent to the file badoutput and Standard Output be sent to the same place as Standard Error:
[[email protected] ~]#
[[email protected] ~]#
ls: cannot access
/etc/hosts
[[email protected] ~]#
ls /etc/hosts /etc/h 2>badoutput >&2
cat badoutput
/etc/h: No such file or directory
_
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
301
In all of the examples used earlier, the contents of the files used to store the output from
commands were cleared prior to use by the BASH shell. Another example of this is shown
in the following output when redirecting the Standard Output of the date command to the
file dateoutput:
[[email protected] ~]# date >dateoutput
[[email protected] ~]# cat dateoutput
Fri Aug 20 07:54:00 EDT 2015
[[email protected] ~]# date >dateoutput
[[email protected] ~]# cat dateoutput
Fri Aug 20 07:54:00 EDT 2015
[[email protected] ~]# _
To prevent the file from being cleared by the BASH shell and append output to the existing
output, you can specify two > metacharacters alongside the file descriptor, as shown in the
following output:
[[email protected] ~]# date >dateoutput
[[email protected] ~]# cat dateoutput
Fri Aug 20 07:54:32 EDT 2015
[[email protected] ~]# date >>dateoutput
[[email protected] ~]# cat dateoutput
Fri Aug 20 07:54:32 EDT 2015
Fri Aug 20 07:54:48 EDT 2015
[[email protected] ~]# _
You can also redirect a file to the Standard Input of a command using the < metacharacter.
Because there is only one file descriptor for input, there is no need to specify the number 0
before the < metacharacter to indicate Standard Input, as shown next:
[[email protected] ~]# cat </etc/issue
Fedora release 20 (Heisenbug)
Kernel \r on an \m (\l)
[[email protected] ~]# _
In the preceding output, the BASH shell located and sent the /etc/issue file to the cat command as Standard Input. Because the cat command normally takes the filename to be displayed as an argument on the command line (e.g., cat /etc/issue), there is no need to
use Standard Input redirection with the cat command as used in the previous example;
however, some commands on the Linux system only accept files when they are passed by
the shell through Standard Input. The tr command is one such command that can be
used to replace characters in a file sent via Standard Input. To translate all of the lowercase
r characters in the /etc/issue file to uppercase R characters, you can run the following
command:
[[email protected] ~]# tr r R </etc/issue
FedoRa Release 20 (Heisenbug)
KeRnel \R on an \m (\l)
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
302
Chapter 7
Working with the BASH Shell
The preceding command does not modify the /etc/issue file; it simply takes a copy of the /etc/
issue file, manipulates it, and then sends the Standard Output to the terminal screen. To save
a copy of the Standard Output for later use, you can use both Standard Input and Standard
Output redirection together:
[[email protected] ~]# tr r R </etc/issue >newissue
[[email protected] ~]# cat newissue
FedoRa Release 20 (Heisenbug)
KeRnel \R on an \m (\l)
[[email protected] ~]# _
As with redirecting Standard Output and Standard Error in the same command, you should
use different filenames when redirecting Standard Input and Standard Output. However, this
is because the BASH shell clears a file that already exists before performing the redirection.
An example of this is shown in the following output:
[[email protected] ~]# sort <newissue >newissue
[[email protected] ~]# cat newissue
[[email protected] ~]# _
The newissue file has no contents when displayed in the preceding output. This is because the
BASH shell saw that output redirection was indicated on the command line, it cleared the contents of the file newissue, then sorted the blank file and saved the output (nothing in our example) into the file newissue. Because of this feature of shell redirection, Linux administrators commonly use the command >filename at the command prompt to clear the contents of a file.
The contents of log files are typically cleared periodically using the
command > /path/to/logfile.
Table 7-1 summarizes the different types of redirection discussed in this section.
Pipes
Note from Table 7-1 that redirection only occurs between a command and a file and vice
versa. However, you can send the Standard Output of one command to another command
as Standard Input. To do this, you use the pipe | shell metacharacter and specify commands
on either side. The shell then sends the Standard Output of the command on the left to the
command on the right, which then interprets the information as Standard Input. This process
is depicted in Figure 7-2.
stdout
stdin
Figure 7-2 Piping information from one command to another
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
303
Command
Description
command 1>file
command >file
The Standard Output of the command is sent to a file instead of to the
terminal screen.
command 2>file
The Standard Error of the command is sent to a file instead of to the
terminal screen.
command 1>fileA 2>fileB
command >fileA 2>fileB
The Standard Output of the command is sent to fileA instead of to the
terminal screen, and the Standard Error of the command is sent to fileB
instead of to the terminal screen.
command 1>file 2>&1
command >file 2>&1
command 1>&2 2>file command >&2 2>file
Both the Standard Output and the Standard Error are sent to the same
file instead of to the terminal screen.
command 1>>file
command >>file
The Standard Output of the command is appended to a file instead of
being sent to the terminal screen.
command 2>>file
The Standard Error of the command is appended to a file instead of
being sent to the terminal screen.
command 0<file
command <file
The Standard Input of a command is taken from a file.
Table 7-1 Common redirection examples
A series of commands that includes the pipe | metacharacter is commonly referred to as a pipe.
The pipe symbol can be created on most keyboards by pressing
Shift+\.
For example, the Standard Output of the ls –l /etc command is too large to fit on one
terminal screen. To send the Standard Output of this command to the less command,
which views Standard Input page-by-page, you could use the following command:
[[email protected] ~]# ls –l /etc | less
total 1836
drwxr-xr-x 3 root root
4096
-rw-r- -r- - 1 root root
16
drwxr-xr-x 2 root root
4096
-rw-r- -r- - 1 root root
1518
drwxr-xr-x 2 root root
4096
drwxr-xr-x 2 root root
4096
-rw-r- -r- - 1 root root
541
-rw-r- -r- - 1 root root
55
Dec
Sep
Sep
Jun
Dec
Sep
Sep
Aug
11
16
17
7
11
24
25
1
2013
21:54
19:44
2013
2013
10:06
2013
2013
abrt
adjtime
akonadi
aliases
alsa
alternatives
anacrontab
asound.conf
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
304
Chapter 7
Working with the BASH Shell
-rw-r- -r- drwxr-xr-x
drwxr-x- drwxr-x- drwxr-xr-x
drwxr-xr-x
-rw-r- -r- drwxr-xr-x
-rw-r----drwxr-xr-x
-rw-r-r- -rw-r-r- drwxr-xr-x
-rw-r-r- :
1
2
3
3
4
2
1
2
1
2
1
1
2
1
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
1
root
4096
root
4096
root
4096
root
4096
root
4096
root
2815
root
4096
brlapi
33
root 12288
root 21929
root
520
root
4096
root
1165
Aug
Dec
Dec
Dec
Dec
Sep
Jun
Dec
Dec
Dec
Sep
Aug
Aug
Aug
2
11
11
11
11
17
7
5
11
11
23
3
3
9
2013
2013
2013
2013
2013
19:36
2013
2013
2013
2013
2013
2013
2013
2013
at.deny
at-spi2
audisp
audit
avahi
bash_completion.d
bashrc
binfmt.d
brlapi.key
brltty
brltty.conf
cagibid.conf
chkconfig.d
chrony.conf
You need not have spaces around the | metacharacter; the commands ls –l/etc|less and
ls –l/etc|less are equivalent.
A common use of piping is to reduce the amount of information displayed on the terminal
screen from commands that display too much information. Take the following output from
the mount command:
[[email protected] ~]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=1015268k,
nr_inodes=253817,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,
noexec,relatime)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts
(rw,nosuid,noexec,relatime,seclabel,gid=5, mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs
(rw,nosuid,nodev,noexec,seclabel, mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup
(rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/
systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup
(rw,nosuid,nodev,noexec, relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup
(rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup
(rw,nosuid,nodev,noexec, relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup
(rw,nosuid,nodev,noexec,relatime,devices)
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
305
cgroup on /sys/fs/cgroup/freezer type cgroup
(rw,nosuid,nodev,noexec, relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup
(rw,nosuid,nodev,noexec, relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup
(rw,nosuid,nodev,noexec, relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup
(rw,nosuid,nodev,noexec, relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup
(rw,nosuid,nodev,noexec,relatime,hugetlb)
/dev/sda3 on / type ext4 (rw,relatime,seclabel,data=ordered)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs
(rw,relatime,fd=35,
pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
configfs on /sys/kernel/config type configfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
tmpfs on /tmp type tmpfs (rw,seclabel)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
[[email protected] ~]# _
To view only those lines regarding ext4 filesystems, you could send the Standard Output of
the mount command to the grep command as Standard Input, as shown in the following
output:
[[email protected] ~]# mount | grep ext4
/dev/sda3 on / type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
[[email protected] ~]# _
The grep command in the preceding output receives the full output from the mount command and then displays only those lines that have ext4 in them. The grep command normally takes two arguments; the first specifies the text to search for and the second specifies
the filename(s) to search within. The grep command used in the preceding output requires
no second argument because the material to search comes from Standard Input (the mount
command) instead of from a file.
Furthermore, you can use more than one pipe | metacharacter on the command line to pipe
information from one command to another command, in much the same fashion as an
assembly line in a factory. Typically, an assembly line goes through several departments,
each of which performs a specialized task very well. For example, one department might
assemble the product, another might paint the product, and yet another might package the
product. Every product must pass through each department to be complete.
You can use Linux commands that manipulate data in the same way, connecting them into
an assembly line via piping. Information is manipulated by one command, and then that
manipulated information is sent to another command, which manipulates it further. The process continues until the information attains the form required by the user. The piping process
is depicted in Figure 7-3.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
306
Chapter 7
Working with the BASH Shell
stdout
stdin
stdout
stdin
stdout
stdin
Figure 7-3 Piping several commands
Any command that can take Standard Input and transform it into Standard Output is called a
filter command. It is important to note that commands such as ls and mount are not filter
commands because they do not accept Standard Input from other commands, but instead find information from the system and display it to the user. As a result, these commands must be at the beginning of a pipe. Other commands, such as vi, are interactive and as such cannot exist between two
pipe symbols because they cannot take from Standard Input and give to Standard Output.
Several hundred filter commands are available to Linux users. Table 7-2 lists some common
ones used throughout this book.
Take, for example, the prologue from Shakespeare’s Romeo and Juliet:
[[email protected] ~]# cat prologue
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Command
Description
sort
sort –r
Sorts lines in a file alphanumerically
wc
wc –l
wc –w
wc –c
Counts the number of lines, words, and characters in a file
Counts the number of lines in a file
pr
Formats a file for printing, with several options available; it places a date and page number at
the top of each page
pr –d
Formats a file double-spaced
tr
Replaces characters in the text of a file
grep
Displays lines in a file that match a regular expression
nl
Numbers lines in a file
awk
Extracts, manipulates, and formats text using pattern-action statements
sed
Manipulates text using search-and-replace expressions
split
Splits input into several files
unexpand
Converts space characters to tab characters
uniq
Omits repeated lines
xargs
Turns a list of input into arguments that can be used with a command
Reverse-sorts lines in a file alphanumerically
Counts the number of words in a file
Counts the number of characters in a file
Table 7-2 Commands that are commonly used within a pipe
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
307
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross’d lovers take their life;
Whole misadventured piteous overthrows
Do with their death bury their parents’ strife.
The fearful passage of their death-mark’d love,
And the continuance of their parents’ rage,
Which, but their children’s end, nought could remove,
Is now the two hours’ traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
Now suppose you want to replace all lowercase “a” characters with uppercase “A” characters in
the preceding file, sort the contents by the first character on each line, double-space the output,
and view the results page-by-page. To accomplish these tasks, you can use the following pipe:
[[email protected] ~]# cat prologue | tr a A | sort | pr –d | less
2015-08-20 08:06
A pAir of stAr-cross’d lovers tAke their life;
And the continuAnce of their pArents’ rAge,
Do with their deAth bury their pArents’ strife.
From Ancient grudge breAk to new mutiny,
From forth the fAtAl loins of these two foes
In fAir VeronA, where we lAy our scene,
Is now the two hours’ trAffic of our stAge;
The feArful pAssAge of their deAth-mArk’d love,
The which if you with pAtient eArs Attend,
Two households, both Alike in dignity,
WhAt here shAll miss, our toil shAll strive to mend.
Where civil blood mAkes civil hAnds uncleAn.
Which, but their children’s end, nought could remove,
Whole misAdventured piteous overthrows
:
Page 1
The command used in the preceding example displays the final Standard Output to the terminal screen via the less command. In many cases, you might want to display the results of
the pipe while at the same time save copy in a file on the hard disk. The filter command for
this job is the tee command, which takes information from Standard Input and sends that
information to a file, as well as to Standard Output.
To save a copy of the manipulated prologue before displaying it to the terminal screen with
the less command, you can use the following command:
[[email protected] ~]# cat prologue|tr a A|sort|pr –d|tee newfile|less
2015-08-20 08:06
Page 1
A pAir of stAr-cross’d lovers tAke their life;
And the continuAnce of their pArents’ rAge,
Do with their deAth bury their pArents’ strife.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
308
Chapter 7
Working with the BASH Shell
From Ancient grudge breAk to new mutiny,
From forth the fAtAl loins of these two foes
In fAir VeronA, where we lAy our scene,
Is now the two hours’ trAffic of our stAge;
The feArful pAssAge of their deAth-mArk’d love,
The which if you with pAtient eArs Attend,
Two households, both Alike in dignity,
WhAt here shAll miss, our toil shAll strive to mend.
Where civil blood mAkes civil hAnds uncleAn.
Which, but their children’s end, nought could remove,
Whole misAdventured piteous overthrows
:q
[[email protected] ~]# _
[[email protected] ~]# cat newfile
2015-08-20 08:06
A pAir of stAr-cross’d lovers tAke their life;
And the continuAnce of their pArents’ rAge,
Do with their deAth bury their pArents’ strife.
From Ancient grudge breAk to new mutiny,
From forth the fAtAl loins of these two foes
In fAir VeronA, where we lAy our scene,
Is now the two hours’ trAffic of our stAge;
The feArful pAssAge of their deAth-mArk’d love,
The which if you with pAtient eArs Attend,
Two households, both Alike in dignity,
WhAt here shAll miss, our toil shAll strive to mend.
Where civil blood mAkes civil hAnds uncleAn.
Which, but their children’s end, nought could remove,
Whole misAdventured piteous overthrows
[[email protected] ~]# _
Page 1
You can also combine redirection and piping, as long as input redirection occurs at the
beginning of the pipe and output redirection occurs at the end of the pipe. An example of
this is shown in the following output, which replaces all lowercase a characters with uppercase A characters in the prologue file used in the previous example, then sorts the file, numbers each line, and saves the output to a file called newprologue instead of sending the output
to the terminal screen.
[[email protected] ~]# tr a A <prologue | sort | nl >newprologue
[[email protected] ~]# cat newprologue
1 A pAir of stAr-cross’d lovers tAke their life;
2 And the continuAnce of their pArents’ rAge,
3 Do with their deAth bury their pArents’ strife.
4 From Ancient grudge breAk to new mutiny,
5 From forth the fAtAl loins of these two foes
6 In fAir VeronA, where we lAy our scene,
7 Is now the two hours’ trAffic of our stAge;
8 The feArful pAssAge of their deAth-mArk’d love,
9 The which if you with pAtient eArs Attend,
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
309
10 Two households, both Alike in dignity,
11 WhAt here shAll miss, our toil shAll strive to mend.
12 Where civil blood mAkes civil hAnds uncleAn.
13 Which, but their children’s end, nought could remove,
14 Whole misAdventured piteous overthrows
[[email protected] ~]# _
Many Linux commands can be used to provide large amounts of useful text information. As
a result, Linux administrators often use the sed and awk filter commands in conjunction
with pipes to manipulate text information obtained from these commands.
The sed command is typically used to search for a certain string of text, and replaces that
text string with another text string using the syntax s/search/replace/. For example,
the following output demonstrates how sed can be used to search for the string “the” and
replace it with the string “THE” in the prologue file used earlier:
[[email protected] ~]# cat prologue | sed s/the/THE/
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth THE fatal loins of these two foes
A pair of star-cross’d lovers take THEir life;
Whole misadventured piteous overthrows
Do with THEir death bury their parents’ strife.
The fearful passage of THEir death-mark’d love,
And THE continuance of their parents’ rage,
Which, but THEir children’s end, nought could remove,
Is now THE two hours’ traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
Notice from the preceding output that sed only searched for and replaced the first occurrence of the string “the” in each line. To have sed globally replace all occurrences of the
string “the” in each line, simply append a g to the search-and-replace expression:
[[email protected] ~]# cat prologue | sed s/the/THE/g
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth THE fatal loins of THEse two foes
A pair of star-cross’d lovers take THEir life;
Whole misadventured piteous overthrows
Do with THEir death bury THEir parents’ strife.
The fearful passage of THEir death-mark’d love,
And THE continuance of THEir parents’ rage,
Which, but THEir children’s end, nought could remove,
Is now THE two hours’ traffic of our stage;
The which if you with patient ears attend,
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
310
Chapter 7
Working with the BASH Shell
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
You can also tell sed the specific lines to search by prefixing the search-and-replace expression. For example, to force sed to replace the string “the” with “THE” globally on lines that
contain the string “love,” you can use the following command:
[[email protected] ~]# cat prologue | sed /love/s/the/THE/g
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross’d lovers take THEir life;
Whole misadventured piteous overthrows
Do with their death bury their parents’ strife.
The fearful passage of THEir death-mark’d love,
And the continuance of their parents’ rage,
Which, but their children’s end, nought could remove,
Is now the two hours’ traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
You can also force sed to perform a search-and-replace on certain lines only. To replace the
string “the” with “THE” globally on lines 5 to 8 only, you can use the following command:
[[email protected] ~]# cat prologue | sed 5,8s/the/THE/g
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth THE fatal loins of THEse two foes
A pair of star-cross’d lovers take THEir life;
Whole misadventured piteous overthrows
Do with THEir death bury THEir parents’ strife.
The fearful passage of their death-mark’d love,
And the continuance of their parents’ rage,
Which, but their children’s end, nought could remove,
Is now the two hours’ traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
You can also use sed to remove unwanted lines of text. To delete all the lines that contain
the word “the,” you can use the following command:
[[email protected] ~]# cat prologue | sed /the/d
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Command Input and Output
311
Whole misadventured piteous overthrows
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
[[email protected] ~]# _
Like sed, the awk command searches for patterns of text and performs some action on the
text found. However, the awk command treats each line of text as a record in a database,
and each word in a line as a database field. For example, the line “Hello, how are you?”
has four fields: “Hello,” “how,” “are,” and “you?”. These fields can be referenced in the
awk command using $1, $2, $3, and $4. For example, to display only the first and fourth
words only on lines of the prologue file that contains the word “the,” you can use the following command:
[[email protected] ~]# cat prologue | awk ’/the/ {print $1, $4}’
From fatal
A star-cross’d
Do death
The of
And of
Which, children’s
Is two
[[email protected] ~]# _
By default, the awk command uses space or tab characters as delimiters for each field in a
line. Most configuration files on Linux systems, however, are delimited using colon ( : ) characters. To change the delimiter that awk uses, you can specify the –F option to the command. For example, the following example lists the last 10 lines of the colon-delimited file /
etc/passwd and views only the 6th and 7th fields for lines that contain the word “bob” in
the last 10 lines of the file:
[[email protected] ~]# tail /etc/passwd
news:x:9:13:News server user:/etc/news:/sbin/nologin
smolt:x:490:474:Smolt:/usr/share/smolt:/sbin/nologin
backuppc:x:489:473::/var/lib/BackupPC:/sbin/nologin
pulse:x:488:472:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:468::/var/lib/gdm:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
jetty:x:487:467::/usr/share/jetty:/bin/sh
bozo:x:500:500:bozo the clown:/home/bozo:/bin/bash
bob:x:501:501:Bob Smith:/home/bob:/bin/bash
user1:x:502:502:sample user one:/home/user1:/bin/bash
[[email protected] ~]# tail /etc/passwd | awk –F : ’/bob/ {print $6, $7}’
/home/bob /bin/bash
[[email protected] ~]# _
Both awk and sed allow you to specify regular expressions in the
search pattern.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
312
Chapter 7
Working with the BASH Shell
Shell Variables
A BASH shell has several variables in memory at any one time. Recall that a variable is simply a reserved portion of memory containing information that might be accessed. Most variables in the shell are referred to as environment variables because they are typically set by
the system and contain information that the system and programs access regularly. Users can
also create their own custom variables. These variables are called user-defined variables. In
addition to these two types of variables, special variables are available that are useful when
executing commands and creating new files and directories.
Environment Variables
Many environment variables are set by default in the BASH shell. To see a list of these variables and their current values, you can use the set command, as shown in the following
output:
[[email protected] ~]# set | less
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:
force_
fignore:histap/bin/bashpend:interactive_comments:login_shell:progcomp:
promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_REMATCH=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="45" [3]="1" [4]="release"
[5]="x86_64-redhat-linux-gnu")
BASH_VERSION=‘4.2.45(1)-release’
COLORS=/etc/DIR_COLORS
COLUMNS=80
COMP_WORDBREAKS=$’ \t\n"\’>< =;|&(:’
DIRSTACK=()
EUID=0
GROUPS=()
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=server1
HOSTTYPE=x86_64
ID=0
IFS=$’ \t\n’
INCLUDE=
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Variables
313
KDEDIRS=/usr
LANG=en_US.UTF-8
LESSOPEN=‘||/usr/bin/lesspipe.sh %s’
LINES=59
LOGNAME=root
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=1291
PROMPT_COMMAND=‘printf "\033]0;%[email protected]%s:%s\007" "${USER}" "${HOSTNAME%%.*}"
"${PWD/#$HOME/~}"‘
PS1=‘[\[email protected]\h \W]\$ ‘
PS2=‘> ‘
PS4=‘+ ‘
PWD=/root
QT_GRAPHICSSYSTEM_CHECKED=1
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactivecomments:monitor
SHLVL=1
SSH_CLIENT=‘192.168.1.100 43480 22’
SSH_CONNECTION=‘192.168.1.100 43480 192.168.1.105 22’
SSH_TTY=/dev/pts/0
TERM=xterm
TMP=/tmp/.colorlsn3e
UID=0
USER=root
XDG_RUNTIME_DIR=/run/user/0
XDG_SESSION_ID=2
:
Some environment variables shown in the preceding output are used by programs that require
information about the system; the OSTYPE (Operating System TYPE) and SHELL (Pathname
to shell) variables are examples from the preceding output. Other variables are used to set the
user’s working environment; the most common of these include the following:
•
PS1—The default shell prompt
•
HOME—The absolute pathname to the user’s home directory
•
PWD—The present working directory in the directory tree
•
PATH—A list of directories to search for executable programs
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
314
Chapter 7
Working with the BASH Shell
The PS1 variable represents the BASH shell prompt. To view the contents of this variable
only, you can use the echo command and specify the variable name prefixed by the $ shell
metacharacter, as shown in the following output:
[[email protected] ~]# echo $PS1
[\[email protected]\h \W]\$
[[email protected] ~]# _
Note that a special notation is used to define the prompt in the preceding output: \u indicates the user name, \h indicates the host name, and \W indicates the name of the current
directory. A list of BASH notation can be found by navigating the manual page for the
BASH shell.
To change the value of a variable, you specify the variable name followed immediately by an
equal sign (¼) and the new value. The following output demonstrates how you can change
the value of the PS1 variable. The new prompt takes effect immediately and allows the user
to type commands.
[[email protected] ~]# PS1="This is the new prompt: #"
This is the new prompt: # _
This is the new prompt: # date
Fri Aug 20 08:16:59 EDT 2015
This is the new prompt: # _
This is the new prompt: # who
(unknown) :0
2015-08-20 07:14 (:0)
root
tty2
2015-08-20 07:15
This is the new prompt: # _
This is the new prompt: # PS1="[\[email protected]\h \W]#"
[[email protected] ~]# _
The HOME variable is used by programs that require the pathname to the current user’s
home directory to store or search for files; therefore, it should not be changed. If the root
user logs in to the system, the HOME variable is set to /root; alternatively, the HOME variable is set to /home/user1 if the user named user1 logs in to the system. Recall that the tilde ~
metacharacter represents the current user’s home directory; this metacharacter is a pointer to
the HOME variable, as shown here:
[[email protected]
/root
[[email protected]
/root
[[email protected]
[[email protected]
/etc
[[email protected]
/etc
[[email protected]
~]# echo $HOME
~]# echo ~
~]# HOME=/etc
root]# echo $HOME
root]# echo ~
root]# _
Like the HOME variable, the PWD (Print Working Directory) variable is vital to the user’s
environment and should not be changed. PWD stores the current user’s location in the directory tree. It is affected by the cd command and used by other commands such as pwd when
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Variables
315
the current directory needs to be identified. The following output demonstrates how this variable works:
[[email protected] ~]# pwd
/root
[[email protected] ~]# echo $PWD
/root
[[email protected] ~]# cd /etc
[[email protected] etc]# pwd
/etc
[[email protected] ~]# echo $PWD
/etc
[[email protected] ~]# _
The PATH variable is one of the most important variables in the BASH shell, as it allows
users to execute commands by typing the command name alone. Recall that most commands
are represented by an executable file on the hard drive. These executables are typically stored
in directories named bin or sbin in various locations throughout the Linux directory tree. To
execute the ls command, you could either type the absolute or relative pathname to the file
(that is, /usr/bin/ls or ../usr/bin/ls) or simply type the letters “ls” and allow the system to
search the directories listed in the PATH variable for a command named ls. Sample contents
of the PATH variable are shown in the following output:
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]# _
In this example, if the user had typed the command ls at the command prompt and pressed
Enter, the shell would have noticed the lack of a / character in the pathname and proceeded
to search for the file ls in the /usr/local/sbin directory, then the /usr/local/bin directory, the
/usr/sbin directory, and then the /usr/bin directory before finding the ls executable file. If no
ls file is found in any directory in the PATH variable, the shell returns an error message, as
shown here with a misspelled command and any similar command suggestions:
[[email protected] ~]# lss
bash: lss: command not found…
Similar command is: 0 ls 0
[[email protected] ~]# _
Thus, if a command is located within a directory that is listed in the PATH variable, you can
simply type the name of the command on the command line to execute it. The shell will then
find the appropriate executable file on the filesystem. All of the commands used in this book
so far have been located in directories listed in the PATH variable. However, if the executable file is not in a directory listed in the PATH variable, the user must specify either the
absolute or relative pathname to the executable file. The following example uses the myprogram file in the /root directory (a directory that is not listed in the PATH variable):
[[email protected] ~]# pwd
/root
[[email protected] ~]# ls -F
Desktop/ myprogram*
[[email protected] ~]# myprogram
bash: myprogram: command not found…
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
316
Chapter 7
Working with the BASH Shell
[[email protected] ~]# /root/myprogram
This is a sample program.
[[email protected] ~]# ./myprogram
This is a sample program.
[[email protected] ~]# cp myprogram /usr/bin
[[email protected] ~]# myprogram
This is a sample program.
[[email protected] ~]# _
After the myprogram executable file was copied to the /usr/bin directory in the preceding output,
the user was able to execute it by simply typing its name, because the /usr/bin directory is listed in
the PATH variable. Table 7-3 provides a list of environment variables used in most BASH shells.
Variable
Description
BASH
The full path to the BASH shell
BASH_VERSION
The version of the current BASH shell
DISPLAY
The variable used to redirect the output of X Windows to another computer or device that
allows remote X connections using the xhost command
ENV
The location of the BASH run-time configuration file (usually ~/.bashrc)
EUID
The effective UID (User ID) of the current user
HISTFILE
The filename used to store previously entered commands in the BASH shell (usually ~/.bash_history)
HISTFILESIZE
The number of previously entered commands that can be stored in the HISTFILE upon logout
for use during the next login; it is typically 1000 commands
HISTSIZE
The number of previously entered commands that will be stored in memory during
the current login session; it is typically 1000 commands
HOME
The absolute pathname of the current user’s home directory
HOSTNAME
The host name of the Linux system
LC_ALL
Defines the locale used by the iconv function within the shell for character encoding (e.g.
Unicode UTF-8, ISO-8859), overriding the default locale stored in /usr/bin/locale
LOGNAME
The user name of the current user used when logging in to the shell
MAIL
The location of the mailbox file (where e-mail is stored)
OSTYPE
The current operating system
PATH
The directories to search for executable program files in the absence of an absolute or
relative pathname containing a / character
PS1
The current shell prompt
PWD
The current working directory
RANDOM
The variable that creates a random number when accessed
SHELL
The absolute pathname of the current shell
TERM
The variable used to determine the terminal settings; it is typically set to “linux” or “xterm”
on newer Linux systems and “console” on older Linux systems
TERMCAP
The variable used to determine the terminal settings on Linux systems that use
a TERMCAP database (/etc/termcap)
Table 7-3 Common BASH environment variables
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Variables
317
User-Defined Variables
You can set your own variables using the same method discussed earlier to change the contents of existing environment variables. To do so, you simply specify the name of the variable
(known as the variable identifier) immediately followed by the equal sign (¼) and the new
contents. When creating new variables, it is important to note the following features of variable identifiers:
•
They can contain alphanumeric characters (0–9, A–Z, a–z), the dash () character, or
the underscore (_) character.
•
They must not start with a number.
•
They are typically capitalized to follow convention (e.g., HOME, PATH).
To create a variable called MYVAR with the contents “This is a sample variable” and
display its contents, you can use the following commands:
[[email protected] ~]# MYVAR="This is a sample variable"
[[email protected] ~]# echo $MYVAR
This is a sample variable
[[email protected] ~]# _
The preceding command created a variable that is available to the current shell. Most commands that are run by the shell are run in a separate subshell, which is created by the current
shell. Any variables created in the current shell are not available to those subshells and the
commands running within them. Thus, if a user creates a variable to be used within a certain
program such as a database editor, that variable should be exported to all subshells using the
export command to ensure that all programs started by the current shell have the ability to
access the variable.
As explained earlier in this chapter, all environment variables in the BASH shell can be listed
using the set command; user-defined variables are also indicated in this list. Similarly, to see
a list of all exported environment and user-defined variables in the shell, you can use the
env command. Because the outputs of set and env are typically large, you would commonly redirect the Standard Output of these commands to the grep command to display
certain lines only.
To see the difference between the set and env commands as well as export the MYVAR
variable created earlier, you can perform the following commands:
[[email protected] ~]# set | grep MYVAR
MYVAR=’This is a sample variable.’
[[email protected] ~]# env | grep MYVAR
[[email protected] ~]# _
[[email protected] ~]# export MYVAR
[[email protected] ~]# env | grep MYVAR
MYVAR=This is a sample variable.
[[email protected] ~]# _
Not all environment variables are exported; the PS1 variable is an example of a variable that
does not need to be available to subshells and is not exported as a result. However, it is good
form to export user-defined variables because they will likely be used by processes that run in
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
318
Chapter 7
Working with the BASH Shell
subshells. This means, to create and export a user-defined variable called MYVAR2, you can
use the export command alone, as shown in the following output:
[[email protected] ~]# export MYVAR2="This is another sample variable"
[[email protected] ~]# set | grep MYVAR2
MYVAR2=’This is another sample variable.’
_=MYVAR2
[[email protected] ~]# env | grep MYVAR2
MYVAR2=This is another sample variable.
[[email protected] ~]# _
You can also remove a variable from memory using the unset
variablename command.
Other Variables
Other variables are not displayed by the set or env commands; these variables perform specialized functions in the shell.
The UMASK variable used earlier in this textbook is an example of a special variable that performs a special function in the BASH shell and must be set by the umask command. Also recall
that when you type the cp command, you are actually running an alias to the cp –i command.
Aliases are shortcuts to commands stored in special variables that can be created and viewed
using the alias command. To create an alias to the command mount –t ext2 /dev/fd0
/mnt/floppy called mf and view it, you can use the following commands:
[[email protected] ~]# alias mf="mount -t ext2 /dev/fd0 /mnt/floppy"
[[email protected] ~]# alias
alias cp=‘cp -i’
alias egrep=‘egrep --color=auto’
alias fgrep=‘fgrep --color=auto’
alias grep=‘grep - -color=auto’
alias l.=‘ls -d.* - -color=auto’
alias ll=‘ls -l --color=auto’
alias ls=‘ls - -color=auto’
alias mf=‘mount -t ext2 /dev/fd0 /mnt/floppy’
alias mv=‘mv -i’
alias rm=‘rm -i’
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot
--show-tilde’
[[email protected] ~]# _
Now, you simply need to run the mf command to mount a floppy device that contains an
ext2 filesystem to the /mnt/floppy directory, as shown in the following output:
[[email protected] ~]# mf
[[email protected] ~]# mount | grep fd0
/dev/fd0 on /mnt/floppy type ext2 (rw)
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Variables
319
You can also create aliases to multiple commands, provided they are separated by the ; metacharacter introduced in Chapter 2. To create and test an alias called dw that runs the date
command followed by the who command, you can do the following:
[[email protected] ~]# alias dw="date;who"
[[email protected] ~]# alias
alias cp=‘cp -i’
alias dw=‘date;who’
alias egrep=‘egrep - -color=auto’
alias fgrep=‘fgrep - -color=auto’
alias grep=‘grep --color=auto’
alias l.=‘ls -d.* --color=auto’
alias ll=‘ls -l --color=auto’
alias ls=‘ls --color=auto’
alias mf=‘mount -t ext2 /dev/fd0 /mnt/floppy’
alias mv=‘mv -i’
alias rm=‘rm -i’
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot
--show-tilde’
[[email protected] ~]# dw
Thu Oct 2 08:07:22 EDT 2015
(unknown) :0
2015-10-02 07:14 (:0)
root
tty2
2015-10-02 07:15
[[email protected] ~]# _
It is important to use unique alias names because the shell searches
for them before it searches for executable files. If you create an alias
called who, that alias would be used instead of the who command
on the filesystem.
Environment Files
Recall that variables are stored in memory. When a user exits the BASH shell, all variables
stored in memory are destroyed along with the shell itself. To ensure that variables are accessible to a shell at all times, you must place variables in a file that is executed each time a user
logs in and starts a BASH shell. These files are called environment files. Common BASH shell
environment files and the order in which they are typically executed are as follows:
/etc/profile
/etc/bashrc
~/.bashrc
~/.bash_profile
~/.bash_login
~/.profile
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
320
Chapter 7
Working with the BASH Shell
The BASH runtime configuration files (/etc/bashrc and ~/.bashrc) are typically used to set
aliases and variables that must be present in the BASH shell. They are executed immediately
after a new login as well as when a new BASH shell is created after login. The /etc/bashrc file
contains aliases and variables for all users on the system, whereas the ~/.bashrc file contains
aliases and variables for a specific user.
The other environment files are only executed after a new login. The /etc/profile file is executed after login for all users on the system and sets most environment variables, such as
HOME and PATH. After /etc/profile finishes executing, the home directory of the user is
searched for the hidden environment files. bash_profile, .bash_login, and .profile. If these
files exist, the first one found is executed; as a result, only one of these files is typically used.
These hidden environment files allow a user to set customized variables independent of
BASH shells used by other users on the system; any values assigned to variables in these files
override those set in /etc/profile, /etc/bashrc, and ~/.bashrc due to the order of execution.
To add a variable to any of these files, you simply add a line that has the same format as the
command used on the command line. To add the MYVAR2 variable used previously to
the .bash_profile file, simply edit the file using a text editor such as vi and add the line
export MYVAR2="This is another sample variable" to the file.
Variables are not the only type of information that can be entered into an environment file; any
command that can be executed on the command line can also be placed inside any environment
file. If you want to set the UMASK to 077, display the date after each login, and create an alias,
you can add the following lines to one of the hidden environment files in your home directory:
umask 077
date
alias dw="date;who"
Also, you might want to execute cleanup tasks upon exiting the shell; to do this, simply add
those cleanup commands to the .bash_logout file in your home directory.
Shell Scripts
In the previous section, you learned that the BASH shell can execute commands that exist
within environment files. The BASH shell also has the ability to execute other text files containing commands and special constructs. These files are referred to as shell scripts and are
typically used to create custom programs that perform administrative tasks on Linux systems.
Any command that can be entered on the command line in Linux can be entered into a shell
script because it is a BASH shell that interprets the contents of the shell script itself. The most
basic shell script is one that contains a list of commands, one per line, for the shell to execute
in order, as shown here in the text file called myscript:
[[email protected] ~]# cat myscript
#!/bin/bash
#this is a comment
date
who
ls –F /
[[email protected] ~]# _
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Scripts
321
The first line in the preceding shell script ( #!/bin/bash) is called a hashpling; it specifies
the pathname to the shell that interprets the contents of the shell script. Different shells can
use different constructs in their shell scripts. Thus, it is important to identify which shell was
used to create a particular shell script. The hashpling allows a user who uses the C shell to
use a BASH shell when executing the myscript shell script shown previously. The second line
of the shell script is referred to as a comment because it begins with a # character and is
ignored by the shell; the only exception to this is the hashpling on the first line of a shell
script. The remainder of the shell script shown in the preceding output consists of three commands that will be executed by the shell in order: date, who, and ls.
If you have read permission to a shell script, you can execute the shell script by starting
another BASH shell and specifying the shell script as an argument. To execute the myscript
shell script shown earlier, you can use the following command:
[[email protected] ~]# bash myscript
Fri Aug 20 11:36:18 EDT 2015
user1
tty1
2015-08-20 07:47 (:0)
root
tty2
2015-08-20 11:36
bin/
dev/
home/
media/ proc/
sbin/
sys/
boot/
etc/
lib/
mnt/
public/ selinux/ tmp/
data/
extras/
lost+found/ opt/
root/
srv/
usr/
[[email protected] ~]# _
var/
Alternatively, if you have read and execute permission to a shell script, you can execute the
shell script like any other executable program on the system, as shown here using the myscript
shell script:
[[email protected] ~]# chmod a+x myscript
[[email protected] ~]# ./myscript
Fri Aug 20 11:36:58 EDT 2015
user1
tty1
2015-08-20 07:47 (:0)
root
tty2
2015-08-20 11:36
bin/
dev/
home/
media/ proc/
sbin/
sys/
boot/
etc/
lib/
mnt/
public/ selinux/ tmp/
data/
extras/
lost+found/ opt/
root/
srv/
usr/
[[email protected] ~]# _
var/
The preceding output is difficult to read because the output from each command is not separated by blank lines or identified by a label. Utilizing the echo command results in a more
user-friendly myscript, as shown here:
[[email protected] ~]# cat myscript
#!/bin/bash
echo "Today’s date is:"
date
echo ""
echo "The people logged into the system include:"
who
echo ""
echo "The contents of the / directory are:"
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
322
Chapter 7
Working with the BASH Shell
ls –F /
[[email protected] ~]#./ myscript
Today’s date is:
Fri Aug 20 11:37:24 EDT 2015
The people logged into the system include:
user1 tty1
2015-08-20 07:47 (:0)
root
tty2
2015-08-20 11:36
The contents of the / directory are:
bin/
dev/
home/
media/ proc/
sbin/
sys/
boot/ etc/
lib/
mnt/
public/ selinux/ tmp/
data/ extras/ lost+found/ opt/
root/
srv/
usr/
[[email protected]r1 ~]# _
var/
Escape Sequences
In the previous example, you used the echo command to manipulate data that appeared on
the screen. The echo command also supports several special notations called escape
sequences. You can use escape sequences to further manipulate the way text is displayed to
the terminal screen, provided the –e option is specified to the echo command. Table 7-4
provides a list of these echo escape sequences.
The escape sequences listed in Table 7-4 can be used to further manipulate the output of the
myscript shell script used earlier, as shown in the following example:
[[email protected] ~]# cat myscript
#!/bin/bash
Escape Sequence
Description
\???
Inserts an ASCII character represented by a three-digit octal number (???)
\\
Backslash
\a
ASCII beep
\b
Backspace
\c
Prevents a new line following the command
\f
Form feed
\n
Starts a new line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
Table 7-4 Common echo escape sequences
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Scripts
323
echo -e "Today’s date is: \c"
date
echo –e "\nThe people logged into the system include:"
who
echo –e "\nThe contents of the / directory are:"
ls –F /
[[email protected] ~]#./ myscript
Today’s date is: Fri Aug 20 11:44:24 EDT 2015
The people logged into the system include:
user1
tty1
2015-08-20 07:47 (:0)
root
tty2
2015-08-20 11:36
The contents of the
bin/
dev/
boot/
etc/
data/
extras/
[[email protected] ~]# _
/ directory are:
home/
media/ proc/
sbin/
sys/
lib/
mnt/
public/ selinux/ tmp/
lost+found/ opt/
root/
srv/
usr/
var/
Notice from preceding output that the \c escape sequence prevented the newline character at
the end of the output “Today’s date is:” when myscript was executed. Similarly, newline
characters (\n) were inserted prior to displaying “The people logged into the system
include:” and “The contents of the / directory are:” to create blank lines between command
outputs. This eliminated the need for using the echo "" command shown earlier.
Reading Standard Input
At times, a shell script might need input from the user executing the program; this input can
then be stored in a variable for later use. The read command takes user input from Standard Input and places it in a variable specified by an argument to the read command. After
the input has been read into a variable, the contents of that variable can then be used, as
shown in the following shell script:
[[email protected] ~]# cat newscript
#!/bin/bash
echo –e "What is your name? --> \c"
read USERNAME
echo "Hello $USERNAME"
[[email protected] ~]# chmod a+x newscript
[[email protected] ~]# ./newscript
What is your name? - ->Fred
Hello Fred
[[email protected] ~]# _
Note from the preceding output that the echo command used to pose a question to the user
ends with --> to simulate an arrow prompt on the screen and the \c escape sequence to
place the cursor after the arrow prompt; this is common among Linux administrators when
writing shell scripts.
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
7
324
Chapter 7
Working with the BASH Shell
Decision Constructs
Decision constructs are the most common type of construct used in shell scripts. They alter
the flow of a program based on whether a command in the program completed successfully
or based on a decision that the user makes in response to a question posed by the program.
Figures 7-4 and 7-5 illustrate some decision constructs.
The if Construct The most common type of decision construct, the if construct, has
the following syntax:
if this is true
then
do these commands
elif this is true
then
do these commands
else
do these commands
fi
Program
starts here
Program
starts here
Did the
command
complete
successfully?
Question
Yes
No
List of
commands
Question
Yes
List of
commands
No
List of
commands
Figure 7-4 A two-question decision construct
Yes
List of
commands
No
List of
commands
Figure 7-5 A command-based decision construct
Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Shell Scripts
325
Some common rules govern if constructs:
1. elif (else if) and else statements are optional.
2. You can have an unlimited number of elif statements.
3. The do these commands section can consist of multiple com