Friday, June 29, 2007

Sort Files by Their Filesizes

Here's a convenient way of finding those space hogs in your home directory (can be any directory). For me, those large files are usually a result of mkfile event (testing purposes) and can be promptly deleted. Here's an example of its use.

# cd /export/home/esofthub
# ls -l | sort +4n | awk '{print $5 "\t" $9}'

Find recursively (a little awkward)
# ls -lR | sort +4n | awk '{print $5 "\t" $9}' | more

Thursday, June 28, 2007

Recursively Display File and Directory Inodes

I've had a few requests on how to show inodes recursively. I would recommend using the ls command with the recursive option. Using the /etc/security directory as an example, here's a quick way of doing that.

# cd /etc/security
# ls
audit audit_startup bsmunconv lib
audit_class audit_user dev policy.conf
audit_control audit_warn device_allocate prof_attr
audit_data auth_attr device_maps spool
audit_event bsmconv exec_attr

# ls -iR | more
429447 audit
425561 audit_class
425562 audit_control
425751 audit_data
425563 audit_event
425747 audit_startup
425564 audit_user
425565 audit_warn
425568 auth_attr
425566 bsmconv
425567 bsmunconv
437267 dev
425749 device_allocate
425750 device_maps
425569 exec_attr
445066 lib
425571 policy.conf
425570 prof_attr
448971 spool

433367 localhost

433368 files

437268 audio
437269 fd0
437270 sr0
437271 st0
437272 st1

445067 audio_clean
445068 fd_clean
445069 sr_clean
445070 st_clean

257757 S92volmgt

Show inodes for a filesystem

# ncheck /dev/dsk/c0t0d0s0

Delete and inode

Wednesday, June 27, 2007

Paginate a File or Command Output for Printing

There are times when you might need to print out documents that can span numerous pages. Often times, it's helpful to have the pages paginated with a detailed header (date, time, filename, and page number). The pr command can do these things. Here are some examples of its use.

Prints 5 Line Header and Trailer. It also provides date, time, filename, and page number.
# pr esofthub.txt | lp

Jan 9 08:52 2006 esofthub.txt Page 1

Suppresses 5 Line Header and Trailer. It also suppresses date, time, filename, and page number.
#pr -t esofthub.txt | lp

Double spaced and prints 5 Line Header and Trailer. It also provides date, time, filename, and page number.
# pr -d esofthub.txt | lp

Double spaced, numbered lines, and prints 5 Line Header and Trailer. It also provides date, time, filename, and page number.
# pr -dn esofthub.txt | lp

Lists a directory and prints its contents in 6 columns without a header
# ls | pr -t6 | lp

Lists a directory and starts printing on page 4 with a header
#ls | pr +4 | lp

Lists a directory and prints a modified header (filename is replaced with esofthub pagetest)
#ls | pr -h 'esofthub pagetest' | lp

Tuesday, June 26, 2007

Unzip Zip Files in Unix

Since the last post dealt with the zip command, it's only fitting that its counterpart, the unzip command, is presented here. The unzip command is compatible with the PC pkunzip command. Here's the syntax.

# cd /tmp (or whatever your destination directory is supposed to be)
# unzip

Recursively Zip Files and Directories

It's not uncommon for system administrators to zip files and directories, especially if they are frequently sending data over email. Personally, I prefer it over the UNIX compress command. By the way, the UNIX zip command is compatible with the PC pkzip command. Here's the syntax.

Files and directories added to myzipfile
# zip -r myzipfile esofthub softhub.tar archive_dir mypic.gif mydir restrict_dir
# ls -l

Don't try to compress these file extensions
# zip -rn .zip: .jpg: .png: .gif myzipfile esofthub softhub.tar archive_dir mypic.gif mydir restrict_dir

Files only
#zip zipfile myfile1 myfile2 myfile3 myfile4 ...
#ls -l

Monday, June 25, 2007

Add, Change, Delete, Monitor, and Flush the Routing Table

Awhile ago, I posted a post regarding adding a route, but it only covered a static route. Why? For that particular day's post, I was dealing with a static route issue.

Here's a quick and dirty guide to adding (nets, default route), deleting, changing, flushing, and monitoring the routing table.

Add a route to the routing table
# route add net 192.168.X.0 esofthub 1
add net 192.168.X.0: gateway esofthub

Change destination in the routing table
# route change net 192.168.X.0 router-1
change net 192.168.X.0: gateway router-1

Add default route
# route add default esofthub 1
add net default: gateway esofthub

Delete route from the routing table
# route delete net 192.168.X.0 esofthub
delete net 192.168.X.0: gateway esofthub

Determine which interface will be used to contact the URL/hostname
# route get
route to:
destination: default
mask: default
gateway: router-1
interface: hme0
recvpipe sendpipe ssthresh rtt,ms rttvar,ms hopcount mtu expire
0 0 0 0 0 0 1500 0

Monitor routing requests
# route monitor

Remove all entries from the routing table
# route flush

Sunday, June 24, 2007

Check Link Mode, Link Status, and Link Speed for Ethernet Interface

The ndd command is also used to check for link mode, link status, and link speed of an Ethernet interface. Here's an example.

# ifconfig -a
lo0: flags=1000849 mtu 8232 index 1
inet netmask ff000000
hme0: flags=1000843 mtu 1500 index 2
inet netmask ffffff00 broadcast
ether 8:0:20:xx:xx:xx

# ndd -set /dev/hme instance 0
# ndd -get /dev/hme link_mode
# ndd -get /dev/hme link_status
# ndd -get /dev/hme link_speed

What do the return values mean?
mode: 0 is half duplex and 1 is full duplex
status: 0 is down and 1 is up
speed: 0 is 10 megabits and 1 is 100 megabits

Saturday, June 23, 2007

Full Duplex Settings for Network Interface

The other day I was having problems auto negotiating duplex settings between a remote switch and my workstation. Initially, the ndd command was used to set up the 100Mb interface in full duplex mode. But later, the /etc/system file was modified to make the settings permanent following a reboot.

Determine status
# netstat -k hme0 | grep link

# ndd -set /dev/hme adv_autoneg_cap 0
# ndd -set /dev/hme adv_100hdx_cap 0
# ndd -set /dev/hme adv_100fdx_cap 1

# vi /etc/system
#set hme:hme_adv_autoneg_cap=0
#set hme:hme_adv_100hdx_cap=0
#set hme:hme_adv_100fdx_cap=1

Friday, June 22, 2007

User Switching to root or root Switching to User

For auditing purposes, it could be useful to know who was switching to the root user or vice versa. You can find this type of information in the /var/adm/sulog file. Here's an example of its output.

# cd /var/adm
# more sulog | more
SU 05/27 22:29 + pts/5 root-esofthub
SU 05/27 22:59 + pts/9 root-topbloglists
SU 05/29 23:45 + pts/6 esofthub-root
SU 05/29 23:46 + pts/6 root-freeadlists
SU 05/29 23:49 + pts/6 root-esofthub
SU 05/30 22:02 + pts/6 root-freeadlists
SU 05/30 22:14 + pts/3 esofthub-root
SU 06/02 19:40 + pts/3 root-esofthub
SU 06/02 19:44 + pts/3 esofthub-root
SU 06/16 23:37 + pts/3 root-esofthub3
SU 06/17 20:57 + pts/8 root-esofthub
SU 06/17 21:12 + pts/8 esofthub-root
SU 06/22 20:31 + pts/7 root-freeadlists

Display Binary Audit Log Information in ASCII Format

The files in the /var/audit directory are useful for inspection purposes; however, they are in a binary format. I use the praudit command to translate them into a human readable format. Here's an example of the praudit's output, which are logged telnet/local connections.

To terminate the current audit file.
#cd /var/audit
#audit -n
#praudit 20070621120225.20070621120111.esofthub | more
file,Thu 21 Jun 2007 09:02:25 PM KST, + 843354 msec,
header,44,2,system booted,na,Thu 21 Jun 2007 09:02:09 PM KST, + 449998911 msec
text,booting kernel
header,81,2,login - local,,Thu 21 Jun 2007 09:02:41 PM KST, + 300000097 msec
subject,root,root,other,root,other,314,314,0 0 esofthub
text,successful login
header,81,2,login - telnet,,Fri 22 Jun 2007 08:00:38 PM KST, + 889999653 msec
subject,root,root,other,root,other,606,606,746 65559 192.168.X.XXX
text,successful login
header,81,2,login - telnet,,Fri 22 Jun 2007 08:03:24 PM KST, + 630007314 msec
subject,root,root,other,root,other,619,619,749 196631 192.168.X.XXX
text,successful login


#cd /var/audit
#auditreduce | praudit | more

Wednesday, June 20, 2007

Display System or User Defined Mail Aliases

On various occasions, I've been asked to show the members of a system or user defined mail alias. To satisfy this request, I could have simply performed a more or cat on the local /etc/mail/aliases file. But I prefer to use the praliases command. Here's an example.


If you create another mail alias, you will need to execute the newaliases command.

#vi /etc/mail/aliases
egalias:member1,member2, member3, member4

Tuesday, June 19, 2007

Find Total Number of Reboots for a Workstation or Server

Here's a quick and dirty way of checking how many times your server/workstation was rebooted. First, I obtained the total number of reboots since Oct and then obtained the total number for each month. Here's an example.

Total reboots
wtmp begins Sat Oct 22 20:10
# last | grep reboot | wc -l

Find the total number of reboots for each month
# for i in Oct Nov Dec Jan Feb Mar Apr May Jun
> do
> echo server was rebooted `last | grep $i | grep reboot | wc -l` in $i
> done
server was rebooted 10 in Oct
server was rebooted 3 in Nov
server was rebooted 0 in Dec
server was rebooted 0 in Jan
server was rebooted 1 in Feb
server was rebooted 0 in Mar
server was rebooted 1 in Apr
server was rebooted 3 in May
server was rebooted 1 in Jun

Monday, June 18, 2007

Disk to Disk Backup using ufsdump

I was asked the other day how to create a bootable backup disk. In a previous post, I used the dd command to perform this task. However, you can use the ufsdump/ufsrerestore procedure, too. Here are 2 generic scripts, one c shell and the other Bourne shell.

Note: This was done on a Solaris 8 system and in single user mode.

C Shell Script

foreach myslice ( 0 3 4 )
echo y | newfs /dev/rdsk/c1t1d0s$myslice

cd /
foreach myslice ( 0 3 4 )
mount /dev/dsk/c1t1ds$myslice /mnt
cd /mnt
ufsdump 0uf - /dev/rdsk/c1t0d0s$myslice | ufsrestore rf -
rm restoresymtable
cd /
umount /mnt

/usr/sbin/installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/c1t1d0s0
mount /dev/dsk/c1t1d0s0 /mnt
sed 's/t0/t1/g' /mnt/etc/vfstab > /var/tmp/vfstab
cp /var/tmp/vfstab /mnt/etc/vfstab
umount /mnt

Bourne Shell Script

for myslice in 0 3 4
echo y | newfs /dev/rdsk/c1t1d0s$myslice

cd /
for myslice in 0 3 4
mount /dev/dsk/c1t1ds$myslice /mnt
cd /mnt
ufsdump 0uf - /dev/rdsk/c1t0d0s$myslice | ufsrestore rf -
rm restoresymtable
cd /
umount /mnt

/usr/sbin/installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/c1t1d0s0
mount /dev/dsk/c1t1d0s0 /mnt
sed 's/t0/t1/g' /mnt/etc/vfstab > /var/tmp/vfstab
cp /var/tmp/vfstab /mnt/etc/vfstab
umount /mnt

Sunday, June 17, 2007

Delete a User Account via passmgmt

You can also delete a user account via passmgmt. However, you will have to delete the home directory manually. Here's an example.

# passmgmt -d esofthub
# more /etc/passwd | grep esofthub
# more /etc/shadow | grep esofthub
# rm -r /export/home/esofthub

Modify a User Account via passmgmt

You can modify a user account with passmgmt. In this example, I'm modifying the comment field. Here's a before and after look.

# more /etc/passwd | grep esofthub
esofthub:x:1006:10:esofthub account:/export/home/esofthub:/bin/csh
# passmgmt -m -c "My SysAd Blog" esofthub
# more /etc/passwd | grep esofthub
esofthub:x:1006:10:My SysAd Blog:/export/home/esofthub:/bin/csh

Add a User Account via passmgmt

You can use admintool and useradd utilities to add user accounts. But there is another utility called passmgmt that will perform a similar function. However, it will not create a home directory. You will have to do that manually. Here's an example.

# passmgmt -a -c "esofthub account" -h /export/home/esofthub -u 1006 -g 10 -s /bin/csh esofthub
# pwck
esofthub:x:1006:10:esofthub account:/export/home/esofthub:/bin/csh
Login directory not found

# mkdir -p /export/home/esofthub
# chown -R esofthub:staff /export/home/esofthub
# pwck esofthub
# passwd esofthub
New password:
Re-enter new password:
passwd (SYSTEM): passwd successfully changed for esofthub
# su - esofthub
Sun Microsystems Inc. SunOS 5.8 Generic Patch October 2001

Saturday, June 16, 2007

Find the Prime Factors of a Number

The factor utility finds the prime factors of a number. This is a very convenient and cool UNIX command. Here is its syntax.

# factor 101

# factor 111

# factor 1000

# factor 999

Convert Quantities in Standard Scales to Their Equivalents in Other Scales

The units command can convert a quantity in a standard scale to its equivalent in another scale. It can handle converting feet to meters, miles to kilometers, pounds to kilograms, degrees to radians, grams to ounces, and many more. Here's an example of a few conversions.

# units
you have: 1inch
you want: cm
* 2.540000e+00
/ 3.937008e-01

you have: 1ft
you want: meter
* 3.048000e-01
/ 3.280840e+00

you have: 1mile
you want: km
* 1.609344e+00
/ 6.213712e-01

you have: 127radian
you want: degrees
* 7.276564e+03
/ 1.374275e-04

you have: 127degrees
you want: radian
* 2.216568e+00
/ 4.511479e-01

you have: 1km
you want: mile
* 6.213712e-01
/ 1.609344e+00

you have: 1kg
you want: lbs
* 2.204623e+00
/ 4.535924e-01

you have: 1lbs
you want: grams
* 4.535924e+02
/ 2.204623e-03

you have: c
you want: m
2.997925e+08 m/sec
1.000000e+00 m

you have: g
you want: m
9.806650e+00 m/sec2
1.000000e+00 m

you have: mole
you want: fuzz
* 6.022137e+23
/ 1.660540e-24

you have: 1mile
you want: feet
* 5.280000e+03
/ 1.893939e-04

Friday, June 15, 2007

Clone Primary Partition Table to Second Hard Drive

A colleague of mine was trying to duplicate the partition table of the primary disk onto the backup hard drive (same size, same brand/model). I mentioned using prtvtoc command to display the Volume Table of Contents and then pipe its output to the fmthard command. Unfortunately, he was already populating the second hard drive via the manual process. At any rate, here is an example.

# prtvtoc /dev/rdsk/c1t10d0s2 | fmthard -s - /dev/rdsk/c1t11d0s2

Thursday, June 14, 2007

Display the Internet to Ethernet Address Translation Tables

Here is an easy way to find the Internet Protocol address that is mapped to a hardware address on a local area network. The ubiquitous arp command outputs this information as entries in a table. Here is an example.

# arp -a

Wednesday, June 13, 2007

Disable All Users From Logging Into the System

There are times when you may need to prevent users from logging into the system. Here is a way of doing that.

# cat > /etc/nologin
The system is currently unavailable to all users. Try back in two hours.

Here is the message you will get when you try to login as a user. As you can see, it echoes the contents of the /etc/nologin file.

# login
login: esofthub
The system is currently unavailable to all users. Try back in two hours.

Tuesday, June 12, 2007

Select Fields or Columns Using the cut Command

Selecting fields and columns is a common task for system administrators. The cut command is an ideal utility for accomplishing this task. Here are examples of its use (selecting disk slices and checking for passwords).

#df -k grep c0 grep d0s cut -b16-17

#cut -d: -f1-3 /etc/shadow

# cut -d : -f 1-2,7 /etc/passwd

Monday, June 11, 2007

Wrap or Fold Long Lines in a File for Printing

Occasionally, you may have to deal with files that exceed common printing parameters, e.g. columns 80 or 132. The fold command is a filter designed to deal with this type of issue. Personally, I have used it on a few log files. Here is an example of its use.

# fold –w 132 longlinefile | lp

Or output to a file
# fold –w 132 longlinefile > new_file

Sunday, June 10, 2007

Locate Commands by Keyword Lookup

I'm often asked whether Solaris has an index for commands or not. If the /usr/share/man/windex database exists, it does. The apropos command is used to perform the lookups. Here's an example.

If /usr/share/man/windex doesn't exist, use the following command to build the database:
#catman -w


#apropos shadow

The apropos command has the same output as the man -k pattern.

#man -k shadow

Saturday, June 09, 2007

The Universal Java FTP Client -- Free for Non-Commerical Use

Java FTP or JFTP is a GUI-based FTP client software. It can transfer files between various operating systems. It also incorporates Secured Sockets Layer (SSL) to securely transfer sensitive data.

JFTP is written entirely in Java, which means it can run on various platforms such as Windows, Mac OS X, Linux, Solaris, HP-UX, OS/2, UNIX, or any other platform with a compatible JVM (Java Virtual Machine).

I am a personal user of this product and it's a convenient tool, especially the multiple FTP sessions. Of course, I'm using the non-commercial version. However, the commerical version can be obtained for a nominal fee.

One last thing, you can run a Java Applet from their site. I would recommend downloading a copy because it seems to be faster. Btw, this was not a paid post.

Other FTP Clients/Servers

Friday, June 08, 2007

Getting Technical: "Be all you can be"

Today, I'm taking a break from my normal technical posting. So far, I have enjoyed blogging mostly about these HOWTOs, and I hope to continue into the future. Frankly speaking, I almost quit blogging in January 2007 (after 3 months) because it seemed too "obligatory" but nowadays I'm not minding it.

At any rate, I was asked to guest blog about my military experiences while in the U.S. Army by Steve of HMTK, also a former trooper. Thanks Steve.

Here is an excerpt of the post:
"This post was guest blogged by Roy Wood aka esofthub of My SysAd Blog and FreeAdLists. HMTK asked me to guest blog and share some of my military experiences with you. Let me preface this post by saying that I always thought I had the potential to make great strides in my life, but I was just surprised by how much of the confidence and determination I lacked would be gained from having been a soldier for the United States Army. I would like to dedicate this post to all of those who have and will serve in the U.S. Army, Hooah! ..."

The rest of this post can be found at "Be all you can be." I hope you find it somewhat interesting.

Thursday, June 07, 2007

Display Kernel Statistics with kstat

The kstat command is a handy command for providing kernel statistics to users. For each matching statistic, it reports the instance, module, name fields and actual values. Here are some examples of its use.

#kstat | more

#kstat -p | grep cpu
#kstat -m cpu_info
#kstat -n cpu_stat0
#kstat -s dfree

Do a listing
#kstat -l | more

Wednesday, June 06, 2007

Display Input and Output (I/O) Statistics with iostat

The input and output (I/O) statistics (iostat) command shows statistics regarding terminal, disk, tape, and CPU activity. I primarily use it in the extended format to monitor CPU and hard disk stats. Here's an example of its use at 3 second intervals.

#iostat 3

#iostat -ctx 3

#iostat -D 3

Tuesday, June 05, 2007

Display Virtual Memory Statistics with vmstat

The virtual memory statistics (vmstat) command shows statistics regarding process, virtual memory, disk, trap, and CPU activity. Frankly speaking, I primarily use it for CPU stats. Here's an example of its use at 3 second intervals.

#vmstat 3

Monday, June 04, 2007

Adding and Deleting a Swap File

On rare occasions, there might be a need to increase your system's swap space via a local file(s) on your filesystem. However, this should only be done for a temporary situation. Here is an example on how to set up a 100MB swap file and then delete it.

Get a summary of the VM
# swap -s

Create the swap file
# mkfile 100m /data/myswap

Add the swap
# swap -a /data/myswap

Do a listing of the added swap space
# swap -l

Get a summary of the VM
# swap -s

To remove the file swap space, do the following:
# swap -d /data/myswap
# rm /data/myswap

Verify swap file allocation was removed
# swap -s
# swap -l

Saturday, June 02, 2007

ASCII, HTML and Conversion Tables - Decimal, Hexadecimal, Octal, Binary

Here are two convenient lookup tables for ASCII and HTML. Also included is a conversion table for decimal, hexadecimal, octal, and binary. The tables are in Spanish and English.

Create a Crontab That Preserves Existing Entries

I've had a few queries on how to create a new crontab that preserves the existing entries.

You will need to ensure that the user making the edits has cron permissions -- check /etc/cron.d/cron.allow and /etc/cron.d/cron.deny. Note: for the first example, you may have to set the EDITOR variable. Here's the syntax.

#cd /var/spool/cron/crontabs
#crontab -l > esofthub
#crontab -e esofthub
make your edits


#vi esofthub
make your edits
#crontab esofthub

FYI: What do the six fields mean
minute 1-60
hour 0-23
day of month 1-31
month of year 1-12
day of week 0-6 (zero is Sunday)
Your edit/addition/script

The following entry will kick off every minute
* * * * * echo "I'm kicked off every minute"

Friday, June 01, 2007

Download Solaris 10 Operating System at no Cost

Get access to Solaris 10 software, the most advanced operating system on the planet -- at no charge.

Join the Free Solaris Program
Get the following free:
Solaris 10 Download or Free Software Kit
Solaris 10 Entitlement Document
Solaris How to Guides
Access to Solaris Learning Centers
and more...