Tuesday, May 17, 2011

hosts.allow and hosts.deny in Linux

hosts.allow and hosts.deny files are mainly used for restricting the ssh to th editing  your server in which you are    
editing the files.

These files are maintained by the TCP wrappers

These are mainly used for securing your server or system from outside acccess.

The way it happens when a outside ip asks for permission to ssh to your system:
1)The Tcp wrapper first checks for entry in /etc/hosts.allow.If a entry is present ,it does not go to the /etc/hosts.deny.
2)If entry is not present ,then it goes for hosts.deny ->if entry is present , the ip is denied
                                                                                  if entry is not present ,the ip is allowed.

If the entry is not present in both hosts.allow and hosts.deny , the entry is allowed.











"=" condition in shell

uae -ne for number and "=" for string

Comparators in shell


In case of shell comparison one need to be very careful as one extra space may lead to the code throwing errors

For example :
if [$dd=1]
then
.....statement
fi

This is completely wrong as it will throw a "syntax error near unexpected token near then"
as there are three errors in this:
1)there must be minimum of one space between if and square bracket "["
2)For comparison in shell  ,there must be minimum of one space before and after the equal to sign"="
3)minimum of one space needed between 1 and closing bracket
giving more spaces will not cause error to happen.

proper code:

if  [ $dd = 1 ]
then
.....statement
fi

Shell assignments:

Ironic in the case of assignments when compared to comparison,is that there should not be any space before and after the equal-to sign("=")

FOR EXAMPLE:
Inside the shell file you provide like this:
gg  =  1

This will throw a critical error like "unary operater expected " and "command not found"
So it is must in Shell that the assignments must not have space before and after the "=" sign.
It should be:
gg=1

Checking not-equal condition in Shell:

you can use "-ne" or "!= "  can be used for numbers.

you can use "!=" for comparing strings are not equal.

Logical Operators in Shell:

and     -a  can be used

cond1 -a  cond2  -  True if both conditions are true.

ex: [  $gg -eq  1 -a $cc -eq  2 ]  .Maintain spaces here else you may get "  Too many arguments error"

or     -  o can be used

ex: [ $gg -eq 1 -o $cc -eq   2 ]

Checking files in Shell:


-f somefile True if somefile exists and is an ordinary file.
-d somefile True if somefile exists and is a directory.
-s somefile True if somefile contains data (the size is not zero).
-r somefile True if somefile is readable.
-w somefile True if somefile is writable.
-x somefile True if somefile is executable.







Monday, May 16, 2011

exporting variable in Shell

You can create your own environmental variables in shell

It is very simple just type export followed by your variable and the value.

For exporting a variable gg

export gg="hello"

echo $gg
hello
you will be able to see the content of the variable gg - "hello"

But it stays untill reboot.Once you reboot you have to set the variable again.

To make it permanent you can add it to /etc/profile to make the change visible for all users using your system or you can add the change to the ~/.bash_rc to make the change visible only for your shell.

Changing the prompt in Shell

For changing the prompt in shell

we need to export the PS1 variable

Its by default set to \$ and # for root

To change it  simply follow the below step:

you can change to many special characters.but commonly used are:

\$     -  shows the user prompt($) or root prompt(#) ,depending on which user you are.

\W   -  shows the current working directory base name.if the current working directory is "/home/karthik/perl" then it simply appears as "perl"

\w   -  Displays the full path of the current working directory.

\t    -   prints the current time in hours.minutes and seconds(for ex:9:30:40)

\s   - prints the current shell name.For bash shell it simply prints bash as prompt.

\u - in this case it prints the current username.

These options can also be used together.

For ex:
export PS1="\t \w \$"
makes the prompt as
[18:30:40 /home/karthik]$>

export PS1="\W"
makes the prompt as  if the full path is "/home/karthik/perl"
perl>

Files for configuring your shell


/etc/profile     
Sets up user information for every user.
It is executed when you first log in.
 It sets up the path as well as the environmental variables,the location of your mailbox and size                                                                            of your history files.
Finally,files under the /etc/profile.d are executed.

/etc/bashrc   
 Runs for every user who runs the bash shell.
 It sets a default prompt and may add one or more aliases.
But this file can be overridden by information in ~/.bashrc file.

~/.bash_profile 
It is run when the particular user logs in.
Used by the user to enter information that is specific to  his or her own use of the shell.
This file inturn calls the user's .bashrc file

~/.bashrc         
Contains the information specific to the particular user.
This is the best location to add environmenta  variables and your aliases so that shell      
 will pick up when you log in.
 
~/.bash_logout   
Executes when you log out of your bash shell.
 By default just simply clears the screen.(this is also for a particular user)

using chmod command (or) Setting File permissions in Linux,Shell or Unix


chmod -is used to change the permissions for a file.
when you type the name of the file


ls -ld  - you will be able to see the permissions provided for the file

there are three groups to which permissions can be provided:
o - others
g - user group
u -users

to set the permissions for the file use:
chmod command followed by the fiename.

there are three permissions namely read,write,execute which can be given to the users,user group and others

For providing permission to a file we must be owner of the file or root
read permssion -4
write permission-2
execute permission-1

to give all permissions to a particular group such as user
provide 7=4+2+1 and no permission to usergroup and others
chmod 700 filename
ls -ld
-rwx------ 1 chris sales 4983 jan 23 22:13 ch3
 ---   ---
 user  others
    ---
group
1nd)r - read
2nd)w - write
3nd)x - execute

Other way to do that:
a - all users
g - owner group
u - owner user
o - others

for permissions use:
r-read
x-execute
w-write

yu can either add or subtract the permissions:

assume the permission for the file to be rwxrwxrwx
permission change when each command is executed:
chmod a-w file     r-xr-xr-x
chmod o-x file     rwxrwxrw-
chmod go-rwx file  rwx------

assume the file to have a permission of ---------
chmod u+rw files   rw-------
chmod a+x files    --x--x--x
chmod ug+rw files  rw-rw----

to change the permissions for the entire directory we have to -R opton to recuresively change the permission for the entire directory

chmod -R 777 /tmp/hello/
this command willchange the perission for the entire hello directory to 777