 |
|
Oracle UNIX
Administration Command History
Oracle UNIX/Linux Tips by Burleson Consulting |
Using the Command history file in UNIX
A special file in the UNIX user home
directory called .sh_history is used by UNIX to record and allow for
fast retrieval of prior commands. For example, if you place
the set ?o vi command in your .profile file, the use can use <esc> k
to scroll back through your command history, and you can use <esc> /
string to quickly display a previous command that contains the
string.
You can also use the UNIX history command or
fc command to display the previous UNIX commands, and you see a
command number that can be used to retrieve the command:.
root>
history
423 chmod 700 *.file
424 ls -al
425 chmod 404 *.file
426 chmod 400 *.file
427 ls -al
428 sqlplus /
Here is a listing of a .sh_history file.
Note that it even includes the command that was used to display
itself.
root> tail
-10 .sh_history
cat alert_envtest.log|wc
cat alert_envtest.log|wc -l
cat alert_envtest.log|grep ORA-00600
cat alert_envtest.log|grep ORA-00600|wc -l
cd
ls -al .s*
tail -10 .sh_history
The .sh_history file is commonly used as an
audit mechanism, since each and every UNIX command entered by the
UNIX user is stored in their .sh_history file. Many shops use
it to track the behavior of new Oracle DBAs, since inappropriate or
inept UNIX commands are easily spotted.
Here is the script that I use to spy on my
Oracle DBAs. When run as root, it provides a list of every
command executed by everyone on the Oracle server:
audit_commands.ksh
#!/bin/ksh
for user
in `cat /etc/passwd|cut -d ':' -f1`
do
echo *************************************
echo UNIX command security audit for $user
echo *************************************
cat ~$user/.sh_history
done
Let?s carefully inspect the behavior of this
script:
1. It lists the /etc/passwd file using (cat
/etc/passwd)
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:
daemon:x:2:2:daemon:/sbin:
news:x:9:13:news:/var/spool/news:
uucp:x:10:14:uucp:/var/spool/uucp:
operator:x:11:0:operator:/root:
piranha:x:60:60::/home/httpd/html/piranha:/dev/null
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
carl:x:500:100:Carl Marx:/home/carl:/bin/bash
pamela:x:501:100:Pamela Zeus:/home/pamela:/bin/bash
olaf:x:512:512::/home/olaf:/bin/bash
mario:x:514:514::/home/mario:/bin/bash
mysql:x:100:101:MySQL server:/var/lib/mysql:/bin/bash
bugz:x:515:515::/etc/httpd/sites/bugz.rovia.com:/bin/bash
oracle:x:516:517::/home/oracle:/bin/bash
afshin:x:522:523::/home/adamf:/bin/bash
celora:x:525:526::/dev/null:/bin/false
weber:x:527:528:Web CVS:/home/webcvs:/bin/bash
2. It extracts the first colon-delimited
field using (cut ?d?:? ?f1)
root
bin
daemon
news
uucp
operator
piranha
postgres
squid
carl
pamela
olaf
marion
mysql
bugz
oracle
afshin
celora
weber
3. It loops through each UNIX user and
4. Lists the contents of their .sh_history
file
Next, let?s take a look at the common UNIX
shells and see how they allow us to perform common Oracle
maintenance.
UNIX shells
There are several ?shells? that are
available to support UNIX commands. A shell can be thought of
as a scripting environment, and each shell has different syntax, so
it is important that you know what shell you are using when you
enter UNIX commands. In this book we will use the Korn shell, but
you can write Oracle scripts using any of the available shells.
* Bourne Shell (sh) - The Borne shell was
the original UNIX command processor, which was developed at AT&T by
Stephen R. Bourne in the early 1970s. This is the official shell
that is distributed with UNIX systems. The Bourne shell is the
fastest UNIX command processor.
* Bourne Again Shell (bash) - Many UNIX
purists prefer the Bourne Again shell, also known as the bash shell.
* C Shell (csh) - Another command processor,
developed by William Joy and others at the University of California
in the early 1980s, is known as the C shell. The C shell borrows
many concepts from the C language, and offers greater versatility
than the Bourne shell.
* Korn Shell (ksh) ? Another popular command
processor was developed by David Korn in the early 1980s, and is
appropriately called the Korn shell. The Korn shell combines many of
the best features of the earlier command processors, and it is
gaining in popularity among Oracle DBAs. All of the shell
scripts in the book are Korn shell scripts.
Your default shell is set in the /etc/passwd
file. For example, here we see that the root user has the
Bourne shell (/bin/sh), the janet user uses the Bourne Again shell
(/bin/bash), the oracle user uses the Korn shell (/bin/ksh), and the
john user uses the C-Shell (/bin/csh).
root>cat
/etc/passwd
root:x:0:1:Super-User:/:/bin/sh
janet:x:100:1::/export/home/janet:/sbin/bash
oracle:x:108:102::/export/home/oracle:/bin/ksh
john:x:108:102::/export/home/john:/bin/csh
Each shell has specific syntax and features
(Table 1-2). For the Oracle server environment, the shell chosen is
not as important as uniformity. In other words, the Oracle DBA
should choose a shell environment and then requires that all Oracle
UNIX scripts be written in that shell.
Remember, it is quite easy to change your
shell environment. You can change your UNIX environment by
entering the name of the shell at the UNIX command prompt, and you
can change the shell for a UNIX script by entering a shell directive
as the first line of the script. For example, you can change
your shell environment to the Korn shell by entering the ksh
command, and you can make a UNIX script use the Korn shell by
entering #!/bin/ksh as the first line of the script. Table 1-3
shows a complete list of commands for changing shell environments.
Interactive shell |
Shell Script Command |
Shell Name |
ksh |
#!/bin/ksh |
Korn shell |
csh |
#!/bin/csh |
C-Shell |
sh |
#!/bin/sh |
Bourne Shell |
bash |
#!/bin/bash |
Bourne Again Shell |
Table 3: Changing UNIX shell environments
In most Oracle shops, the DBA has a choice
of shell environments. As soon as you request a login account
for your Oracle server, the first thing the system administrator
usually asks you is which shell you prefer.
Using UNIX command options
All UNIX commands support from one to many
command options. To understand how this works, let?s examine
the ls command. To illustrate the complexity of UNIX commands, the
man ls command shows that the ls command accepts 22 arguments (Table
1-2):
-a |
Lists all entries, including those that
begin with a dot (.), which are normally not listed. |
-A |
Lists all entries, including those that
begin with a dot (.), with the exception of the working
directory (.) and the parent directory (..). |
-b |
Forces printing of non-printable
characters to be in the octal \ddd notation. |
-c |
Uses time of last modification of the
i-node |
-C |
Multi-column output with entries
sorted down the columns. |
-d |
If an argument is a directory, lists
only its name |
-f |
Forces each argument to be interpreted
as a directory |
-F |
Marks directories with a trailing
slash (/) |
-g |
The same as -l, except that the owner is
not printed. |
-I |
For each file, prints the i-node number
in the first column of the report. |
-l |
Lists in long format, giving mode,
ACL indication. |
-L |
If an argument is a symbolic link, lists
the file or directory the link references rather than the
link itself. |
-m |
Streams output format; files are
listed across the page, separated by commas. |
-o |
The same as -l, except that the group is
not printed. |
-p |
Puts a slash (/) after each filename if
the file is a directory. |
-q |
Forces printing of non-printable
characters in file names as the character question mark (?). |
-r |
Reverses the order of sort to get
reverse alphabetic or oldest first as appropriate. |
-R |
Recursively lists subdirectories
encountered. |
-s |
Gives size in blocks, including
indirect blocks, for each entry. |
-t |
Sorts by time stamp (latest first)
instead of by name. The default is the last modification time.
(See -u and -c.) |
-u |
Uses time of last access instead of
last modification for sorting (with the -t option) or printing
(with the -l option). |
-x |
Multi-column output with entries sorted
across rather than down the page. |
Table 4: Arguments to the UNIX ls command
One or many of these arguments may be used
every time the ls command is invoked and a single minus sign ?-? is
used to tell Oracle that arguments are being passed to the command.
For example, the following is a invocation of the ls command using
the F, a, r, and t options:
root> ls
-Fart
.solregis/
l.ksh*
imp_envtest.lst
One common use of the ls command in Oracle
is used to locate the most recently modified file in a directory. We
do this by using the ls command with the a, l, and t options, and
then pipe the output to the head command.
root> ls -alt|head
total 928228
-rw------- 1 oracle dba
3372 Sep 3 14:43 .sh_history
-rw-r--r-- 1 oracle dba
19159 Sep 3 08:27 man_sort.lst
drwxr-xr-x 21 oracle dba
2048 Sep 3 08:27 .
-rwxr-xr-x 1 oracle dba
55 Aug 22 11:56 afiedt.buf
-rw------- 1 oracle qmail
437 Aug 12 20:43 .bash_history
-rwxr-xr-x 1 oracle qmail
415 Aug 1 20:37 mail.out
drwxr-xr-x 10 root root
512 Jul 26 08:49 ..
drwxr-xr-x 3 oracle dba
512 Jul 26 08:49 book
-rw-r--r-- 1 oracle dba
2198 Jul 26 08:43 sqlnet.log
Next, net?s examine some common UNIX
commands that are used in an Oracle environment.
 |
If you like Oracle tuning, see the
book "Oracle
Tuning: The Definitive Reference", with 950 pages of tuning
tips and scripts.
You can buy it direct from the publisher for 30%-off and get
instant access to the code depot of Oracle tuning scripts. |