Techniques — Miscellaneous Ubuntu/Linux
Back to techniques
https://rcardinal.ddns.net/techniques/ubuntu.html

Based on Ubuntu 10.04 (a variety of Debian Linux).

Before it's booted

To interrupt GRUB2

... hold down SHIFT during boot.

Kernel and GNU

How much stuff is in this directory (and its subdirectories?)

du -bch

Reboot semi-gracefully after a crash

Hold ALT and SysRq, and type (fairly slowly) R E I S U B. See http://en.wikipedia.org/wiki/Magic_SysRq_key.

The very basics...

# Find file matching filespec within current directory or subdirectories (case-insensitive)
find . -iname "filespec"

# Print filenames of filename matching grep pattern (case-insensitive)
grep -il pattern filespec

# Do much the same, if executed from the command line:
grep pattern filespec

# grep options include
# -i		ignore case
# -l		print filenames, not file contents
# -C num	print context: this number of lines either side of the match (or -A num for after, and/or -B num for before)
# -r		recurse directories (though not terribly well e.g. grep -r pattern *.html won't match a file in a subdirectory if the directory isn't called *.html)

# Better recursion method for "find files containing...": see also http://en.wikipedia.org/wiki/Find
find . -iname "filespec" -exec grep -il "pattern" {} \;

# Show file access/modify/change times:
stat filespec

Repair a broken Ubuntu system (with RAID and LVM2) from a LiveCD

The problems: suppose we rebooted a Ubuntu system during an upgrade, which therefore failed and left the system broken. Suppose also that the Alternate CD doesn't boot, but the LiveCD does. The Alternate CD supports RAID/LVM; the LiveCD doesn't. So we need to coax in RAID/LVM support on the fly.

Our hypothetical system (well, this one!) has two hard disks, /dev/sda and /dev/sdb. It has two RAID1 devices, /dev/md0 (= /dev/sda5 + /dev/sdb5, mapped to /boot) and /dev/md1 (= /dev/sda1 + /dev/sdb1). On this second RAID device there are some LVM2 volumes, which are eventually mapped to the root (/) and swap areas. Off we go:

  1. Boot from LiveCD to the Ubuntu desktop
  2. Network
  3. RAID
    sudo bash
    apt-get update
    apt-get install mdadm # ... go through the Postfix rubbish; ... ignore the MAKEDEV error
    mdadm --assemble --verbose /dev/md0 /dev/sda5 /dev/sdb5 # system-specific
    mdadm --assemble --verbose /dev/md1 /dev/sda1 /dev/sdb1 # system-specific
    cat /proc/mdstat # all happy
    
  4. LVM
    apt-get install lvm2
    pvs # shows volume group to be "egret"
    lvdisplay /dev/egret # shows LVs to be /dev/egret/root and /dev/egret/swap_1
    # vgscan # not necessary; also finds volume group "egret"
    vgchange -a y # activate all VGs; this makes /dev/egret/*
    
  5. Mount / and /boot, and then bind necessary things into the root area to be repaired before we chroot into it.
    mkdir /mnt/repair
    mkdir /mnt/repair_boot
    mount /dev/egret/root /mnt/repair # system-specific
    mount /dev/md0 /mnt/repair_boot # system-specific
    
    mount --bind /var/run/dbus/ /mnt/repair/var/run/dbus/
    mount --bind /proc/ /mnt/repair/proc/
    mount --bind /dev/ /mnt/repair/dev/
    mount --bind /dev/pts/ /mnt/repair/dev/pts/
    mount --bind /sys/ /mnt/repair/sys/
    mount --bind /mnt/repair_boot /mnt/repair/boot # so it sees the right boot area
    # if there isn't one already on the system to be repaired, also: cp /etc/resolv.conf /mnt/repair/etc/resolv.conf
    
  6. Repair
    sudo chroot /mnt/repair su
    
    apt-get update
    apt-get upgrade
    aptitude safe-upgrade
    apt-get -f install
    dpkg --configure -a
    apt-get upgrade
    
    update-grub
    
  7. If that all went smoothly, fingers crossed:
    exit # from the chroot environment
    reboot
    

Repair a slightly less broken system

For example: remove duff video drivers.

mount -n -o remount,rw /
apt-get purge nvidia-current
# rm /etc/X11/xorg.conf
reboot

Disable EHCI_HCD

Why? Because it's buggy and sometimes causes USB problems. Run this, or add it at the end of /etc/rc.local just before "exit 0"

cd /sys/bus/pci/drivers/ehci_hcd/
sh -c 'find ./ -name "0000:00:*" -print| sed "s/\.\///">unbind'

See also http://www.linux-usb.org/FAQ.html; this TimeSinker blog. Also useful:

lsusb
lsusb -v

Remove an external HD from the desktop

Mount it in /mnt, not in /media; stuff mounted in /media shows up on the desktop.

Mount an ISO image file

sudo mount -o loop imagefile.iso /mnt/mountpointwherever

Mount an Android phone via USB using MTP

Not easy:http://www.mysolutions.it/mounting-your-mtp-androids-sd-card-on-ubuntu/

Easy: http://www.webupd8.org/2012/12/how-to-mount-android-40-ubuntu-go-mtpfs.html

Mount a path via SSH

Package sshfs required. For details, see http://www.howtogeek.com/howto/ubuntu/how-to-mount-a-remote-folder-using-ssh-on-ubuntu/. In brief:

sshfs user@host:/remotepath localmountpoint
# ... Then to dismount:
fusermount -u localmount point

Which processes are using a particular device?

For example: fuser -m /dev/sdg1

Automatically remount a disk

Run this regularly from /etc/crontab:

#!/bin/sh

DEVICE=/dev/sdg1
MOUNTPOINT=/mnt/exthd
FSTYPE=ext3
MOUNTOPTIONS=relatime,defaults

# If it's mounted, but not in the right place, unmount it first (this is in case the desktop has mounted it)

if ! df | grep $DEVICE | grep $MOUNTPOINT > /dev/null; then
	echo "not mounted correctly"
	# Not mounted at the correct point. So:
	if df | grep $DEVICE > /dev/null; then
		# But it is mounted... so it's mounted in the wrong place:
		echo "mounted incorrectly"
		umount $DEVICE
	fi
	# Now it's not mounted.
	echo "about to mount"
	mount -t $FSTYPE $DEVICE $MOUNTPOINT -O $MOUNTOPTIONS
else
	echo "Already mounted correctly"
fi

What's the Ubuntu boot sequence?

See man 7 boot, and the Debian policy. For a normal boot:

  1. BIOS
  2. Master boot record (MBR)
  3. GRUB2
  4. Kernel. This creates process 1, which is /sbin/init.
  5. init: see man 5 init. This runs a bunch of files (config files, not plain scripts) in /etc/init/. For example, /etc/init/mountall.conf calls /sbin/mountall, which reads /etc/fstab...
  6. Runlevels.

Upgrade packages via the command line

sudo apt-get update
sudo apt-get dist-upgrade

Hold packages, and other Debian package management tips

See http://www.cyberciti.biz/ref/apt-dpkg-ref.html. Note that aptitude is different from APT itself.

Show installed packages

See http://www.linuxquestions.org/questions/debian-26/%5Baptitude%5D-how-to-get-a-list-of-all-installed-packages-458119/.

GNOME

Disable username prompts at login

sudo -u gdm gconftool-2 --set --type boolean /apps/gdm/simple-greeter/disable_user_list true

Extend the length of the most-recent-file list in gedit

gconftool-2 --type int --set /apps/gedit-2/preferences/ui/recents/max_recents 10

Have a "go" command to auto-execute different types of file via GNOME

Create /usr/local/bin/go that looks like this (download as go or see below). Then use go filename:

#!/bin/bash
# Acts as a shorter synonym for gnome-open.
gnome-open "$*"

Take a screenshot

gnome-screenshot -i

Bind the Windows (Super_L) key to the GNOME panel menu

See http://ramblings.narrabilis.com/wp/binding-windows-key-to-gnome-foothat-panel-menu/. Assuming the key is already called Super_L (check with kev):

gconftool-2 -s -t string /apps/metacity/global_keybindings/panel_main_menu Super_L

Move the minimize/maximize/close buttons to the top right of windows

Run gconf-editor; find the key apps/metacity/general/button_layout; change it from close,minimize,maximize: to menu:minimize,maximize,close (the colon being the separator between the left and right sides of the window).

Add TrueType fonts

You'll need to restart things like OpenOffice to see the changes.

bash

See Cooper M, Advanced Bash-Scripting Guide.

A few specific things:

Iterating over files (inc. as a one-liner)

In this case, convert all *.cpp and *.h files from tabs to spaces, using a tab width of 4.

for file in *.cpp *.h; do echo $file; expand -t4 $file > $file.expanded; rm $file; mv $file.expanded $file; done

To do this in Windows, use Cygwin (the Windows directories are then in /cygwin/c/ for drive C, etc.).

Setting the PATH

In Ubuntu, edit /etc/environment (a line like PATH="/aaa:/bb1/bb2:/ccc"). This is a system-wide file.

But when that fails, there are several options (see here); one (per-user) way is to edit ~/.bashrc, adding a line like export PATH=$HOME/local/bin:$PATH.

Printing

Print an image to fit the page

lpr -P PRINTER -o scaling=100 FILENAME

Add a CUPS-PDF paper size

e.g. PA4, 210 x 280 mm, as a 4:3 paper size for slideshows: http://www.bricsys.com/common/support/forumthread.jsp?id=14714

Administer CUPS

http://localhost:631/

Cloning a Windows 8 boot disk with Linux

The critical step is to copy the disk ID, or Windows 8 will refuse to boot.

VirtualBox VDI management

See https://forums.virtualbox.org/viewtopic.php?t=8046

.
Valid HTML 4.01 Transitional
Valid CSS