Techniques — Miscellaneous Ubuntu/Linux
|
Back to techniques
https://rcardinal.ddns.net/techniques/ubuntu.html
|
Based on Ubuntu 10.04 (a variety of Debian Linux).
... hold down SHIFT during boot.
du -bch
Hold ALT and SysRq, and type (fairly slowly) R E I S U B
. See http://en.wikipedia.org/wiki/Magic_SysRq_key.
# 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
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:
ifconfig eth0
; may need to delete/recreate the connection, or disable "autoconnect" and disconnect/reconnect manually/etc/network/interfaces
contains something likeauto eth0 iface eth0 inet static address 192.168.10.91 netmask 255.255.255.0 network 192.168.10.0 broadcast 192.168.10.255 gateway 192.168.10.1
/etc/resolv.conf
with a line like nameserver 131.111.8.42
(the Ubuntu norm is via /etc/resolvconf/resolv.conf.d/tail
etc., but ignore that for now).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
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/*
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
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
exit # from the chroot environment reboot
For example: remove duff video drivers.
mount -n -o remount,rw / apt-get purge nvidia-current # rm /etc/X11/xorg.conf reboot
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
Mount it in /mnt
, not in /media
; stuff mounted in /media shows up on the desktop.
sudo mount -o loop imagefile.iso /mnt/mountpointwherever
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
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
For example: fuser -m /dev/sdg1
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
See man 7 boot
, and the Debian policy. For a normal boot:
/sbin/init
.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
.../etc/init/rc-sysinit.conf
which then calls /sbin/telinit
); use runlevel
to find out what the previous and current runlevels are./etc/rc[0-6S].d/
. These directories contain symlinks to scripts in /etc/init.d
. Files beginning with "S" (start) are executed with a "start" parameter, and files beginning with "K" (kill) are executed with a "stop" parameter. The K scripts are called before the S scripts, and the order within these sets is determined by the filenames (which have a two-digit number in, i.e. they get called in numerical order). This system is automanaged: see http://www.debian.org/doc/manuals/debian-faq/ch-customizing.en.html, question 11.6./sbin/init
reads /etc/init/rc.conf
, which calls /etc/init.d/rc $RUNLEVEL
, which calls everything relevant in /etc/rc[0-6S].d/
(... which typically calls stuff in /etc/init.d
with either a "start" or a "stop" parameter, as above)./etc/rc.local
is also called at the end of each multiuser runlevel. (It's actually called by /etc/init.d/rc.local
, which is called by—for example—/etc/rc2.d/S99rc.local
in the case of runlevel 2.)sudo apt-get update sudo apt-get dist-upgrade
See http://www.cyberciti.biz/ref/apt-dpkg-ref.html. Note that aptitude is different from APT itself.
sudo -u gdm gconftool-2 --set --type boolean /apps/gdm/simple-greeter/disable_user_list true
gconftool-2 --type int --set /apps/gedit-2/preferences/ui/recents/max_recents 10
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 "$*"
gnome-screenshot -i
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
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).
You'll need to restart things like OpenOffice to see the changes.
See Cooper M, Advanced Bash-Scripting Guide.
A few specific things:
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.).
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
.
lpr -P PRINTER -o scaling=100 FILENAME
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
/dev/sdd1
TO a new drive on /dev/sdc
, where the destination is at least as large as the source.dd if=/dev/sdd of=/dev/sdc bs=512 count=63
(note: it may be sufficient just to copy the MBR (the first sector = 512 bytes) MINUS the partition table at its end, meaning the first 446 bytes, with: dd if=/dev/sdd of=/dev/sdc bs=446 count=1
)
fdisk -l -u /dev/sdd
fdisk /dev/sdc
p
(and write down the old disk ID just in case)d
, and repeat as necessaryn
, and e.g. accept the defaults, starting at 2048 and finishing at the end of the diska
t
x
for expert mode, then i
and enter the ID from the source diskw
ntfsclone --overwrite /dev/sdc1 /dev/sdd1
(this example is FROM /dev/sdd1
TO /dev/sdc1
)ntfsresize /dev/sdc1
The critical step is to copy the disk ID, or Windows 8 will refuse to boot.