Slackersbible/UNIX Basics
Synopsis
[edit | edit source]The following chapter will cover the basic commands and functionality of the Slackware Linux operating system. Much of this material is relevant for any UNIX® like operating system. Feel free to skim over this chapter if you are familiar with the material. If you are new to Slackware, then you will definitely want to read through this chapter carefully.
After reading this chapter, you will know:
- How to use the "virtual consoles" of Slackware.
- How UNIX file permissions work.
- The default Slackware file system layout.
- The Slackware disk organization.
- How to mount and unmount file systems.
- What processes, daemons, and signals are.
- What a shell is, and how to change your default login environment.
- How to use basic text editors.
- What devices and device nodes are.
- What binary format is used under Slackware.
- How to read manual pages for more information.
Virtual Consoles and Terminals
[edit | edit source]Slackware can be used in various ways. One of them is typing commands to a text terminal. A lot of the flexibility and power of a UNIX® operating system is readily available at your hands when using Slackware this way. This section describes what terminals and consoles are, and how you can use them.
The Console
[edit | edit source]By default, Slackware is configured to boot into the command line interface, unless you've changed this, the system will present you with a login prompt after it boots, right after the startup scripts finish running. You will see something similar to:
Loading OSS compatibility modules for ALSA. Welcome to Linux 2.4.26 (tty1) hawk login:
The messages might be a bit different on your system, but you will see something similar. The last two lines are what we are interested in right now. The second last line reads:
Welcome to Linux 2.4.26 (tty1)
This line contains some bits of information about the system you have just booted. You are looking at a Slackware console. The name of this machine (every UNIX machine has a name) is hawk, and you are now looking at its system console--the tty1 terminal. Finally, the last line is always:
login:
This is the part where you are supposed to type in your username to log into Slackware. The next section describes how you can do this.
Logging into Slackware
[edit | edit source]Slackware is a multiuser, multiprocessing system. This is the formal description that is usually given to a system that can be used by many different people, who simultaneously run a lot of programs on a single machine. Every multiuser system needs some way to distinguish one user from the rest. In Slackware (and all the UNIX like operating systems), this is accomplished by requiring that every user must log into the system before being able to run programs. Every user has a unique name (the username) and a personal, secret key (the password). Slackware will ask for these two before allowing a user to run any programs. Right after Slackware boots and finishes running its startup scripts, it will present you with a prompt and ask for a valid username:
login:
For the sake of this example, let us assume that your username is john. Type john at this prompt and press Enter. You should then be presented with a prompt to enter a password:
login: john Password:
Type in john's password now, and press Enter. The password is not echoed! You need not worry about this right now. Suffice it to say that it is done for security reasons. If you have typed your password correctly, you should by now be logged into Slackware and ready to try out all the available commands. You should see the MOTD or message of the day followed by a command prompt (a #, $, or % character). This indicates you have successfully logged into Slackware. 3.2.3 Multiple Consoles
Running UNIX commands in one console is fine, but Slackware can run many programs at once. Having one console where commands can be typed would be a bit of a waste when an operating system like Slackware can run dozens of programs at the same time. This is where virtual consoles can be very helpful. Slackware can be configured to present you with many different virtual consoles. You can switch from one of them to any other virtual console by pressing a couple of keys on your keyboard. Each console has its own different output channel, and Slackware takes care of properly redirecting keyboard input and monitor output as you switch from one virtual console to the next.
Special key combinations have been reserved by Slackware for switching consoles. You can use Ctrl+Alt-F1, Alt-F2, through Alt-F8 to switch to a different virtual console in Slackware. TTY1 (Ctrl+Alt+F1) is the default virtual console that you see at login. TTY1 through TTY6 are available at all times in text mode. TTY7 and higher become graphical virtual consoles when the graphical system is started.
As you are switching from one console to the next, Slackware takes care of saving and restoring the screen output. The result is an ``illusion of having multiple ``virtual screens and keyboards that you can use to type commands for Slackware to run. The programs that you launch on one virtual console do not stop running when that console is not visible. They continue running when you have switched to a different virtual console.
Permissions
[edit | edit source]Slackware is based on several key UNIX concepts. The first and most pronounced is that Slackware is a multi-user operating system. The system can handle several users all working simultaneously on completely unrelated tasks. The system is responsible for properly sharing and managing requests for hardware devices, peripherals, memory, and CPU time fairly to each user.
Because the system is capable of supporting multiple users, everything the system manages has a set of permissions governing who can read, write, and execute the resource. These permissions are stored as three octets broken into three pieces, one for the owner of the file, one for the group that the file belongs to, and one for everyone else. This numerical representation works like this:
Value Permission Directory Listing 0 No read, no write, no execute --- 1 No read, no write, execute --x 2 No read, write, no execute -w- 3 No read, write, execute -wx 4 Read, no write, no execute r-- 5 Read, no write, execute r-x 6 Read, write, no execute rw- 7 Read, write, execute rwx
You can use the -l command line argument to ls(1) to view a long directory listing that includes a column with information about a file's permissions for the owner, group, and everyone else. For example, a ls -l in an arbitrary directory may show:
% ls -l total 530 -rw-r--r-- 1 root wheel 512 Sep 5 12:31 myfile -rw-r--r-- 1 root wheel 512 Sep 5 12:31 otherfile -rw-r--r-- 1 root wheel 7680 Sep 5 12:31 email.txt
...
Here is how the first column of ls -l is broken up:
-rw-r--r--
The first (leftmost) character tells if this file is a regular file, a directory, a special character device, a socket, or any other special pseudo-file device. In this case, the - indicates a regular file. The next three characters, rw- in this example, give the permissions for the owner of the file. The next three characters, r--, give the permissions for the group that the file belongs to. The final three characters, r--, give the permissions for the rest of the world. A dash means that the permission is turned off. In the case of this file, the permissions are set so the owner can read and write to the file, the group can read the file, and the rest of the world can only read the file. According to the table above, the permissions for this file would be 644, where each digit represents the three parts of the file's permission.
This is all well and good, but how does the system control permissions on devices? Slackware actually treats most hardware devices as a file that programs can open, read, and write data to just like any other file. These special device files are stored on the /dev directory.
Directories are also treated as files. They have read, write, and execute permissions. The executable bit for a directory has a slightly different meaning than that of files. When a directory is marked executable, it means it can be traversed into, that is, it is possible to "cd" (change directory) into it. This also means that within the directory it is possible to access files whose names are known (subject, of course, to the permissions on the files themselves).
In particular, in order to perform a directory listing, read permission must be set on the directory, whilst to delete a file that one knows the name of, it is necessary to have write and execute permissions to the directory containing the file.
There are more permission bits, but they are primarily used in special circumstances such as setuid binaries and sticky directories. If you want more information on file permissions and how to set them, be sure to look at the chmod manual page. 3.3.1 Symbolic Permissions
Contributed by Tom Rhodes.
Symbolic permissions, sometimes referred to as symbolic expressions, use characters in place of octal values to assign permissions to files or directories. Symbolic expressions use the syntax of (who) (action) (permissions), where the following values are available:
Option Letter Represents (who) u User (who) g Group owner (who) o Other (who) a All (world) (action) + Adding permissions (action) - Removing permissions (action) = Explicitly set permissions (permissions) r Read (permissions) w Write (permissions) x Execute (permissions) s Set UID or GID
These values are used with the chmod command just like before, but with letters. For an example, you could use the following command to block other users from accessing FILE:
% chmod go= FILE
A comma separated list can be provided when more than one set of changes to a file must be made. For example the following command will remove the groups and world write permission on FILE, then it adds the execute permissions for everyone:
% chmod go-w,a+x FILE
Processes
[edit | edit source]Slackware is a multi-tasking operating system. This means that it seems as though more than one program is running at once. Each program running at any one time is called a process. Every command you run will start at least one new process, and there are a number of system processes that run all the time, keeping the system functional.
Each process is uniquely identified by a number called a process ID, or PID, and, like files, each process also has one owner and group. The owner and group information is used to determine what files and devices the process can open, using the file permissions discussed earlier. Most processes also have a parent process. The parent process is the process that started them. For example, if you are typing commands to the shell then the shell is a process, and any commands you run are also processes. Each process you run in this way will have your shell as its parent process. The exception to this is a special process called init(8). init is always the first process, so its PID is always 1. init is started automatically by the kernel when Slackware starts.
Foreground and Background
When you run a command, usually that command is in the foreground (you see it) and it's using the computer until it is finished. This might seem obvious until you realize that there are things that can be done about this. For example, if you start a command and then decide that you would like to terminate it, you can usually use Ctrl+C to terminate it. Also, if you'd like to start other commands rather than wait for the program to quit, you may use Ctrl+Z to stop it, so that it will no longer run and be "in your way".
A stopped application can be brought to the foreground once again using the "fg" command, where it will resume running. This is useful if you want to let the command continue, or if you'd like to use Ctrl+C to terminate it.
Another thing that can be done is to use an ampersand (&) after a command to cause it to run in the background. When you do this, you might see the following:
root@darkstar:~# updatedb & [1] 12353
This means that the program has been started, and has been given the PID (program identification number) of 12353, which is useful should you want to terminate it with "kill 12353".
Two commands are particularly useful to see the processes on the system, ps(1) and top(1). The ps command is used to show a static list of the currently running processes, and can show their PID, how much memory they are using, the command line they were started with, and so on. The top command displays all the running processes, and updates the display every few seconds, so that you can interactively see what your computer is doing.
By default, ps only shows you the commands that are running and are owned by you. For example:
raven@hawk:~$ ps PID TTY TIME CMD 31674 pts/1 00:00:00 bash 31686 pts/1 00:00:00 ps
As you can see in this example, the output from ps(1) is organized into a number of columns. PID is the process ID discussed earlier. PIDs are assigned starting from 1, go up to 99999, and wrap around back to the beginning when you run out. The TT column shows the tty the program is running on, and can safely be ignored for the moment. STAT shows the program's state, and again, can be safely ignored. TIME is the amount of time the program has been running on the CPU--this is usually not the elapsed time since you started the program, as most programs spend a lot of time waiting for things to happen before they need to spend time on the CPU. Finally, COMMAND is the command line that was used to run the program.
ps(1) supports a number of different options to change the information that is displayed. One of the most useful sets is auxww. a displays information about all the running processes, not just your own. u displays the username of the process' owner, as well as memory usage. x displays information about daemon processes, and ww causes ps(1) to display the full command line, rather than truncating it once it gets too long to fit on the screen. The output from top(1) is similar. A sample session looks like this:
raven@hawk:~$ top top - 22:52:10 up 11:29, 2 users, load average: 0.13, 0.04, 0.01 Tasks: 34 total, 1 running, 33 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0% user, 0.0% system, 0.0% nice, 100.0% idle Mem: 190788k total, 187268k used, 3520k free, 44340k buffers Swap: 976712k total, 740k used, 975972k free, 86176k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 8 0 240 240 212 S 0.0 0.1 0:04.64 init 2 root 9 0 0 0 0 S 0.0 0.0 0:00.24 keventd 3 root 19 19 0 0 0 S 0.0 0.0 0:00.35 ksoftirqd_CPU0 4 root 9 0 0 0 0 S 0.0 0.0 0:01.16 kswapd 5 root 9 0 0 0 0 S 0.0 0.0 0:00.05 bdflush 6 root 9 0 0 0 0 S 0.0 0.0 0:00.30 kupdated 10 root -1 -20 0 0 0 S 0.0 0.0 0:00.00 mdrecoveryd 11 root 9 0 0 0 0 S 0.0 0.0 0:00.00 kreiserfsd 60 root 9 0 588 588 508 S 0.0 0.3 0:00.07 syslogd 63 root 9 0 448 448 392 S 0.0 0.2 0:00.04 klogd 75 root 8 0 688 688 520 S 0.0 0.4 0:00.17 cardmgr 157 root 9 0 492 460 428 S 0.0 0.2 0:00.00 dhcpcd 272 root 9 0 0 0 0 S 0.0 0.0 0:00.00 khubd 762 root 9 0 520 520 464 S 0.0 0.3 0:00.00 inetd 765 root 9 0 1408 1408 1308 S 0.0 0.7 0:00.21 sshd 776 root 4 0 564 564 496 S 0.0 0.3 0:00.00 crond 778 daemon 9 0 616 616 548 S 0.0 0.3 0:00.01 atd
The output is split into two sections. The header (the first five lines) shows the PID of the last process to run, the system load averages (which are a measure of how busy the system is), the system uptime (time since the last reboot) and the current time. The other figures in the header relate to how many processes are running (47 in this case), how much memory and swap space has been taken up, and how much time the system is spending in different CPU states.
Below that are a series of columns containing similar information to the output from ps(1). As before you can see the PID, the username, the amount of CPU time taken, and the command that was run. top(1) also defaults to showing you the amount of memory space taken by the process. This is split into two columns, one for total size, and one for resident size--total size is how much memory the application has needed, and the resident size is how much it is actually using at the moment.
top(1) automatically updates this display every two seconds; this can be changed with the s option.
Text Editors
[edit | edit source]Much of UNIX administration involved editing configuration files, most of which are simply text files. Here we will address several of the most common UNIX text editors and their basic operations.
vi
[edit | edit source]There is arguably no text editor more ubiquitous in the UNIX world than vi. This fact alone serves as a reason that all serious UNIX (and Slackware) users should learn at least its basic features. The name comes from the command "visual" used to switch the ex line editor into visual mode. This fact serves to illustrate two interesting things about vi. First, that vi is a modal editor, which means that its editing functions change depending on the state, or mode, that the program is in. The second interesting thing to note is that the name is, like much of UNIX programs, and extension of another. vi has all the user customization and extensibility UNIX users demand, which makes it an extremely powerful program.
Modal Editing
[edit | edit source]By contrast to some text editors that simply echo keystrokes to the screen and rely on function key combinations for extended features, modal editors--and by extension, vi--use the editor mode to define what actions are perform on the text file. This is best explained by example.
1 ~ ~ ~ ~ ~ ~ ~ ~ ~ VIM - Vi IMproved ~ ~ version 7.0.122 ~ by Bram Moolenaar et al. ~ Vim is open source and freely distributable ~ ~ Become a registered Vim user! ~ type :help register<Enter> for information ~ ~ type :q<Enter> to exit ~ type :help<Enter> or <F1> for on-line help ~ type :help version7<Enter> for version info ~ ~ ~ ~ ~ ~ ~ ~
What you see is a modern version of vi called Vim. Vim inherits nearly all of the features of the original, proprietary version of vi, and adds a multitude of its own features which are distributed under a GPL-compatible free software license. The screen above is what Vim looks like when it first loads. In this state, it is in Normal mode. Normal mode is also referred to as Command mode, because from this mode you can execute various commands, or actions, on the text, such as changing, deleting, copying, etcetera. To enter text in standard fashion, you must first switch vi to Insert mode.
From this initial mode we can enter text by switching to Insert mode in one of several ways. The easiest is just to press the "i" key (for insert). Now you can enter text as you wish. All standard alphanumeric characters, including punctuation, are echoed to the display as typed:
- Figure 5.1.1-1
1 vi is my favorite text editor. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- INSERT --
You can see from this example that Vim displays the text as typed, "vi is my favorite text editor.", as well as the fact that it is now in Insert mode. To switch back to Command mode, simply press the escape key (Esc). vi will return to Command mode (and Vim will change its display to reflect this by removing "-- INSERT --" from the bottom of the screen.
- Important: Commands Are Case Sensitive
- Be careful. vi commands (such as "i" here) are case sensitive, so a capital "I" is not the same command as a lower case "i". By the same token it is highly recommended not to use CAPSLOCK on your keyboard while editing in vi. Despite being largely superfluous to most typists anyway, using CAPSLOCK can lead to unexpected results when you switch back to Command mode.
Basic Commands
[edit | edit source]"So what good is Command mode?" you ask. Good question. Let's take a look at some basic commands that illustrate the power of modal editing.
Moving the Cursor
[edit | edit source]Command mode may seem to add complication to the text editing process, but it is actually designed to increase productivity. One of the first fruits you will glean from Command mode is increased speed editing text. Command mode makes text editing faster than standard text editing even if you only know a handful of commands. Take the previous example (see Figure 5.1.1-1). If you were to press "Esc" at the end of the line, vi would enter Command mode and the cursor would be placed atop the last character typed. From this state, you can use the "h", "j", "k", and "l" keys to move the cursor left, down, up, and right, respectively, throughout the file one character at a time.
Yanking, Putting and Deleting Text
[edit | edit source]vi uses registers to store text temporarily. Using the yank and put commands, you can copy or remove text from a portion of the file into the register and put it somewhere else.
File Operations
[edit | edit source]So now that you can do a few commands, you probably want to open a file do some editing.
Emacs
[edit | edit source]Emacs is another popular UNIX text editor and, like vi, is extremely powerful. Emacs was originally conceived by Richard Stallman, the founder of the GNU project, as a set of editor macros for another text editor. This eventually became what is known today as Emacs (derived from Editor MACroS).