Monday, February 27, 2012

RHEL 5 yum: Get repo package was installed from

If you need to get the source (repo) that a package was installed from:


1) yum -y install yum-utils 

2) sudo repoquery --repoid=<reponame> -a |xargs sudo yum list installed| egrep -i <packagename>


Also found:

Available:
yum --disablerepo=* --enablerepo=<repo_name> list | grep -v "installed" | grep <package_name>

Installed:
yum --disablerepo=* --enablerepo=<repo_name> list | grep "installed" | grep <package_name>

Thursday, February 16, 2012

RHEL: Identify if you are on a virtual machine (vm)

So you need to tell if you are working on a server that is a virtual machine or on physical hardware? This is for RedHat (RHEL), but may work on other *nix flavors...


cat /sys/class/dmi/id/product_name

or

/usr/sbin/virt-what

or

If you suspect that it may be a vmware vm, you can search for vmware tools.  Make sure path is set correctly, and:

vm<tab>

Tuesday, February 14, 2012

linux:shell Loop on a space delimited varialble


var="string with spaces"

# note that $var must NOT be quoted here!
for X in $var
do
    echo "$X"
done

Linux:shell see if arg1 has been provided

An easy way to tell if arg1 has been given when user called the script:

# Note the quotes around $1

if [ -z "$1" ]; then
    usage
else
    do-something
fi

Tuesday, February 7, 2012

Convert epoch time to date/time format

date -d @<epoch_time>

Example: date -d @1328608176
Gives: Tue Feb  7 04:49:36 EST 2012



Monday, January 30, 2012

Quickly Make a Backup of a File in Linux

So you are Admin-ing away, and you are following SOP and making backups of all the files you edit.  Right?  However, as you have 100 servers to do and 5 files on each, that is a lot of typing!  You become lazy and forget the backup on one file, and then it happens.  A reboot hangs and you have no idea why, except that you were just editing system files...

Here is a quick way to backup any file:
cp /path/to/file{,.bak}

Example:
cp /etc/fstab{,.bak.2012-01-30}

The above example will produce a file named: /etc/fstab.bak.2012-01-30

This may seem a trivial savings in typing, but when you are scripting or in a hurry it can be a great time saver.  It also protects the backup file name from any typos. 

Make those backups and save yourself the frustration of having to go into single user to fix it!  Better procedures equal less downtime.

Wednesday, January 25, 2012

Command to see info on GPG key

Command to see info on GPG key
 rpm -q --qf "%{NAME}-%{VERSION}-%{RELEASE} \n %{SUMMARY} \n" gpg-pubkey


Operate on data input from the command line

So you want to quickly sort a list without going through a lot of steps.  This is what was happening to me, and I found a quick and easy way to do that from the command line.  This technique will work for any other operation that you want to preform as well.

Here is the command:
cat << EOF | sort [enter]
[enter text here]
EOF [enter]

The [enter text here] will then be output to the command line sorted.  You can get very fancy with this technique. This comes in handy when you have a set of text already stored in the copy buffer (clipboard).  Then you can just copy and paste into the [enter text here] section and very quickly have your desired operation.

Here is another example:
cat << EOF |sort > test [enter]
[enter text here]
EOF [enter]

This does the same thing, but puts the results into a file called 'test'.  Obviously you can get very creative with this.  Then again, if you are going crazy with this a small shell script may be better.  Then again, small shell scripts can replace many of us... :)