Showing posts with label Unix Commands. Show all posts
Showing posts with label Unix Commands. Show all posts

Tuesday 13 August 2019

How do I add environment variables in Unix



Question: How to print the environment values?
printenv

OR
env



Question: How to SET the environment values For Korn shell (KSH)?
var=value
export varname



Question: How to SET the environment values For Bourne shell (sh and bash)?
export var=value



Question: How to SET the environment values For C shell (csh or tcsh)?
setenv var value



Question: What is .bashrc?
.bashrc is a shell script that Bash runs whenever it is started interactively.


Question: Can we set the environment value in .bashrc?
Yes, you can set the value in this also. like below
export PATH="$PATH:/some/addition"




Wednesday 25 October 2017

iptables tutorial for Beginner - iptables commands

iptables tutorial for Beginner

Question: What is iptables?
iptables is a utility program that allows a system administrator to configure the tables provided by the Linux kernel firewall.
Different kernel modules and programs are used for different protocols, For Example
iptables applies to IPv4
ip6tables applies to IPv6
arptables applies to ARP
ebtables to Ethernet frames


Question: What privileges required to manage iptables?
iptables requires user root to manage.


Question: How to install iptables?
sudo apt-get install iptables



Question: Why we use iptables?
We use Iptables to allow/deny traffic for incoming/outgoing.


Question: How iptables works?
We define the rules for All type Incoming and Outgoing connection.
For Example
When someone try to established a connection, iptables looks for a rule in its list to do as per rules.


Question: What is Policy Chain default Behavior?
If someone trying to connect and that is not in existing rules, that rules come under default behavior.


Question: How to check all the iptables Rules?
iptables -L -n -v


Output
Chain INPUT (policy ACCEPT 1129K packets, 415M bytes)
pkts bytes target prot opt in out source destination 
0 0 ACCEPT tcp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
0 0 ACCEPT udp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
0 0 ACCEPT tcp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:67
0 0 ACCEPT udp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination 
0 0 ACCEPT all -- * lxcbr0 0.0.0.0/0 0.0.0.0/0 
0 0 ACCEPT all -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0


Chain OUTPUT (policy ACCEPT 354K packets, 185M bytes)
pkts bytes target prot opt in out source destination



Question: What are different type of connection defined in iptables?
  1. Input: This chain is used to control the behavior for incoming connections.
  2. Forward : This chain is used to control the behavior for incoming connections that local but forward from another like router.
  3. Output : This chain is used to control the behavior for outgoing connections



Question: What are different type of iptables Responses for connections?
  1. Accept: Allow the connection.
  2. Drop : Drop the connection. It is used when you don't want the source to realize your system exists
  3. Reject: Don't allow the connection and send back an error.



Question: How to block a connection from specific IP Address?
iptables -A INPUT -s 10.20.20.20 -j DROP
10.20.20.20 is IP Address.


Question: How to block a connection from IP Address range?
iptables -A INPUT -s 10.10.10.0/24 -j DROP

10.20.20.20 is IP Address.


Question: How to un-block a connection from specific IP Address?
iptables -D INPUT -s 10.20.20.20 -j DROP

10.20.20.20 is IP Address.


Question: How to block outgoing connections on a specific port?
iptables -A OUTPUT -p tcp --dport 8082 -j DROP

8082 is Port.


Question: How to block outgoing connections from mulitple port?
iptables -A OUTPUT  -p tcp -m multiport --dports 8081,8082,8083 -j ACCEPT

8081, 8082, 8083 is Port.


Question: How to block incoming connections on a specific port?
iptables -A INPUT -p tcp --dport 8082 -j DROP

8082 is Port.


Question: How to Limit the Number of Concurrent Connections per IP Address?
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT



Question: How to search a string in iptables?
iptables -L $table -v -n | grep $string



Question: How to add new rule in iptables?
iptables -N custom-filter



Question: How to Disable Outgoing Mails through IPTables?
iptables -A OUTPUT -p tcp --dports 25,465,587 -j REJECT



Question: How to get help on IPTables?
man iptables 



Thursday 7 September 2017

Shell Scripting Interview Questions and Answer

Shell Scripting Interview Questions and Answer

Question: How to Check if a directory exist?
DIRECTORY='directoryname'
if [ -d "$DIRECTORY" ]; then
  echo "Directory is found"
else
  echo "Directory is Not found"
fi



Question: How to concatenate two string variable?
str1='Web'
str2='Technology'
fullStr="$str1 $str2";
echo $fullStr; #Web Technology 



Question: What do you mean by echo file1.txt > file2.txt?
> is used to redirect output.
echo file1.txt > file2.txt

It just redirect the output to file2.txt


Question: What do you means by 2>&1?
0 is stdin
1 is stdout
2 is stderr
>& is the syntax to redirect a stream to another file descriptor

So, Its saying display the error on stdout


Following two do same thing.
echo test 1>&2
OR
echo test >&2




Question: What is scp and why use? Give example?
SCP is used to copy the file/dir from one server to another server.
Example:
scp -r user@your.server.example.com:/path/to/source /home/user/destination/




Question: What is command to copy a file?
cp file1.txt \another\folder\file1.txt



Question: What is command to move a file?
mv file1.txt \another\folder\file1.txt



Question: How to count number of words in file?
 wc -w file.txt



Question: How to append in file from source file?
 cat sourcefile.txt >> destinationfile.txt
It will add the content in destinationfile.txt from sourcefile.txt



Question: How to search a string and then delete that line?
 sed '/removeTxt/d' ./file
It will remove the line having content removeTxt and print the output to console.



Question: How to set a variable to the output from a command?
 OUTPUT="$(ls -1)"
echo "${OUTPUT}"



Question: How to convert a string in lowercase?
a="Hello World!" 
echo "$a" | tr '[:upper:]' '[:lower:]' #hello world!



Question: What is use of export command?
export command is used to make the available of variable in child process.
Example 
export name=test

Now name variable will be accessible in child process.



Question: How to print date in yyyy-mm-dd format?
YYYY-MM-DD format
echo `date +%Y-%m-%d`
YYYY-MM-DD HH:MM:SS format
echo `date '+%Y-%m-%d %H:%M:%S'`



Question: How to reverse the lines?
tac file1.txt
It will revere the lines and display in console output.


Question: How to redirect output to file?
 ls -l 2>&1 | tee -a errorfile.txt

It will store error and output(both) in error_file.txt


Question: How to determine whether a Linux is 32 bit or 64 bit?
uname -m

Output
x86_64 ==> 64-bit kernel
i686   ==> 32-bit kernel



Question: How to compare two directories?
diff --brief -r dir1/ dir2/
diff -qr -r dir1/ dir2/



Question: When do we need curly braces around shell variables?
When you want to expand the variable (like foo) in the string.
For Example you want to make $foobar.
echo "${foo}bar"




Wednesday 6 September 2017

Unix Shell Scripting Tutorial - page 8

Unix Shell Scripting Tutorial - page 8

Question: What are two different way to executing a file?
Script file with name script8.sh
myname='Arun kumar';
echo $myname 

Way #1
sh script8.sh

After executing this script, if we run echo $myname it will not print anything because $myname is not in scope.

Way #2
source script8.sh

After executing this script, if we run echo $myname it will print because $myname is in scope.


Question: How to get input from user?
echo "Enter your name?"
read name
echo "Your name is '$name'"



Question: How to include configuration file in main file?
use source to include file.
source config.sh
myname='Arun kumar';
echo $myname 



Question: Give example of for?
for  table in {2..20..2}
do 
echo "$table,"
done

Output 2,4,6,8,10,12,14,16,18,20


Question: What is difference between while and until?
 Until loop always executes at least once. while executes till it returns a zero value and until loop executes till it returns non-zero value.

Question: Give example of function with parameters?
function fname(){
    echo $1; #first argument
    echo $2; #second argument
    echo $3; #third argument
}

fname a b c


Question: Give example of Switch case?
FRUIT="kiwi"

case "$FRUIT" in
   "apple") echo "Apple is good for health." 
   ;;
   
   "banana") echo "I like banana." 
   ;;
   
   "kiwi") echo "New Zealand is famous for kiwi fruit." 
   ;;
esac


Question: What does do eval? Give example eval
execute the statement.
name='name_variable'
name_variable='This is value'
echo $name; #name_variable
echo \${$name}; #${name_variable}
eval echo \${$name}; #This is value

Monday 4 September 2017

Unix Shell Scripting Tutorial - page 7

Unix Shell Scripting Tutorial - page 7

Question: What is meaning of first line in unix?
#!/bin/bash

This indicates that the script should be run in the bash shell regardless of which interactive shell.


Question: What sign is used in unix for comment a line?
Pound Sign

Pound sign (#), is used to comment a line.

# echo "this is comment" 



Question: When to use single quote OR Double quote?
Single quote: When you are assigning string without variable, then you should use single quote.
Double quote: When you are assigning string with variable, then you should use Double quote.


Question: What's wrong in following variable?
strVar = "Hello World! How are you?" 
echo $strVar;

There should not be space before and after the equal sign (=). Correct code are below:
strVar="Hello World! How are you?" 
echo $strVar;



Question: Give sample example of If else?
if condition1
then
 statement1
 statement2
 statement3
 
else
 statement4
 statement5
fi



Question: Give sample example of If elseif?
if condition1
then
 statement1
 statement2
 statement3
 
elif condition2
then
 statement4
 statement5
 
elif condition3
then
 statement6
 statement7
 statement8
fi



Question: Give list of Operator used in unix shell scripting?
Operator Results number of operands
-noperand non zero length1
-zoperand has zero length1
-dthere exists a directory whose name is operand1
-fthere exists a file whose name is operand1
-eqthe operands are integers and they are equal2
-neqthe opposite of -eq2
=the operands are equal (as strings)2
!=opposite of = 2
-ltoperand1 is strictly less than operand2 (both operands should be integers)2
-gtoperand1 is strictly greater than operand2 (both operands should be integers)2
-geoperand1 is greater than or equal to operand2 (both operands should be integers)2
-leoperand1 is less than or equal to operand2 (both operands should be integers)2