The commands in this section will help sysadmins get to know the systems they are responsible for like the back of their hands:
When run without options,
hostname will show the system's host name as defined in /etc/hosts or /etc/hostname. By adding
-I (or its equivalent
--all-ip-addresses), all the configured addresses on all network interfaces will be returned.
Disk usage and available storage space can be easily found out using
Strictly speaking, the former will estimate the file space usage by file or directory, whereas the latter will report the same information on a per-filesystem basis. Both support the
-h flag, which formats the output of human-readable form (10K, 15M, 3G, for example) instead of using bytes.
When followed by a directory and the star wildcard
*, the combined switch
du to summarize totals for the directory and each subdirectory within. Also, a grand total is provided. In other words,
du -sch /var/log/*
will print the file space usage of all files and subdirectories inside, along with the grand total, for /var/log. To view only the grand total, remove the
For example, you would not run a mail service on a machine with only 1 GB of RAM. To find out the architecture, number of cores, vendor id, model name, and speed (and more) of the CPU(s), you can use
lscpu. On the other hand, the
free command (which, by the way, also supports the
-h for returning output in human-readable form) shows the amount of free and used memory in the system.
-poption. If you remove that flag,
uptimewill also return the current system time, the number of logged users, and the load average for the past 1, 5, and 15 minutes.
It is often said that in Unix and its derivatives everything is a file, and thus the need to master command-line text processing tools. The following commands are a must for every system administrator. When in the Linux community we speak of everything being a file, we mean that every system object can be opened, read from, and written to as a file in the regular sense of the word (given you have the necessary permissions to do so).
If you are interested in inspecting a different number of lines from the beginning or the bottom of the file, you can use
head -n or
tail -n followed by an integer and the name of the file. For example,
head -n 2 /etc/passwd
will return the first two lines of /etc/passwd. Often, we will be interested in viewing the last lines of a file and monitoring it as more lines are added. This is the case when we want to monitor a system log as events are recorded. To do so, we use
tail -f followed by the path to the log file, such as
tail -f /var/log/httpd/access_log
Incidentally, the above command will allow you to monitor the access log of the Apache web server in a CentOS 7 server.
grepis the tool for the job.
To illustrate, let's return the line(s) in /etc/passwd where the word student (a simple pattern) is found:
grep student /etc/passwd
To ignore case so that both Student and StUdEnT are also matches, use
grep -i student /etc/passwd
You can also use a regular expression instead of a simple pattern. To do so, add the
-E switch as in
grep -E 'svm|vmx' /proc/cpuinfo
where 'svm|vmx' is a regular expression that matches either the string svm or vmx. By the way, this command checks if virtualization is enabled on your CPU(s).
First, we need to indicate the directory where we must start the search, the object type, and the name. Although there are other filter criteria that we can add to narrow down our search, this is a typical example of the use of
find. For example,
find /etc -type f -name sshd_config
will search for a regular file (
type f) named sshd_config starting at /etc.
cutwill be our best ally.
This tool is often used with
-d to specify a delimiter surrounded in single quotes, and
-f followed by a number, range, or a comma-separated list of files that indicate which section(s) we're interested in.
If you want to list the users (1st field) found in /etc/passwd with their default shell (7th field), you can use
cut -d':' -f1,7 /etc/passwd
To add the home directories (6th field), you would use
cut -d':' -f1,6-7 /etc/passwd
Linux allows you to create empty files using the
touch command followed by the name of the new file. This is useful to initialize files for a sandbox environment. If
touch is followed by the name of an existing file, its modification time will be updated to the current system time.
cut -d':' -f1,7 /etc/passwd > usersandshells.txt
will create a file named usersandshells.txt in the current working directory (if it does not exist previously) or overwrite its contents (if such file exists) with the output of the command.
It is important to not that using > or >> is not a one-size-fits-all solution, but depends on the specific scenario where it is to be utilized.
To do this, we will use the vertical bar |, also known as pipeline. Thus,
cut -d':' -f1,7 /etc/passwd | grep student
will filter out the line containing the word student from the output of
cut -d':' -f1,7 /etc/passwd.
To illustrate, you can do
cut -d':' -f7 /etc/passwd | sort | uniq
which will return the default shell(s) for the accounts listed in /etc/passwd. There is a good reason why
sort follows the first pipeline and then sends its output to
uniq and not the other way around. The latter requires its input to be sorted beforehand to return unique lines.
Please continue on to the next Guide in this Series User and Group Management in Linux to continue learning about Linux Administration.