Snott

Category: Ubuntu

Worlds most popular Linux Distribution, also provides a server version offering easy cloud setup out of the box.

  • Handy Arch Linux commands

    Handy Arch Linux commands

    Here I will show you some useful commands that will make your life a lot easier, some of them are specific to Arch Linux while the majority will run on any distro.

    # = means execute as root or with sudo

     

    Get a list of installed packages

    # comm -23 <(pacman -Qeq|sort) <(pacman -Qmq|sort) > pkglist.txt

    This gets all explicitly installed top-level packages, i.e. those that are not required by other packages, and sorts them from a to z and then outputs the result to a file.

    Store this package list somewhere safe and handy (you can also set up a cron job so you always have the latest version of the file), and when you need it, just copy the pkglist.txt file to the new installation, and navigate to the directory containing it.

    This command comes extremely handy when you need or want to reinstall Arch Linux, because pacman can take a list of packages from a text file and use that as input, so by doing:

    # pacman -S $(< pkglist.txt)

    you can tell pacman to install everything you had before formatting, or to replicate the package list of another machine saving you hours (if not days).

    Command Line shortcuts (distro agnostic)

    This shortcuts are totally awesome and easy to learn, the web is filled with pages explaining how to use them, but for reference, here is a quick explanation:

    ctrl + u = cuts a command from the cursor position until de start of the line

    ctrl + y = recovers a command previously cut with ctrl + u

    ctrl + r = search a command you have typed before (very useful for long one-liner commands that are hard to remember). If the first command returned is not the one you are looking for, press ctrl + r again and it will get to the next result. To execute the command one you have found it, just press enter.

    Wipe a hard drive or USB/Flash media (including the MBR)

    • Filling the disk with all zeros (This may take a while, as it is making every bit of data 0) :
      # dd if=/dev/zero of=/dev/sdX bs=1M
    • If you are wiping your hard drive for security, you can populate it with random data rather than zeros (This is going to take even longer than the first example.) :
      # dd if=/dev/urandom of=/dev/sdX bs=1M

    Replace sdX with the drive you want to wipe, you can check with a tool such as Gparted so you know the exact location of the drive (it can be different from sdX in some circunstances).

    Incremental backup using rsync

    You don’t need to have expensive or bloated tools to have a nice backup of your system, this command:

    # rsync --delete --backup --backup-dir=/media/usborexternaldrive/backup-`date +%A` -avz /home/ /media/usborexternaldrive/current-backup


    Will backup all your users home directories in an incremental way, with the latest backup (current-backup) being a complete file snapshot, while the others contain just the differences between the current-backup folder and the day of the week you performed the backup.

    You can also mount a network folder or give it your dropbox directory so backups will land there. At the end of the week you will have one backup for each day of the week + a full backup without wasting any hard drive space, very neat!

    Download an entire website

    wget –random-wait -r -p -e robots=off -U mozilla http://www.example.com

    -p parameter tells wget to include all files, including images.

    -e robots=off you don’t want wget to obey by the robots.txt file

    -U mozilla as your browsers identity.

    –random-wait to let wget chose a random number of seconds to wait, avoid get into black list.

    Other Useful wget Parameters:

    –limit-rate=20k limits the rate at which it downloads files.

    -b continues wget after logging out.

    -o $HOME/wget_log.txt logs the output

    Remove spaces from all file names in a folder

    for file in *; do mv "$file" `echo $file | sed -e 's/  */_/g' -e 's/_-_/-/g'`; done

    Windows users like to put spaces in filenames (which is a pain on the linux console because
    you have to "escape" every space), this command will replace all spaces with dashes and
    underscores, saving you lots of time!

  • Recovering from a totally dead boot partition

    Recovering from a totally dead boot partition

    Here is the case:

    The hard disk containing your /boot partition has just died, you have a separate RAID1 array (using mdadm) with all the information on the array intact, but the system is unable to boot because all the files in /boot got lost.

    Your best bet is to get the following iso onto a USB flash drive or CD:

    http://sourceforge.net/p/ubuntu-secured/home/Home/

    Ubuntu secure remix provides the following functionalities:

    • Full Ubuntu DVD distro
    • integrates Clean-Ubiquity: automatic backup of the MBRs and partition tables when installing Ubuntu, which is highly recommended for dual-boot Ubuntu-Windows (or MacOS)
    • integrates OS-Uninstaller (tool to uninstall any Operating System from your PC)
    • integrates Boot-Repair (tool to fix the PC boot)

    the one highlighted in bold is what we are going to use to fix the boot partition.

    With the ubuntu DVD and Boot-repair you will be able replace the missing kernels and initrds, reinstall any broken packages and get grub again onto the MBR, successfully restoring the computer or server to a bootable state.

    Now, burn your iso to cd (I assume you know how to do this) or get into an USB drive like this:

    dd if=/path/to/file.iso of=/dev/sdb

    where /dev/sdb is your USB drive, you can use the mount command to double check this. If you make a mistake here (for example if you input /dev/sda and /dev/sda is your laptop hard drive) you will end up writing the iso to your hard drive and DESTROY all of your data. So please double-check this.

    After you boot into the Ubuntu Secure Remix, first thing to do is chroot and then create a /boot directory (since the old one is lost). Chroot is a command that will mount the operating system stored on the hard drive so you are able to execute commands inside it. This is a very important step. To do this:

    # means run with root or sudo
    1. Create a mountpoint

    # mkdir /mnt/mountpoint

    2. Mount /dev and /dev/pts to chroot

    # mount -o bind /dev /mnt/mountpoint/dev

    # mount -o bind /dev/pts /mnt/mountpoint/dev/pts

    3.Open bash in chroot

    # chroot /mnt/mountpoint /bin/bash

    4. create the /boot directory

    #mkdir /boot

    At this point, you are now inside your unbootable linux system with and empty /boot directory, now do:

    sudo apt-get install linux-image-server

    That command (on a debian/ubuntu based system) will pull the latest kernel image and install it, populating the /boot directory with some files (which are the kernel and initrd images). If you have a different distro do the same with your distro’s package manager.

    Next open up Boot-repair from the GUI and follow the onscreen instructions to restore grub into the mbr. I won’t post them here because:

    a) it varies from system to system and depends on how screwed the OS got

    b) On screen instructions are easy to understand and get the job done

    After Boot-repair finishes, exit your chrooted terminal (by typing exit at the terminal) and restart your system.

    Everything should be in order now and the machine should boot without a problem. If it does not, feel free to leave a comment below and I will do my best to help you.

    NOTE: This procedure was tested under a Software RAID using mdadm on Ubuntu Server 10.04.4 LTS, I haven’t tested this with any other RAID array configuration or distro, be it fake, firmware or hardware RAID.