Showing posts with label Unix Tech Tips for DBA. Show all posts
Showing posts with label Unix Tech Tips for DBA. Show all posts

CHMOD - Undertanding File Permissions on a Unix-Based Server

The basic command for changing file permissions in unix based operating systems is the 'chmod' command. This command is executed with a set of parameters after it. With most FTP clients, you can simply select the filename you want to change the permissions of, right click, and choose the chmod command.

There are three main groups of permissions when dealing with the chmod command. The three groups are yourself or owner, your unix group, and everyone else. A typical chmod command would look like this - 'chmod 755' This allows you to add, remove or rename files as well as read or edit a file. It also specifies that your unix group and everyone else can only read and edit the file in question. They can't add, delete, or rename the file.

How the numbers in the chmod command work

There are three types of permissions that can be given to a file. They are read, write, and execute. The read permission is assigned a value of 4, the write permission is assigned a value of 2 and the execute permission is assigned a value of 1. So when a chmod command specified the digit 7 as the first number, it means that all three types of permissions are to be assigned to it. If a party is assigned a value of '0' then it means it has no rights whatsoever to a file. '6' tells the server that the user can read and write to the file but can't execute it. I think you get the idea.

Common permissions to set

Most of your html files will do fine with a permission of 644. Most script files will need a permission of 755. Any configuration files in a script package will often need a file permission of 777. Be very careful about assigning a file permission of '777' for everyone. With this permission, it means that anyone who gains access to your server will be able to add, remove, rename, read or edit the file. It is always a good idea with security in mind to assign the least amount of permissions necessary to get the job done.

An FTP client can keep things simple

Most people on shared servers will not have access to a command line to be able to use the chmod command. For most people, changing file permissions will be done through an FTP client. To do this, right click on the file you want to alter, and choose the chmod command. You will usually get a popup that will show nine checkboxes. The will be arranged in three rows. On the left you will usually see Read, Write, and Execute for the row headings. You can accomplish the same thing as typing in the numbers above by clicking on one, two or three boxes for each party. Once you're done, you click the apply or ok button and the file permissions will be changed.

If you keep in mind that you need to always go with the most restrictive file permissions possible that still allows your files to execute, and that there are three parties to define permissions for, you'll do fine. The three parties again are the owner, group, and everyone else.

Intro to UNIX Shells

A UNIX Shell is in simplest terms, a command line interpreter, that takes the users input and gives it to the Kernel. If you are familiar with DOS, you will remember the 'command.com' that file takes the users information and passes it to the operating system in a way it can be understood by the system. With DOS you only had one type of shell, but with UNIX you have a variety, each with their own abilities, pluses and minuses.

Keep in mind as you read about these shells, that though they may have major differences when they were created, that they have been updated and many features that were once only available in a single shell, may be available in other shells. This is more of a history lesson.

'sh'
The Bourne shell, named after its creator Steve Bourne is the oldest shell for most major distributions of UNIX and LINUX. Most shells today are in some fashion derived from the Bourne shell.

'csh'
The C Shell. The C shell is a great shell that has many advantages over the original Bourne shell, it was the first to introduce a history (the ability to scroll up, to view past commands). Also the C Shell as the name implies also integrates a great portion of the C programming language. If you are able to program in C, you can pretty much do it all in a shell script or from the command line, if you so desired.

'ksh'
The Korn Shell. Named after its creator David Korn. This shell is able to do most of the things both the Bourne, and C Shell can do, and improves on them. For instance, the history feature is available, plus you are able to call up the history, edit the command, and then re-run it.

'bash'
The Bourne-Again Shell. This is probably the most widely used shell to date, and is the default shell for most newer Linux distributions. Again it integrates most of the above features and improves upon them. It was developed by the Free Software Foundation.

There are many other shells out there, but chances are if you are using a UNIX style operating system, you are using one of the above mentioned shells. My personal favourite is the bash shell, which also happens to be the first one I was exposed towards, so I may be biased. However typically when shell scripting, I use the korn shell because of its programming friendly structure.

How do you know what UNIX shell I am using?

Type "ps" and then hit return at your shell prompt and you should get something similar to:

$ ps
PID TTY TIME CMD
13087 pts/1 00:00:00 bash
13121 pts/1 00:00:00 ps

The numbers under PID, TTY, TIME etc should all be different from machine to machine, and the number of lines of programs running may also. But one line you have will be probably "bash" "csh" "sh" "ksh".

Another way to check is to type the following:

grep your-user-name /etc/passwd (eg grep ken /etc/passwd)

Which will return you something along the lines of:

ken:x:501:501:Ken:/home/ken:/bin/bash

The last portion of that line is '/bin/bash' which tells you your default shell when you login.

Sun Solaris Command Tips

All commands are specific to Sun Solaris operating system

UNIX backups/Restore

Commands to take backup of /usr /var /lib directories to tape and disk using "tar"

ANS: a) tar -cvf /dev/rmt/0 /usr /var /lib [Taking into tape ]

b) tar -cvf backup_file.tar /usr /var /lib [ Taking disk image] Taking backup to a tape device attached to a remote system

ANS: a) tar -cv /var /usr /lib | rsh remote_hostname dd of=/dev/rm/0 obs=128

b) ufsdump 0ucf remote_hostname:/dev/rmt/0 /file_system Extracting / Restore the contents of a tape /tar file

ANS: a) tar -xvf /dev/rmt/0 [ restore contents of a tar tape ]

b) tar -xvf filename.tar [ Restore contents of a tar file ]

c) ufsrestore f /dev/rmt/0 filename [Restore from ufsdump tape backup]

d) ufsrestore rf remote_hostname:/dev/rmt/0 filename [ Restore from remote host tape device ]

List contents of backup

ANS: a) tar -tvf /dev/rmt/0 [ List contents of a tar tape ]

b) tar -tvf filename.tar [ List contents of a tar file ]

c) ufsrestore tf /dev/rmt/0 [ list contents of a tape of ufsdump]

Processes and Processor

What are the types of priority classes supported by Solaris ?

ANS: timesharing, system and reat-time

How will you place a running process in real time class?

ANS: priocntl -s –c RT –I uid process_id_number

How will you start a process in real time class?

ANS : priocntl –c RT –e process_name

How will you bind a process to processor ?

Ans: pbind –b processor_no process_id

Network

What is the pocket size used by SQL*NET Version 2 on Solaris

Ans : Default is 2K

What is the procket size used by network layer TCP/IP in SQL*NET v2?

Ans : Default 1K

Inter process communication (IPC) tuning

What is post wait driver ?

The post wait driver reduces the overhead incurred by the more expensive use of semaphore operation for interprocess communication.

Memory

What is the kernel parameter which controls the UNIX buffer cache on solaris ?

Ans : bufhwm ( bufhwm is the maximum amount of physical memory, in kilobytes that can be used by

I/O buffers) While starting an oracle process , the unix system displays an erro message like "Cannot allocate more shared memory segment for the processes" If oracle person approaches you, how will you fix this problem?

ANS: increase the value for kernel memory parameter "set semsys:seminfo_semmns" in /etc/system file. [ this is a blind way]

System configurations

How do you find out total RAM installed on your machine

Ans: $prtconf |grep size

How do you find no. of processors installed on your sun box and processing speed?

Ans: $psrinfo –v

How do you find out how many disks are available on your system?

Ans: $ format (for sysadmins)

$ iostat –E (for users – count only the disks which has disk size correctly)

System Boot Options

How do you boot sun box in single user mode?

ANS: At ok prompt type "boot -s" [ ok boot -s OR ok boot -1 OR ok boot -S ]

What are the commands to shutdown the system?

ANS: shutdown, init , halt, reboot

Devices

What is the meaning of logical name of a disk drive format like c0t0d0s0?

ANS: c0 - Controller number

t0 - SCSI bus target number

d0 - Disk number

s0 - Partition or slice number

List some of the main device types?

ANS: /dev/dsk - Disk devices

/dev/rdsk - Raw or character devices

/dev/rmt - Tape devices

/dev/term - Serial line devices

/dev/pts - Pseudo terminals

List commands to display physical devices ?

ANS: prtconf

Sysdef

Dmesg

sysinfo

Program Performance Optimization

For those of you working on large programs performance is a critical issue. This is especially true with the database folks who write DB precompiler code like pro*C.

Well good news is that you can do what is called as 'compiler based FBO' (compiler based feedback optimization). The concept large and need to be discussed in specific details of the situation present.

Generally speaking here is how it works...

[1]: Compile your program with symbol tables

[2]: Run the instrumentor

[3]: Test your program. This will generate a data file for the program performance. Rule of the thumb is bigger the data file better the performance. Keep in mind though this is linear and not vertical.

[4]: Recompile your program with symbol tables using the performance data file. This will tell the compiler to generate better and faster code by looking at the performance problem area earlier collected in the FBO file [3].

[5] Strip off the symbol table and check the program performance with the pervious versions. Theoritacally speaking it is possible to extract 'n' time linear performance factor from perviously unoptimized programs. On already optimized programs law of "diminishing marginal returns" applies. That is to say that the performance gain you get is would be less evertime you increase an optimization factor. [heeee bloody economists are right here]. In my experience I've seen upto 35% gains in
performances coupled with other variables. Compilers that I know of, can do optimization: Sun Solaris, HP-UX, DEC, SCO, AIX and IRIX. Using 'gcc' or gnu compiler is not advised for performance optimizations. It does very poorly. And is not recomended. Native compilers and tools have better understanding of exec image being optimized and what to look for. Although you may find some tools from universities and research organizations which are better than native tools. But again
they are not commericial products and their algorithims are still under development [though some are really good]

Stream - Oriented Editor One-liner Tips

Replace ORACLE with UNIX-ORACLE in a data_file

$sed 's/ORACLE/UNIX-ORACLE/' data_file

Replace two words or more words at a time

$sed -e 's/ORACLE/UNIX-ORACLE/' -e 's/DOS/UNIX/' data_file

Remove blank lines from a file

$sed '/^$/d' file_name

Delete lines between two patterns(including the pattern lines)

$ sed '/Unix-abcd/,/Dos-abcd/d' file_name

Delete lines from 10 to end of the file

$ sed 10,\$d tst

Delete lines from 10 to 300 in a file

$ sed 10,300d tst

Delete all lines expept lines in between the patterns

$sed '/ORACLE/,/UNIX/\!d' file_name

Print first line of a file

$sed q file_name

print 200th line of a file

$sed 200\!d file_name

Print last line of a file

$sed '$\!d' file_name

$sed -n '$p' file_name

list only first 20 lines of a file

$sed '20q' file_name

Print only the lines where ORACLE is present

$sed -n '/ORACLE/p' file_name

Print only the lines where ORACLE doesn't exist

$sed -n '/ORACLE/\!p' file_name

Add double spacing to a file

$ sed G file_name

How many number of lines in a file

$ sed -n '$=' file_name

Change DOS into UNIX for only the lines containing ORACLE

$sed '/ORACLE/s/DOS/UNIX/g' file_name

Change DOS into UNIX, except lines which contains ORACLE

$sed /ORACLE/\!s/DOS/UNIX/g' file_name

Reverse the order of lines

$sed -n 1\!G;h;$p' file_name

Delete all blank lines

$sed '/^$/d' file_name

awk one-liner Tips

Print column1, column5 and column7 of a data file or output of any columns list

$awk ‘{print $1, $5, $7}’ data_file

$cat file_name |awk ‘{print $1 $5 $7}’

$ls –al |awk ‘{print $1, $5, $7}’ -- Prints file_permissions,size and date

Syntax of running an awk program

Awk ‘program’ input file(s)

List all files names whose file size greater than zero.

$ls –al |awk ‘$5 > 0 {print $9}’

List all files whose file size equal to 512bytes.

$ls –al |awk ‘$5 == 0 {print $9}’

print all lines

$awk ‘{print }’ file_name

$awk ‘{print 0}’ file_name

Number of lines in a file

$awk ‘ END {print NR}’ file_name

Number of columns in each row of a file

$awk ‘ {print NF’} file_name

Sort the output of file and eliminate duplicate rows

$awk ‘{print $1, $5, $7}’ |sort –u

List all file names whose file size is greater than 512bytes and owner is "oracle"

$ls –al |awk ‘$3 == "oracle" && $5 > 512 {print $9}’

List all file names whose owner could be either "oracle" or "root"

$ls –al |awk ‘$3 == "oracle" || $3 == "root" {print $9}’

list all the files whose owner is not "oracle

$ls –al |awk ‘$3 != "oracle" {print $9}’

List all lines which has atlease one or more characters

$awk ‘NF > 0 {print }’ file_name

List all lines longer that 50 characters

$awk ‘length($0) > 50 ‘{print }’ file_name

List first two columns

$awk ‘{print $1, $2}’ file_name

Swap first two columns of a file and print

$awk ‘{temp = $1; $1 = $2; $2 = temp; print }’ file_name

Replace first column as "ORACLE" in a data file

$awk ‘{$1 = "ORACLE"; print }’ data_file

Remove first column values in a data file

$awk ‘{$1 =""; print }’ data_file

Calculate total size of a directory in Mb

$ls –al |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Calculate total size of a directory including sub directories in Mb

$ls –lR |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Find largest file in a directory including sub directories

$ls –lR |awk ‘{print $5 "\t" $9}’ |sort –n |tail -1

RAID (Redundant Array of Inexpensive/Independent Disks)

Level Explanation Advantages Disadvantages Remarks
0 Striping across disks Larger I/Os or throughput

Full utilization of disk array capacity

Good for read & write

No fault tolerance

One disk fails, entire volume fails.

Cannot recover from failed disk.

Usually 2-4 drives. Applications requiring high performance but non –critical data and low cost.
1 Mirroring Highest fault tolerance.

Good for read

Higher cost Smaller applciations with high availability
0 + 1 Striping + Mirroring Very high reliability

Good for read & write

High performance

Higher cost Samller applications with high performance
2 Inherently parallel mapping and protection technique. Mostly it is not deployed because it needs special disk futures. Disk production is not economical cost wise.
3 Data striped across disks. Min. 3 disks are required Cost is lower than other redundant levels Bottleneck for small I/O operations

RAID 3 is not found on all controllers.

Large I/O request like CAD, CAM imaging.
4 Similar to Raid 3. Unlike Raid 3 it wirtes parity in a single disk. Parity data for whole array requires just one disk. Bottleneck for small I/O operations due to Large file transfers
5 Calculates parity, and writes the data in stripes across disks. Rotational parity Smaller datafiles high throughput.

Even if one disk fails system will be up and runing.

Reasonable cost.

Slower Write than Raid-3 and Raid-4.

Write performance poor. Recovery is slow.

Very high read rate. Less write applications.

OLTP

File server

Web server

ORACLE - RAID MATRIX


Oracle files Raid Levels
Parameter files 5 , 0+1 , ANY
Control files 5, 0+1, ANY
Redo logs 1, 0+1
System Tablespace 5,1, 0+1
Temporary Tablespace 0
Rollback Tablespace 5
Data files 5 , 0+1
Index Tablespace 5, 0+1