Spying on the console.

Some software prints error messages to the console that may not necessarily show up on your SSH session. Using the vcs devices can let you examine these. From within an SSH session, run the following command on a remote server:

# cat /dev/vcs1

This will show you what is on the first console. You can also look at the other virtual terminals using 2, 3, etc. If a user is typing on the remote system, you’ll be able to see what he typed.

SSH login without password.

If you need to login from a serverA to a serverB using no password.

First what you have to do is login to a serverA as a user to generate a pair of authentication keys.
Do not enter a passphrase!

user@serverA:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
90:48:02:a5:3a:55:28:91:e2:29:7d:f8:e6:93:a1:e4 user@serverA
user@serverA:~>

Now use the ssh to create a directory ~/.ssh as a user “user” on a serverB. The directory may already exist there, in this case do not create that directory:

user@serverA:~> ssh user@serverB mkdir -p .ssh
user@serverB password:

Finally copy a new public key to user@serverB .ssh/authorized_keys and enter the userB password the last time:

user@serverA:~> cat .ssh/id_rsa.pub | ssh user@serverB 'cat >> .ssh/authorized_keys'
user@serverB password:

From now, you can login to a serverB as a user “user” from a serverA without typing a password.

Network File System in Debian.

Installing NFS in Debian

Making your computer an NFS server or client is very easy. The Debian NFS client needs:

# apt-get install nfs-common portmap

while a Debian NFS server needs:

# apt-get install nfs-kernel-server nfs-common portmap

NFS Server Configuration.

NFS exports from a server are controlled by the file /etc/exports. Each line begins with the absolute path of a directory to be exported, followed by a space-seperated list of allowed clients.

/etc/exports
/home 192.168.1.2(rw,no_root_squash) www.domain.com(ro)
/usr 192.168.1.2/24(ro,insecure)

A client can be specified either by name or IP address. Wildcards (*) are allowed in names, as are netmasks (e.g. /24) following IP addresses, but should usually be avoided for security reasons.

A client specification may be followed by a set of options, in parenthesis. It is important not to leave any space between the last client specification character and the opening parenthesis, since spaces are intrepreted as client seperators.

For each options specified in /etc/exports file can be check export man pages.
If you make changes to /etc/exports on a running NFS server, you can make these changes effective by issuing the command:

# exportfs -a

NFS Client Configuration

NFS volumes can be mounted by root directly from the command line. For example:

# mount files.domain.com:/home /mnt/nfs

mounts the /home directory from the machine files.domain.com as the directory /mnt/nfs on the client. Of course, for this to work, the directory /mnt/nfs must exist on the client and the server must have been configured to allow the client to access the volume.

It is more usual for clients to mount NFS volumes automatically at boot-time. NFS volumes can be specified like any others in /etc/fstab.

/etc/fstab
192.168.1.3:/home /home nfs rw,rsize=4096,wsize=4096,hard,intr,async,nodev,nosuid 0 0
192.168.1.3:/usr /usr nfs ro,rsize=8192,hard,intr,nfsvers=3,tcp,noatime,nodev,async 0 0

There are two kinds of mount options to consider: those specific to NFS and those which apply to all mounts. Consider first those specific to NFS.

For each options menctioned in /etc/fstab file check the man pages of fstab.

How to mount SMB/CIFS/Windows shares under Linux.

Just type:
apt-get install smbfs
then use the mount command to mount remote windows partition or windows share under Linux as follows:

mount -t cifs //192.168.1.10/download -o username=julia,password=julia /mnt/Julia

or with all rwx permissions:

mount -t cifs //192.168.1.10/download -o iocharset=utf8,file_mode=0777,dir_mode=0777,username=julia,password=julia /mnt/Julia

also you can add it to /etc/fstab (make it “automatic”)

//192.168.1.10/download /mnt/Julia smbfs auto,username=julia,password=julia,uid=1000,umask=000,user 0 0

but the better way is keep your username & password in a file only readable by root

//192.168.1.10/download /mnt/Julia cifs credentials=/root/.mnt_Julia,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

what should be in .mnt_Julia and how to make it:

touch /root/.mnt_Julia
echo "username=julia" >> /root/.mnt_Julia
echo "password=julia" >> /root/.mnt_Julia

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"

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.

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.