Adding color to your bash prompt

As a big mac user, one of my long time complaints have always been about color in my bash shell not translating over to a Linux host I’m ssh’ing into (predominantly CentOS/RHEL hosts).  A work around I’ve found to address this was to edit my .bashrc on linux systems I use as follows:

export LS_OPTIONS=’–color=auto’
eval `dircolors`
alias ls=’ls $LS_OPTIONS’

If you want color in your mac terminal you can add the following lines to your .bash_profile under your home directory in /Users (e.g. /Users/jdoe).

For dark terminal theme users:

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

For light terminal theme users:

export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

Special thanks and credit to OSX Daily for the color OSX Terminal tips.

Edit:

So this above is all fine and dandy, but I’ve found that declaring terminal type as ‘terminal-color’ a better option for me.

SSH Tools :: Keychain

If you are a sysadmin that manages Linux systems, you’ve probably found that using ssh keys and keychain a must have.  If not, here are ways you can get setup.

CentOS/RHEL users can use rpmforge’s software repository to yum install keychain as opposed to building it themselves.  The CentOS wiki has very easy to follow documentation on how to do this.

For OSX, this is a pretty straight forward install from Funtoo’s keychain wiki page, with a .bash_profile update to make life easier for you.

After you leverage rpmforge’s software repo and install keychain, you will notice a .keychain directory in your home directory.  Generate a key for yourself via ssh-keygen.  You can specify key types as well (e.g. ssh-keygen -t dsa, the default generates rsa).

Next, you will need to copy your .ssh/id_rsa.pub key values over to a host you want to leverage ssh keys, and keychain with.

Manual edit/OSX solution:

Edit .ssh/authorized_keys on the remote host with your id_rsa.pub key (e.g. rsync -av –progress id_rsa.pub remotehost.fqdn:/home/user/.ssh/, then cat id_rsa.pub >> authorized_keys in the your .ssh directory)

On Linux simply utilize ssh-copy-id remotehost.fqdn.

OSX users can edit as noted above, or can create their own ssh-copy-id script.  You can also try trusting bastardized OSX ssh-copy-id scripts from the web.  Be sure to scour the code at your own risk if you decide to go this route.

Once your keys are setup, we can go ahead and start utilizing keychain.

keychain -Q –ignore-missing –nogui –timeout  ~/.ssh/id_rsa

  • –ignore-missing doesn’t warn if some keys can’t be found.  This is useful if you have a shared .bash_profile and your keys aren’t available on every machine keychain is run against.  
  • –nogui doesn’t honor SSH_ASKPASS, if it is set, it will cause the ssh-add to prompt on the terminal instead of any graphical program.  
  • -Q/–quick will take any existing ssh-agent process and use it.  

You can explore additional options in the keychain man pages.

Personally, I prefer using an alias in my .bashrc/.bash_profile:

alias keychain=’keychain -Q –ignore-missing –nogui –timeout 86400 ~/.ssh/id_dsa ; . ~/.keychain/myhostname.fqdn-sh’.

The funtoo keychain wiki page suggests updating your .bashrc/.bash_profile with eval:

eval `keychain –eval –agents ssh id_dsa`

For OSX:

eval `keychain –eval –agents ssh –inherit any id_dsa`

Make sure to reference id_rsa if it is the key type you generated.

Now that you’re all setup, source your .bashrc or .bash_profile to finalize everything.  You can now start ssh’ing to hosts you have your keys setup on without a password or passphrase.

Linux Disk Usage Commands

So as a sysadmin we should already be pretty familiar with the du command.  It’s a command that allows us to see file and directory sizes, as well as view both decreasing or increasing from the smallest or biggest file.

Here are a few basic du command arguments that have helped me over the years.

du -a

du -all shows all files and directories included in the directory you’re currently in

du -h

du -human-readable shows all files and directories in a format that’s better than just bytes.  So we’ll see listings in KB, MB, and GB.

du -s

du -summarize displays only a total of everything in a directory in bytes.  A better way to do this is (du -hs).

du -S

du -separate-directories does not include sub directory sizes.  Sometimes you just want to see files.

du –time

This shows the last modification time for each file, directory, and sub-directory.

Here are some more intermediate to advanced du commands that might be useful:

find -type f -exec du -sh {} +  | sort -r | head

Search the current directory you are in, as well as sub-directories in it for the biggest files.  Great for auditing.

du -skh .[!.]* *| sort -n

Shows file, directory, and sub-directory sizes including hidden with increase sorting.

du -skh .[!.]* *| sort -nr

Shows file, directory, and sub-directory sizes including hidden files with decrease sorting.

du –max-depth 1 -xh / or du -msh * | sort -n | tail -5

Shows file, directory, and sub-directory sizes including hidden at a max depth of a directory.  The second command is just another way to do that.  Sometimes you don’t need everything within everything.

Linux-Fu Part 1

I’ll do this from time to time when I come across some tidbits of information that I find useful to me.

  • pgrep and pkill – The first command will return the pid (process id) based on a name or other attribute.  pkill will signal a process with a patching name or attribute.  So if you want to kill processes being run by a user per say, you would execute a pkill -U USERNAME.
  • lsof – This command is usually pretty well known to most SysAdmins.  It allows us to see any files that are currently open on a particular filesystem.  It’s great for tracking down files that are locked open by processes or users, or finding files that have been deleted, but still alive for reasons I just mentioned.
  • iptraf – If you ever wanted to know where your traffic goes to and comes from, yum/apt-get install this sucker.  While you don’t get packet break down like you would from an app like WireShark, it’s still a pretty cool tool to track network traffic.
  • htop – This is like top on steroids.  Great tool that can be yum installed if you have rpmforge repos setup.

There, and back again…

So my bittersweet trip to Anaheim finally ended this past Saturday.  There were a couple things I enjoyed during my stay out there despite the fact that it was the city of Anaheim.

  • Walking to class/work is awesome.  I wish more things in life were pedestrian accessible to me.
  • An L.A. Fitness with a basketball court was conveniently next door to my hotel.  I loved coming home to the hotel, and heading straight over for some pick up games.  My best day there was Thursday,  as I finally adjusted my game to the regulars that played there.
  • I passed my RHCSA (Red Hat Certified System Administrator) exam and had my results and certification digitally delivered to me on the same day.  Initially, I was a little shaky on how I did due to relying on system-config-lvm rather than pvcreate for physical volume extent creation.
  • The Amtrak Surfliner train station was 5 minutes away from my hotel.  I was so happy to pick up Kristina on Friday night to top off a week away from San Diego.
  • The greatest place on earth, Disneyland!  I had not been to Disneyland since grad night in High School prior to Saturday’s visit.  I still remember exiting the park to the music of Bones Thugs and Harmony, The Crossroads.  At any rate, using Disney’s Fast Track as opposed to wasting an hour in line is a great idea.  Kristina and I were able to visit every ride in the park other than Matterhorn because it was down for maintenance.  At the end of the day I think we satisfied our Disneyland appetites for the next decade or so.
So Anaheim, I officially bid you adieu… See you at the Crossroads…

Mounting Images In Linux

Small useful info. that even “Jimmy Google” knows (Information/solutions found from people by performing a Google search).  I had to use this more when I was involved with game server hosting for system image cracking and modification.

To Mount:
losetup /dev/loop0 imagename.iso (.img, etc..)
mount /dev/loop0 /mnt
To Umount:
umount /mnt
losetup -d /dev/loop0
If you’re not familiar with the losetup command, in short it sets up and controls loop devices.  We use it here to associate a loop device with regular files.  A loop device is a psuedo-device that makes a file accessible as a block device.  You can  man losetup for more usage information.