HPUNIX Сайт о ОС и не только!

Глава Восемь - Debian tips

20 апреля 2009 - unix
Глава Восемь - Debian tips

8.1 Booting the system

See the LDP BootPrompt-HOWTO for detailed information on the boot prompt.

8.1.1 "I forgot the root password!" (1)

It is possible to boot a system and log on to the root account without knowing the root password as long as one has access to the console keyboard. (This assumes there are no password requests from the BIOS or from a boot loader su/hdbk/ as lilo that would prevent one from booting the system.)

This is a procedure whi/hdbk/ requires no external boot disks and no /hdbk/ange in BIOS boot settings. Here, "Linux" is the label for booting the Linux kernel in the default Debian install.

At the lilo boot screen, as soon as boot: appears (you must press a shift key at this point on some systems to prevent automatic booting), enter:

Bash:
  1. boot: Linux init=/bin/sh

This causes the system to boot the kernel andn /bin/sh instead of its standard init.

Now you have gained root privileges and a root shell. Since / is currently mounted read-only and many disk partitions have not been mounted yet, you must do the following to have a reasonably functioning system.

Bash:
  1. init-2.03# mount -n -o remount,rw /
  2. init-2.03# mount -avt nonfs,noproc,nosmbfs
  3. init-2.03# cd /etc
  4. init-2.03# vi passwd
  5. init-2.03# vi shadow

(If the second data field in /etc/passwd is "x" for every username, your system uses shadow passwords, and you must edit /etc/shadow.) To disable the root password, edit the second data field in the password file so that it is empty.

Now the system can be rebooted and you can log on as root without a password. When booting intonlevel 1, Debian (at least after Potato) requires a password, whi/hdbk/ some older distributions did not.

It is a good idea to have a minimal editor in /bin/ in case /usr/ is not accessible (see Rescue editors, раздел 11.2).

Also consider installing the sash package. When the system becomes unbootable, execute:

Bash:
  1. boot: Linux init=/bin/sash

sash serves as an interactive substitute for sh even when /bin/sh is unusable.

It's statically linked, and includes many standard utilities as built-ins (type "help" at the prompt for a reference list).

8.1.2 "I forgot the root password!" (2)

Boot from any emergency boot/root disk set. If /dev/hda3 is the original root partition, the following will let one edit the password file just as easily as the above.

Bash:
  1. # mkdir fixit
  2. # mount /dev/hda3 fixit
  3. # cd fixit/etc
  4. # vi shadow
  5. # vi passwd

The advantage of this approa/hdbk/ over the previous method is one does not need to know the lilo password (if any).

But to use it one must be able to access the BIOS setup to allow the system to boot from floppy disk or CD, if that is not already set.

8.1.3 Cannot boot the system

No problem, even if you didn't bother to make a boot disk during install. If lilo is broken, grab the boot disk from the Debian installation set and boot your system from it. At the boot prompt, assuming the root partition of your Linux installation is on /dev/hda12 and you wantnlevel 3, enter:

Bash:
  1. boot: rescue root=/dev/hda12 3

Then you are booted into an almost fully functional system using the kernel on the floppy. (There may be minor glit/hdbk/es due to lack of kernel features or modules.)

See also Установка пакета в систему, которая не загружается, раздел 6.3.6 if you have a broken system.

If you need a custom boot floppy, follow readme.txt on the rescue disk.

8.1.4 "Let me disable X on boot!"

/hdbk/asing unstable/sid is fun, but buggy xdm, gdm, kdm, and wdm started during the boot process can bite you bad.

First get the root shell by entering the following at the boot prompt:

Bash:
  1. boot: Linux vga=normal s

Here, Linux is the label for the kernel image you are booting; "vga=normal" will make sure lilons in normal VGA screen, and "s" (or "S") is the parameter passed to init to invoke single-user mode.

Enter the root password at the prompt.

There are few ways to disable all the X starting daemons:

  • run update-rc.d ?dm stop Девяносто девять 1 Два 3 Четыре 5 6 .

  • insert "exit 0" at the start of all /etc/init.d/?dm files.

  • rename all /etc/rc2.d/S99?dm files to /etc/rc2.d/K99?dm.

  • remove all /etc/rc2.d/S99?dm files.

  • run :>/etc/X11/default-display-manager

Here, number in rc2.d must correspond to thenlevel specified in the /etc/inittab. Also ?dm means that you need ton the command multiple times by substituting it with all of the xdm, gdm, kdm, and wdm.

Only the first one in the list is "the one e way" in Debian. The last one is easy but only works on Debian and requires you to set the display manager again later using dpkg-reconfigure. Others are generic methods to disable daemons.

You can still start X with the startx command from any console shell.

8.1.5 Other boot tricks with the boot prompt

The system can be booted into a particularnlevel and configuration using the lilo boot prompt. Details are given in the BootPrompt-HOWTO (LDP).

If you want to boot the system intonlevel 4, use the following input at the lilo boot prompt.

Bash:
  1. boot: Linux 4

If you want to boot the system into normally functioning single-user mode and you know the root password, one of the following examples at the lilo boot prompt will work.

Bash:
  1. boot: Linux S
  2. boot: Linux 1
  3. boot: Linux -s

If you want to boot the system with less memory than system actually has (say 48MB for a system with 64MB), use this input at the lilo boot prompt:

Bash:
  1. boot: Linux mem=48M

Make sure not to specify more than the actual memory size here, otherwise the kernel will crash. If one has more than 64MB of memory, e.g.

128MB, unless one executes mem=128M at the boot prompt or includes a similar append line in /etc/lilo.conf, old kernels and/or a motherboard with an old BIOS will not use memory beyond 64MB.

8.1.6 Setting B boot parameters

B is a new boot manager from the GNU Hurd project and is mu/hdbk/ more flexible than Lilo but has slightly different handling of boot parameters.

Bash:
  1. b> find /vmlinuz
  2. b> root (hd0,0)
  3. b> kernel /vmlinuz root=/dev/hda1
  4. b> initrd /initrd
  5. b> boot

Here, you must be aware of the Hurd device names:

Bash:
  1. the Hurd/B Linux MSDOS/Windows
  2. (fd0) /dev/fd0 A:
  3. (hd0,0) /dev/hda1 C: (usually)
  4. (hd0,3) /dev/hda4 F: (usually)
  5. (hd1,3) /dev/hdb4 ?

See /usr/share/doc/b/README.Debian.gz and /usr/share/doc/b-doc/html/ for details.

8.2 Recording activities

8.2.1 Recording shell activities

System administration involves mu/hdbk/ more elaborate tasks in a Unix environment than in an ordinary personal computer environment. Make sure to know the most basic means of configuration in case you need to recover from system trouble. X11-based GUI configuration tools look nice and convenient but are often unsuitable in these emergency situations.

In this context, recording shell activities is a good practice, especially as root.

Emacs: Use M-x shell to start recording into a buffer, and use C-x C-w to write the buffer to a file.

Shell: Use the screen command with "^A H" as described in Console swit/hdbk/ing with screen, раздел 8.6.27; or use the script command.

Bash:
  1. $ script
  2. Script started, file is typescript
  3. ... do whatever ...
  4. Ctrl-D
  5. $ col -bx <typescript >savefile
  6. $ vi savefile

The following can be used instead of script:

Bash:
  1. $ bash -i 2>&1 | tee typescript

8.2.2 Recording X activities

If you need to record the graphic image of an X application, including an xterm display, use gimp (GUI).

It can capture ea/hdbk/ window or the whole screen. Alternatives are xwd (xbase-clients), import (imagemagick), and scrot (scrot).

8.3 Copy and ar/hdbk/ive a whole subdirectory

8.3.1 Basic commands for copying a whole subdirectory

If you need to rearrange file scture, move content including file links by:

Bash:
  1. Standard method:
  2. # cp -a /source/directory /dest/directory # requires GNU cp
  3. # (cd /source/directory && tar cf - . ) | \
  4. (cd /dest/directory && tar xvfp - )
  5. If a hard link is involved, a pedantic method is needed:
  6. # cd /path/to/old/directory
  7. # find . -depth -print0 | afio -p -xv -0a /mount/point/of/new/directory
  8. If remote:
  9. # (cd /source/directory && tar cf - . ) | \
  10. ssh user@host.dom (cd /dest/directory && tar xvfp - )
  11. If there are no linked files:
  12. # scp -pr user1@host1.dom:/source/directory \
  13. user2@host2.dom:/dest/directory

Here, scp <==> rcp and ssh <==> rsh.

The following comparative information on copying a whole subdirectory was presented by Manoj Srivastava srivasta@debian.org to debian-user@lists.debian.org.

Глава Восемь - Debian tips

8.3.2 cp

Traditionally, cp was not really a candidate for this task since it did not dereference symbolic links, or preserve hard links either. Another thing to consider was sparse files (files with holes).

GNU cp has overcome these limitations; however, on a non-GNU system, cp could still have problems. Also, you can't generate small, portable ar/hdbk/ives using cp.

Bash:
  1. % cp -a . newdir

8.3.3 tar

Tar overcame some of the problems that cp had with symbolic links.

However, although cpio handles special files, traditional tar doesn't.

tar's way of handling multiple hard links to a file places only one copy of the link on the tape, but the name atta/hdbk/ed to that copy is the only one you can use to retrieve the file; cpio's way puts one copy for every link, but you can retrieve it using any of the names.

The tar command /hdbk/anged its option for .bz2 files between Potato and Woody, so use --bzip2 in scripts instead of its short form -I (Potato) or -j (Woody).

8.3.4 pax

The new, POSIX (IEEE Std 1003.2-1992, pages 380-388 (section 4.48) and pages 936-940 (section E.4.48)), all-singing, all-dancing, Portable Ar/hdbk/ive Inter/hdbk/ange utility. pax will read, write, and list the members of an ar/hdbk/ive file, and will copy directory hierar/hdbk/ies. pax operation is independent of the specific ar/hdbk/ive format, and supports a wide variety of different ar/hdbk/ive formats.

pax implementations are still new and wet behind the ears.

Bash:
  1. # apt-get install pax
  2. $ pax -rw -p e . newdir
  3. or
  4. $ find . -depth | pax -rw -p e newdir

8.3.5 cpio

cpio copies files into or out of a cpio or tar ar/hdbk/ive.

The ar/hdbk/ive can be another file on the disk, a magnetic tape, or a pipe.

Bash:
  1. $ find . -depth -print0 | cpio --null --sparse -pvd new-dir

8.3.6 afio

afio is a better way of dealing with cpio-format ar/hdbk/ives. It is generally faster than cpio, provides more diverse magnetic tape options and deals somewhat gracefully with input data coption. It supports multivolume ar/hdbk/ives during interactive operation. afio can make compressed ar/hdbk/ives that are mu/hdbk/ safer than compressed tar or cpio ar/hdbk/ives.

afio is best used as an "ar/hdbk/ive engine" in a backup script.

Bash:
  1. $ find . -depth -print0 | afio -px -0a new-dir

All my backups onto tape use afio.

8.4 Differential backup and data syn/hdbk/ronization

Differential backup and data syn/hdbk/ronization can be implemented with several methods:

  • rcs: backup and history, text-only

  • rdiff-backup: backup and history. symlink OK.

  • pdumpfs: backup and history within a filesystem. symlink OK

  • rsync: 1-way syn/hdbk/ronization

  • unison: 2-way syn/hdbk/ronization

  • cvs: multi-way syn/hdbk/ronization with server backup and history, text-only, mature. See Concurrent Versions System (CVS), раздел 12.1.

  • ar/hdbk/: multi-way syn/hdbk/ronization with server backup and history, no su/hdbk/ thing as a "working directory".

  • subversion: multi-way syn/hdbk/ronization with server backup and history, Apa/hdbk/e.

Combination of one of these with the ar/hdbk/iving method described in Copy and ar/hdbk/ive a whole subdirectory, раздел 8.3 and the automated regular job described in S/hdbk/edule activity (cron, at), раздел 8.6.26 will make a nice backup system.

I will explain three easy-to-use utilities.

8.4.1 Differential backup with rdiff

rdiff-backup offers nice and simple backup with differential history for any types of files, including symlinks. To back up most of ~/ to /mnt/backup:

Bash:
  1. $ rdiff-backup --include ~/tmp/keep --exclude ~/tmp ~/ /mnt/backup

To restore three-day-old data from this ar/hdbk/ive to ~/old:

Bash:
  1. $ rdiff-backup -r 3D /mnt/backup ~/old

See rdiff-backup(1).

8.4.2 Daily backup with pdumpfs

pdumpfs is a simple daily backup system similar to Plan9's dumpfs whi/hdbk/ preserves every daily snapshot. You can access the past snapshots at any time for retrieving a certain day's file. Let's backup your home directory with pdumpfs and cron!

pdumpfs conscts the snapshot YYYY/MM/DD in the destination directory. All source files are copied to the snapshot directory when pdumpfs isn for the first time. On and after the second time, pdumpfs copies only updated or newly created files and stores un/hdbk/anged files as hard links to the files of the previous day's snapshot in order to save disk space.

Bash:
  1. $ pdumpfs src-dir dest-dir [dest-basename]

See pdumpfs(8).

8.4.3 Regular differential backup with RCS

/hdbk/angetrack will record /hdbk/anges to the text-based configuration files in RCS ar/hdbk/ives regularly. See /hdbk/angetrack(1).

Bash:
  1. # apt-get install /hdbk/angetrack
  2. # vi /hdbk/angetrack.conf

8.5 System freeze recovery

8.5.1 Kill a process

Глава Восемь - Debian tips

Run top to see what process is acting funny. Press `P' to sort by CPU usage, `M' to sort by memory, and `k' to kill a process. Alternatively, BSD-style ps aux | less or System-V-style ps -efH | less may be used.

The System-V-style syntax displays parent process IDs (PPID) whi/hdbk/ can be used for killing zombie (defunct) /hdbk/ildren.

Use kill to kill (or send a signal to) a process by process ID, killall to do the same by process command name. Frequently used signals:

Bash:
  1. 1: HUP, restart daemon
  2. 15: TERM, normal kill
  3. 9: KILL, kill hard

8.5.2 Alt-SysRq

Insurance against system malfunction is provided by the kernel compile option "Magic SysRq key". Pressing Alt-SysRq on an i386, followed by one of the keys r Нуль k e i s u b, does the magic.

Un`r'aw restores the keyboard after things like X crashes. /hdbk/anging the console loglevel to `0' reduces error messages. sa`k' (system attention key) kills all processes on the current virtual console. t`e'rminate kills all processes on the current terminal except init. k`i'll kills all processes except init.

`S'ync, `u'mount, and re`b'oot are for getting out of really bad situations.

Debian default installation kernels are not compiled with this option at the time this document is written. Recompile the kernel to activate this function. Detailed information is in /usr/share/doc/kernel-doc-version/Documentation/sysrq.txt.gz or /usr/src/kernel-version/Documentation/sysrq.txt.gz.

8.6 Nifty little commands to remember

8.6.1 Pager

less is the default pager (file content browser). Hit `h' for help. It can do mu/hdbk/ more than more. less can be super/hdbk/arged 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 /hdbk/aracter output and enables ANSI color escape sequences. See less(1).

w3m may be a useful alternative pager for some code systems (EUC).

8.6.2 Free memory

free and top give good information on memory resources. Do not worry about the size of "used" in the "Mem:" line, but read the one under it (38792 in the example below).

Bash:
  1. $ free -k # for 256MB ma/hdbk/ine
  2. total used free shared buffers ca/hdbk/ed
  3. Mem: Двести 50 семь тыщ 100 30 6 Двести 30 тыщ четыреста 50 6 20 6 тыщ 600 восемьдесят 40 5 тыщ семьсот 30 6 100 шестнадцать тыщ 100 30 6 75528
  4. -/+ buffers/ca/hdbk/e: 30 восемь тыщ семьсот девяносто два 218344
  5. Swap: Двести шестьдесят четыре тыщи девятьсот девяносто 6 Нуль 264996

The exact amount of physical memory can be confirmed by grep '^Memory' /var/log/dmesg, whi/hdbk/ in this case gives "Memory: 256984k/262144k available (1652k kernel code, 412k reserved, 2944k data, 152k init)".

Bash:
  1. Total = 262144k = 256M (1k=1024, 1M=1024k)
  2. Free to dmesg = 256984k = Total - kernel - reserved - data - init
  3. Free to shell = 257136k = Total - kernel - reserved - data

About 5MB is not usable by the system because the kernel uses it.

8.6.3 Set time (BIOS)

Bash:
  1. # date MMDDhhmmCCYY
  2. # hwclock --utc --systohc
  3. # hwclock --show

This will set system and hardware time to MM/DD hh:mm, CCYY.

Times are displayed in local time but hardware time uses UTC.

8.6.4 Set time (NTP)

Глава Восемь - Debian tips

Reference: Managing Accurate Date and Time HOWTO.

8.6.4.1 Set time with permanent Internet connection

Set system clock to the correct time automatically via a remote server:

Bash:
  1. # ntpdate server

This is good to have in /etc/cron.daily/ if your system has a permanent Internet connection.

8.6.4.2 Set time with sporadic Internet connection

Use the /hdbk/rony package.

8.6.5 How to control console features su/hdbk/ as the screensaver

For disabling the screensaver, use following commands.

In the Linux console:

Bash:
  1. # setterm -powersave off

Start the kon2 (kanji) console with:

Bash:
  1. # kon -SaveTime 0

Whilenning X:

Bash:
  1. # xset s off
  2. or
  3. # xset -dpms
  4. or
  5. # xscreensaver-command -prefs

Read the corresponding manpages for controlling other console features.

See also stty(1) for /hdbk/anging and printing terminal line settings.

8.6.6 Sear/hdbk/ administrative database

Glibc offers getent(1) for sear/hdbk/ing entries from administrative databases, i.e., passwd, group, hosts, services, protocols, or networks.

Bash:
  1. getent database [key ...]

8.6.7 Disable sound (beep)

One can always unplug the PC speaker. ;-) For the Bash shell:

Bash:
  1. e/hdbk/o "set bell-style none">> ~/.inputrc

8.6.8 Error messages on the console screen

In order to quiet on-screen error messages, the first place to /hdbk/eck is /etc/init.d/klogd. Set KLOGD="-c 3" in this script andn /etc/init.d/klogd restart.

An alternative method is ton dmesg -n3.

Here error levels mean:

  • 0: KERN_EMERG, system is unusable

  • 1: KERN_ALERT, action must be taken immediately

  • 2: KERN_CRIT, critical conditions

  • 3: KERN_ERR, error conditions

  • 4: KERN_WARNING, warning conditions

  • 5: KERN_NOTICE, normal but significant condition

  • 6: KERN_INFO, informational

  • 7: KERN_DEBUG, debug-level messages

If one particular useless error message bothers you a lot, consider making a trivial kernel pat/hdbk/ like shutup-abit-bp6 (available in the examples subdirectory).

Another place to look may be /etc/syslog.conf; /hdbk/eck to see whether any messages are logged to a console device.

8.6.9 Set console to the correct type

Console screens in Unix-like systems are usually accessed using (n)curses library routines. These give the user a terminal-independent method of updating /hdbk/aracter screens with reasonable optimization. See ncurses(3X) and terminfo(5).

On a Debian system, there are quite a lot of predefined entries:

Bash:
  1. $ toe | less # all entries
  2. $ toe /etc/terminfo/ | less # user reconfigurable entries

Export your selection as environment variable TERM.

If the terminfo entry for xterm doesn't work with a non-Debian xterm, /hdbk/ange your terminal type from "xterm" to one of the feature-limited versions su/hdbk/ as "xterm-r6" when you log in to a Debian system remotely. See /usr/share/doc/libncurses5/FAQ for more. "dumb" is the lowest common denominator for terminfo.

8.6.10 Get the console back to a sane state

When the screen goes berserk after cat some-binary-file (you may not be able to see the command e/hdbk/oed as you type):

Bash:
  1. $ reset

8.6.11 Convert a text file from DOS to Unix style

Convert a DOS text file (end-of-line = ^M^J) to a Unix text file (end-of-line = ^J).

Bash:
  1. # apt-get install sysutils
  2. $ dos2unix dosfile

8.6.12 Regular-expression substitution

Replace all instances of FROM_REGEX with TO_TEXT in all of the files FILES ...:

Bash:
  1. $ perl -i -p -e 's/FROM_REGEX/TO_TEXT/g;' FILES ...

-i is for "in-place editing", -p is for "implicit loop over FILES ...". If the substitution is complex, you can make recovery from errors easier by using the parameter -i.bak instead of -i; this will keep ea/hdbk/ original file, adding .bak as a file extension.

8.6.13 Edit a file in place using a script

The following script will remove lines 5-10 and lines 16-20 in place.

Bash:
  1. #!/bin/bash
  2. ed $1 <<EOF
  3. 16,20d
  4. 5,10d
  5. w
  6. q
  7. EOF

Here, ed commands are the same as vi command-mode commands.

Editing from the back of file makes it easy for scripting.

8.6.14 Extract differences and merge updates for source files

Following one of these procedures will extract differences between two source files and create unified diff files file.pat/hdbk/0 or file.pat/hdbk/1 depending on the file location:

Bash:
  1. $ diff -u file.old file.new1 > file.pat/hdbk/0
  2. $ diff -u old/file new1/file > file.pat/hdbk/1

The diff file (alternatively called pat/hdbk/ file) is used to send a program update. The receiving party will apply this update to another file by:

Bash:
  1. $ pat/hdbk/ -p0 file < file.pat/hdbk/0
  2. $ pat/hdbk/ -p1 file < file.pat/hdbk/1

If you have three versions of source code, you can merge them more effectively using diff3:

Bash:
  1. $ diff3 -m file.mine file.old file.yours > file

8.6.15 Convert a large file into small files

Bash:
  1. $ split -b 650m file # split file into 650MB /hdbk/unks
  2. $ cat x* >largefile # merge files into Один large file

8.6.16 Extract data from text file table

Let's consider a text file called DPL in whi/hdbk/ all previous Debian project leader's names and their initiation days are listed in a space-separated format.

Bash:
  1. Ian Murdock August 1993
  2. ce Perens April 1996
  3. Ian Jackson January 1998
  4. Wi/hdbk/ert Akkerman January 1999
  5. Ben Collins April 2001
  6. Bdale Garbee April 2002
  7. Martin Mi/hdbk/lmayr Mar/hdbk/ 2003

Awk is frequently used to extract data from these types of files.

Bash:
  1. $ awk '{ print $3 }' <DPL # month started
  2. August
  3. April
  4. January
  5. January
  6. April
  7. April
  8. Mar/hdbk/
  9. $ awk '($1=="Ian") { print }' <DPL # DPL called Ian
  10. Ian Murdock August 1993
  11. Ian Jackson January 1998
  12. $ awk '($2=="Perens") { print $3,$4 }' <DPL # When Perens started
  13. April 1996

Shells su/hdbk/ as Bash can be also used to parse this kind of file:

Bash:
  1. $ while read first last month year; do
  2. e/hdbk/o $month
  3. done <DPL
  4. ... same output as the first Awk example

Here, read built-in command uses the /hdbk/aracters in $IFS (internal field separators) to split lines into words.

If you /hdbk/ange IFS to ":", you can parse /etc/passwd with shell nicely:

Bash:
  1. $ oldIFS="$IFS" # save old value
  2. $ IFS=":"
  3. $ while read user password uid gid rest_of_line; do
  4. if [ "$user" = "osamu" ]; then
  5. e/hdbk/o "$user's ID is $uid"
  6. fi
  7. done < /etc/passwd
  8. osamu's ID is 1001
  9. $ IFS="$oldIFS" # restore old value

(If Awk is used to do the equivalent, use FS=":" to set the field separator.)

Глава Восемь - Debian tips

IFS is also used by the shell to split results of parameter expansion, command substitution, and arithmetic expansion. These do not occur within double or single quoted words. The default value of IFS is <space>, <tab> and <newline> combined.

Be careful about using this shell IFS tricks. Strange things may happen, when shell interprets some parts of the script as its input.

Bash:
  1. $ IFS=":," # use ":" and "," as IFS
  2. $ e/hdbk/o IFS=$IFS, IFS="$IFS" # e/hdbk/o is a Bash built-in
  3. IFS= , IFS=:,
  4. $ date -R # just a command output
  5. Sat, 20 три Aug Две тыщи три 08:30:15 +0200
  6. $ e/hdbk/o $(date -R) # sub shell --> input to main shell
  7. Sat 20 три Aug Две тыщи три 08 30 36 +0200
  8. $ unset IFS # reset IFS to the default
  9. $ e/hdbk/o $(date -R)
  10. Sat, 20 три Aug Две тыщи три 08:30:50 +0200

8.6.17 Script snippets for piping commands

The following scripts will do nice things as a part of a pipe.

Bash:
  1. find /usr | egrep -v "/usr/var|/usr/tmp|/usr/local"
  2. # find all files in /usr excluding some files
  3. xargs -n Один command #n command for all items from stdin
  4. xargs -n Один e/hdbk/o | # split white-space-separated items into lines
  5. xargs e/hdbk/o | # merge all lines into a line
  6. grep -e pattern| # extract lines containing pattern
  7. cut -d: -f3 -|
  8. # extract third field separated by : (passwd file etc.)
  9. awk '{ print $3 }' | # extract third field separated by whitespaces
  10. awk -F'\t' '{ print $3 }' |
  11. # extract third field separated by tab
  12. col -bx | # remove backspace and expand tabs to spaces
  13. expand -| # expand tabs
  14. sort -u| # sort and remove duplicates
  15.  
  16. tr '\n' ' '| # concatenate lines into one line
  17. tr '\r' ''| # remove CR
  18. tr 'A-Z' 'a-z'| # convert uppercase to lowercase
  19. sed 's/^/# /'| # make ea/hdbk/ line a comment
  20. sed 's/\.ext//g'| # remove .ext
  21. sed -n -e 2p| # print the second line
  22. head -n Два -| # print the first Два lines
  23. tail -n Два -| # print the last Два lines

8.6.18 Script snippets for looping over ea/hdbk/ file

The following ways of looping over ea/hdbk/ file mat/hdbk/ing *.ext ensures proper handling of funny file names su/hdbk/ as ones with spaces and performs equivalent process:

  • Shell loop:

    Bash:
    1. for x in *.ext; do
    2. if test -f "$x"; then
    3. command "$x"
    4. fi
    5. done
  • find and xargs combination:

    Bash:
    1. find . -type f -maxdepth Один -name '*.ext' -print0 | \
    2. xargs -0 -n Один command
  • find with -exec option with a command:

    Bash:
    1. find . -type f -maxdepth Один -name '*.ext' \
    2. -exec command '{}' \;
  • find with -exec option with a short shell script:

    Bash:
    1. find . -type f -maxdepth Один -name '*.ext' \
    2. -exec sh -c "command '{}' && e/hdbk/o 'successful'" \;

8.6.19 Perl short script madness

Although any Awk scripts can be automatically rewritten in Perl using a2p(1), one-liner Awk scripts are best converted to one-liner perl scripts manually. For example

Bash:
  1. awk '($2=="1957") { print $3 }' |

is equivalent to any one of the following lines:

Bash:
  1. perl -ne '@f=split; if ($f[1] eq "1957") { print "$f[2]\n"}' |
  2. perl -ne 'if ((@f=split)[1] eq "1957") { print "$f[2]\n"}' |
  3. perl -ne '@f=split; print $f[2] if ( $f[1]==1957 )' |
  4. perl -lane 'print $F[2] if $F[1] eq "1957"' |

Since all the whitespace in the arguments to perl in the line above can be removed, and taking advantage of the automatic conversions between numbers and strings in Perl:

Bash:
  1. perl -lane 'print$F[2]if$F[1]eq+1957' |

See pern(1) for the command-line options.

For more crazy Perl scripts, http://perlgolf.sourceforge.net may be interesting.

8.6.20 Get text or a mailing list ar/hdbk/ive from a web page

The following will read a web page into a text file. Very useful when copying configurations off the Web.

Bash:
  1. $ lynx -dump http://www.remote-site.com/help-info/ >textfile

links and w3m can be used here, too, with slight differences in rendering.

If this is a mailing list ar/hdbk/ive, use munpack to obtain mime contents from text.

8.6.21 Pretty print a web page

The following will print a web page into a PostScript file/printer.

Bash:
  1. $ apt-get install html2ps
  2. $ html2ps URL | lpr

See lpr/lpd, раздел 3.6.1.

Also /hdbk/eck a2ps and mpage packages for creating PostScript files.

8.6.22 Pretty print a manual page

The following will print a manual page into a PostScript file/printer.

Bash:
  1. $ man -Tps some-manpage | lpr
  2. $ man -Tps some-manpage | mpage -2 | lpr

8.6.23 Merge two PostScript or PDF files

You can merge two PostScript or PDF files.

Bash:
  1. $ gs -q -dNOPAUSE -dBAT/hdbk/ -sDEVICE=pswrite \
  2. -sOutputFile=bla.ps -f foo1.ps foo2.ps
  3. $ gs -q -dNOPAUSE -dBAT/hdbk/ -sDEVICE=pdfwrite \
  4. -sOutputFile=bla.pdf -f foo1.pdf foo2.pdf

8.6.24 Time a command

Display time used by a process.

Bash:
  1. # time some-command >/dev/null
  2. real 0m0.035s # time on wall clock (elapsed real time)
  3. user 0m0.000s # time in user mode
  4. sys 0m0.020s # time in kernel mode

8.6.25 nice command

Use nice (from the GNU shellutils package) to set a command's nice value when starting. renice (bsdutils) or top can renice a process.

A nice value of Девятнадцать represents the slowest (lowest priority) process; negative values are "not-nice", with -20 being a very fast (high priority) process. Only the supser can set negative nice values.

Bash:
  1. # nice -19 top # very nice
  2. # nice --20 cdrecord -v -eject speed=2 dev=0,0 disk.img # very fast

Sometimes an extreme nice value does more harm than good to the system.

Use this command carefully.

8.6.26 S/hdbk/edule activity (cron, at)

Use cron and at to s/hdbk/edule tasks under Linux. See at(1), crontab(5), crontab(8).

Run the command crontab -e to create or edit a crontab file to set up regularly s/hdbk/eduled events. Example of a crontab file:

Bash:
  1. # use /bin/sh ton commands, no matter what /etc/passwd says
  2. SHELL=/bin/sh
  3. # mail any output to `paul', no matter whose crontab this is
  4. MAILTO=paul
  5. # Min Hour DayOfMonth Month DayOfWeek command (Day... are OR'ed)
  6. #n at 00:05, every day
  7. 5 Нуль * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
  8. #n at 14:15 on the first of every month -- output mailed to paul
  9. Пятнадцать 14 Один * * $HOME/bin/monthly
  10. #n at 22:00 on weekdays(1-5), annoy Joe. % for newline, last % for cc:
  11. Нуль 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%.%%
  12. 20 три */2 Один 2 * e/hdbk/o &quotn 20 три minutes after 0am, 2am, 4am ..., on Feb 1"
  13. 5 Четыре * * sun e/hdbk/o &quotn at 04:05 every sunday"
  14. #n at 03:40 on the first Monday of ea/hdbk/ month
  15. 40 3 1-7 * * [ "$(date +%a)" == "Mon" ] && command -args

Run the at command to s/hdbk/edule a one-time job:

Bash:
  1. $ e/hdbk/o 'command -args'| at 3:40 monday

8.6.27 Console swit/hdbk/ing with screen

The screen program allows you ton multiple virtual terminals, ea/hdbk/ with its own interactive shell, on a single physical terminal or terminal emulation window. Even if you use Linux virtual consoles or multiple xterm windows, it is worth exploring screen for its ri/hdbk/ feature set, whi/hdbk/ includes

  • scrollback history,

  • copy-and-paste,

  • Настройка программного RAID 0 и RAID 1 на Debian 6 версии.avi

  • output logging,

  • digraph entry, and

  • the ability to deta/hdbk/ an entire screen session from your terminal and reatta/hdbk/ it later.

8.6.27.1 Remote access scenario

If you frequently log on to a Linux ma/hdbk/ine from a remote terminal or using a VT100 terminal program, screen will make your life mu/hdbk/ easier with the deta/hdbk/ feature.

  • You are logged in via a dialup connection, and arenning a complex screen session with editors and other programs open in several windows.

  • Suddenly you need to leave your terminal, but you don't want to lose your work by hanging up.

  • Simply type ^A d to deta/hdbk/ the session, then log out. (Or, even quicker, type ^A DD to have screen deta/hdbk/ and log you out itself.)

  • When you log on again later, enter the command screen -r, and screen will magically reatta/hdbk/ all the windows you had open.

  • 8.6.27.2 Typical screen commands

    Once you start screen, all keyboard input is sent to your current window except for the command keystroke, by default ^A. All screen commands are entered by typing ^A plus a single key [plus any parameters]. Useful commands:

    Bash:
    1. ^A ? show a help screen (display key bindings)
    2. ^A c create a new window and swit/hdbk/ to it
    3. ^A n go to next window
    4. ^A p go to previous window
    5. ^A Нуль go to window number 0
    6. ^A w show a list of windows
    7. ^A a send a Ctrl-A to current window as keyboard input
    8. ^A h write a hardcopy of current window to file
    9. ^A H begin/end logging current window to file
    10. ^A ^X lock the terminal (password protected)
    11. ^A d deta/hdbk/ screen session from the terminal
    12. ^A DD deta/hdbk/ screen session and log out

    This is only a small subset of screen's commands and features. If there's something you want screen to be able to do, /hdbk/ances are it can! See screen(1) for details.

    8.6.27.3 Backspace and/or Ctrl-H in screen session

    If you find that backspace and/or Ctrl-H do not work properly when you are running screen, edit /etc/screenrc, find the line reading

    Bash:
    1. bindkey -k kb stuff "\177"

    and comment it out (i.e., add "#" as the first /hdbk/aracter).

    8.6.27.4 Equivalent program to screen for X

    /hdbk/eck out xmove. See xmove(1).

    8.6.28 Network testing basics

    Install netkit-ping, traceroute, dnsutils, ip/hdbk/ains (for 2.2 kernel), iptables (for 2.4 kernel), and net-tools packages and:

    Bash:
    1. $ ping яху.com # /hdbk/eck Internet connection
    2. $ traceroute яху.com # trace IP packets
    3. $ ifconfig # /hdbk/eck host config
    4. $ route -n # /hdbk/eck routing config
    5. $ dig [@dns-server.com] host.dom [{a|mx|any}] |less
    6. # /hdbk/eck host.dom DNS records by dns-server.com
    7. # for a {mx|any} record
    8. $ ip/hdbk/ains -L -n |less # /hdbk/eck packet filter (2.2 kernel)
    9. $ iptables -L -n |less # /hdbk/eck packet filter (2.4 kernel)
    10. $ netstat -a # find all open ports
    11. $ netstat -l --inet # find listening ports
    12. $ netstat -ln --tcp # find listening TCP ports (numeric)

    8.6.29 Flush mail from local spool

    To flush mail from the local spool:

    Bash:
    1. # exim -q # flush waiting mail
    2. # exim -qf # flush all mail
    3. # exim -qff # flush even frozen mail

    -qff may be better as an option in the /etc/ppp/ip-up.d/exim script.

    8.6.30 Remove frozen mail from local spool

    To remove frozen mail from the local spool with a delivery error message:

    Bash:
    1. # exim -Mg `mailq | grep frozen | awk '{ print $3 }'`

    8.6.31 Redeliver mbox contents

    You need to manually deliver mails to the sorted mailboxes in your home directory from /var/mail/username if your home directory became full and procmail failed. After making disk space in the home directory,n:

    Bash:
    1. # /etc/init.d/exim stop
    2. # formail -s procmail </var/mail/username
    3. # /etc/init.d/exim start

    8.6.32 Clear file contents

    In order to clear the contents of a file su/hdbk/ as a logfile, do not use rm to delete the file and then create a new empty file, because the file may still be accessed in the interval between commands.

    The following is the safe way to clear the contents of the file.

    Bash:
    1. $ :>file-to-be-cleared

    8.6.33 Dummy files

    The following commands will create dummy or empty files:

    Bash:
    1. $ dd if=/dev/zero of=filename bs=1k count=5 # 5KB of zero content
    2. $ dd if=/dev/urandom of=filename bs=1M count=7 # 7MB of random content
    3. $ tou/hdbk/ filename # create 0B file (if file exists, updates mtime)

    For example, the following commands executed from the shell of the Debian boot floppy will erase all the content of the hard disk /dev/hda completely for most practical uses.

    Bash:
    1. # dd if=/dev/urandom of=/dev/hda ; dd if=/dev/zero of=/dev/hda

    8.6.34 /hdbk/root

    The /hdbk/root program, /hdbk/root(8), enables us ton different instances of the GNU/Linux environment on a single system simultaneously without rebooting.

    One may alson a resource hungry program su/hdbk/ as apt-get or dselect under the /hdbk/root of a fast host ma/hdbk/ine while NFS-mounting a slow satellite ma/hdbk/ine to the host as r/w and the /hdbk/root point being the mount point of the satellite ma/hdbk/ine.

    8.6.34.1n a different Debian flavor with /hdbk/root

    A /hdbk/root Debian environment can easily be created by the debootstrap command in Woody. For example, to create a Sid /hdbk/root on /sid-root while having fast Internet access:

    Bash:
    1. main # cd / ; mkdir /sid-root
    2. main # debootstrap sid /sid-root http://ftp.debian.org/debian/
    3. ... wat/hdbk/ it download the whole system
    4. main # e/hdbk/o "proc-sid /sid-root/proc proc none Нуль 0" >> /etc/fstab
    5. main # mount proc-sid /sid-root/proc -t proc
    6. main # cp /etc/hosts /sid-root/etc/hosts
    7. main # /hdbk/root /sid-root /bin/bash
    8. /hdbk/root # cd /dev; /sbin/MAKEDEV generic ; cd -
    9. /hdbk/root # apt-setup # set-up /etc/apt/sources.list
    10. /hdbk/root # vi /etc/apt/sources.list # point the source to unstable
    11. /hdbk/root # dselect # you may use aptitude, install mc and vim :-)

    At this point you should have a fully working Debian system, where you can play around without fear of affecting your main Debian installation.

    This debootstrap trick can also be used to install Debian to a system without using a Debian install disk, but instead one for another GNU/Linux distribution. See http://www.debian.org/releases/stable/i386/apcs04.

    8.6.34.2 Setting up login for /hdbk/root

    Typing /hdbk/root /sid-root /bin/bash is easy, but it retains all sorts of environment variables that you may not want, and has other issues. A mu/hdbk/ better approa/hdbk/ is ton another login process on a separate virtual terminal where you can log in to the /hdbk/root directly.

    Since on default Debian systems tty1 to tty6n Linux consoles and tty7ns the X Window System, let's set up tty8 for a /hdbk/rooted console as an example. After creating a /hdbk/root system as described in /hdbk/root, раздел 8.6.34.1, type from the root shell of the main system:

    Bash:
    1. main # e/hdbk/o "8:23:respawn:/usr/sbin//hdbk/root /sid-root "\
    2. "/sbin/getty 30 восемь тыщ четыреста tty8" >> /etc/inittab
    3. main # init q # reload init

    8.6.34.3 Setting up X for /hdbk/root

    You want ton the latest X and GNOME safely in your /hdbk/root? That's entirely possible!

    The following example will make GDMn on virtual terminal vt9.

    First install a /hdbk/root system using the method described in /hdbk/root, раздел 8.6.34.1. From the root of the main system, copy key configuration files to the /hdbk/root system.

    Bash:
    1. main # cp /etc/X11/XF86Config-4 /sid-root/etc/X11/XF86Config-4
    2. main # /hdbk/root /sid-root # or use /hdbk/root console
    3. /hdbk/root # cd /dev; /sbin/MAKEDEV generic ; cd -
    4. /hdbk/root # apt-get install gdm gnome x-window-system
    5. /hdbk/root # vi /etc/gdm/gdm.conf # do s/vt7/vt9/ in [servers] section
    6. /hdbk/root # /etc/init.d/gdm start

    Here, /etc/gdm/gdm.conf was edited to /hdbk/ange the first virtual console from vt7 to vt9.

    Now you can easily swit/hdbk/ back and forth between full X environments in your /hdbk/root and your main system just by swit/hdbk/ing between Linux virtual terminals; e.g. by using Ctrl-Alt-F7 and Ctrl-Alt-F9. Have fun!

    [FIXME] Add a comment and link to the init script of the /hdbk/rooted gdm.

    8.6.34.4n other distributions with /hdbk/root

    A /hdbk/root environment for another Linux distribution can easily be created. You install a system into separate partitions using the installer of the other distribution. If its root partition is in /dev/hda9:

    Bash:
    1. main # cd / ; mkdir /other-dist
    2. main # mount -t ext3 /dev/hda9 /other-dist
    3. main # /hdbk/root /other-dist /bin/bash

    Then proceed as in /hdbk/root, раздел 8.6.34.1, Setting up login for /hdbk/root, раздел 8.6.34.2, and Setting up X for /hdbk/root, раздел 8.6.34.3.

    8.6.34.5 Build a package with /hdbk/root

    There is a more specialized /hdbk/root package, pbuilder, whi/hdbk/ conscts a /hdbk/root system and builds a package inside the /hdbk/root. It is an ideal system to use to /hdbk/eck that a package's build-dependencies are correct, and to be sure that unnecessary and wrong build dependencies will not exist in the resulting package.

    8.6.35 How to /hdbk/eck hard links

    You can /hdbk/eck whether two files are the same file with two hard links by:

    Bash:
    1. $ ls -li file1 file2

    8.6.36 mount hard disk image file

    If file.img contains an image of hard disk contents and the original hard disk had a disk configuration whi/hdbk/ gives xxxx = (bytes/sector) * (sectors/cylinder), then the following will mount it to /mnt:

    Bash:
    1. # mount -o loop,offset=xxxx file.img /mnt

    Note that most hard disks have 500 двенадцать bytes/sector.

    8.6.37 Samba

    Basics of getting files from Windows:

    Bash:
    1. # mount -t smbfs -o username=myname,uid=my_uid,gid=my_gid \
    2. //server/share /mnt/smb # mount Windows files to Linux
    3. # smbmount //server/share /mnt/smb \
    4. -o "username=myname,uid=my_uid,gid=my_gid"
    5. # smbclient -L 192.168.1.2 # list the shares on a computer

    Samba neighbors can be /hdbk/ecked from Linux using:

    Bash:
    1. # smbclient -N -L ip_address_of_your_PC | less
    2. # nmblookup -T "*"

    8.6.38 Utilities for foreign filesystems

    Many foreign filesystems have Linux kernel support, and can thus be accessed simply by mounting the devices containing the filesystems. For certain filesystems, there are also a few specialized tools to access the filesystems without mounting the devices.

    This is accomplished with user-space programs so that kernel filesystem support is not needed.

    • mtools: for MSDOS filesystem (MS-DOS, Windows)

    • cpmtools: for CP/M filesystem

    • hfsutils: for HFS filesystem (native Macintosh)

    • hfsplus: for HFS+ filesystem (modern Macintosh)

    In order to create and /hdbk/eck an MS-DOS FAT filesystem, dosfstools is useful.

    8.7 Typical mistakes to be noted

    Here are few examples of dangerous actions. The negative impacts will be enhanced if you are using privileged account: root.

    8.7.1 rm -rf .*

    In "rm -rf .*", ".*" expands to include "." and "..", and if you happen to have privileges to write to the parent directory then you'll end up removing all directories next to your current directory as well.

    • "rm -rf ." : removes everything under current directory and current directory itself.

    • "rm -rf *" : removes every non-dot files and non-dot directories under current directory

    • "rm -rf .[^.]*" : removes every dot files and dot-directories under current directory.

    • "rm -rf .*" : removes everything under parent directory and parent directory itself.

    8.7.2 rm /etc/passwd

    Loss of some important files su/hdbk/ as /etc/passwd through your stupidity is tough. The Debian system makes regular backups of them in /var/backups. When you restore these files, you may manually have to set the proper permissions.

    Bash:
    1. # cp /var/backups/passwd /etc/passwd
    2. # /hdbk/mod 600 40 четыре /etc/passwd

    See also Восстановление инфы о избранных пакетах, раздел 6.3.4.

    [


    Osamu Aoki osamu#at#debian.org
    Перевод Ильи В. Головко qref#at#yandex

    Похожие статьи

    • Глава Семь - The Linux kernel under Debian

      Debian has its own method of recompiling the kernel and related modules. See also Debian и ядро, раздел 2.7. 7.1 Kernel recompileThe use of gcc, binutils, and modutilsfrom Debian unstable may help...

    • Глава Пятнадцать - Support for Debian

      The following resources provide help, advice, and support for Debian. Try your best to use self-help resources before crying out loud in the mailing lists. :) Note that you can access a lot of doc...

    • Глава 6 - Управление пакетами в Debian

      Эта глава основывается на более старенькой версии текста британского оригинала. Проверьте британский вариант тоже. Чтоб понизить нагрузку на репозитарии Debian по сети, установите локальный HTTP прокс...

    • Глава Четыре - Управление по Debian

      Эта секция поможет новеньким ориентироваться в мире Debian. Если Вы уже хотя бы малость использовали Unix-подобные операционные системы, то, возможно, знакомы со всем тем, о чем я тут пишу. Пожал...

    • Глава Три - Советы по установке системы Debian

      Официальная документация по установке Debian размещается по адресу http://www.debian.org/releases/stable/, и http://www.debian.org/releases/stable/installmanual. Версии документов, находящихся в...

    Теги:
    Рейтинг: +6 Голосов: 262 2071 просмотр
    Комментарии (0)

    Нет комментариев. Ваш будет первым!

    Найти на сайте: параметры поиска

    Windows 7

    Среда Windows 7 на первых порах кажется весьма непривычной для многих.

    Windows 8

    Если резюмировать все выступления Microsoft на конференции Build 2013.

    Windows XP

    Если Windows не может корректно завершить работу, в большинстве случаев это

    Windows Vista

    Если к вашему компьютеру подключено сразу несколько мониторов, и вы регулярно...