Basic Linux commands you need to know for CompTIA’s new A+
As is the norm every few years, the CompTIA A+ exams (currently numbers 220-801 and 220-802) are being updated to reflect the current technology environment. The new exams (to be named 220-901 and 220-902) are expected by the end of the year and — for the most part — they expand what is being currently tested: adding more content than subtracting.
One of the topics being added is that of Basic Linux commands. The most recent iterations of the exams focused on Microsoft Windows, but with administrators regularly encountering Linux-based servers it makes sense that the coverage is being enlarged to require them to know of a few command-line tools that work there. The commands they have honed in on, in domain order, are:
● pwd vs passwd
Because of space limitations, we will look at approximately the first half of the commands this month and the remainder next month. Before getting to the commands, though, it is important to understand some basics. With Linux, there is a shell that serves as an interpreter between the user and OS: this is often bash, but can be one of several others as well. Because a shell interprets what you type, knowing how the shell processes the text you enter is important. All shell commands have the following general format (assuming the command takes options — some commands have no options).
command [option1] [option2] . . . [optionN]
On a command line, you enter a command, followed by zero or more options (or arguments). The shell uses a blank space or a tab to distinguish between the command and options. This means you must use a space or a tab to separate the command from the options and the options from one another. If an option contains spaces, you put that option inside quotation marks. For example, to search for a name in the password file, enter the following grep command (grep is used for searching for text in files):
grep “Emmett Dulaney” /etc/passwd
When grep prints the line with the name, it looks like this:
If you create a user account with your username, type the grep command with your username as an argument to look for that username in the /etc/passwd file. In the output from the grep command, you can see the name of the shell (/bin/bash) following the last colon (:). Because the bash shell is an executable file, it resides in the /bin directory; you must provide the full path to it.
The number of command-line options and their format depend on the actual command. Typically, these options look like -X, where X is a single character. For example, you can use the -l option with the ls command. The command lists the contents of a directory, and the option provides additional details. Here is a result of typing ls -l in a user’s home directory:
drwxr-xr-x 2 edulaney users 48 2015-09-08 21:11 bin
drwx—— 2 edulaney users 320 2015-09-08 21:16 Desktop
drwx—— 2 edulaney users 80 2015-09-08 21:11 Documents
drwxr-xr-x 2 edulaney users 80 2015-09-08 21:11 public_html
drwxr-xr-x 2 edulaney users 464 2015-09-17 18:21 sdump
If a command is too long to fit on a single line, you can press the backslash key (\) followed by Enter. Then, continue typing the command on the next line. For example, type the following command. (Press Enter after each line.)
The cat command then displays the contents of the /etc/passwd file.
You can concatenate (that is, string together) several shorter commands on a single line by separating the commands by semicolons (;). For example, the following command …
cd; ls -l; pwd
… changes the current directory to your home directory, lists the contents of that directory, and then shows the name of that directory.
You can combine simple shell commands to create a more sophisticated command. For example, suppose that you want to find out whether a device file named sbpcd resides in your system’s /dev directory because some documentation says you need that device file for your CD-ROM drive. You can use the ls /dev command to get a directory listing of the /dev directory and then browse through it to see whether that listing contains sbpcd.
Unfortunately, the /dev directory has a great many entries, so you may find it hard to find any item that has sbpcd in its name. You can, however, combine the ls command with grep and come up with a command line that does exactly what you want. Here’s that command line:
ls /dev | grep sbpcd
The shell sends the output of the ls command (the directory listing) to the grep command, which searches for the string sbpcd. That vertical bar (|) is known as a pipe because it acts as a conduit (think of a water pipe) between the two programs — the output of the first command is fed into the input of the second one.
There are literally hundreds, if not thousands, of Linux commands that exist within the shell and the system directories. Fortunately, CompTIA asks that you know a much smaller number than that. The following table lists the Linux commands by category.
Linux Commands by Category
|Managing Files and Directories|
|cd||Change the current directory|
|chmod||Change file permissions|
|chown||Change the file owner and group|
|ls||Display the contents of a directory|
|mv||Rename a file and move the file from one directory to another|
|pwd||Display the current directory|
|dd||Copy blocks of data from one file to another (used to copy data from devices)|
|grep||Search for regular expressions in a text file|
|apt-get||Download files from a repository site|
|ps||Display a list of currently running processes|
|shutdown||Shut down Linux|
|vi||Start the visual file editor|
|passwd||Change the password|
|su||Start a new shell as another user (the other user is assumed to be root when the command is invoked without any argument)|
|sudo||Allows you to run a command as another user (usually the root user)|
|ifconfig||View and change information related to networking configuration|
|iwconfig||Similar to ifconfig, but used for wireless configuration|
Becoming the Root/Superuser
When you want to do anything that requires a high privilege level (for example, administering your system), you have to become root. Normally, you log in as a regular user with your everyday username. When you need the privileges of the superuser, though, use the following command to become root:
That’s su followed by a space and the minus sign (or hyphen). The shell then prompts you for the root password. Type the password and press Enter. After you’ve finished with whatever you want to do as root (and you have the privilege to do anything as root), type exit to return to your normal username.
Instead of becoming root by using the su – command, you can also type sudo followed by the command that you want to run as root. In some distributions, such as Ubuntu, you must use the sudo command because you don’t get to set up a root user when you install the operating system. If you’re listed as an authorized user in the /etc/sudoers file, sudo executes the command as if you were logged in as root. Type man sudoers to read more about the /etc/sudoers file.
Every time the shell executes a command that you type, it starts a process. The shell itself is a process as are any scripts or programs that the shell runs. Use the ps ax command to see a list of processes. When you type ps ax, bash shows you the current set of processes. Here are a few lines of output from the command e ps ax -cols 132. (The -cols 132 option is used to ensure seeing each command in its entirety.)
PID TTY STAT TIME COMMAND
1 ? S 0:01 init 
2 ? SN 0:00 [ksoftirqd/0]
3 ? S< 0:00 [events/0]
4 ? S< 0:00 [khelper]
9 ? S< 0:00 [kthread]
19 ? S< 0:00 [kacpid]
75 ? S< 0:00 [kblockd/0]
115 ? S 0:00 [pdflush]
116 ? S 0:01 [pdflush]
118 ? S< 0:00 [aio/0]
117 ? S 0:00 [kswapd0]
711 ? S 0:00 [kseriod]
1075 ? S< 0:00 [reiserfs/0]
2086 ? S 0:00 [kjournald]
2239 ? S < s 0:00 /sbin/udevd -d
. . . lines deleted . . .
6374 ? S 1:51 /usr/X11R6/bin/X :0 -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
6460 ? Ss 0:02 /opt/gnome/bin/gdmgreeter
6671 ? Ss 0:00 sshd: edulaney [priv]
6675 ? S 0:00 sshd: edulaney@pts/0
6676 pts/0 Ss 0:00 -bash
6712 pts/0 S 0:00 vsftpd
14702 ? S 0:00 pickup -l -t fifo -u
14752 pts/0 R+ 0:00 ps ax –cols 132
In this listing, the first column has the heading PID and shows a number for each process. PID stands for process ID (identification), which is a sequential number assigned by the Linux kernel. If you look through the output of the ps ax command, you see that the init command is the first process and has a PID of 1. That’s why init is referred to as the mother of all processes.
The COMMAND column shows the command that created each process, and the TIME column shows the cumulative CPU time used by the process. The STAT column shows the state of a process: S means the process is sleeping, and R means it’s running. The symbols following the status letter have further meanings; for example < indicates a high-priority process, and + means that the process is running in the foreground. The TTY column shows the terminal, if any, associated with the process.
The process ID, or process number, is useful when you have to forcibly stop an errant process. Look at the output of the ps ax command and note the PID of the offending process. Then, use the kill command with that process number to stop the process. For example, to stop process number 8550, start by typing the following command:
In Linux, when you log in as root, your home directory is /root. For other users, the home directory is usually in the /home directory, for example the home directory for a user logging in as edulaney is /home/edulaney. This information is stored in the /etc/passwd file. By default, only you have permission to save files in your home directory, and only you can create subdirectories in your home directory to further organize your files.
Linux supports the concept of a current directory, which is the directory on which all file and directory commands operate. After you log in, for example, your current directory is the home directory. To see the current directory, type the pwd command.
To change the current directory, use the cd command. To change the current directory to /usr/lib, type the following:
Then, to change the directory to the cups subdirectory in /usr/lib, type this command:
Now, if you use the pwd command, that command shows /usr/lib/cups as the current directory.
These two examples show that you can refer to a directory’s name in two ways: Absolute or Relative. An example of an absolute pathname is /usr/lib, which is an exact directory in the directory tree (think of the absolute pathname as the complete mailing address for a package that the postal service will deliver to your next-door neighbor). An example of a relative pathname is cups, which represents the cups subdirectory of the current directory, whatever that may be (think of the relative directory name as giving the postal carrier directions from your house to the one next door so the carrier can deliver the package).
If you type cd cups in /usr/lib, the current directory changes to /usr/lib/cups. However, if you type the same command in /home/edulaney, the shell tries to change the current directory to /home/edulaney/cups.
Use the cd command without any arguments to change the current directory back to your home directory. No matter where you are, typing cd at the shell prompt brings you back home. The tilde character (~) is an alias that refers to your home directory. Thus, you can change the current directory to your home directory also by using the command cd ~. You can refer to another user’s home directory by appending that user’s name to the tilde. Thus, cd ~superman changes the current directory to the home directory of superman.
Also, a single dot (.) and two dots (. .) , often referred to as dot-dot, also have special meanings. A single dot (.) indicates the current directory, whereas two dots (. .) indicate the parent directory. For example, if the current directory is /usr/share, you go one level up to /usr by typing the following:
cd . .
You can get a directory listing by using the ls command. By default, the ls command, without any options, displays the contents of the current directory in a compact, multicolumn format. To tell the directories and files apart, use the -F option (ls –F). The output will show the directory names with a slash (/) appended to them. Plain filenames appear as is. The at sign (@) appended to a indicates that this file is a link to another file. (In other words, this filename simply refers to another file; it’s a shortcut.) An asterisk (*) is appended to executable files (the shell can run any executable file).
You can see even more detailed information about the files and directories with the -l option. The rightmost column shows the name of the directory entry. The date and time before the name show when the last modifications to that file were made. To the left of the date and time is the size of the file in bytes. The file’s group and owner appear to the left of the column that shows the file size. The next number to the left indicates the number of links to the file. (A link is like a shortcut in Windows.) Finally, the leftmost column shows the file’s permission settings, which determine who can read, write, or execute the file.
This column shows a sequence of nine characters, which appear as rwxrwxrwx when each letter is present. Each letter indicates a specific permission. A hyphen (-) in place of a letter indicates no permission for a specific operation on the file. Think of these nine letters as three groups of three letters (rwx), interpreted as follows:
Leftmost group: Controls the read, write, and execute permission of the file’s owner. In other words, if you see rwx in this position, the file’s owner can read (r), write (w), and execute (x) the file. A hyphen in the place of a letter indicates no permission. Thus, the string rw- means the owner has read and write permission but not execute permission. Although executable programs (including shell programs) typically have execute permission, directories treat execute permission as equivalent to use permission: A user must have execute permission on a directory before he or she can open and read the contents of the directory.
Middle group: Controls the read, write, and execute permission of any user belonging to that file’s group.
Rightmost group: Controls the read, write, and execute permission of all other users (collectively thought of as the world).
Thus, a file with the permission setting rwx—— is accessible only to the file’s owner, whereas the permission setting rwxr–r– makes the file readable by the world.
Most Linux commands take single-character options, each with a hyphen as a prefix. When you want to use several options, type a hyphen and concatenate (string together) the option letters, one after another. Thus, ls -al is equivalent to ls -a -l as well as ls -l -a.
Next month, we will look at the rest of the basic Linux commands CompTIA wants you to be familiar with for the upcoming A+ certification exams.