Color-ize your scripts

To color your shell script output, you will need to echo out an escape sequence \033[$color1;$color2;$stylem.

You can specify one or more colors or styles, just delimit the colors or styles with a semicolon and end the list with a ‘m’.

Color is given as:

Color Foreground Background
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47

Style is given by:

Stle Code
Normal 0
Bold 1
Underline 4

Examples:
To print something as underlined green:

\033[33;4mYellow Underlined Text\033[0m

Example:
Yellow Underlined Text

To print something underlined, red, with a blue background:

\033[31;44;4mRed Underlined with a Blue Background Text\033[0m

Example:
Red Underlined with a Blue Background Text

To print the above without the underline, just remove the ‘4’

\033[31;44mRed with a Blue Background Text\033[0m

Example:
Red with a Blue Background Text

SunOS 5.10: Quick Notes


Where are programs like make, gcc, python, etc?

They are all under /usr/ccs/bin and /usr/sfw/bin.

Utilities like whoami are under /usr/ucb

You should set them in your PATH.


Setting up the default gateway under SunOS

Run route add default x.x.x.x and also add the IP to the /etc/defaultrouter file such that it will survive a reboot

This is analogous to route add default gw x.x.x.x under linux.


Extracting a .tar.gz archive

Run gzip -dc File.tar.gz | tar xf - to extract File.tar.gz

This is analogous to tar -xzf File.tar.gz under linux.

What does the linux ‘top’ CPU fields stand for?

The first few lines of the top command looks similar to this:

Tasks: 137 total, 1 running, 136 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 4.2%sy, 90.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 5.8%st
Mem: 786432k total, 715152k used, 71280k free, 61012k buffers
Swap: 2096472k total, 18304k used, 2078168k free, 168068k cached

The fields on the second line breaks down the CPU usage into 8 categories.

us: user CPU used by user processes
sy: system CPU used by system/kernel processes
ni: nice CPU used by processes that were reniced
id: idle CPU not used
wa: io wait Essentially idle CPU waiting on IO devices
hi: hardware irq CPU used to service hardware IRQs
si: software irq CPU used to service soft IRQs
st: steal time CPU time which the hypervisor dedicated (or ‘stole’) for other guests in the system.

RRDTool yum dependencies on CentOS

If you are trying to compile RRDTool (at the time of writing, 1.4.5) on CentOS 5.x, you will need install the following packages on yum:

yum install cairo-devel libxml2-devel pango-devel pango libpng-devel freetype freetype-devel libart_lgpl-devel

Then, just compile rrdtool as usual:

wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.5.tar.gz
tar -xzf rrdtool-1.4.5.tar.gz
cd rrdtool*
./configure --prefix=/opt/rrdtool
make
make install

Redirecting / Merging stdout and stderr streams

To merge the standard out and standard error streams from a program, use 2>&1.

Example: umount /adsf/asdf 2>&1 will output the errors into standard out instead of standard error as usual.

Note that:

  • 2 is the file descriptor for stderr.
  • 1 is the file descriptor for stdout.
  • & accepts a file descriptor which can then be treated as a file.

Thus, 2>&1 in turns ‘writes data’ from stderr into stdout.

Reset MySQL Root Password

Here’s a quick way to reset your mysql root password. Replace ‘NewRootPass’ with your new root password and run the following as root.

/etc/init.d/mysql stop
mysqld --skip-grant-tables --user mysql &
mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('NewRootPass') WHERE User='root'"
/etc/init.d/mysql restart

Note: This was done on CentOS. You might need to adjust the path to mysql’s startup scripts accordingly.

Restarting Services in Solaris

To restart a service under solaris, run the scripts under

/lib/svc/method/

This is more or less equivalent to the startup scripts in /etc/init.d/ under linux.

For instance, to restart SSH, run:

/lib/svc/method/sshd restart

You may also want to take a look at svcadm. To restart SSH, you can run

svcadm restart ssh

or

svcadm restart svc:/network/ssh

How to create a linux user with an empty/blank password

I had a need to create a ‘guest’ account in one of my linux installs which allows anonymous users within a trusted intranet to login to a specially crafted script. One of the issues that I was faced with was with passwd refusing to accept a blank password. To let empty password logins, we must manually change the account password.

When we look at /etc/shadow, we will see something similar to:

username:$1$ADUODeAy$gRz7rO6P5lFcPpYwqd7Eb0:14929:0:99999:7:::

The underlined part is the password hash. The hash is delimited by $. Things to know:

  1. first $1 means the hash is a md5 hash
  2. second $ADUODeAy is the salt
  3. third $gRz7rO6P5lFcPpYwqd7Eb0 is the actual password hash

To generate this hash manually, we can use openssl:

# openssl passwd -1 -salt ADUODeAy
Password: [enter]
$1$ADUODeAy$eCJ1lPSxhSGmSvrmWxjLC1

Note that the first parameter, -1, tells openssl to use md5 to generate the hash. This is the same 1 from the original hash above.

Replace the existing hash in /etc/shadow with the hash generated by openssl. The account now essentially has an empty password.

Notes:

  • You will need to temporarily change the permission of /etc/shadow in order to write to it.
  • You will need to enable ‘PermitEmptyPasswords’ in /etc/ssh/sshd_config for empty password logins to work
  • This can easily be a security risk to your machine! Ensure the account and server is locked down or use SSH keys for passwordless logins! Remember, by default, users can SSH tunnel through this guest account. You must consider the implications of enabling such an account on your machine.