Working with configuration files in Linux systems
Learn how to find and edit configuration files in your customers' Linux systems by using the shell commands provided in this chapter excerpt.
Administration Tools and Techniques
Working in Terminal
Most Linux systems configured as servers are managed in a command-line interface (CLI) and many Linux power-users prefer to manager even their desktop system in the CLI. Although the graphical user interface (GUI) tools available for use in many Linux distros have improved considerably over past versions, the CLI continues to provide the greatest power and flexibility for configuring and managing a Linux system. The other benefit to working in a CLI is that each Linux distro is much more similar in the CLI than in the GUI. For the purpose of this workshop, you will do most of your configurations in the CLI, thus allowing you to make smoother and simpler transitions from Debian to other distros such as SuSE, Ubuntu, RedHat, Slackware, or even traditional UNIX systems.
There are a variety of switches available for use with "ls":
|
|
ls –a | Lists all files including hidden files |
ls –l | Long listing, includes permissions, owners, groups, etc. |
ls –R | Lists sub-directories recursively |
ls –sh | Shows file size (s) in human-readable format (h) |
ls -1 (the number "1") | Displays one file per line |
ls –d | Tells "ls" to list directory names, but not their contents |
You can also use common shell metacharacters with "ls":
- * is the string wildcard
- ? is the character wildcard
- [ ] encloses a character set
- [ -] is a character range
- { } is a string set
When you issue the "cd" command with no parameters, you will be returned to your home directory.
#mkdir creates directories.
About the book
This chapter excerpt on Linux Administration (download PDF) is taken from the book The Accidental Administrator: Linux Server Step-by-Step Configuration Guide. Among the topics covered in this book are Linux installation, administration network services and performance monitoring for solution providers.
Other Helpful Commands
#su – [username] is the switch user command. The hyphen switches the present working directory to the new user's home directory. When used with no parameters, the "su" command switches to "root".
#pwd displays the present working directory's full path
#ls lists the directory contents
#cat [filename] concatenates files and prints on the standard output (usually the display screen)
#less [filename] (from the man page) less is a program similar to more, but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting.
#more [filename] is a program that filters text to allow paging through a file one page at a time
whereis is a helpful command for finding configuration files and executable programs. It does not search through user directories.
Try this:
#whereis ifconfig
find is another helpful command that will search based on various criteria including file name, file size, modification date, and permissions. The find command can only be issued by a user who has permission to view the target files and directories.
Try this:
#find [filename within the current directory]
There are many options available for use with find:
#find / -type d -name conf will find all the directories named "conf"
#find / -user donc will find all files owned by "donc"
#find / -name donc will find all files with the same name as "donc"
#find -name 'index.html' would search for any file named index.html in the current directory and any subdirectory.
#find / -name 'index.html' would search for any file named index.html in the root directory and all subdirectories from root
#find -name 'sshd* would search for any file beginning with the text string "sshd" in the current directory and any subdirectory.
#find -name '*' -size +500k would search for any file larger then 500k.
locate is also a command that is useful for finding files on a Linux system. It uses a database when searching for files, so it's faster than find.
You can use locate like this:
#locate [filename]
Files that have been created recently, however, may not be in the database. You can force an update of the database like this:
#updatedb or
#locate –u
du is a way of estimating disk usage. When used with no arguments, du reports the disk space for the current directory. By default, disk space is printed in units of one kilobyte (1024 bytes). For example, to find out which directories are largest, use this command:
#du -S | sort –n (The "S" switch 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 is a program that helps users print out bootup messages:
#dmesg | less
This command will pipe to "less"
An alternative is to pipe the dmesg output to a file. Try this:
#dmesg > boot.messages
You will find the boot.messages file in the present working directory. Try using cat, more, and less to view the contents of the file.
The "who" command displays currently logged on users:
#who displays currently logged on users, their terminal, and their login times.
#who –u adds idle time.
#whoami displays the name of the user initiating the command.
There are a variety of switches available for use with "ls":
Viewing the contents of a file
|
|
|
cat | cat [filename] | "cat" is for "concatenate", cat displays the contents of file(s) named in the command |
file | file [filename] | "file" identifies the type of file as directory, text, or binary. |
head | head [filename] | "head" shows the top ten lines of the named file. You can change the number of lines shown by using the -n option (where "n" is the number of lines you wish to display). |
tail | tail [filename] | "tail" shows the bottom ten lines of the named file. As with "head", you can change the number of lines shown by using the -n option (where "n" is the number of lines you wish to display). |
more | more [filename] | "more" shows the contents of a file, one page at a time. You can see additional pages by pressing the space bar or view additional lines, one at a time, by pressing the enter key. |
less | less [filename] | "less" is similar to "more" in that it shows the contents of a file, one page at a time, but "less" allows you to move forward and backward through the file using the arrow keys. |
wc | wc [filename] | When used with no options, "wc" displays the number of lines, words, and characters in the named file. Options are available which allow you to specify bytes, characters, lines, and words. |
Editing configuration files
In addition to managing a Linux system by executing various commands in the CLI or using tools in graphical interface, you will also need to frequently modify various configuration files.
There are several text editors which are commonly used to edit the Linux configuration files. In this document, we will be using "vim", a programmers‟ text editor. "vim" is an enhanced version of "Vi". Most people use "vim", but refer to it as "vi". This exercise will help you become more comfortable with "vi" (pronounced "VEE-eye"), a traditional text editor found on most Linux and UNIX systems. Although many people consider "vi" to be somewhat awkward, its wide availability makes a fundamental understanding of its basic commands well worthwhile.
The traditional "vi" text editor has been replaced on most systems with "vim" ("vi" improved). The command set is substantially the same for both "vi" and "vim". On most systems, the "vi" command has been aliased to "vim".
To open a file with Vim, type the following command:
#vim [filename]
Operation within Vim is done with a variety of commands, some of which are listed here:
:set nu displays line numbers along the left margin
:q! quits without saving
:wq writes and quits (saves and quits)
Arrow keys can be used to move the cursor or letter keys can be used:
h 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
Vim has many more commands and options available. Help is available by typing :help.
About the author:
Don R. Crawley is the president and chief technologist of the Seattle-based IT training company, soundtraining.net. He has more than 35 years experience working with workplace technology and automation and holds multiple technical certifications including Linux+ and CCNA Security.
Printed with permission from Soundtraining.net. Copyright 2010. The Accidental Administrator: Linux Server Step-by-Step Configuration Guide by Don R. Crawley. For more information about this title and other similar books, please visit www.soundtraining.net/bookstore.