Tag Archive for linux

Automatically Mount A USB Drive In Ubuntu Server

USB drives can be easily and automatically mounted on Ubuntu Server with the help of USBmount which is available from the Ubuntu and Debian repositories. Fortunately, USBmount requires little configuration in order to work. The following was tested in Debian 7 Wheezy and Ubuntu 13.04.

Installation and Configuration

Make sure you run apt-get update/upgrade before starting the tutorial.

sudo apt-get update
sudo apt-get upgrade

Now install USBmount from the repositories.

sudo apt-get install usbmount

USBmount mounts all USB drives in /media/usb*.

Now plug a USB drive and wait for it to be detected and mounted. As long as the host OS supports the File System it should be mounted.

USBMOUNT

To verify whether the USB drive was mounted correctly you can use df -h to view all available drives and their respective mount points. My 4GB USB drive is accessible from the /media/usb0 mount point.

user@debian:~$ df -h
Filesystem                                              Size  Used Avail Use% Mounted on
rootfs                                                   49G   18G   29G  38% /
udev                                                     10M     0   10M   0% /dev
tmpfs                                                   100M  432K  100M   1% /run
/dev/disk/by-uuid/af874479-b4ee-4d83-a3da-d2fdb48511ea   49G   18G   29G  38% /
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   404M     0  404M   0% /run/shm
/dev/sdb1                                               138G   14G  118G  11% /media/hdd1
/dev/sdc1                                               3.8G  6.6M  3.8G   1% /media/usb0

To un-mount a drive you can use umount.

sudo umount /media/usb0

Conclusion

USBmount is quite handy for mounting USB drives with no effort.

Mount A USB Drive In Ubuntu Server

Before we can mount a USB drive we need to gather some information about it. With the help of fdisk we can see that my 4GB USB drive was recognized as /deb/sdc1 and is formatted with FAT32.

sudo fdisk -l

Disk /dev/sdc: 4049 MB, 4049600512 bytes
255 heads, 63 sectors/track, 492 cylinders, total 7909376 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x04c91666

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     7909375     3953664    b  W95 FAT32

Now create a mount point where the USB drive will be mounted. Only one drive per mount point.

sudo mkdir /media/usb

There is a good change your USB drive will be formatted with FAT16/32 therefore using the parameter -t vfat will suffice, otherwise you can use -t ntfs or -t ext3 depending on the file system.

sudo mount -t vfat /dev/sdb1 /media/usb

Make sure drive was mounted with the help of df -h. The 4GB USB drive is mounted and accessible from the /media/usb mount point.

user@debian:~$ df -h
Filesystem                                              Size  Used Avail Use% Mounted on
rootfs                                                   49G   18G   29G  38% /
udev                                                     10M     0   10M   0% /dev
tmpfs                                                   100M  432K  100M   1% /run
/dev/disk/by-uuid/af874479-b4ee-4d83-a3da-d2fdb48511ea   49G   18G   29G  38% /
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   404M     0  404M   0% /run/shm
/dev/sdb1                                               3.8G  6.6M  3.8G   1% /media/usb

To un-mount the drive you can simply use the umount command folowed the mount point.

sudo umount /media/usb

SSH Permission Denied (publickey)

Had a peculiar problem last week where the computer at home I use to pull backups from a remote VPS could not longer SSH into the remote system.

user@deb7h:~$ ./vps-01-backup-www.sh
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(605) [Receiver=3.0.9]

After looking at directory permissions on both systems and user credentials as the possible source of the problem I came across this post where it recommends that permission for the private key file be set to 600.

chmod 600 ~/.ssh/id_rsa

The change in permission worked, perhaps the file had too high of a permission number which in not allowed.

As to how this happen I have no idea, but I probably must have carelessly changed the permission at some point. This happen in a Debian Wheezy system.

How to Split Files with the Split command in Linux

Split is a useful Linux command used to split files into chunks of various sizes. The Split command is a straight forward utility all you do is specify the target size of each chunk in bytes (-b) followed by the name of the file to be split.

split -b 200m -d ubuntu-12.04.2-server-amd64.iso

The 657MB Ubuntu ISO was split into four chunks. Notice the incremental numbering.

x00  x01  x02  x03

To join all the files back together we use another utility, cat will look for all files starting with X*, regroup them, and output the results in to the file ubuntu-12.04.2-server-amd64.iso.

cat x* > ubuntu-12.04.2-server-amd64.iso

Split is a simple utility that can split files of any size into more manageable chunks. I regularly use split when I am forced to download files over unreliable connections and with no possibility of transfering with Rsync.

Reference

http://linux.die.net/man/1/split

Monitor MDADM Rebuild Progress

Just in case you find your self creating or rebuilding an MDADM array here is a simple combination that will output every two seconds the status of the array.

We combine watch and cat retrieve and update the status of the array.

watch cat /proc/mdstat
Every 2.0s: cat /proc/mdstat                            Tue Mar  5 00:34:35 2013

Personalities : [raid1]
md1 : active raid1 sdc1[1] sdb1[0]
      262011712 blocks super 1.2 [2/2] [UU]
      [>....................]  resync =  3.1% (8126080/262011712) finish=20.7min
 speed=204065K/sec

unused devices: 

Change the Keyboard Layout for CentOS / Scientific Linux

Yes, for some reason one of my VMs was configured with a different keyboard layout:

[root@system7]# cat /etc/sysconfig/keyboard

KEYTABLE="it"
MODEL="pc105"
LAYOUT="it"
KEYBOARDTYPE="pc"

The solution is quite simple all you have to do is edit /etc/sysconfig/keyboard with your favorite editor to make the keyboard layout US friendly. I am using VI as an example you can use whatever text editor you prefer.

[root@system7]# vi /etc/sysconfig/keyboard

KEYTABLE="us"
MODEL="pc105+inet"
LAYOUT="us"
KEYBOARDTYPE="pc"

Save and Exit the text editor. Restart the system so the changes can take effect, now you should have a keyboard layout that you are familiar with.

Linux: How to Find a Hard Drive Serial Number

Like many other times I had to retrieve the serial number of a faulty SATA drive on a Linux box. You should retrieve and compare the serial number before replacing to reduce the chance of replacing a working drive instead of a faulty drive. Hdparm is a utility that allows for the retrieval of the serial number, if I remember correctly hdparm is included by default on Ubuntu, Scientific Linux users can install hdparm from the repositories by using yum install hdparm.

Note: hdparm requires access to the root account or similar.

Retrieve The Info

The hdparm output will be rather long, but all you need to look for is Serial Number: xx-xxxxxxxxx, this is where the serial number for the drive is contained.

# hdparm -I /dev/sdf

/dev/sda:

ATA device, with non-removable media
	Model Number:       WDC WD5001AALS-00L3B2
	Serial Number:      WD-WMASZ0044725
	Firmware Revision:  01.03B01
	Transport:          Serial, SATA 1.0a, SATA II Extensions, SATA Rev 2.5
Standards:
	Supported: 8 7 6 5
	Likely used: 8

Now you can physically verify whether the drive label matches the serial number you just obtained with the help of hdparm. Check twice you wouldn’t want to pull a working drive instead.

Wipe Multiple Drives Simultaneously

This is one way you can wipe or overwrite multiple drives simultaneously it requires a little tool called Dcfldd which is available for all distributions.

If you are an Ubuntu user you can install Dcfldd from the repositories:

# apt-get install dcfldd

CentOS and Scientific Linux can install Dcfldd from the RepoForge repositories:

# yum install dcfldd

Dcfldd Wipe

Dcfldd will overwrite drive sdb and sdc with zeroes. You can also increase the block size if you want.

# dcfldd if=/dev/zero of=/dev/sdb of=/dev/sdc bs=4M

If the operation completes successfully you should receive the message No space left on device meaning there is nothing left to overwrite on the drive(s).

5120 blocks (20480Mb) written.dcfldd:: No space left on device

If you don’t beliieve me here is a screenshot displaying the activity taking place on both sdb and sdc.

nmon

Conlusion

Dcfldd can become useful for when you have get rid of several drives at once, as you saw the command is quite easy to remember. If you have any questions feel free to leave a comment below and I will do my best to reply as soon as possible.

Home page: Dcfldd

Ubuntu: How to Create and Open Zip Files from the Command Line

If you have an Ubuntu/Debian server installation and need to open or create Zip file then you need to install the necessary packages to work with Zip files. Fortunately, we can easily add support for Zip to our installation by installing two packages(Zip, Unzip) both available from the repositories.

Note: This post assumes you have the ability to install packages, root or sudo access.

Creating a Zip Archive

Install Zip from the repositories.

# apt-get install zip

The entire process is quite simple to understand.

# zip -r new-file-name.zip directory-to-compress/
  • zip – This is the name of the application we will be using to compress
  • -r – Short for Recursive, only use this option is you are creating a compressed archived from a directory
  • new-file-name.zip – Name of the resulting compressed archive, the naming is up to you
  • directory-to-compress/ – Name of the directory we are about to compress

It is possible to add more than one directory in to the same archive.

# zip -r new-file-name.zip directory-1/ directory-2/

But, what if I only need to compress a single file. Similar process except we don’t need to use the -r(recurse) option.

# zip report.zip LitenDuplicateReport.csv

Opening a Zip archive

Opening or extracting the contents from a compressed archive is handled not by the Zip package but by a second package called Unzip.

# apt-get install unzip

With Unzip you have two options as to how the contents will be extracted, either you can extract the contents of the compressed archive to a directory or simply extract the contents to the current directory.

# unzip nc111nt.zip -d netcat/
  • unzip – Tool that will extract contents from Zip compressed archive
  • nc111nt.zip – Name of the archive we are extracting from
  • -d – Short for destination
  • netcat – Unzip will create a new directory called netcat where the contents will be extracted to

Or you can extract the contents of the archive to the current directory you are in.

# unzip nc111nt.zip

Conclusion

All you need is two packages and your installation will be able to work with Zip archives. I know Ubuntu/Debian have support for Gzip but if you work with Windows users and need to share files with them then Zip might be the only way to go.

Format a Volume as XFS in Debian and Ubuntu

To format a drive with the XFS file system make sure xfsprogs is installed, if not you can install xfsprogs from the repositories with the help of apt-get.

root@ubuntu:~# apt-get install xfsprogs

Before you start partitioning and formatting drives make sure you have the right drive. We can list all available drives in the system with the help of the ls command.

root@ubuntu:~# ls /dev/sd*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5  /dev/sdb  /dev/sdb1  /dev/sdc

In this installation I will format the drive /sdc, I know this because this is a blank drive with no partitions.

Now we partition the drive with the help of fdisk. Fdisk can be scary if you have never used it before, in order to partition the drive use the commands below. Note: this will create one big partition, use the values inside the brackets [] as a guide.

root@ubuntu:~# fdisk /dev/sdb

Command (m for help): [n]
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): [p]
Partition number (1-4, default 1): [1]
First sector (2048-335544319, default 2048): [press Enter]
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-335544319, default 335544319): [press Enter]
Using default value 335544319

Command (m for help): [w]
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Because we installed xfsprogs we can make use of mkfs.xfs to format the partition we created in the step above. -L will label the drive as media.

root@ubuntu:~# mkfs.xfs -L media /dev/sdc1

Create a mount point.

root@ubuntu:/dev# mkdir /mnt/dat1

Mount the drive.

root@ubuntu:~# mount -t xfs /dev/sdc1 /mnt/dat1

Let’s check the newly mounted volume, visible at the bottom.

root@ubuntu:/dev# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G  1.9G   46G   4% /
udev             80M  4.0K   80M   1% /dev
tmpfs            36M  272K   35M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none             88M     0   88M   0% /run/shm
/dev/sdc1       160G   33M  160G   1% /mnt/dat1

To permanently mount the new volume every time the system boots you will need to edit and add a new entry to /etc/fstab. You can use the echo command to easily add the new entry.

root@ubuntu:# echo '/dev/sdc1 /mnt/dat1 xfs defaults 0 0' >> /etc/fstab

Or manually add the entry below to fstab with the text editor of your choice.

/dev/sdc1 /mnt/dat1 xfs defaults 0 0

Conclusion

XFS is a good file system for those of us working with large files. If you have any questions leave a comment below.