Wednesday

Linux/ HP-UX/Solaris

Linux directory structure - tree like structure, with directories like-
etc- contains system configuration information,
tmp - contains temporary files,
user - contains user programs and utilities, includes subdirectories as /usr/bin, /usr/sbin and /usr/local/bin

/usr/bin - contains utilities to be used by end users. Some of these are -
awk - a pattern searching and processing language,
df - displays available disk space,
ftp - File Transfer Protocol tool,
gzip/zip - used to compress and decompress files,
ssh - secure shell processor,
sudo - used to run a program as root,
tail - reads from end of a file,
top - displays top CPU processes,
who - displays a list of users on the system.

/usr/sbin -
useradd - used to add new user accounts to the system,
groupadd - used to add groups to system.
crond - this daemon runs regularly scheduled jobs submitted by cron which might be user or system programs that run every hour, every day, every month etc.
sshd - this is the secure shell server, SSH clients connect through this daemon.

Network utilities - arp - Address Resolution Protocol.
ifconfig - used to configure and manage network adaptors.


Linux Web Server utilities - it is an open source HTTP web server that runs on many platforms including Linux/UNIX and Windows. It contains many modules e.g. a PERL module, an authentication module and a proxy module. In order to web users to access the Oracle db, a CGI or Common Gateway Interface is used.

Processes and Threads- Within the OS, many programs or processes are simultaneously running on the system. Each process has its own virtual memory space and run independently of other processes. Threads are concurrent threads of instructions which run within a single process.

Logical Volume Manager (LVM) and ASM (Automatic Storage Management) - LVM manages storage by combining physical disk blocks from physical disks into a logical disk volume. Oracle's ASM is an LVM which has been developed specifically for Oracle and simplifies Oracle storage management and optimize Oracle data storage.



Linux Commands/programs-
Commands are also referred as programs since whenever you run a command, it is the corresponding program code, written for the command that is being executed.

sar ( System Activity Reporter) - useful for monitoring the performance of system in general. There are multiple options with sar depending upon what you want to monitor. Using sar, you monitor CPU utilization, memory and network interfaces.

sar with no parameters displays the CPU utilization for last 24 hours. $sar -A displays a lot of system information for last 24 hours. $sar -n DEV displays information about network interfaces. This is a repeating command that will run at intervals and duration as specified e.g. $sar 10 100

vmstat - provides information about processes, memory, paging etc. Use this command to determine if there is free memory. This command can be run in a loop.
$vmstat 10 100 - specifies that the vmstat is run every 10 seconds for 100 times.

top- continually displays the top running processes and overall view of the activity on the system. $top -c displays the command line rather than just the command that is being run.

iostat - displays information about the I/O subsystem e.g. reads and writes per second, blocks written per second, wait time and response time. $iostat -k displays detailed I/O information. $iostat -x 10 100 runs 100 times at every 10 seconds. The important data points in output are - 1) await - average wait time, 2) svctm - the service time, 3) r/s - the number of reads per second and 4) w/s - number of writes per second.
the

ps - command provides process statistics, information on the processes that are currently running. There are a number of additional parameters that provide more detailed information on currently running processes. The second column of output from ($ps -elf) shows the process state code.

Different state codes are -
R, Runnable - process currently running on a CPU,
S, Sleeping - process is waiting for something to happen,
D, uninterrupted sleep usually indicates that the process is waiting an I/O.
X, Dead,
T, traced or stopped.

df - to find the size of file system. df -h displays the information in human readable format. K, M, G for kilobytes, Megabytes and Gigabytes. -h flag can be used with many file system commands e.g. df, du, ls etc.

sed (the stream editor) -  special editor for modifying files automatically.
s option with sed for substitution - the most useful command that changes all occurrences of a regular expression into a new value.
$sed s/d1sdt1d1/p1sdt1d1/ old > new   =>  the command changes d1sdt1d1 in old file to p1sdt1d1 in the new file.


Mostly used Linux commands- 

ls         Lists all files and directories in the present working directory
ls -R     Lists files in sub-directories as well
ls –a     Lists hidden files as well Lists hidden files as well
ls –la  Lists files and directories with detailed information like permissions , size , owner etc
cat > <filename> Creates a new file
cat <ilename> isplays the file content
cat file file2 > file3 Joins two files (file1, file2) and stores the output in a new file (file3)
mv  file “new file path” Moves the files to the new location
mv filename new_file_name  renames the file to a new filename
sudo Allows regular users to run programs with the security privileges of the superuser or root
rm Deletes a file
history Gives a  list of all past commands typed in the current terminal session
clear Clears the terminal
mkdir Creates a new directory in the present working directory
rmdir Deletes a directory
mv Renames a directory

tr copies the input to produce output with translation/substitution.
find used to locate files on UNIX system.
cut - cuts out selected fields of each line.


File Permissions in Linux -For effective security , Linux divides authorization into 2 levels
1) Ownership
2) Permission


Ownership in Linux files  - Every file and directory in your Unix/Linux system is assigned 3 types -
User - A user is the owner of the file. By default, the person who created a file becomes its owner.
Group - A user- group can contain multiple users. All users belonging to a group will have the same access permissions to the file. Instead of manually assigning permissions to each user, you could add all users to a group, and assign group permission to file such that only this group members and no one else can read or modify the files.
Other - Any other user; this person has neither created the file nor he belongs to a user-group who could own the file. When you set the permission for others, it is also referred as set permissions for world.

Permissions -
Every file and directory in your UNIX/Linux system has following 3 permissions defined for all the 3 owners discussed above
Read : This permission gives you the authority to open and read a file. Read permission on a directory gives you the ability to lists it’s content.
Write: The right permission gives you the authority to modify the contents of a file. The write permission on a directory gives you the authority to add , remove and rename files stored in the directory.  Consider a scenario where you have write permission on the file but do not have write permission on the directory where the file is stored. You will be able to modify the file contents. But you will not be able rename, move or remove the file from the directory.
Execute : In windows an executable program usually has an extension “.exe” and which you can easily run. In Unix/Linux , you cannot run a program unless the execute permission is set. If the execute permission is not set, you might still be able to see/modify the program code(provided read & write permissions are set), but not actually run it.

r = read permission
w = write permission
x = execute permission
- = no permission
chmod command which stands for ‘change mode’. Using the command, we can set permissions (read, write, execute) on a file/directory for the owner, group and the world.

There are 2 ways to use the command -

Absolute mode
Symbolic mode

1) Absolute(Numeric) Mode - In this mode file permissions are not represented as characters but a three digit octal number.


Number - Permission Type - Symbol
0 No Permission ---
1 Execute --x
2 Write -w-
3 Execute + Write -wx
4 Read r--
5 Read + Execute r-x
6 Read +Write rw-
7 Read + Write +Execute rwx


e.g. $chmod 770 filename - sets all permissions on filename for owner and group leaving no permissions for others.

2) Symbolic Mode -In the symbolic mode you can modify permissions of a specific owner. It makes use of mathematical symbols to modify the file permissions

+ Adds a permission to a file or directory
- Removes the permission
= Sets the permission and overrides the permissions set earlier.

The various owners are represented as -

e.g. $chmod o=rwx filename  - sets all the read+write+execute permissions on file for others.


u - user/owner
g - group
o - other
a - all


Changing Ownership and Group - For changing the ownership of a file/directory , you can use the following command:

$chown user filename
$chown user:group filename - In case you want to change the user as well as group for a file or directory use the command.


Change group - The file /etc/group contains all the groups defined in the system. The command “groups” lists all the groups you are a member of.
$groups
To change the group of a file, use

$chgrp newgroupname file_name


You cannot have 2 groups owning the same file. Though one group can be a sub-group of another which can own a file. You can use the command newgrp to work as a member a group other than your default group.

$newgrp oracle

Pipes in Linux -The symbol ‘|’ denotes a pipe. Pipes ‘|’ help combine 2 or more commands. If you want to use two or more commands at the same time and run them consecutively, you can use pipes.

$cat filename | less
$cat Filename | pg
$cat Filename | more


* less, pg and more commands are used for dividing a long file into readable bits.

The ‘grep’ command - the grep command scans the file for the desired information and present the result in a format you want. The grep command can be used to find strings and values in a text document. Following options can be used with this command.
-v Shows all the lines that do not match the searched string
-c Displays only the count of matching lines
-n Shows the matching line and its number
-i Match both (upper and lower) case
-l Shows just the name of the file with the string


$cat sample | grep -i a


The ‘sort’ command - This command helps in sorting out the contents of a file alphabetically. The options with this command are -

-r Reverses  sorting
-n Sorts numerically
-f Case insensitive sorting
$sort -r file

A filter in a pipe is an output of one command which serves as input to the next. When you pipe 2 commands, the “filtered ” output of the first command is given to the next.


Regular expressions - Regular expressions are a set of characters used to check patterns in strings. They are also called ‘regexp’ and ‘regex’. Some popular regular expressions are -
. replaces any character
^ matches start of string
$ matches end of string

$cat filename | grep ^<character> - only lines in a file that starts with the <character> are filtered in output.

Redirection - The standard input (stdin) device is the keyboard.
The standard output (stdout) device is the screen.


Output Redirection - The ‘>’ symbol is used for output (STDOUT) redirection. Example:
$ls –al > listings - the output of command ls –al is re-directed to file “listings” instead of your screen.

“>>” appends output to an existing file.
* You can re-direct standard output, to not just files, but also devices!

Input redirection - The ‘<’ symbol is used for input(STDIN) redirection. e.g.

$Mail –s “Subject” to-address < Filename

$Mail –s “News Today” abc@gmail.coms < Newsflash

Example - The mail program in Linux can help you send e-mails from the Terminal. 
You can type the contents of the email using the standard device keyboard. But if you want to attach a File to email you can use the input re-direction operator in above format.


“>&” which writes the output from one file to the input of another file.

Error Redirection - Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error.

By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other than the screen. Error re-direction is one of the very popular features of Unix/Linux.

$myprogram 2>errorsfile -Here Using “2>” we re-direct the error output to a file named “errorfile”, thus, program output is not cluttered with errors.
$find . -name 'my*' 2>error.log  -Using the “find” command we are searching the “.” current directory for a file with “name” starting with “my”
$ls Documents ABC> dirlist 2>&1 - lists contents of the two directories - Document and ABC. Standard output is redirected to the file dirlist and error output(2) is written (by >&) to input of 1(standard output i.e. screen)


File Descriptors - In Linux/Unix everything is a file. Regular file, Directories and even Devices are files. Every File has an associated number called File Descriptor (FD).

FD0 - Standard Input STDIN
FD1 - Standard Output STDOUT
FD2 - Standard Error STDERR

Variables - unique names  given to memory locations/addresses in system RAM. There are two types of variables-
1) System variables - these are Linux created and maintained variables defined in CAPITALs
2) User Defined variables - created and maintained by user usually in lower case.

Environment variables - Dynamic values which affect the processes or programs on a computer. These can be created, edited, saved and deleted. e..g. $LANG environment variable stores the value of the language that the user understands.

Some common environment variables -
PATH - This variable contains a colon(:) separated list of directories in which your system looks for executable files. When you enter a command on terminal, the shell looks for the command in  different directories mentioned in the $PATH variable.
USER - The username.
HOME - Default path to the user’s home directory.
UID - User’s unique ID.
SHELL - Shell being used by the user.

$echo $VARIABLE - display value of a variable.
$VARIABLE_NAME= variable_value - Creates new variable.
$unset VARIABLE_NAME - to remove a Variable from the system. This would remove the $Variable and its value permanently.
$export Variable=value - To set value of an environment variable.


No comments:

Post a Comment

Followers