Hdd clone, image file, dd + netcat in Linux.

Suppose you have a ~250GB hard disk and a removable hard disk whose capacity is ~320GB, and you want to backup all the files from the hard disk to the removable disk. With “dd”, it is a very easy task. Again, suppose your hard disk’s Linux device name is /dev/sda and the removable disk is /dev/sdb. The following command can copy all the content from /dev/sda to /dev/sdb:

dd if=/dev/sda of=/dev/sdb

Here, if=… sets the source and of=… sets the destination. “dd” doesn’t care of the contents of the hard disk. It just reads bytes from /dev/sda and writes them into /dev/sdb. It doesn’t know what are files. So, the hard disk file system and how many partitions it has are not important. For example, if /dev/sda is splitted into three partitions, the /dev/sdb will have the same partitions.

Most of time you don’t want to make a complete duplication of your hard disk. You may prefer to creating an image file of the hard disk and save it in other storage devices. The following command will create an image file “disk1.img” in your user’s directory from /dev/sda:

dd if=/dev/sda of=~/disk1.img

Since you have created an image file, you can compress it with “gzip” or “bzip2”:

gzip disk1.img #generates disk1.img.gz

or

bzip2 disk1.img #generates disk1.img.bz2

Backing up a hard disk partition is much similar to backing up a whole hard disk. The reason is that Unix/Linux uses device name, such as /dev/sda1, /dev/sda5… to indicate the partitions. For example, if you want to create an image file from the first partition of /dev/sda, use “dd” like this:

dd if=/dev/sda1 of=~/disk2.img

By the way, you can copy a partition to another partition completely, just set “of” to the partition’s device name. For example:

dd if=/dev/sda1 of=/dev/sdb5

This command will copy all the contents from /dev/sda1 to /dev/sdb5. You must be sure that the capacity of /dev/sdb5 is larger than /dev/sda1.

To restore a partition or a hard disk from an image file, just exchange the arguments “if” and “of”. For example, restore the whole hard disk from the image file “disk1.img”:

dd if=disk1.img of=/dev/sda

Restore the first partition of /dev/sda from the image file “disk2.img”:

dd if=disk2.img of=/dev/sda1

How to do it by network ?
Netcat is also a really handy utility for cloning drives as it allows you to do the transfer over the network (make sure you have a fast connection) without putting copy-from and copy-to drives in the same machine.
On the machine you are copying from:

dd if=/dev/sda | nc 10.1.1.2 9000
 
On the machine you are copying to (assume IP is 10.1.1.2):

nc -l -p 9000 | dd of=/dev/sda

Increase port range available for applications in Linux.

By default an average Linux distribution allows applications to use the following TCP port range for outgoing connections: 32,786-65,536. That’s why your system can handle up to 28,232 TCP sessions at time. Notice, this is more than enough if your Linux system is installed on the laptop or desktop and you just use it for occasional visits to yahoo.com, google.com. But if you run proxy like squid or some other services which open a lot of outgoing TCP connections you will likely hit ceiling of 28,232 soon.

First of all, let’s see current port range available for TCP sessions:

cat /proc/sys/net/ipv4/ip_local_port_range

Most likely the output will show something like this one “32786 65536″. In order to expand this range you can either echo modified range into above file in /proc filesystem (temporary solution) or add corresponding line into /etc/sysctl.conf (constant solution).

To temporarily expand port range from 28,232 to 40,000 do the following:

root# echo "24000 64000" > /proc/sys/net/ipv4/ip_local_port_range

To make sure new port range will be applied after reboot add the following line to /etc/sysctl.conf:

net.ipv4.ip_local_port_range="24000 64000"

or just execute this:

root# sysctl -n net.ipv4.ip_local_port_range="24000 64000"

How to wireless in Linux box.

Quick description of how to do it:

cat /etc/wpa_supplicant/wpa_supplicant.conf


network={
ssid="name_of_a_wireless_network"
key_mgmt=WPA-PSK
psk="password_to_a_wireless_network"
}

next step:

cat /etc/network/interfaces


#if your wireless card is wlan0, otherwise change to the right wireless card.
allow-hotplug wlan0
iface wlan0 inet static
address your_ip
netmask your_mask
gateway your_gateway
dns-nameservers your_dns
pre-up wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -B
post-down killall -q wpa_supplicant

Why that ? because it works on all linux distributions.

Change the default editor on Debian.

Debian uses for visudo, crontab, reportbug and other similar applications the default text editor, if you want to change it use the update-alternatives command.

Run this on a CLI.

root# update-alternatives --config editor

The options in my Debian system are:

There are 6 alternatives which provide `editor’.

Selection Alternative
———————————————–
1 /bin/ed
2 /bin/nano
3 /usr/bin/vim.tiny
*+ 4 /usr/bin/vim.gnome
5 /usr/bin/mcedit-debian
6 /usr/bin/emacs21

Press enter to keep the default[*], or type selection number:
The + sign denotes the default option, and the * sign denotes the actual selection, to change from vim to nano, just press 2 in this example and the ENTER.

Listen online Irish/any radio station.

A few things what we have to do to listen the radio in linux console.

1. Get the radio stream from website:

wget http://dynamic.rte.ie/av/live/radio/radio1.smil

2. After that find the stream:

user@zeus:~$egrep "rtsp|mms" radio1.smil

Some of radio stations broadcasts the stream by rtsp, some of them uses mms, but what’s sure one of them must be there.

If you know the stream of your favorite station, just write:

user@zeus:~$ mplayer rtsp://live2.rte.ie/redundant/1516.ra

Turn off periodic FSCK at booting time.

Forces drives to be checked once for every 30 times the filesystem is mounted. This means that on an average, once every 30 times you bootup your computer, the filesystem integrity is checked.
To disable filesystem integrity check forever do:

$sudo tune2fs -c -1 `mount | awk '$3 == "/" {print $1}'`

or

$sudo tune2fs -c -1 /dev/yourhdd

but if you don’t want disable filesystem check forever, only for the next bootup, create a file called /fastboot.

$sudo touch /fastboot

will disable filesystem check for the next time you bootup. Since the /fastboot file is removed during bootup, this will disable filesystem check only once – for the one time you bootup after you create the /fastboot file
On the contrary to force a filesystem check the next time you bootup, create a file called /forcefsck by doing

$sudo touch /forcefsck

How to save CPU and life of battery.

apt-get install cpufrequtils

next step:

cpufreq-set -g performance

if have 2 cpu:

cpufreq-set -c1 -g performance

get more information about your cpu by:

cpufreq-info

Which governor to use? Available governors:

* performance (default) — The performance governor is built into the kernel and runs the CPU(s) at maximum clock speed
* cpufreq_ondemand (recommended) — Dynamically increases/decreases the CPU(s) clock speed based on system load
* cpufreq_conservative — Similar to ondemand, but more conservative (clock speed changes are more graceful)
* cpufreq_powersave — Runs the CPU at minimum speed
* cpufreq_userspace — Manually configured clock speeds by user

I use at home “performance” and “powersave” on travel with my laptop.

How do I find text within files in Linux ?

This document is for users looking for information about finding text within one or more files on their computer. One of the easiest methods of locating text contained within a file on a computer running Linux is to use the grep command. Below is a basic example of a command used to locate any file containing the word “linux”.

find / -type f -exec grep -H 'linux' {} \;

Added by the LiNiO long before his died:
try also:
fgrep -rw linux path