The Accidental Administrator Linux Server Step by Configuration Guide
The Accidental Administrator Linux Server Step by Configuration Guide
Administrator
®
The Accidental Administrator :
Linux Server
Step-by-Step
Configuration Guide
Edition 2.0
®
Seattle, Washington
www.soundtraining.net
Reasonable attempts have been made to ensure the accuracy of
the
information contained in this publication as of the date on
which it was
written. This publication is distributed in the hope that it
will be helpful,
but with no guarantees. There are no guarantees made as to
the accuracy,
reliability, or applicability of this information for any
task or purpose
whatsoever.
The author recommends that these procedures be used only as a
guide to
configuration of computers and/or devices in a test
environment prior to
usage in a production environment. Under no circumstances
should these
procedures be used in a live, production environment without
first being
tested in a laboratory environment to determine their
suitability, their
accuracy, and any security implications.
ISBN: 978-1453689929
Copyright 2014, Don R. Crawley.
All rights reserved.
This is a copyrighted work in which all rights are retained
by the author.
You may not copy this work in any form, nor change this work,
nor store
this document in a retrieval system, nor distribute or
otherwise transmit this
work in any form by any means, electronic, mechanical,
photocopying,
recording, or otherwise, without the written prior permission
of the
copyright holder. The preceding restrictions apply to this
document in
whole or in part.
Trademarks, Registered Trademarks, and Service Marks: This
book
identifies and uses product names and services known to be
trademarks,
registered trademarks, or service marks of their respective
holders. Such
marks are used throughout this book in an editorial fashion
only.
Additionally, terms suspected of being trademarks, registered
trademarks,
or service marks have been appropriately capitalized,
although
soundtraining.net cannot attest to the accuracy of such
information. Use of a
term in this book should not be regarded as affecting the
validity of any
trademark, registered trademark, or service mark. Neither the
author nor
soundtraining.net are associated with any vendor or product
mentioned in
this book.
Please do not make illegal copies of this book, either in its
entirety or
any portion thereof.
PO Box 48094
Seattle, Washington 98148-0094
United States of America
On the web: www.soundtraining.net
On the phone: (206) 988-5858
Email: [email protected]
To Janet
“Technology, like art, is a soaring exercise of the human
imagination.”
—Daniel Bell
The Winding Passage
Contents
PRELUDE
The Base Config for the Systems in the Book ……… 2
The Revisions in this Edition ……… 3
CHAPTER 1:
Introduction to Linux
Chapter Introduction ……… 7
Chapter Objectives ……… 8
Red Hat and CentOS ……… 9
Installing CentOS Linux Server ……… 9
Minimum Hardware Requirements ……… 9
Which Version of the Operating System Should You Download?
……… 10
Performing the Installation ……… 10
Adding VMWare Tools ……… 20
CHAPTER 2:
Understanding Linux Commands
Introduction ……… 23
Chapter Objectives ……… 23
Some Basic Rules About Linux Commands ……… 24
The Shell ……… 24
Some Commonly Used Linux Commands ……… 26
CHAPTER 3:
Linux User Accounts
Introduction ……… 37
Objectives ……… 37
Understanding /etc/passwd ……… 38
Creating a New User ……… 39
Passwords ……… 39
Default Values ……… 40
Adding Groups ……… 41
Deleting Users ……… 41
Changing Ownership for a File or Directory ……… 41
Adding a User to a Group ……… 42
Viewing Information About the Current User ……… 43
Additional User Management Commands ……… 46
CHAPTER 4:
File and Directory Management
Introduction ……… 47
Objectives ……… 47
Working with File Systems and Mount Points ……… 48
Linux File Types ……… 49
Mounting a Device ……… 51
Understanding /etc/fstab ……… 52
Understanding Mount Points ……… 53
Octal (Numeric) Permissions ……… 56
Setting Default Permissions ……… 59
Disk Configuration Tools ……… 59
CHAPTER 5:
Linux Administration
Introduction ……… 61
Objectives ……… 62
GUI vs. CLI ……… 63
Linux Directories ……… 64
Linux Profiles ……… 65
Administration Tools and Techniques ……… 66
Editing Configuration Files ……… 74
Other Commonly Used Text Editors ……… 75
vim Cheat Sheet ……… 77
Using grep ……… 78
Using the alias Command ……… 80
Making Aliases Persistent ……… 81
Starting and Stopping Services (The Daemons) ……… 83
Linux Compression and Archiving Tools ……… 84
Understanding the Linux Boot Process ……… 86
Run Levels ……… 88
Controlling the Boot Process ……… 89
System Shutdowns and Reboots ……… 92
How to Shut Down the System ……… 92
X Windows ……… 93
Getting Help ……… 93
CHAPTER 6:
Red Hat/CentOS Linux Package Management
Introduction ……… 99
Objectives ……… 99
Using yum to Update Your System ……… 100
Additional Repositories ……… 107
RPM: The RedHat Package Manager ……… 110
CHAPTER 7:
Networking with Red Hat/CentOS Linux
Introduction ……… 115
Objectives ……… 116
Network Administration ……… 117
Installing Networking Tools ……… 118
RHEL/Fedora/CentOS Network Configuration ……… 119
Using ifconfig ……… 123
/etc/resolv.conf ……… 123
DHCP (Dynamic Host Configuration Protocol) ……… 126
CHAPTER 8:
DNS: The Domain Name System
Introduction ……… 131
Objectives ……… 131
Installing BIND DNS ……… 132
Understanding the Fundamentals of DNS ……… 133
Primary, Secondary, and Caching Zones ……… 134
Building Name Servers ……… 134
A Primary DNS Server ……… 136
Creating the Primary Master Zone Database File ……… 137
DNS Resource Records ……… 138
Creating the Secondary Master ……… 142
DNS Tools ……… 143
DNS Resources ……… 145
CHAPTER 9:
Using SSH (Secure Shell)
Introduction ……… 147
Objectives ……… 147
What is SSH? ……… 148
When is SSH Used? ……… 148
How Do I Configure SSH? ……… 148
Transferring Files with scp ……… 149
Transferring Files with SFTP ……… 150
CHAPTER 10:
Linux Security
Introduction ……… 151
Objectives ……… 152
Physical Security ……… 153
Keep the Software Up to Date ……… 153
Employ the Principle of Least Privilege ……… 153
Use Encryption ……… 154
Avoid Non-Secure Protocols ……… 154
Clean Up Your Systems ……… 154
Minimize the Number of Services per System ……… 154
Enforce a Good Password Policy ……… 155
Disable Root Login ……… 155
Disable Unneeded Services ……… 155
Delete X Windows ……… 155
Implement a Firewall ……… 155
Implementing NAT (Network Address Translation) ……… 160
Separate Partitions ……… 161
Block SSH Attacks ……… 161
Perform Security Scans and Audits ……… 162
Using sudo ……… 162
Bypassing sudo ……… 165
Using lastlog ……… 165
Using last ……… 166
Port Scanning ……… 167
Password Recovery (Resetting) ……… 168
Additional Security Tools ……… 170
Develop and Maintain a Good Backup Strategy ……… 171
Summary ……… 172
CHAPTER 11:
Automating Administration Tasks with cron
Introduction ……… 173
Objectives ……… 173
Using cron ……… 174
CHAPTER 12:
Monitoring Your Red Hat/CentOS Linux Server
Introduction ……… 177
Objectives ……… 177
Log Files ……… 178
Viewing Log Files ……… 179
Other Linux Monitoring Tools ……… 180
The sysstat Package of Utilities ……… 185
Network Monitoring Tools ……… 187
CHAPTER 13:
How to Build and Configure a Basic File Server for Windows
and Other
Clients
Introduction ……… 189
Objectives ……… 190
Using NFS to Share Files ……… 194
Using rsync to Synchronize Files Between Servers ……… 197
CHAPTER 14:
How to Build and Configure a Basic Web Server
Introduction ……… 201
Objectives ……… 202
Apache Web Server ……… 203
Understanding Apache ……… 204
Creating Content for the Web Site ……… 208
Installing and Configuring an FTP Server ……… 212
CHAPTER 15:
How to Build and Configure a Basic Database Server
and Add a Scripting Language (PHP)
Introduction ……… 215
Objectives ……… 215
Adding a Database Server ……… 216
Adding a Scripting Language ……… 218
PHPMyAdmin ……… 219
CHAPTER 16:
How to Build and Configure a Basic Email Server
Introduction ……… 221
Objectives ……… 221
Some Email Terminology ……… 222
CHAPTER 17:
Remote Administration with Webmin
Introduction ……… 225
Objectives ……… 225
Installing Webmin ……… 226
POSTLUDE
APPENDICES
Appendix A: How to Create a New Virtual Machine in VMWare ………
232
Appendix B: Don’s Online Resources ……… 240
Appendix C: Other Helpful Websites ……… 241
INDEX
Prelude
Writing any book is a huge undertaking. One of the biggest
challenges in
writing a technical book such as this lies in deciding what
to include. Even
more difficult is the challenge of deciding what to exclude.
As you read
through this book, you could well find yourself thinking I
should have
included a particular technology or that something I did
include is
extraneous. If you do feel that way, please let me know. Post
something on
one of my social media channels or send me an email. I love
getting
feedback.
I write books and create training workshops based on how I
like to learn.
My preference is to learn how to build a simple, working
configuration and
then use other resources to learn how to finesse the
configuration. In other
words, show me how to build a simple Apache web server and
later I can
learn how to add virtual hosts, SSL, or other more advanced
configs. That’s
exactly what this book attempts to do. I try to focus on
building
configurations and include only enough theory as required to
make sense of
the config. I hope this approach works for you. If you want
more theory and
more advanced configs, there are plenty of 1000 – 1200 page
books
available that do an excellent job of providing that. Oh, and
there’s always
Google.
What about support? As an Accidental Administrator , you
might feel a bit
overwhelmed by all the new terminology and strange names in
the IT
world. I remember well my first few months in IT. I felt like
I was on a
different planet. That was in the days before a ubiquitous
Internet, so
support options were limited to books and BBSs. Today, there
are many
great forums that provide outstanding support for all flavors
of Linux,
including Red Hat and CentOS. I do not provide one-on-one
support. I
simply don’t have enough time to do that and still write
books, produce
videos, play music, and hang out with my family, so please
don’t ask. If you
do, I’ll politely refer you to resources such as
linuxquestions.org,
wiki.centos.org/HowTos, or any of the many other excellent
Linux support
®
forums on the Web. There is a fairly lengthy list of Linux
support websites
in the appendix at the end of this book. If you feel like you
really need oneon-one support, consider purchasing a copy of
Red Hat Enterprise Linux
which comes with varying levels of support, depending on the
package you
purchase. (www.redhat.com/apps/store/server/)
The Base Config for the Systems in the Book
I built the configs in this book using CentOS Linux 6.5
running in virtual
machines in VMWare Workstation 10.0.1.
Download the installation ISO image from
http://wiki.centos.org/Download. The instructions and
exercises in this
book are based on CentOS Linux version 6.5. Any version whose
number
starts with a 6 should be compatible with this book.
I created two VMs: LinuxServer01 and LinuxServer02. In
general,
LinuxServer01 has an IP address of 192.168.0.1/24 and
LinuxServer02 has
an IP address of 192.168.0.2. When required, I use the domain
soundtraining.local, since my company’s name is
soundtraining.net. Feel
free to replace that with whatever you choose. Frankly, as
you work through
this book, it will probably be simpler for you to just use
the same names as
I have.
LinuxServer01
Chapter Introduction
Technically speaking, Linux is not an operating system, but
the kernel of an
operating system. The Linux kernel was developed by Linus
Torvalds while
he was a student at the University of Helsinki in Finland.
Linux is inspired
by UNIX and bears much similarity to it in terms of commands
and
directory structure.
Various organizations package the Linux kernel and offer it
to the public as
a distribution, or distro for short. Some of the more common
distros include
Red Hat and its variants Fedora and CentOS, SuSE, Gentoo,
Ubuntu, Mint,
Debian, and Slackware, just to name a few. You can learn
about the many
Linux distros at www.distrowatch.com.
There are many excellent sources of background information on
Linux,
including the Linux Foundation at www.linuxfoundation.org.
Performing a
Web search on the keyword “Linux” will return millions of
results. Since
the purpose of this book is to help you configure a Linux
server, I’ll let
others supply the background information, but I encourage you
to get
familiar with the fascinating and important stories of Linux,
the GNU
project, and the people who were and are involved in open
source software.
Chapter Objectives
Complete a CentOS Linux minimal installation
Login to a newly installed server
Enable the network interface
Add VMWare tools
Red Hat and CentOS
Red Hat Enterprise Linux (RHEL) is a popular Linux
distribution, available
only through a paid subscription model. RHEL, however, is
comprised
largely of software packages distributed under the free
software licenses.
The source code for the packages is made available by Red
Hat.
CentOS (Community Enterprise Operating System) developers use
the
source code from Red Hat to create CentOS, a product very
similar to
RHEL. Red Hat’s proprietary branding and logos have been
removed, but
otherwise the CentOS product will behave much the same as
RHEL.
In January of 2014, Red Hat and the CentOS Project joined
forces, which
should further enhance the compatibility of the two operating
systems.
This book is based on CentOS. The things you learn in this
book and in
other documentation should apply equally to either RHEL or
CentOS. Of
course, it’s possible that there may be differences, but I’m
not aware of any
substantial differences in configuration between the two.
Installing CentOS Linux Server
The examples in this book are written based on installing
CentOS Linux
Server 6.5 in a virtualized environment. I used VMWare
Workstation 10
(www.vmware.com). The procedures I’m going to show you should
work
in other virtualization environments or in a physical
environment. I say
“should” because there’s no way for me to anticipate every
possible
environment or configuration.
If you prefer, you can certainly use other virtualization
environments such
as VMWare Player, VirtualBox (www.virtualbox.org) or Hyper-V,
which is
included with Windows 8 and 8.1
(http://windows.microsoft.com/enus/windows-8/hyper-v-run-
virtual-machines).
Minimum Hardware Requirements
It’s nearly impossible to give minimum hardware requirements
for Linux
installations, because Linux operating systems can be
installed on a
tremendous variety of systems. The minimum requirements
depend on the
intended use of the system. As with most things related to
computers, more
is usually better. Having said that, and knowing that you
might be thinking,
“Oh come on, Don. Just give me some minimums!”, here are some
very
general guidelines:
RAM: 256 MB
Hard drive: 1 GB
CentOS version 6.x, like many other current versions of
Linux, requires a
CPU that supports PAE (Physical Address Extension), a feature
that allows
x86 processors to access a physical address space larger than
four
gigabytes. If you’re trying to install CentOS on an older
system, you may
have to use CentOS 5 instead. Many of the commands and
examples will
still work perfectly well with version 5. If your CPU doesn’t
support PAE,
the installation process will throw off an error.
I configured my VM for the examples in the book with 1 GB of
RAM and a
20 GB hard drive, which should be sufficient for most
learning exercises
you’ll perform, either from this book or on your own. Again,
it depends on
what you ultimately want to do with your system.
A production system, of course, will usually require much
more in terms of
memory and hard disk resources.
Which Version of the Operating System Should You Download?
Visit http://www.centos.org/download/. You can click on the
big button to
download the latest X86 64-bit DVD version, but there are
lots of other
options. There are a variety of ISOs available to download.
Click on the
alternative downloads link and you’ll see what I mean. Once
you click
through to a mirror, you’ll see LiveCDs, LiveDVDs, full
DVD .iso
downloads (it takes two), minimal .iso downloads, and
netinstall .iso
downloads. The live versions are fun because they allow you
to boot nearly
any computer from a CD, DVD, or USB thumb drive and play
around with
Linux without actually installing it on your computer’s hard
drive. I usually
download the minimal version, simply because it’s smaller and
faster to
download and I always install software packages and updates
from the
Internet. If you have limited Internet connectivity, you
might want to go
somewhere with a good Internet connection and download the
two fullversion DVDs. The exercises in this book are based on
using the minimal
version.
Performing the Installation
I’m going to assume you’ve already downloaded the CentOS 6.5
ISO from
http://wiki.centos.org/Download, that you’ve chosen the
appropriate version
(32-bit or 64-bit) for your system architecture, and that
you’ve configured
your environment, virtual or physical, for the installation.
After all, this is a
server installation for Pete’s sake. Frankly, if you don’t
understand how to
do those sorts of things, this book will probably be too
advanced for you.
Soundthinking Point:
Which Processor Are You Using?
In the examples, I use a machine with a 64-bit processor. For
that reason, you’ll
often see “x86_64” in many of the filenames. If you’re using
a system with a 32bit processor, you can simply replace
x86_64 with i386.
If you’re not sure, use the 32-bit version of the operating
system. After you finish
the installation, use the command
to identify your processor.
uname -p
1. Now, you get to choose the root password. The user root is
the
administrator on Linux systems. root is all knowing, all
seeing, and all
powerful, kind of like the great and powerful Oz in The
Wizard of Oz. In
the real world, make this a very difficult to guess password.
For our
purposes in this book, we’ll always use p@ss5678 for the root
password.
Enter p@ss5678 and confirm it, press Enter to continue.
Introduction
Working in Linux, especially on a Linux server, means working
in the
command-line interface or the CLI. If you’re a Windows or a
Mac kind of
person, this may seem unfamiliar, old-school, and daunting.
Please don’t
worry. It’s just another way of managing a system and, once
you get
familiar with the basic commands and some shortcuts, you’ll
probably find
it pretty easy. You might even decide you like it better than
clicking through
a series of menus, checkboxes, and radio buttons. Seriously.
That’s how I
feel.
Chapter Objectives
Learn basic rules for Linux commands
Get comfortable working in the Linux shell (command line
environment)
Learn basic Linux commands
Perform a system upgrade
Some Basic Rules About Linux Commands
In the same way that advanced Windows users will often create
simple
batch scripts or Powershell scripts to automate certain
processes in
Windows, Linux users can do similar things with shell
scripts. Shell
scripting is a very powerful tool, even when used with simple
shell scripts,
and I encourage you to explore shell scripting. Entire books
have been
written on shell scripting, so I’m not going to attempt to
teach it as part of
this book. I have included, however, some online resources
that will help
you learn shell scripting.
Shell Scripting Resources
http://www.ibm.com/developerworks/library/l-bash/
http://www.math.utk.edu/~vasili/shell-scripts/
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.howtogeek.com/67469/the-beginners-guide-to-
shellscripting-the-basics/
Some Commonly Used Linux Commands
The following are some of the more commonly used commands in
the
wonderful world of Linux. Some of them won’t work until
they’re installed,
which we’ll do later, so take a few minutes and peruse this
list. Try some of
the commands, but know that some of them won’t work until
later. The real
value of this list will come later, after you’ve gotten more
familiar with
Linux and you’re trying to remember a particular command.
Working with Directories and Files
cat
cd
cd ..
cd
cp
cp
head
head -15
ls
ls -a
ls -l
mkdir
more
mv
pwd
rm
rm -r
rmdir
tail
tail -15
Starting from the root directory, search for the file with
the specified name
find / -name
grep
//
locate
updatedb
whereis
Search $PATH (your default path), man pages and source files
for the specified application
which
Note: You can display your user profile’s default path with
the command
echo $PATH
Working with Archived and Compressed Files
Archive
Compress
bzip2 -c > .bz2 Compress
gzip -c filename > filename.gz
Decompress
bunzip2
gunzip
Uncompress specified file
specified file
tar -xzf
tar -xzf
yum remove
yum search
yum update
yum update
yum info
yum grouplist
yum groupinstall
yum groupremove
yum repolist
The above yum commands are covered in more detail with screen
captures
and step-by-step guides in chapter six.
Starting and Stopping the System
halt
reboot
system now
minutes
Mounting Filesystems
mount -t iso9660 /dev/cdrom
/mnt/cdrom
Mounts the first partition (1) of the first hard disk drive
(a) which is in fat32 vfat format to the
/mnt/c_drive directory
umount /mnt/hda1
Unmounts /mnt/hda1
mount /dev/cdrom /media/cdrom
User Administration
adduser
exit
groupadd Create
groups
passwd
su
su -
useradd
usermod
Change properties of the specified user account
users
who
whoami
Process
&
ctrl+c
Interrupt a program
ctrl+z
Suspend a program
kill
ps
top
Networking
hostname
ifconfig
ip address
ifup
Brings an interface up
ifdown Brings
an interface down
Disable networking
Enable networking
System Information
cp //. Copy filename
df -T -h
fdisk -l
uname -a
2. It will take several minutes the first time you run yum
finished, as before, it will return a shell prompt.
update.
When it’s
Figure 27: A completed system packages upgrade
Introduction
Even if you think your server needs only one or two user
accounts, there are
many others which are added as you add services (daemons). As
with most
things in Linux, at first the user account files may seem
intimidating, but as
you work with them they’ll make more sense.
In this chapter, I’ll go over the user account files, user
profiles, and group
accounts. For the sample usernames, I’ve used some of my
favorite
composers and names of several former colleagues. See if you
can spot ‘em!
Objectives
Learn how Linux user accounts are organized
Configure default values for user profiles
Add and modify user accounts
Add and modify group accounts
Understanding /etc/passwd
The user list is in /etc/passwd. /etc/passwd is a simple text
file
containing entries such as
this: don:x:1000:1000:Don R. Crawley,,,:/home/don:/bin/bash
In the above example, there are seven fields, each separated
by colons.
Field Number Example Value Description
1
don
Username
2
X
1000
1000
/home/don
/bin/bash
Soundthinking Point:
What If There’s No Password?
You may be wondering what happens if you create a user, but
don’t create a
password for that user. The answer is simple: He or she
cannot log on without a
password.
passwd dlawrence
passwd –l
passwd –u
Shadow Passwords
Default Values
Default values for useradd are found in /etc/default/useradd.
Traditionally, such values were stored in /etc/login.defs
which is still
maintained, even if it’s not used. By modifying the values
found in
/etc/default/useradd,
you can set default values for all new users created
with “useradd”. Values found in /etc/default/useradd include
minimum
and maximum password age, the location of user mailboxes,
starting and
ending UIDs and GIDs, and whether or not to create home
directories for
new users.
You’ll also find hidden files that control user profile
behavior in /etc/skel.
Figure 30: Displaying the hidden files that control user
profile behavior
Adding Groups
Using the groupadd command in the CLI: groupadd
group “sales” to your system
sales
S.
Bach”, making him a member of the primary group “musicians”,
plus
additional group membership in “baroque” and “organists”
useradd wloman -g sales -e 2021-06-18 creates a new user
named
wloman, in the sales group, and sets the account to expire on
June 18,
2021.
will create a new user
named dmilhaud, put him in the composers group, and create
the
password p@ss1234 for his account.
useradd dmilhaud -g composers -p p@ss1234
Deleting Users
Using the “userdel” command in the CLI:
deletes the user account. The “-r” deletes the user’s
home directory and its contents.
userdel –r jbach
group
usermod -g
group.
Additionally, the usermod command can be used after a user
account is
created to add comments to the user account, change the
user’s home
directory, add an expiration date to the account, and modify
various other
account parameters. Group accounts are stored in the
/etc/group file,
which, like /etc/passwd, is readable by everyone.
To view a user’s group membership, type this command: groups
enter it)
BAD PASSWORD: it is based on a dictionary word
Retype new password: password
enter it.)
Retype new password: p@ss1234
2. You can view the new groups you created with the following
command:
less /etc/group
Introduction
CentOS/Red Hat 6 uses the ext4 file system by default. Older
Linux distros
use the ext3 file system by default. ext4 is a journaling
file system which
offers greater stability and reliability than predecessor
file systems. Among
the benefits of ext4 are larger volume sizes, larger file
sizes, and slightly
longer filenames than its predecessor.
Objectives
Learn about Linux filesystems and file types
Learn about links, both hard links and symbolic links
Understand how to mount a device
Gain familiarity with /etc/fstab
Manage file and directory permissions
Working with File Systems and Mount Points
There are several methods you can use to identify the file
systems in use on
your computer. An easy method is to use the mount command:
mount | grep ^/dev
In the output, you can see that the root partition (/) is
using ext4 and the
boot partition (/boot) is also using ext4.
In case you’re wondering about the syntax, here’s the
explanation:
mount Mounts
grep
/dev
This is the filter being used with the caret and grep. This
says, “Look for lines that begin with /dev and ignore
everything else.”
Linux can also read and/or write to many other file systems
including ext2,
ext3, FAT, FAT32, NTFS, HPFS, and others. Partitions are
mounted onto
existing directories called “mount-points”.
Linux uses a tree model to organize directories and files.
Directories are the
basic unit of storage in the Linux file system. Directories
can contain files
or other directories. In the same way that a tree cannot
exist without its
roots, the Linux file system starts at root. Root is
designated by “/”. (Recall
from chapter two that the term “root” is used in three
different ways in
Linux: “Root” is the name of the superuser, it is also used
to identify the
superuser’s home directory , and to indicate the root of the
file
system . It can be difficult to know which “root” someone is
talking
about. It helps to be clear about what is meant when
referring to “root”.)
Linux File Types
When you issue the “ls -l” command, Linux will display a
listing of files
along with information about the files. The far left hand
column of the
listing indicates the type of file. Three common file types
are regular files,
links, and directories.
In the screen capture, notice along the far left side, the
file ssh is identified
with the letter l, indicating that it is a link. The file
file1 is identified with a
hyphen (-), indicating that it is a regular file, and the
file MyDir is identified
with a d, indicating that it is a directory.
Regular files
Regular files are the most common file type on Linux or UNIX
systems.
They can be used to store various types of data including
text that you can
read or binary data that can be executed by the system. It is
often helpful to
identify more information about the file than just whether it
is a regular file
or not. For example, you might want to know whether the file
is an ASCII
text file or a shell script. You can use the “file” command
to identify the file
type.
file
Links
Understanding /etc/fstab
The file /etc/fstab contains descriptive information about
the various file
systems. The fstab file is read at boot. Here is a brief
explanation of
/etc/fstab.
Pound signs (#) indicate comments and are ignored by the
system.
The first column indicates the device file which points to
the device with
the file system which will be mounted.
The second column is the mount point.
The third column indicates the file system type in use on the
file system
being mounted.
The fourth column is used for mount options.
The fifth column is for the dump utility to decide whether or
not to back
up the file.
The sixth column determines the order in which fsck checks
the file
system at boot time. A zero means the filesystem will not be
checked.
Take a look at the last line in the screen capture, which I
added to simplify
the explanation of the part of /etc/fstab. Here is an
explanation of each of
the columns in that line:
is the device file for a floppy drive. (Yeah, it’s weird to
see a
floppy drive, but it is what it is. I don’t know, maybe I’m
just feeling
nostalgic for limited and unreliable storage media for some
strange
reason.)
/media/floppy0 is the directory which will be mounted to give
us access
/dev/fd0
to that whopping 1.544MB of data.
The entry auto in the third column means the system will
attempt to
identify the filesystem type. (Notice that the entry for
/dev/mapper/LinuxServer01--vg-root specifies ext4 as the
filesystem
type.)
In the fourth column, rw means the filesystem will be mounted
as
read/write, user means that any user can mount the
filesystem, but only
root or the user who mounted it can unmount it, noauto means
it will not
be automatically mounted at boot time, exec allows the
execution of
binaries that are on the partition, and the last entry adds
support for utf8.
The zero in the next column disables the dump option.
The zero in the sixth column means that the filesystem will
not be
checked by fsck at boot time.
The fstab file holds information about how to mount
partitions and storage
devices. If you’re having trouble mounting, say, a DVD drive,
it may be a
missing entry in /etc/fstab.
Understanding Mount Points
You can think of mount points as a way of accessing a
partition. Recall that
in Linux, everything is oriented around the file system.
Drives are identified
with letters, so the first SCSI drive on a computer might be
known as
/dev/sda, the second as /dev/sdb, and so on. The first IDE
drive would be
known as /dev/hda. Partitions are numbered, so the first
partition on the
first SCSI drive would be /dev/sda1, the second partition
would be
/dev/sda2, and so on.
You cannot, however, access partitions through /dev files;
you must create
mount points which, as you’ll recall from earlier, are simply
a means of
gaining access to a partition through the computer’s file
system.
A basic partitioning scheme will usually have three
partitions: /, /boot,
and a swap partition. Server administrators will frequently
create separate
partitions for other purposes as shown below:
Mount Point Purpose
/boot
/usr
/home
/var
/tmp
Temporary files
The three options above are the options you’ll deal with most
of the time.
There are other file types which you will encounter from
time-to-time,
which are listed below.
“b” is a block file
“c” is a character device file
“p” is a named pipe file or a pipe file
“s” is a socket file
file1.
chmod g-w file2
(1 makes it sticky)
7. Now, what are the permissions for the group on file2? The
permissions
should be “rw-“ for file2.
8. Execute the following command:
chmod a+x file*
–l
fdisk –t
mkfs
fsck
fsck /mbr
Soundthinking Point:
Partition Management Tool
The open source tool gparted is a great tool for managing
disks and partitions.
CHAPTER 5:
Linux Administration
Videos are available for many of the procedures in this
chapter at www.soundtraining.net/videos
Introduction
This is a long chapter and one of the most important chapters
in this book.
In this chapter, I’ll talk about default Linux directories
and how to generally
find your way around in Linux. You’ll learn about the grep
tool, which will
become one of your best friends. Additionally, we’ll discuss
Linux
compression and archiving tools, plus I’ll show you four
different ways to
get help (in addition, of course, to Google).
Objectives
Gain familiarity with default Linux directories
Learn about Linux profiles, both system-wide and user-
specific
Practice commonly-used shell commands
Practice switching user accounts without logging off
Move, copy, and rename files
Use the find command to locate files on the system
Practice editing configuration files with the vi text editor
Use the grep tool to filter output (conditional searching)
Create aliases to simplify commands
Learn how to start and stop services (daemons)
Learn how to use Linux compression and archiving tools
Gain familiarity with the Linux boot process, including run
levels
Learn the proper way to shut down your system, including
shutdown
options
How to get help in Linux
GUI vs. CLI
Since the first graphical user interface (GUI) was created in
the Xerox Palo
Alto Research Center in the early 1970s, those of us who work
in IT have
debated its benefits and drawbacks. The real issue is not
whether to use a
GUI or a command-line interface (CLI); it is about choosing a
tool that
works for you and helps you work most effectively. For most
of us, that
means that sometimes we’ll use a GUI and sometimes we’ll use
a CLI.
I once had a student in a Linux workshop who said his
nickname was “No
GUI Louie”. While I remember Louie as a very knowledgeable
and capable
IT pro, I have also had knowledgeable and capable students
who avoid the
CLI because of its complexity without considering the power
it affords an
administrator. I think we limit ourselves when we arbitrarily
limit the tools
at our disposal by eliminating GUI or CLI tools. In my own
work, I find
that I use both the CLI and GUI, depending on the task at
hand and my
personal familiarity with the tools in question. (Okay, I use
the commandline most often, but I’m very grateful for a GUI
when performing unfamiliar
tasks!)
Pros to Using a GUI
Faster (sometimes)
Fewer typing errors
Less minutia
Safer (harder to make mistakes)
Can help teach you CLI commands
Cons to Using a GUI
Father away from the “road”
Less control
Java and other issues might make GUI unavailable
Some of the names and labels it creates are strange
Some people are more familiar with the CLI
The minimal and basic server installations of Red Hat/CentOS
server do not
include a GUI, although you certainly can install one if you
feel the need.
My experience, however, is that most sys admins do not use a
graphical
interface with Linux servers and that’s how this book is
designed and
written.
One final comment on the subject of GUIs in general: In the
past, they were
often buggy and unreliable. Today, graphical interfaces are
much improved
over those in the past. If your experience with GUIs in the
past was less
than stellar, you might want to consider giving the newer
graphical
interfaces a try. Still, the bulk of this book is based on
the command line
interface.
Linux Directories
As mentioned previously, everything in Linux/UNIX is based on
the file
system. The file system is comprised of various directories
(Windows calls
them “folders”.) The root directory (“/”) is at the base of
the file system.
Some directories may be on different partitions or drives,
but they are still a
part of the file system. Some directories may even be on
completely
different computers, perhaps running a completely different
operating
system, but they are still part of the file system. What
follows is a list of
some of the more commonly found directories in the Linux file
system (not
all directories are included on every system):
is the root directory
/bin/ and /usr/bin/ store user commands. For example, cp, a
user
command is found in /bin.
/boot/ contains files used for system startup including the
kernel.
/dev/ contains device files
/etc/ is where configuration files and directories are
located.
/home/ is the default location for users’ home directories.
/initrd/ is used to load required device modules and mount
the
initrd.img image file during system startup.
/
/lib/ and /usr/lib/
/sbin/.
There are many others. You can view the hidden files in any
directory by
using “ls -a”.
As mentioned previously, the default settings for user
profiles are in
/etc/skel.
System-Wide Configurations
Soundthinking Point:
How to Get Out of a Long File Display
You can usually use the command
to cancel an operation in Linux. For
example, if you use the ls command to display the contents of
a directory with
hundreds of files, you may decide you don’t want to wait for
your computer to
display all of them. You can just enter
to return to a shell prompt.
CTRL+c
CTRL+c
Notice that you now see the demo directory, along with
several other
directories which are all child directories under the
parent /.
Enter the following command to change your working directory
to /demo:
cd /demo
You should now see the three subdirectories you just created.
To remove
a directory, use the command rmdir.
The “mv”, “cp”, and “rm” commands are commonly used commands
for
basic file management.
moves a file to a new
location. This is also used when you want to rename a file.
mv renames a file.
cp copies a file to a new location.
rm deletes a file.
mv
ls
ifconfig
-y
du -S | sort -n
The upper-case “S” option tells it to report the size of each
directory
separately, not including subdirectories. The pipe (|)
redirects the output of
“du” to the “sort” utility. The “-n” switch sorts
numerically.)
dmesg
dmesg | less
who -u
whoami
cat
cat
“cat”
file
file
“file”
head
tail
“tail” shows the bottom ten lines of the named file. As with
“head”,
tail
option (where “n” is the number of lines you wish to
display).
more
“more” shows the contents of a file, one page at a time. You
can see
more
additional lines, one at a time, by pressing the enter key.
less
less
and
wc
file. You can change the number of lines shown by using the -
n option (where “n”
Arrow keys can be used to move the cursor or letter keys can
be used:
to go left
j to go down
k to go up
l to go right
G goes to the end of the file
nG (where “n” is a line number) goes to the specified line in
the file
h
:wq
Inserts text
dw
Deletes to the end of a word (d2w deletes two words, d3w
deletes three words, etc.)
d$
dd
cw
Is the “change word” command, that deletes the word (from the
cursor to the right) and places you in “insert” mode
c$
Is the “change line” command, that deletes the line (from the
cursor to the right) and places you in “insert” mode
/apache”
?apache”
:s//
:s///g
:#,#s///g
:!
:nohlsearch
:s/blue/red
Using grep
grep (global regular expression print) is a filtering utility
used in the ‘nix
world to aid in searches. grep is one of the most useful
tools in IT. (There’s
even a version available for Windows.)
Some examples:
grep red blue
will display lines of text from the blue file that contain
name
Here is a handy way to use grep. Suppose you need to find a
file (or files)
containing a particular text string. Use grep with the –r and
–H options to
find all files containing that particular string (remember
that everything in
Linux is case sensitive). By default, grep only prints the
text string. If
you’re looking for files containing the text string, you must
tell grep to print
the filename, too. The –H command does that.
In the following statement, -H prints the filename, –r
searches recursively
from the starting point (/etc), and -n displays the line
number(s) in the
found files for the text string PASS_MAX_DAYS:
grep -Hrn PASS_MAX_DAYS /etc
This is the output from the previous command:
Figure 38: Using grep to search through the content of files
In the screen capture, you can see where the text string
“PASS_MAX_DAYS” was found in /etc/login.defs on line 20 and
again, in the same file, on line 25.
Hands-On Exercise 5.4:
Conditional Searching
In this exercise, you will search for a unique text string
within a file buried
deep within a directory tree.
Figure 39: Creating a file for use with the grep exercise
would always
Soundthinking Point:
Simplify Upgrades with an Alias
A great example of a way to use an alias is to simplify the
CentOS Linux upgrade
process by creating this alias:
alias yu=”yum -y update”
With this alias enabled, you can simply type to upgrade all
existing packages.
yu
3. Using the vim text editor open your .bashrc file for
editing:
vi .bashrc
6. Touch the ESC key, then type :wq to save and exit the
editor.
7. Check your work by using the command less .bashrc. It
should look
like this screen capture:
Figure 43: Viewing the newly created persistent alias
Stop a service
/etc/init.d/sshd stop
Restart a service
/etc/init.d/sshd restart
Soundthinking Point:
The “Service” Tool
Many modern Linux distros, including Red Hat and CentOS,
include a script
called service which runs other scripts located in . The
service script will do
essentially the same thing as the above commands, but in a
simpler form:
/sbin