Quick System Info – BASH

Here is a BASH script to get basic system inventory information about your computer and parse it into CSV format.

MANUFACTURER="$(dmidecode -s system-manufacturer)"
MODEL="$(dmidecode -s system-product-name)"
SERVICETAG="$(dmidecode -s system-serial-number)"
MEMORY="$(free |grep "Mem:" |awk '{print $2}')"
echo "${FIELDS}"$'\n'"${MYINFO}"

Removable EXTx drive for non-root Linux user

Today, we will cover setting up an external removable drive using the EXT3 file system, and to allow us to mount the drive as a normal (non-root) user. This allows us to keep existing Linux file permissions when we write to the drive, and features journaling which ensures that what we write to the removable drive, is completely written to disk.

This system also allows us to easily create separate mount points for distinct removable drives, which facilitates the use of multiple drives in a scripted backup strategy.

So let’s get started!

First thing we do is determine how the drive is recognized by the kernel:

From the command prompt:

sudo tail -f /var/log/messages
su -c tail -f /var/log/messages

and look for something on the order of “[sdb] Attached SCSI disk” after we plug in our removeable device.

The kernel output will tell us if there are any existing partitions (look for “sdb1″, “sdc1″ etc.)

Ctrl-C to exit tail

Next we need to create a partition table, if there are existing partitions you’ll need to ensure thay do not contain important data before you remove or overwrite them.

Again from the command prompt:

sudo fdisk /dev/sdb
su -c fdisk /dev/sdb

Choose “n” for “new partition”

Then “p” for “primary partition”

Then “1” for the first partition.

Then the defaults for the sectors.

Then “w” to write changes and quit.

Our next step is to create a filesystem

Again from the command prompt:

sudo mkfs.ext3 /dev/sdb1
su -c mkfs.ext3 /dev/sdb1

This might take a while depending on the drive size

While we wait, open another terminal window and continue working.

We are going to name our disk “BU_Linux” but feel free to change the name, but avoid spaces and wildcard/reserved characters in the name (No: “.”, “\”, “?”, “!”, etc.). We will mount the disk using the same name under the “/media” directory.

Again from the command prompt:

sudo mkdir -p /media/BU_Linux
su -c mkdir -p /media/BU_Linux

Next we need to backup “/etc/fstab”

sudo cp /etc/fstab /etc/fstab.org
su -c cp /etc/fstab /etc/fstab.org

Then we can edit “etc/fstab” using vi:

sudo vi /etc/fstab
su -c /etc/fstab

and add a line as below:

"LABEL=BU_Linux  /media/BU_Linux  ext3  defaults,user,noauto  0 0"

We then will go back to our prior console window, hopefully mkfs.ext3 has finished.

We now create the disk label using “e2label” which can display or change the filesystem label on the ext2, ext3, or ext4 filesystems.

Again from the command prompt:

sudo e2label /dev/sdb1 BU_Linux
su -c e2label /dev/sdb1 BU_Linux

Now let’s mount the drive!

Again from the command prompt:

mount -L BU_Linux

Let’s check if the drive is mounted.

Again from the command prompt:

mount |grep BU_Linux

Which should return:

“/dev/sdb1 on /media/BU_Linux type ext3 (rw,noexec,nosuid,nodev,user=MyUserName)”

Now let’s examine the drive.

Again from the command prompt:

df -h /media/BU_Linux

Which tells us how much free space is available


ls -al /media/BU_Linux

which should show us the “lost+found” directory

Now if we try to write a file ie.:

echo "This is a test" > /media/BU_Linux/test.txt

You’ll see that you cannot write to the disk.

So let’s fix this!

Again from the command prompt:

sudo chmod 777 /media/BU_Linux
su -c chmod 777 /media/BU_Linux

And try again!

echo "This is a new test" > /media/BU_Linux/new_test.txt

And we should have success.

This change will survive remounts because the permission you just changed is resident on the volume’s filesystem.

When you are finished you’ll need to unmount the file system before removing the drive.

Again from the command prompt:

umount /dev/sdb1

Learning the BASH shell

Today we’ll explore the BASH shell a bit.

BASH is extremely powerful, yet succinct.

To view the built-in BASH commands available on a Linux system try:
“man -k bash”

To save these to a file in your home directory for later review try:
“man -k bash |awk ‘{print $1}’ > ~/bash_commands_available.txt”

And to view the available options for each command try:
“help command_I_want_to_learn”
replacing “command_I_want_to_learn” with something like “cat”

To learn about the help system try:
“help help”

Timestamp your bash history

In Memoriam: Dennis Ritchie

I want to begin this blog with something incredibly useful, yet simple.

When you find yourself trying to remember when you made that all important, late night configuration change, it really helps to have timestamps in your bash history.

Try adding the lines below to your “.bashrc”

       export HISTTIMEFORMAT