Unix for the System Administrator

cpio
cron
filesystem and files
find
ftp
logging
mail
man pages
more
nfs
performance monitoring
printing
processes
security
shells
tar
user administration

Cpio

Copies files to and from archives.

-i  --> extract (copy-in mode)
-d -->  create directories as needed
-m -->  preserve modification time
-v  -->  verbose

cpio -idmv < file.cpio

Cron

Consists of the cron daemon and the cron table files (crontab).

ps -ef|grep cron   <-- checks to see if cron is running

To start cron:
/etc/init.d/cron start   --> (Solaris, Linux)

cron table files are located in /var/spool/cron/crontabs

cron.allow - allows a user ability to use cron - just add user to file using vi
cron.deny - deny's a user ability to use cron

crontab -e  -->  edit the crontab file
crontab -l  -->  list the crontab file
crontab -r -->  remove the crontab file

Safely editing cron files:

Crontab file layout

Minute          Hour          Day         Month         Day of Week           Command

Comments start with #

*  means all allowed values

Minute = 0-59

Hour = 0-23

Day = 1-31

Month = 1-12

Day of the Week

Example:  To schedule a job to run one time at 9:30AM on August 1st enter the following:

30   09   01   08   *   /dir/subdir/command

Example:  To schedule a job to run once a week at 6:00AM on Sunday enter the following:

00   06   *   *   0   /dir/subdir/command

Example:  To schedule a job to run every day at 1:15AM enter the following:

15   01   *   *   *   /dir/subdir/command

Filesystem and Files

If you can not do an ls (bash: /bin/ls: Argument list too long)

find . -name "*" -exec ls -l {} \;

df

drwxr-sr-x  -->  sgid --> set the group owner on created files to the group that owns the directory instead of the group creating the file.

To remotely copy a directory from one server to another:

If a filesystem can not be unmounted

fuser -cu  <mount point>    <-- files listed, with the name of who is using it
fuser -k  <mount point>      <-- kills the connection (can have bad effects on open databases)

Non-destructive test of a disk:
dd if=/dev/rdsk/cxxxxxx of=/dev/null bs=1024k

file <file>  --> shows the type of the file (ascii, commands, etc)

grep  -->  finds a pattern in a file or the output of a command
grep <pattern> <file>  or  <command>|grep <pattern>
grep -e "<pattern1>" -e "<pattern2>" -e "<pattern3>" <file>    --> pattern1 or pattern2 or pattern3
grep -v <pattern> <file>    --> anything but the pattern  (good for eliminating unwanted information in a display)

Filesystem Check (fsck)

Symbolic Links

ln -s <file or directory> <symbolic link>  -->  Creates a symbolic link to a file or a directory

find

find . -type d -user bill -group engr -exec chgrp mktg '{}' \;

Find will identify all directories that are owned by both the user bill and the group engr.  Find will then change the group ownership to marketing.  This command will execute down the entire relative subdirectory tree (using a /path would specifiy exactly the starting point rather than the "." which is used in this example)..

find ./u*/oradata -user root -group sys -exec chown oracle:dba '{}' \;

Find will identify all directories that are owned by root:sys and change their ownership oracle:dba.  This is useful for correcting a database refresh script error that results in the incorrect permissions being established.

find . -size +2000 -print

find files that are larger that 2000 512 byte blocks and list their names.

find . -atime +400 -type f -name '*' |xargs ls -l '{}' | more

files not accessed in 400 days (does not recurse in subdirectories - bacause of the -type f).  The xargs ls -l prints the size of the file and date to confirm the command is working properly.

find . -size +100000c | xargs ls -l '{}'

finds files that are larger than 100,000 bytes and does an ls -l of each one.

 find /<DIR>/<SUBDIR> -size +2147483648c

Finds files that are larger than 2 GB - this is useful if a tar copy was performed, since this will not copy files larger than 2 GB.

find / -type f -fsonly vxfs -size +10000000c -exec ls -l '{}' \; > /tmp/bigfiles.txt

Finds files that are larger that 10MB on local vxfs filesystems and does an ls -l and writes the result to a text file.

FTP

cd - change remote working directory
pwd - pwd on remote machine
ls - ls on remote machine
lcd - change local working directory
rstatus - remote status
binary - set transfer type to binary
text - set transfer type to text
put - upload a single file
mput - upload multiple files (ie mput .*)
get - download a single file
mget - download multiple files
user - login info
open - connection info

Grep/Egrep


Logging

/etc/logrotate.d  -->  Files that specify rotation of specific log files
/etc/logrotate.conf  --> Configuration file for rotation of logs not specified in /etc/logrotate.d
/etc/syslog.conf -->  What files are used log type of information

Do not rotate:
/var/log/lastlog
/var/log/utmp

If there is no built in log rotation functionality use this script via cron:

Log rotation scripts - logrotate.sh:

LOGDIR="/var/adm/syslog"

cp $LOGDIR/$1.3 $LOGDIR/$1.4
cp $LOGDIR/$1.2 $LOGDIR/$1.3
cp $LOGDIR/$1.1 $LOGDIR/$1.2
cp $LOGDIR/$1 $LOGDIR/$1.1
>$LOGDIR/$1

Sample cron entries:

0 1 * * 0 /opt/scripts/rotate.sh syslog.log > /dev/null 2>&1
5 1 * * 0 /opt/scripts/rotate.sh mail.log > /dev/null 2>&1


Mail

To start the sendmail daemon on a server:
  1. Edit /etc/rc.config.d/mailservs
  2. Set the variable:  export SENDMAIL_SERVER=1
  3. Start the process:  /sbin/init.d/sendmail start
  4. Check to see if the process is running:  ps -ef|grep sendmail

Man Pages

To manually look at a man page:

nroff <man page file> | more


More

Utility for viewing files 1 page at a time.   Commands are similar to vi editor.

Command
Explanation
b
Go backwards 1 screen
g
Go to the beginning of the file
G
Go to the end of the file
h
Help
j
Go forward 1 line
k
Go backward 1 line
<space>
Go forward 1 screen
/<expression>
Search for an expression
q
Quit


NFS commands


showmount -a <host>  <-- shows who is importing from the host
showmount -e <host>  <-- shows what is being exported

/etc/fstab  <-- list of file systems mounted
/etc/mnttab <--  currently mounted file systems
/etc/exports <-- file systems exported

exportfs -a  <-- forces NFS server to export filesystems in /etc/exports file
NOTE: For changes to be registered on the client, the filesystem must be unmounted and remounted

exportfs  <-- forces NFS server to show list of filesystems that are being exported

Performance Monitoring


sar -q
sar -u
sar -d

vmstat

vminfo

Printing

Command
Description
cancel
deletes print jobs from queue
hppi
jet admin
ls -d<destination> -o<optioni> file
prints a file
lpadmin

lpsched -v

lpstat
lists jobs in queue. -s  -t  -d --> lists default printer
pr .profile | lp -n3
prints
pr -o10 -l64 -F -h <HEADER> <file> | lp prints using margins of 10, page length of 64, header on each page

/etc/lp

/usr/lib/lp
/var/adm/lp
/var/spool/lp

/etc/lp/interface - interface scripts

Troubleshooting a printer:

Processes

nohup <command>  --> runs the command so that it is immune to hangups
nohup <command> &   --> runs the command in the background so that it is immune to hangups (ie you can logout and it will still be running).

Putting a process in the background

Security

User/Group/Other Permissions



ACLs
Enhance to normal user/group/other permissions of rwx:

getfacl  - reads the access control list

setfacl - sets the access control list
-m  modies
-u:uid:permissions   --> sets the user permissions
-g:gid:permisions   -->  sets the group permissions

Example of adding a user to the acl of a file:

#: getfacl bill

# file: bill
# owner: root
# group: other
user::rw-
group::r--              #effective:r--
mask:r--
other:r--

#: setfacl -m user:better:r-- bill
#: getfacl bill

# file: bill
# owner: root
# group: other
user::rw-
user:better:r--         #effective:r--
group::r--              #effective:r--
mask:r--
other:r--


Shells

History

Tar

Command Description
tar xvf phkl.tar extract files from tar file phkl.tar
tar cvf adabas.tar *.*
creates tar file adabas.tar and puts all files in it
tar -cvf - . | ( cd /tmp ; tar -xvf - )
"push" tar copy - this will copy all files < 2GB in size from the current directory to /tmp.  It will copy directory trees and maintain the proper ownership and permissions.  This is faster than cp -rp.
remsh SERVER -l root "cd SOURCEPATH; tar cvf - . " | tar xvf - .
"pull" tar copy across a network - this will copy all files (even > 2GB??)  in SOURCEPATH on a remote system to the current directory.  It will copy directory trees and maintain the proper ownership and permissions.
tar cvf - . | remsh SERVER -l root "cd SOURCEPATH; tar xvf - . "
"push" tar copy across a network - this will copy all files (even > 2GB??)  from the current directory to the SOURCEPATH on a remote system.  It will copy directory trees and maintain the proper ownership and permissions


Terminal/Display Info

From the command line:
stty erase ^H   -->  sets the <Backspace> key to deleter characters  (^H here is actually the key sequence <Ctrl><H>)

To put it into the.profile or other configuration file:
#  5/14/04  wte   Make backspace key work properly
stty erase '^H'


User Administration

Disabling a user account
In either /etc/shadow or /etc/passwd replace the password field with an asterisk "*".

Problem:  You receive an error message when logging in as the user:  -sh  PATH=$PATH:/usr/local/bin is not an identifier       
Solution:  The sh shell does not like to export and set a variable at the same time.  This can be cured by using ksh or bash.