Friday, March 13, 2009

Linux Dark Tips

Top 5 Tips for Migrating from Windows to Linux

Runlevels

Linux
Once booted, the kernel starts init which then starts rc which starts all scripts belonging to a runlevel. The scripts are stored in /etc/init.d and are linked into /etc/rc.d/rcN.d with N the runlevel number.
The default runlevel is configured in /etc/inittab. It is usually 3 or 5:

# grep default: /etc/inittab                                       
id:3:initdefault:

The actual runlevel can be changed with init. For example to go from 3 to 5:

# init 5                             # Enters runlevel 5

    * 0 Shutdown and halt
    * 1 Single-User mode (also S)
    * 2 Multi-user without network
    * 3 Multi-user with network
    * 5 Multi-user with X
    * 6 Reboot

Use chkconfig to configure the programs that will be started at boot in a runlevel.

# chkconfig --list                   # List all init scripts
# chkconfig --list sshd              # Report the status of sshd
# chkconfig sshd --level 35 on       # Configure sshd for levels 3 and 5
# chkconfig sshd off                 # Disable sshd for all runlevels

Debian and Debian based distributions like Ubuntu or Knoppix use the command update-rc.d to manage the runlevels scripts. Default is to start in 2,3,4 and 5 and shutdown in 0,1 and 6.

# update-rc.d sshd defaults          # Activate sshd with the default runlevels
# update-rc.d sshd start 20 2 3 4 5 . stop 20 0 1 6 .  # With explicit arguments
# update-rc.d -f sshd remove         # Disable sshd for all runlevels
# shutdown -h now (or # poweroff)    # Shutdown and halt the system

FreeBSD
The BSD boot approach is different from the SysV, there are no runlevels. The final boot state (single user, with or without X) is configured in /etc/ttys. All OS scripts are located in /etc/rc.d/ and in /usr/local/etc/rc.d/ for third-party applications. The activation of the service is configured in /etc/rc.conf and /etc/rc.conf.local. The default behavior is configured in /etc/defaults/rc.conf. The scripts responds at least to start|stop|status.

# /etc/rc.d/sshd status
sshd is running as pid 552.
# shutdown now                       # Go into single-user mode
# exit                               # Go back to multi-user mode
# shutdown -p now                    # Shutdown and halt the system
# shutdown -r now                    # Reboot

The process init can also be used to reach one of the following states level. For example # init 6 for reboot.

    * 0 Halt and turn the power off (signal USR2)
    * 1 Go to single-user mode (signal TERM)
    * 6 Reboot the machine (signal INT)
    * c Block further logins (signal TSTP)
    * q Rescan the ttys(5) file (signal HUP)

Reset root password
Linux method 1
At the boot loader (lilo or grub), enter the following boot option:

init=/bin/sh

The kernel will mount the root partition and init will start the bourne shell instead of rc and then a runlevel. Use the command passwd at the prompt to change the password and then reboot. Forget the single user mode as you need the password for that.
If, after booting, the root partition is mounted read only, remount it rw:

# mount -o remount,rw /
# passwd                             # or delete the root password (/etc/shadow)
# sync; mount -o remount,ro /        # sync before to remount read only
# reboot

FreeBSD method 1
On FreeBSD, boot in single user mode, remount / rw and use passwd. You can select the single user mode on the boot menu (option 4) which is displayed for 10 seconds at startup. The single user mode will give you a root shell on the / partition.

# mount -u /; mount -a               # will mount / rw
# passwd
# reboot

Unixes and FreeBSD and Linux method 2
Other Unixes might not let you go away with the simple init trick. The solution is to mount the root partition from an other OS (like a rescue CD) and change the password on the disk.

    * Boot a live CD or installation CD into a rescue mode which will give you a shell.
    * Find the root partition with fdisk e.g. fdisk /dev/sda
    * Mount it and use chroot:

# mount -o rw /dev/ad4s3a /mnt
# chroot /mnt                        # chroot into /mnt
# passwd
# reboot

Python Basics