Chapter 2. GNU/Linux tutorials

Table of Contents

2.1. Console basics
2.1.1. The shell prompt
2.1.2. The shell prompt under X
2.1.3. The root account
2.1.4. The root shell prompt
2.1.5. GUI system administration tools
2.1.6. Virtual consoles
2.1.7. How to leave the command prompt
2.1.8. How to shutdown the system
2.1.9. Recovering a sane console
2.1.10. Additional package suggestions for the newbie
2.1.11. An extra user account
2.1.12. sudo configuration
2.1.13. Play time
2.2. Unix-like filesystem
2.2.1. Unix file basics
2.2.2. Filesystem internals
2.2.3. The filesystem permission system
2.2.4. Control of permissions for newly created files: umask
2.2.5. Permissions for groups of users (group)
2.2.6. Timestamps
2.2.7. Links
2.2.8. Named pipes (FIFOs)
2.2.9. Sockets
2.2.10. Device files
2.2.11. Special device files
2.2.12. procfs and sysfs
2.3. Midnight Commander (MC)
2.3.1. Customization of MC
2.3.2. Starting MC
2.3.3. File manager in MC
2.3.4. Command-line tricks in MC
2.3.5. The internal editor in MC
2.3.6. The internal viewer in MC
2.3.7. Auto-start features of MC
2.3.8. FTP virtual filesystem of MC
2.4. The basic Unix-like work environment
2.4.1. The login shell
2.4.2. Customizing bash
2.4.3. Special key strokes
2.4.4. Unix style mouse operations
2.4.5. The pager
2.4.6. The text editor
2.4.7. Setting a default text editor
2.4.8. Customizing vim
2.4.9. Recording the shell activities
2.4.10. Basic Unix commands
2.5. The simple shell command
2.5.1. Command execution and environment variable
2.5.1.1. LANG variable
2.5.1.2. PATH variable
2.5.1.3. HOME variable
2.5.2. Command line options
2.5.3. Shell glob
2.5.4. Return value of the command
2.5.5. Typical command sequences and shell redirection
2.5.6. Command alias
2.6. Unix-like text processing
2.6.1. Unix text tools
2.6.2. Regular expressions
2.6.3. Replacement expressions
2.6.4. Extract data from text file table
2.6.5. Script snippets for piping commands
2.6.6. Perl one liner for the regular-expression substitution

I think learning a computer system is like learning a new foreign language. Although tutorial books and documentation are helpful, you have to practice it yourself. In order to help you get started smoothly, I will elaborate a few basic points.

The powerful design of Debian GNU/Linux comes from the Unix operating system, i.e., a multiuser, multitasking operating system. You must learn to take advantage of the power of these features and the similarities between Unix and GNU/Linux.

Don't shy away from Unix oriented texts and don't rely solely on GNU/Linux texts, as this will rob you of much useful information.

"Rute User's Tutorial and Exposition", in the Debian non-free archive as rutebook package (popcon: I:0.3), provides a good online resource to the generic system administration.

[Note] Note

If you have been using any Unix-like system for a while with command line tools, you probably know everything I explain here. Please use this as a reality check and refresher.

2.1. Console basics

2.1.1. The shell prompt

Upon starting the system, you are presented with the character based login screen if you did not install X Window System with the display manager such as gdm. Suppose your hostname is foo, the login prompt looks like:

foo login:

If you did install a GUI environment such as Gnome of KDE, then you can get to a prompt by Ctrl-Alt-F1, and you can return to the GUI screen via Alt-F7 (see Virtual Consoles below for more information).

Following what you selected during the installation process, you type your username, e.g. penguin, and press the Enter-key, then type your password and press the Enter-key again.

[Note] Note

Following the Unix tradition, the username and password of the Debian system are case sensitive. The username is usually chosen only from the lowercase.

Now you are in the shell. The shell interprets your commands. The system starts with the greeting message stored in /etc/motd (Message Of The Day) and with the command prompt as:

Debian GNU/Linux lenny/sid foo tty1
foo login: penguin
Password:
Last login: Sun Apr 22 09:29:34 2007 on tty1
Linux snoopy 2.6.20-1-amd64 #1 SMP Sun Apr 15 20:25:49 UTC 2007 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
foo:~$

Here, the main part of the greeting message can be customized by editing the /etc/motd.tail file. The first line is generated from the system information using "uname -snrvm".

2.1.2. The shell prompt under X

If you installed X Window System with a display manager such as Gnome's gdm by selecting "Desktop environment" task during the installation, you will be presented with the graphical login screen upon starting your system. You type your username and your password to login to the non-privileged user account. Use tab to navigate between username and password, or use the mouse and primary click.

You can gain the shell prompt under X by starting a x-terminal-emulator program such as gnome-terminal, rxvt or xterm. Under the Gnome Desktop environment, clicking "Applications" -> "Accessories" -> "Terminal" does the trick.

You can also see the section below Section 2.1.6, “Virtual consoles”.

Under some other Desktop systems (like fluxbox), there may be no obvious starting point for the menu. If this happens, just try (right) clicking the center of the screen and hope for a menu to pop-up.

2.1.3. The root account

The root account is also called superuser or privileged user. From this account, you can perform the following system administration activities:

  • read, write, and remove any files on the system irrespective of their file permissions

  • set file ownership and permission of any files on the system

  • set the password of any non-privileged users on the system

  • login to any accounts without their passwords

This unlimited power of root account requires you to be considerate and responsible when using it.

[Note] Note

The file permission of the file (including hardware devices such as CD-ROM etc. which are just another file for the Debian system) may render them unusable or inaccessible by non-root users. Although the use of root account is a quick way to test this kind of situation, the resolution of this situation should be done through proper setting of the access permission and the group.

[Warning] Warning

Never share the root password with others.

2.1.4. The root shell prompt

Here are a few basic methods to gain the root shell prompt by using the root password:

  • At the character based login prompt, you simply type root.

  • Under the Gnome Desktop environment, click "Applications" -> "Accessories" -> "Root Terminal".

  • From any user shell prompt, type "su -l". (This does not preserve the environment of the current user)

  • From any user shell prompt, type "su". (This preserves most of the environment of the current user)

2.1.5. GUI system administration tools

When your desktop menu does not start GUI system administration tools automatically with the appropriate privilege, you can start them from the root shell prompt of the X terminal emulator, such as gnome-terminal, rxvt, or xterm. See Section 2.1.4, “The root shell prompt”.

[Warning] Warning

Never start the X server under the root account by typing in root to the prompt of the display manager such as gdm because it is considered unsafe (insecure), even when you plan to perform administrative activities. The entire X architecture is considered insecure if run as root. You must always use the lowest privilege level possible, like a regular user account.

[Warning] Warning

Never run untrusted remote GUI program under X window when critical information is displayed since it may eavesdrop your X screen.

2.1.6. Virtual consoles

In the default Debian system, there are six switchable VT100-like character consoles available to start the command shell directly on the Linux host. Unless you are in a GUI environment, you can switch between the virtual consoles by pressing the Left-Alt-key and one of the F1--F6 keys simultaneously. Each character console allows independent login to the account and offers the multiuser environment. This multiuser environment is a great Unix feature, and very addictive.

If you are under the X Window System, you gain access to the character console 1 by pressing Ctrl-Alt-F1 key, i.e., the left-Ctrl-key, the left-Alt-key, and the F1-key are pressed together. You can get back to the X Window System, normally running on the virtual console 7, by pressing Alt-F7.

You can alternatively change to another virtual console, e.g. to the console 1, by the command:

# chvt 1

2.1.7. How to leave the command prompt

You type Ctrl-D, i.e., the left-Ctrl-key and the d-key pressed together, at the command prompt to close the shell activity. If you are at the character console, you will return to the login prompt with this. Even though these control characters are referred as "control D" with the upper case, you do not need to press the Shift-key. The short hand expression, ^D, is also used for Ctrl-D. Alternately, you can type "exit".

If you are at x-terminal-emulator, you can close x-terminal-emulator window with this.

2.1.8. How to shutdown the system

Just like any other modern OS where the file operation involves caching data in memory for improved performance, the Debian system needs the proper shutdown procedure before power can safely be turned off. This is to maintain the integrity of files, by forcing all changes in memory to be written to disk. If the software power control is available, the shutdown procedure automatically turns off power of the system. (Otherwise, you may have to press power button for few seconds after the shutdown procedure.)

Under the normal multiuser mode, use following from the root command prompt to shutdown the system:

# shutdown -h now

Under the single-user mode, use following from the root command prompt to shutdown the system:

# poweroff -i -f

Alternatively, you may type Ctrl-Alt-Delete (The left-Ctrl-key, the left-Alt-Key, and the Delete are pressed together) to shutdown if /etc/inittab contains "ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -h now" in it. See manpage for inittab(5) for details.

2.1.9. Recovering a sane console

When the screen goes berserk after doing some funny things such as "cat <some-binary-file>", type "reset" at the command prompt. You may not be able to see the command echoed as you type. You may also issue "clear" to clean up the screen.

2.1.10. Additional package suggestions for the newbie

Although even the minimal installation of the Debian system without any desktop environment tasks provides the basic Unix functionality, it is a good idea to install few additional commandline and curses based character terminal packages such as mc and vim with aptitude for beginners to get started. From the shell prompt as root:

# aptitude update
...
# aptitude install mc vim sudo
...

If you already had these packages installed, nothing will be installed.

Table 2.1.  List of interesting text-mode program packages.

package

popcon

size

description

mc

V:11, I:25

6316

A text-mode full-screen file manager

vim

V:13, I:28

1720

Unix text editor Vi IMproved, a programmers text editor (standard version)

vim-tiny

V:16, I:87

824

Unix text editor Vi IMproved, a programmers text editor (compact version)

emacs21

V:5, I:11

8112

GNU project Emacs, the Lisp based extensible text editor (version 21)

emacs22

V:2, I:3

10892

GNU project Emacs, the Lisp based extensible text editor (version 22)

w3m

V:15, I:86

1968

Text-mode WWW browsers

gpm

V:4, I:5

560

The Unix style cut-and-paste on the text console (daemon)


It may be a good idea to read some informative documentations.

Table 2.2.  List of informative documentation packages.

package

popcon

size

description

doc-debian

I:84

376

Debian Project documentation, (Debian FAQ) and other documents

debian-policy

I:3

2732

Debian Policy Manual and related documents

developers-reference

I:1.4

1308

Guidelines and information for Debian developers

maint-guide

I:1.1

640

Debian New Maintainers' Guide

debian-history

I:0.5

2544

History of the Debian Project

debian-faq

I:0.4

1190

Debian FAQ

doc-linux-text

I:84

8620

Linux HOWTOs and FAQ (text)

doc-linux-html

I:1.2

62568

Linux HOWTOs and FAQ (html)

sysadmin-guide

I:0.4

964

The Linux System Administrators' Guide

rutebook

I:0.3

8264

Linux: Rute User's Tutorial and Exposition (non-free)


You can install some of these packages by issuing the following command from the root shell prompt:

# aptitude install package_name

2.1.11. An extra user account

If you do not want to use your main user account for the following training activities, you can create a training user account, e.g. fish. Type at root shell prompt:

# adduser fish
  • answer all the questions

This will create a new account named as fish. After your practice, you can remove this user account and its home directory by:

# deluser --remove-home fish

2.1.12. sudo configuration

For the typical single user workstation such as the desktop Debian system on the laptop PC, it is common to deploy simple configuration of sudo(8) as follows to let the non-privileged user, e.g. penguin, to gain administrative privilege just with his user password (not with the root password).

# echo "penguin  ALL=(ALL) ALL" >> /etc/sudoers

This trick should only be used for the single user workstation which you administer and where you are the only user.

[Warning] Warning

Do not set up accounts of regular users on multiuser workstation like this because it would be very bad for system security.

[Caution] Caution

The password and the account of the penguin in the above example requires as much protection as the root password and the root account.

[Caution] Caution

Administrative privilege in this context belongs to someone authorized to perform the system administration task on the workstation. Never give some manager in the Admin department of your company or your boss such privilege unless they are authorized and capable.

[Note] Note

For providing access privilege to limited devices and limited files, you should consider to use group to provide limited access instead of using the root privilege via sudo(8).

[Note] Note

With more thoughtful and careful configuration, the sudo facility may provide means to grant limited administrative privileges to other users on a shared system without sharing the root password. This can help with accountability with hosts with multiple administrators so you can tell who did what. On the other hand, you might not want anyone else to have such privileges.

2.1.13. Play time

Now you are ready to play with the Debian system without risks as long as you use the non-privileged user account.

This is because the Debian system is, even after the default installation, configured with the proper file permissions which prevent non-privileged users from damaging the system. Of course, there may still be some holes which can be exploited but those who worry about these issues should not be reading this section but should be reading Securing Debian Manual.

We will review basic Unix filesystem theory first. Then we will learn the Debian system with the easy way using Midnight Commander (MC) and with the proper Unix-like ways.

2.2. Unix-like filesystem

In GNU/Linux and other Unix-like operating systems, files are organized into directories. All files and directories are arranged in one big tree, the file hierarchy, rooted at /. It's called a tree because if you draw the file system, it looks like a tree (upside down).

These files and directories can be spread out over several devices. The mount(8) command serves to attach the file system found on some device to the big file tree. Conversely, the umount(8) command will detach it again. On recent Linux kernels, mount(8) operation can bind part of the file hierarchy somewhere else or can mount filesystem as shared, private, slave, or unbindable. Supported mount options for each filesystem are available in /share/doc/linux-doc-2.6.*/Documentation/filesystems/.

Directories on Unix systems are called folders on some other systems. Please also note that there is no concept for drive such as A: on any Unix system. There is one file system, and everything is included. This is a huge advantage compared to Windows, so consider yourself lucky.

2.2.1. Unix file basics

Here are Unix file basics:

  • Filenames are case sensitive. That is, MYFILE and MyFile are different files.

  • The root directory is referred to as simply /. Don't confuse this with the root user or the home directory for the root user: /root. Note that both entities are commonly referred to as "root". The context of the usage should make it clear which is meant.

  • Every directory has a name which can contain any letters or symbols except /. The root directory is an exception; its name is / (pronounced "slash" or "the root directory") and it cannot be renamed.

  • Each file or directory is designated by a fully-qualified filename, absolute filename, or path, giving the sequence of directories which must be passed through to reach it. The three terms are synonymous.

  • All fully-qualified filenames begin with the / directory, and there's a / between each directory or file in the filename. The first / is the top level directory, and the other /'s separate successive subdirectories, until we reach the last entry which is the name of the actual file. The words used here can be confusing. Take the following fully-qualified filename as an example: /usr/share/keytables/us.map.gz. However, people will also refer to its basename us.map.gz alone as a filename.

  • The root directory has a number of branches, such as /etc/ and /usr/. These subdirectories in turn branch into still more subdirectories, such as /etc/init.d/ and /usr/local/. The whole thing viewed collectively is called the directory tree. You can think of an absolute filename as a route from the base of the tree (/) to the end of some branch (a file). You will also hear people talk about the directory tree as if it were a family tree: thus subdirectories have parents, and a path shows the complete ancestry of a file. There are also relative paths that begin somewhere other than the root directory. You should remember that the directory ../ refers to the parent directory. This also applies to other directory like structures, such as data structures or other tree hierarchical organized entities.

  • There's no special directory path name component that corresponds to a physical device, such as your hard disk. This differs from RT-11, CP/M, OpenVMS, MS-DOS, AmigaOS, and Microsoft Windows, where the path contains a device name such as C:\. (However, directory entries do exist that refer to physical devices as a part of the normal filesystem. See Section 2.2.2, “Filesystem internals”.)

[Note] Note

While you can use almost any letters or symbols in a file name, in practice it is a bad idea to do so. It is better to avoid any characters that often have special meanings on the command line, including spaces, tabs, newlines, and other special characters: { } ( ) [ ] ' ` " \ / > < | ; ! # & ^ * % @ $ . If you want to separate words in a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, "LikeThis". Experienced Linux users tend to avoid spaces in filenames.

[Note] Note

The word path is used not only for fully-qualified filename as above but also for the command search path. The intended meaning is usually clear from the context.

The detailed best practices for the file hierarchy are described in the Filesystem Hierarchy Standard (/usr/share/doc/debian-policy/fhs/fhs-2.3.txt.gz and hier(7)). You should remember the following facts as the starter:

Table 2.3.  List of usage of key directories.

directory

usage

/

A simple / represents the root directory.

/etc/

This is the place for the system wide configuration files.

/var/log/

This is the place for the system log files.

/home/

This is the directory which contains all the home directories for all non-privileged users.


2.2.2. Filesystem internals

Following the Unix tradition, the Debian GNU/Linux system provides the filesystem under which physical data on harddisks and other storage devices reside, and the interaction with the hardware devices such as console screens and remote serial consoles are represented in an unified manner [under /dev/].

Each file, directory, named pipe (a way two programs can share data), or physical device on a Debian GNU/Linux system has a data structure called an inode which describes its associated attributes such as the user who owns it (owner), the group that it belongs to, the time last accessed, etc. If you are really interested, see /usr/include/linux/fs.h for the exact definition of struct inode in the Debian GNU/Linux system. The idea of representing just about everything in the file system was a Unix innovation, and modern Linux kernels have developed this idea ever further. Now, even information about processes running in the computer can be found in the file system.

This abstract and unified representation of physical entities and internal processes is very powerful since this allows us to use the same command for the same kind of operation on many totally different devices. It is even possible to change the way the kernel works by writing data to special files that are linked to running processes.

All of your files could be on one disk --- or you could have 20 disks, some of them connected to a different computer elsewhere on the network. You can't tell just by looking at the directory tree, and nearly all commands work just the same way no matter what physical device(s) your files are really on. [This is a good thing. Trust us.] Of course, methods do exist whereby you can tell what devices map to which physical or network devices. mount with no arguments will show how storage is mapped to physical or network devices.

2.2.3. The filesystem permission system

The filesystem permissions of Unix-like system are defined for three categories of affected users:

  • the user who owns the file (u),

  • other users in the group which the file belongs to (g), and

  • all other users (o) also referred to as "world" and "everyone".

For the file, each corresponding permission allows:

  • read (r): to examine contents of the file,

  • write (w): to modify the file, and

  • execute (x): to run the file as a command.

For the directory, each corresponding permission allows:

  • read (r): to list contents of the directory,

  • write (w): to add or remove files in the directory, and

  • execute (x): to access files in the directory.

Here, execute permission on the directory means not only to allow reading of files in its directory but also to allow viewing their attributes, such as the size and the modification time.

To display permission information (and more) for files and directories, ls(1) is used. When ls invoked with the -l option, it displays the following information in the order given:

  • the type of file (first character)

  • the file's access permissions (the next nine characters, consisting of three characters each for user, group, and other in this order)

  • the number of hard links to the file

  • the name of the user who owns the file

  • the name of the group which the file belongs to

  • the size of the file in characters (bytes)

  • the date and time of the file (mtime)

  • the name of the file.

Table 2.4.  List of the first character of "ls -l" output

character

meaning

-

normal file

d

directory

l

symlink

c

character device node

b

block device node

p

named pipe

s

socket


To change the owner of the file, chown(1) is used from the root account. To change the group of the file, chgrp(1) is used from the file's owner or root account. To change file and directory access permissions, chmod(1) is used from the file's owner or root account. Basic syntax to manipulate foo file is:

# chown <newowner> foo
# chgrp <newgroup> foo
# chmod  [ugoa][+-=][rwx][,...] foo

For example, in order to make a directory tree to be owned by a user foo and shared by a group bar, issue the following commands from the root account:

# cd /some/location/
# chown -R foo:bar .
# chmod -R ug+rwX,o=rX .

There are three more special permission bits:

  • set user ID (s or S instead of user's x),

  • set group ID (s or S instead of group's x), and

  • sticky bit (t or T instead of other's x).

Here the output of ls -l for these bits is capitalized if execution bits hidden by these outputs are unset.

Setting set user ID on an executable file allows a user to execute the executable file with the owner ID of the file (for example root). Similarly, setting set group ID on an executable file allows a user to execute the executable file with the group ID of the file (for example root). Because these settings can cause security risks, enabling them requires extra caution.

Setting set group ID on a directory enables the BSD-like file creation scheme where all files created in the directory belong to the group of the directory.

Setting the sticky bit on a directory prevents a file in the directory from being removed by a user who is not the owner of the file. In order to secure the contents of a file in world-writable directories such as /tmp or in group-writable directories, one must not only set write permission off for the file but also set the sticky bit on the directory. Otherwise, the file can be removed and a new file can be created with the same name by any user who has write access to the directory.

Here are a few interesting examples of the file permissions.

$ ls -l /etc/passwd /etc/shadow /dev/ppp /usr/sbin/exim4
crw------- 1 root root   108, 0 2007-04-29 07:00 /dev/ppp
-rw-r--r-- 1 root root     1427 2007-04-16 00:19 /etc/passwd
-rw-r----- 1 root shadow    943 2007-04-16 00:19 /etc/shadow
-rwsr-xr-x 1 root root   700056 2007-04-22 05:29 /usr/sbin/exim4
$ ls -ld /tmp /var/tmp /usr/local /var/mail /usr/src
drwxrwxrwt 10 root root  4096 2007-04-29 07:59 /tmp
drwxrwsr-x 10 root staff 4096 2007-03-24 18:48 /usr/local
drwxrwsr-x  4 root src   4096 2007-04-27 00:31 /usr/src
drwxrwsr-x  2 root mail  4096 2007-03-28 23:33 /var/mail
drwxrwxrwt  2 root root  4096 2007-04-29 07:11 /var/tmp

There is an alternative numeric mode to describe file permissions in chmod(1) commands. This numeric mode uses 3 to 4 digit wide octal (radix=8) numbers.

Table 2.5.  The numeric mode for file permissions in chmod(1) commands.

digit

meaning

1st optional digit

sum of set user ID (=4), set group ID (=2), and sticky bit (=1)

2nd digit

sum of read (=4), write (=2), and execute (=1) permissions for user

3rd digit

ditto for group

4th digit

ditto for other


This sounds complicated but it is actually quite simple. If you look at the first few (2-10) columns from "ls -l" command output and read it as a binary (radix=2) representation of file permissions ("-" being "0" and "rwx" being "1"), the last 3 digit of the numeric mode value should make sense as an octal (radix=8) representation of file permissions to you. For example, try:

$ touch foo bar
$ chmod u=rw,go=r foo
$ chmod 644 bar
$ ls -l foo bar
-rw-r--r-- 1 penguin penguin 17 2007-04-29 08:22 bar
-rw-r--r-- 1 penguin penguin 12 2007-04-29 08:22 foo

2.2.4. Control of permissions for newly created files: umask

What permissions are applied to a newly created file or directory is restricted by the umask shell built-in command. See dash(1), bash(1), and builtins(7).

 (file permission) = (requested file permission) & ~(umask value)

Table 2.6.  The umask value examples.

umask

usage

file permission created

directory permission created

0022

writable only by the user

-rw-rw-r--

-rwxrwxr-x

0002

writable by the group

-rw-r--r--

-rwxr-xr-x


The Debian system uses a user private group (UPG) scheme as its default. A UPG is created whenever a new user is added to the system. A UPG has the same name as the user for which it was created and that user is the only member of the UPG. UPGs makes it is safe to set umask to 0002 since every user has their own private group. (In some Unix variants, it is quite common to setup all normal users belonging to a single users group and is good idea to set umask to 0022 for security in such cases.)

2.2.5. Permissions for groups of users (group)

In order to make the group permission to be applied to a particular user, that user needs to be made a member of the group using "sudo vigr".

[Note] Note

Alternatively, you may dynamically add users to groups during the authentication process by adding "auth optional pam_group.so" line to /etc/pam.d/common-auth and setting /etc/security/group.conf. (See more: Chapter 5, Authentication.)

The hardware devices are just another kind of file on the Debian system. If you have problems accessing devices such as CD-ROM and USB memory stick from a user account, you should make that user a member of the relevant group.

Some notable system-provided groups allow their members to access particular files and devices without root privilege.

Table 2.7.  List of example system-provided groups for file access.

group

accessible files and devices

dialout

Full and direct access to serial ports. (reconfigure modem, dial anywhere, etc.)

dip

"Dialup IP", enough to run ppp or dip commands.

cdrom

CD-ROM, DVD+/-RW drives.

audio

An audio device.

video

A video device.

scanner

Scanner(s).

adm

System monitoring logs.

staff

Some directories for junior administrative work: /usr/local, /home .


Some notable system provided groups allow their members to execute particular commands without root privilege.

Table 2.8.  List of notable system provided groups for particular command executions.

group

accessible commands

sudo

execute sudo without their password.

lpadmin

execute commands to add, modify, and remove printers from printer databases.

plugdev

execute pmount(1) for removable devices such as USB memories.


For the full listing of the system provided users and groups, see the recent version of the "Users and Groups" (/usr/share/doc/base-passwd/users-and-groups.html) document provided by the base-passwd package.

See manpages of passwd(5), group(5), shadow(5), group(5), vipw(8), vigr(8), and pam_group(8) for the management commands of the user and group system.

2.2.6. Timestamps

There are three types of timestamps for a GNU/Linux file.

Table 2.9.  List of types of timestamps.

type

meaning

mtime

the file modification time (ls -l)

ctime

the file status change time (ls -lc)

atime

the last file access time (ls -lu)


Note that ctime is not file creation time.

  • Overwriting a file will change all of the mtime, ctime, and atime attributes of the file.

  • Changing permission or owner of a file will change the ctime and atime attributes of the file.

  • Reading a file will change the atime of the file.

Note that even simply reading a file on the Debian system will normally cause a file write operation to update atime information in the inode. Mounting a filesystem with the noatime option will let the system skip this operation and will result in faster file access for the read. This is often recommended for laptops, because it reduces hard drive activity and saves power. See mount(8).

Use touch(1) command to change timestamps of existing files.

For timestamps, the ls command outputs different strings under the modern English locale (en_US.UTF-8) from under the old one (C).

$ LANG=en_US.UTF-8  ls -l foo
-rw-r--r-- 1 penguin penguin 3 2008-03-05 00:47 foo
$ LANG=C  ls -l foo
-rw-r--r-- 1 penguin penguin 3 Mar  5 00:47 foo

2.2.7. Links

There are two methods of associating a file foo with a different filename bar.

  • a hard link is a duplicate name for an existing file ("ln foo bar"),

  • a symbolic link, or "symlink", is a special file that points to another file by name ("ln -s foo bar").

See the following example for the changes in link counts and the subtle differences in the result of the rm command.

$ echo "Original Content" > foo
$ ls -li foo
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 foo
$ ln foo bar     # hard link
$ ln -s foo baz  # symlink
$ ls -li foo bar baz
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 foo
$ rm foo
$ echo "New Content" > foo
$ ls -li foo bar baz
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398540 -rw-r--r-- 2 penguin penguin 12 2007-04-29 08:17 foo
$ cat bar
Original Content
$ cat baz
New Content

The hardlink can be made within the same file system and shares the same inode number which the "-i" option with ls command reveals.

The symlink always has nominal file access permissions of "rwxrwxrwx", as shown in the above example, with the effective access permissions dictated by the permissions of the file that it points to.

[Caution] Caution

It is generally good idea not to create complicated symbolic links or hardlinks at all unless you have a very good reason. It may cause nightmares where the logical combination of the symbolic links results in loops in the filesystem.

[Note] Note

It is generally preferable to use symbolic links rather than hardlinks unless you have a good reason for using a hardlink.

The "." directory links to the directory that it appears in, thus the link count of any new directory starts at 2. The ".." directory links to the parent directory, thus the link count of the directory increases with the addition of new subdirectories.

If you are just moving to Linux from Windows, it will soon become clear how well-designed the file-name linking of Unix is, compared with the nearest Windows equivalent of "shortcuts". Because it is implemented in the file system, applications can't see any difference between a linked file and the original. In the case of hardlinks, there really is no difference.

2.2.8. Named pipes (FIFOs)

A named pipe is a file that acts like a pipe. You put something into the file, and it comes out the other end. Thus it's called a FIFO, or First-In-First-Out: the first thing you put in the pipe is the first thing to come out the other end.

If you write to a named pipe, the process which is writing to the pipe doesn't terminate until the information being written is read from the pipe. If you read from a named pipe, the reading process waits until there is something to read before terminating. The size of the pipe is always zero --- it does not store data, it just links two processes like the shell "|". However, since this pipe has a name, the two processes don't have to be on the same command line or even be run by the same user. Pipes were a very influential innovation of Unix.

You can try it by doing the following:

$ cd; mkfifo mypipe
$ echo "hello" >mypipe & # put into background
[1] 8022
$ ls -l mypipe
prw-r--r-- 1 penguin penguin 0 2007-04-29 08:25 mypipe
$ cat mypipe
hello
[1]+  Done                    echo "hello" >mypipe
$ ls mypipe
mypipe
$ rm mypipe

2.2.9. Sockets

Sockets are used extensively by all the Internet communication, databases, and the operating system itself. It is similar to the named pipe (FIFO) and allows processes to exchange information even between different computers. For the socket, those processes do not need to be running at the same time nor to be running as the children of the same ancestor process. This is the endpoint for the inter process communication (IPC). The exchange of information may occur over the network between different hosts. The two most common ones are the Internet socket and the Unix domain socket.

[Tip] Tip

"netstat -an" will provide a very useful overview of the sockets that are open on a given system.

2.2.10. Device files

Device files refer to physical or virtual devices on your system, such as your hard disk, video card, screen, or keyboard. An example of a virtual device is the console, represented by /dev/console.

Table 2.10.  The device types.

device type

meaning

character device

This can be accessed one character at a time, that is, the smallest unit of data which can be written to or read from the device is a character (byte).

block device

This must be accessed in larger units called blocks, which contain a number of characters. Your hard disk is a block device.


You can read and write device files, though the file may well contain binary data which may be an incomprehensible-to-humans gibberish. Writing data directly to these files is sometimes useful for the troubleshooting of hardware connections. For example, you can dump a text file to the printer device /dev/lp0 or send modem commands to the appropriate serial port /dev/ttyS0. But, unless this is done carefully, it may cause a major disaster. So be cautious.

[Note] Note

For the normal access to a printer, use the lp(1) command.

The device node number are displayed by executing ls as:

$ ls -l /dev/hda /dev/ttyS0 /dev/zero
brw-rw---- 1 root cdrom   3,  0 2007-04-29 07:00 /dev/hda
crw-rw---- 1 root dialout 4, 64 2007-04-29 07:00 /dev/ttyS0
crw-rw-rw- 1 root root    1,  5 2007-04-29 07:00 /dev/zero

Here,

  • /dev/hda has the major device number 3 and the minor device number 0. This is read/write accessible by the user who belongs to disk group,

  • /dev/ttyS0 has the major device number 4 and the minor device number 64. This is read/write accessible by the user who belongs to dialout group, and

  • /dev/zero has the major device number 1 and the minor device number 5. This is read/write accessible by anyone.

In the Linux 2.6 system, the filesystem under /dev is automatically populated by the udev(7) mechanism.

2.2.11. Special device files

There are some special device files.

Table 2.11.  List of special device files.

device file

action

response

/dev/null

read

it returns "end-of-file (EOF) character".

/dev/null

write

it is a bottomless data dump pit.

/dev/zero

read

it returns "the \0 (NUL) character" (not the same as the number zero ASCII).

/dev/random

read

it returns random characters from a true random number generator, delivering real entropy. (slow)

/dev/urandom

read

it returns random characters from a cryptographically secure pseudorandom number generator.

/dev/full

write

it returns the disk-full (ENOSPC) error.


These are frequently used in conjunction with the shell redirection (see Section 2.5.5, “Typical command sequences and shell redirection”).

2.2.12. procfs and sysfs

The procfs and sysfs mounted on /proc and /sys are the pseudo-filesystem and expose internal data structures of the kernel to the userspace. In other word, these entries are virtual, meaning that they act as a convenient window into the operation of the operating system.

The directory /proc contains (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID).

System utilities that access process information, such as ps, get their information from this directory structure.

The directories under /proc/sys/ contain interface to change certain kernel parameters at run time. (You may do the same through specialized command sysctl(8) or its preload/configuration file /etc/sysctrl.conf.)

[Note] Note

The Linux kernel may complain "Too many open files". You can fix avoid this by executing "echo "65536" > /proc/sys/fs/file-max" from the root shell to increase file-max value.

People frequently panic when they notice one file in particular - /proc/kcore - which is generally huge. This is (more or less) a copy of the contents of your computer's memory. It's used to debug the kernel. It is a virtual file that points to computer memory, so don't worry about its size.

The directory under /sys contains exported kernel data structures, their attributes, and the linkages between them. It also contains interface to change certain kernel parameters at run time.

See proc.txt(.gz), sysfs.txt(.gz) and other related documents in the Linux kernel documentation (/usr/share/doc/linux-doc-2.6.*/Documentation/filesystems/*) provided by the linux-doc-2.6.* package.

2.3. Midnight Commander (MC)

Midnight Commander (MC) is a GNU "Swiss army knife" for the Linux console and other terminal environments. This gives newbie a menu driven console experience which is much easier to learn than standard Unix commands.

You may need to install the Midnight Commander package which is titled mc.

 sudo aptitude install mc                                                                                                   

Use the mc command to explore the Debian system. This is the best way to learn. Please explore few interesting locations just using the cursor keys and Enter key:

  • /etc and its subdirectories.

  • /var/log and its subdirectories.

  • /usr/share/doc and its subdirectories.

  • /sbin and /bin

2.3.1. Customization of MC

In order to make MC to change working directory upon exit and cd to the frequently used directories, I suggest to modify ~/.bashrc to include:

. /usr/share/mc/bin/mc.sh                                                                                                        

See mc(1) (under the "-P" option) for the reason. (If you do not understand what exactly I am talking here, you can do this later.)

2.3.2. Starting MC

MC can be started by:

$ mc

MC takes care of all file operations through its menu, requiring minimal user effort. Just press F1 to get the help screen (see next paragraph if this doesn't work). You can play with MC just by pressing cursor-keys and function-keys.

In some consoles such as gnome-terminal, key strokes of function-keys may be stolen by the console program. You can disable these features by "Edit" -> "Keyboard Shortcuts" for gnome-terminal. In particular, consider removing the mapping of F1 to Gnome Terminal Help; doing so will allow F1 to show Midnight Commander help.

If you encounter character encoding problem which displays garbage characters, adding "-a" to MC's command line may help prevent problems.

If this doesn't clear up your display problems with MC, see Section 10.3.5, “The terminal configuration”.

2.3.3. File manager in MC

The default is two directory panels containing file lists. Another useful mode is to set the right window to "information" to see file access privilege information, etc. Following are some essential keystrokes. With the gpm daemon running, one can use a mouse, too. (Make sure to press the shift-key to obtain the normal behavior of cut and paste in MC.)

Table 2.12.  The key bindings of MC.

key

key binding

F1

Help menu

F3

Internal file viewer

F4

Internal editor

F9

Activate pull down menu

F10

Exit Midnight Commander

Tab

Move between two windows

Insert or Ctrl-T

Mark file for a multiple-file operation such as copy

Del

Delete file (be careful---set MC to safe delete mode)

Cursor keys

Self-explanatory


2.3.4. Command-line tricks in MC

  • Any cd command will change the directory shown on the selected screen.

  • Ctrl-Enter or Alt-Enter will copy a filename to the command line. Use this with the cp or mv command together with command-line editing.

  • Alt-Tab will show shell filename expansion choices.

  • One can specify the starting directory for both windows as arguments to MC; for example, mc /etc /root.

  • Esc + n-key == Fn (i.e., Esc + 1 = F1, etc.; Esc + 0 = F10)

  • Pressing Esc before the key has the same effect as pressing the Alt and the key together.; i.e., type Esc + c for Alt-C. Esc is called meta-key and sometimes noted as "M-"

2.3.5. The internal editor in MC

The internal editor has an interesting cut-and-paste scheme. Pressing F3 marks the start of a selection, a second F3 marks the end of selection and highlights the selection. Then you can move your cursor. If you press F6, the selected area will be moved to the cursor location. If you press F5, the selected area will be copied and inserted at the cursor location. F2 will save the file. F10 will get you out. Most cursor keys work intuitively.

This editor can be directly started on a file:

$ mc -e filename_to_edit
$ mcedit filename_to_edit

This is not a multi-window editor, but one can use multiple Linux consoles to achieve the same effect. To copy between windows, use Alt-F<n> keys to switch virtual consoles and use "File->Insert file" or "File->Copy to file" to move a portion of a file to another file.

This internal editor can be replaced with any external editor of choice.

Also, many programs use the environment variables EDITOR or VISUAL to decide which editor or viewer to use. If you are uncomfortable with vim or nano initially, you may set these to mcedit by adding these lines to ~/.bashrc:

...
export EDITOR=mcedit
export VISUAL=mcedit
...

I do recommend setting these to vim if possible.

If you are uncomfortable with vim, you can keep using mcedit for most system maintenance tasks.

2.3.6. The internal viewer in MC

Very smart viewer. This is a great tool for searching words in documents. I always use this for files in the /usr/share/doc directory. This is the fastest way to browse through masses of Linux information. This viewer can be directly started like so:

$ mc -v path/to/filename_to_view
$ mcview path/to/filename_to_view

2.3.7. Auto-start features of MC

Press Enter on a file, and the appropriate program will handle the content of the file. This is a very convenient MC feature.

Table 2.13.  The reaction to the enter key in MC.

file type

reaction to enter key

executable file

Execute command

man file

Pipe content to viewer software

html file

Pipe content to web browser

tar.gz.deb file

Browse its contents as if subdirectory


In order to allow these viewer and virtual file features to function, viewable files should not be set as executable. Change their status using the chmod command or via the MC file menu.

2.3.8. FTP virtual filesystem of MC

MC can be used to access files over the Internet using FTP. Go to the menu by pressing F9, then type "p" to activate the FTP virtual filesystem. Enter a URL in the form "username:passwd@hostname.domainname", which will retrieve a remote directory that appears like a local one.

Try "http.us.debian.org/debian" as the URL and browse the Debian archive.

2.4. The basic Unix-like work environment

Although MC enables you to do almost everything, it is very important for you to learn how to use the command line tools invoked from the shell prompt and become familiar with the Unix-like work environment.

2.4.1. The login shell

You can select your login shell with the chsh command.

Table 2.14.  List of shell programs.

package

popcon

size

POSIX shell

description

bash

V:86, I:99

1300

Yes

The GNU Bourne Again SHell. (de facto standard)

tcsh

V:11, I:85

736

No

TENEX C Shell, an enhanced version of Berkeley csh.

dash

V:3, I:14

232

Yes

The Debian Almquist Shell. Good for shell script.

zsh

V:2, I:5

11728

Yes

The standard shell with many enhancements.

pdksh

V:0.3, I:1.5

464

Yes

A public domain version of the Korn shell.

csh

V:0.3, I:1.4

400

No

OpenBSD C Shell, a version of Berkeley csh.

sash

V:0.3, I:1.2

848

Yes

Stand-alone shell with built-in commands. (Not meant for standard /bin/sh.)

ksh

V:0.3, I:1.2

2540

Yes

The real, AT&T version of the Korn shell.

rc

V:0.08, I:0.5

204

No

An implementation of the AT&T Plan 9 shell.

posh

V:0.01, I:0.16

232

Yes

Policy-compliant Ordinary SHell. A pdksh derivative.

es

V:0.03, I:0.06

304

No

An extensible shell based on the AT&T Plan 9 shell: rc.


In this tutorial chapter, the interactive shell always means bash.

2.4.2. Customizing bash

You can customize bash behavior by ~/.bashrc. For example, I added followings to ~/.bashrc:

# CD upon exiting MC
. /usr/share/mc/bin/mc.sh

# set CDPATH to good one
CDPATH=.:/usr/share/doc:~/Desktop/src:~/Desktop:~
export CDPATH

PATH="${PATH}":/usr/sbin:/sbin
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
  PATH=~/bin:"${PATH}"
fi
export PATH

EDITOR=vim
export EDITOR

2.4.3. Special key strokes

In the Unix-like environment, there are few key strokes which have special meanings. Please note that on a normal Linux character console, only the left-hand Ctrl and Alt keys work as expected. Here are few notable key strokes to remember.

Table 2.15.  List of key bindings for bash.

key

key binding

Ctrl-U

Erase line before cursor.

Ctrl-H

Erase a character before cursor.

Ctrl-D

Terminate input. (exit shell if you are using shell)

Ctrl-C

Terminate a running program.

Ctrl-Z

Temporarily stop program by moving it to the background job

Ctrl-S

Halt output to screen.

Ctrl-Q

Reactivate output to screen.

Ctrl-Alt-Del

Reboot/halt the system, see manpage for inittab.

Left-Alt-key (optionally, Windows-key)

Meta-key for Emacs and the similar UI.

Up-arrow

Start command history search under bash.

Ctrl-R

Start incremental command history search under bash.

Tab

Complete input of the filename to the command line under bash.

Ctrl-V Tab

Input Tab without expansion to the command line under bash.


[Tip] Tip

The terminal feature of Ctrl-S can be disabled using stty(1) command.

2.4.4. Unix style mouse operations

The Unix style mouse operations are based on the 3 button mouse system.

Table 2.16.  The Unix style mouse operations.

action

response

Left-click-and-drag mouse

Select and copy to the clipboard.

Left-click

Select the start of selection.

Right-click

Select the end of selection and copy to the clipboard.

Middle-click

Paste clipboard at the cursor.


The center wheel on the modern wheel mouse is considered middle mouse button and can be used for middle-click. Clicking left and right mouse buttons together serves as the middle-click under the 2 button mouse system situation. In order to use a mouse in the Linux character console, you need to have gpm running as daemon.

2.4.5. The pager

The less program is the enhanced pager (file content browser). Hit "h" for help. It can do much more than more. This less command can be supercharged by executing eval $(lesspipe) or eval $(lessfile) in the shell startup script. See more in /usr/share/doc/lessf/LESSOPEN. The -R option allows raw character output and enables ANSI color escape sequences. See less(1).

2.4.6. The text editor

You should become proficient in one of the variants of Vim or Emacs programs which are popular in the Unix-like system.

I think getting used to Vim commands is the right thing to do, since Vi-editor is always there in the Linux/Unix world. (Actually, original vi or new nvi are the programs you find everywhere. I chose Vim instead for newbie since it offers you help through F1 key while it is similar enough and more powerful.)

If you chose either Emacs or XEmacs instead as your choice of the editor, that is another good choice indeed, particularly for programming. Emacs has a plethora of other features as well, including functioning as a newsreader, directory editor, mail program, etc.. When used for programming or editing shell scripts, it intelligently recognizes the format of what you are working on, and tries to provide assistance. Some people maintain that the only program they need on Linux is Emacs. Ten minutes learning Emacs now can save hours later. Having the gnu Emacs manual for reference when learning Emacs is highly recommended.

All these programs usually come with tutoring program for you to learn them by practice. Start Vim by typing "vim" and press F1-key. You should at least read the first 35 lines. Then do the online training course by moving cursor to |tutor| and pressing Ctrl-].

[Note] Note

Good editors, such as Vim and Emacs, can be used to handle UTF-8 and other exotic encoding texts correctly with proper option in the x-terminal-emulator on X under UTF-8 locale with proper font settings. Please refer to their documentation on multibyte text.

2.4.7. Setting a default text editor

Debian comes with a number of different editors. We recommend to install the vim package, as mentioned above.

Debian provides unified access to the system default editor via command /usr/bin/editor so other programs (e.g., reportbug(1)) can invoke it. You can change it by:

$ sudo update-alternatives --config editor

The choice /usr/bin/vim.basic is the recommendation for newbies by the author. This supports syntax highlighting.

2.4.8. Customizing vim

You can customize vim behavior by ~/.vimrc. For example, I use:

" -------------------------------
" Local configuration
"
set nocompatible
set nopaste
set pastetoggle=<f2>
syn on
if $USER == "root"
 set nomodeline
 set noswapfile
else
 set modeline
 set swapfile
endif
" filler to avoid the line above being recognized as a modeline
" filler
" filler

2.4.9. Recording the shell activities

The output of the shell command may roll off your screen and may be lost forever. It is good practice to log shell activities into the file for you to review them later. This kind of record is essential when you perform any system administration tasks.

The basic method of recording the shell activity is to run it under the script(1) command.

$ script
Script started, file is typescript
  • do whatever shell commands ...

  • press Ctrl-D to exit script.

$ vim typescript

See Section 10.2.3, “Recording the shell activities cleanly” .

2.4.10. Basic Unix commands

Let's learn the basic Unix commands. Here I use "Unix" in its generic sense. Any Unix clone OSs usually offer the equivalent commands. The Debian system is no exception. Do not worry if some commands do not work as you wish now. If alias is used in the shell, its corresponding command outputs are different. These examples are not meant to be executed in this order.

Try all the following commands from the non-privileged user account:

Table 2.17.  List of basic Unix commands.

command

description

pwd

Display name of current/working directory.

whoami

Display current user name.

file <foo>

Display a type of file for the file <foo>.

type -p <commandname>

Display a file location of command <commandname>.

which <commandname>

, ,

type <commandname>

Display information on command <commandname>.

apropos <key-word>

Find commands related to <key-word>.

man -k <key-word>

, ,

whatis <commandname>

Display one line explanation on command <commandname>.

man -a <commandname>

Display explanation on command <commandname>. (Unix style)

info <commandname>

Display rather long explanation on command <commandname>. (GNU style)

ls

List contents of directory. (non-dot files and directories)

ls -a

List contents of directory. (all files and directories)

ls -A

List contents of directory. (almost all files and directories, i.e., skip ".." and ".")

ls -la

List all contents of directory with detail information.

ls -lai

List all contents of directory with inode number and detail information.

ls -d

List all directories under the current directory.

tree

Display file tree contents.

lsof <foo>

List open status of file <foo>.

mkdir <foo>

Make a new directory <foo> in the current directory.

rmdir <foo>

Remove a directory <foo> in the current directory.

cd <foo>

Change directory to the directory <foo> in the c