Useful Commands

1. To get IP Address of Machine.

#ifconfig eth0| grep "inet addr:"|cut -c '21-35'
192.168.1.207 

# ifconfig eth0| grep "inet addr:"|awk -F ":" '{print $2}'|cut -d " " -f 1
192.168.1.207

# ifconfig eth0 |grep "inet addr:"|cut -d: -f2|awk '{print $1}'
192.168.1.207


2. To get Hardware (MAC) address of Ethernet Device.

#ifconfig eth0| grep "HWaddr"|cut -c '39-55'
00:0C:29:85:C9:E3

#ifconfig eth0| grep "HWaddr"|awk -F " "  '{print $5}'
00:0C:29:85:C9:E3



3. To get load average at 1min, 5min and 15min.

# echo "Load1Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f1|sed "s/ //g"`"
Load1min: 5.01

# echo "Load5Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f2|sed "s/ //g"`"
Load1min: 2.21

# echo "Load15Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f3|sed "s/ //g"`"
Load1min: 1.01

4. To get Memory usage in readable format.

#free -tom | awk '/Total:/ {print "Total memory: "$2" MB\nUsed memory: "$3" MB\nFree memory: "$4" MB"}'

Total Memory: 1503 MB
Used Memory: 2 MB
Free Memory: 1501 MB

Restrict Normal User to run all commands

1. Create user say test.

[root@node ~]# useradd test
[root@node ~]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

2. Create a directory say ‘commands’ under user’s home directory.

[test@node ~]$ pwd
/home/test
[test@node ~]$ mkdir /home/test/commands

3. Modify .bash_profile under user’s home directory.
By default, the environmental variable “PATH” defines the list of directories that contains binary executables which will be called when the user issues a command.

Here we are going to change the PATH variable to user’s home directory/commands folder as follows.

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
PATH=$HOME/commands
export PATH


4. Now login with test user and try to hit some commands.

[test@node ~]$ ls
-bash: ls: command not found
[test@node ~]$ date
-bash: date: command not found
[test@node ~]$ mv
-bash: mv: command not found
[test@node ~]$ mkdir xyz
-bash: mkdir: command not found
[test@node ~]$ touch ab
-bash: touch: command not found
[test@node ~]$

5. Now create the softlinks of commands which are required for user test to execute in the directory /home/test/commands
Some examples are as follows.

[root@node ~]# ln -s /bin/date /home/test/commands/
[root@node ~]# ln -s /bin/mkdir /home/test/commands/
[root@node ~]# ln -s /bin/touch /home/test/commands/
[root@node ~]# ln -s /bin/ls /home/test/commands/

6. Now re-login as a test user and try above commands.

[root@node ~]# su - test
[test@node ~]$
[test@node ~]$ mkdir abc
[test@node ~]$ date
Sat Jun 10 13:14:27 IST 2017
[test@node ~]$ ls
abc  commands
[test@node ~]$ ls -lrth
total 8.0K
drwxrwxr-x. 2 test test 4.0K Jun 10 13:11 commands
drwxrwxr-x. 2 test test 4.0K Jun 10 13:14 abc
[test@node ~]$


How to create First Job in Jenkins with Subversion.

1. Login to Jenkins URL:

http://<server-ip>:8080, By Default Jenkins works on port 8080

Provide username and password to log in.    



2. Click ‘New Item’ on the left side to create a job.

Enter an Item Name i.e. the Job name, select Freestyle project and click OK to proceed.


Click on ‘This project is parameterized’, click on Add Parameter and select ‘String Parameter’


Now Under String Parameter provides Name as ‘Revision’, Default Value as ‘Head’, and add any Description.
This is needed when you build project with the parameter, it will ask for Revision Number of your choice else will build with Head revision by default.


3. Go to Source Code Management tab.

Select ‘Subversion’, provide the Repository URL, provide credentials who has the access to the Repository URL that you have provided.
Go to the “How to Create Project in SCM-Manager” for more details.


4. Go to the Build Tab.

Click on Add build step and select Execute Shell.
Provide the shell script path or shell script that Jenkins going to execute when you build the project.



For the easy installation of Jenkins please go to the http://pkg.jenkins-ci.org, select your OS ( like RPM or Debian based ) and the follow the steps given.




SCM-Manager Installation

1. RPM installation.

Create a new file at /etc/yum.repos.d/SCM-Manager.repo with the following content to install the scm-manager repository:

[scm-releases]
name=SCM-Manager Releases
baseurl=http://maven.scm-manager.org/nexus/content/repositories/releases
enabled=1
protect=0
gpgcheck=0
metadata_expire=30s
autorefresh=1
type=rpm-md


2. Install openjdk package.

  yum install java-1.x.0-openjdk

3. Install scm-server package.

  yum install scm-server
                           
4. By default, it runs on port 8080

Hit http://localhost:8080/scm on web browser, pProvide username, and password as ‘scmadmin’ and login.


Reference Link: SCM-Manager Configuration.

How to Create Project in SCM-Manager

1. Under Repositories tab click on Add to create a new project. 



2. Provide the Name of the Project under “Name:” field say project1
Select Subversion in drop down box under field “Type:”
Provide contact details such as email id under field “Contacts:”
Provide description of the project under field “Description:”
And click OK.



3. After creating the repository you will get the svn url under field “Url:”
This url is used for taking repository checkout and also committing the code in svn.    



4. Now add the users under Permission tab who are going to access this repository for taking checkout and committing new codes under the repository.  



Local SVN

Step 1: Installation of Packages.
The Two packages needed for svn is as below

#yum install -y subversion mod_dav_svn
* httpd package needs to be installed.

Step 2: Create and configure Subversion Repository.
The steps that I am going to follow are as follows.
1. Create subversion directory under /usr/local/ and change the owner and group as apache.apache.

#mkdir /usr/local/subversion
#chown –R apache.apache subversion/  #subversion and inside all the directories will have                                                                apache.apache ownership

Here the subversion folder will contain all the repository that we are going to create now and in future.

2. Create repository by using svnadmin command as follows.

#svnadmin create myproject1
* Here my repository name is myproject1

3. Touch svnupload.sh file and add following lines and save.
#vim svnupload.sh   
       
#!/bin/bash
home_dir=/usr/local/subversion/myproject1/
svnlook changed /usr/local/subversion/myproject1 >> /usr/local/subversion/myproject1/test.txt
cat /usr/local/subversion/myproject1/test.txt| sed 's/^[A-Z]//g'| sed 's/^  //g' >> /usr/local/subversion/myproject1/upload.txt
for i in `cat /usr/local/subversion/myproject1/upload.txt`;do svn export --username "svn" --password "svn123" --no-auth-cache http://localhost/myproject1/$i /var/www/html/$i --force;done
for i in `cat /usr/local/subversion/myproject1/upload.txt`;do chown -R apache:apache /var/www/html/$i ;done
for i in `cat /usr/local/subversion/myproject1/upload.txt`;do chmod -R 775 /var/www/html/$i ;done
cat /usr/local/subversion/myproject1/test.txt |grep D | sed 's/^[A-Z]//g'| sed 's/^  //g' >> /usr/local/subversion/myproject1/delete.txt
for i in `cat /usr/local/subversion/myproject1/delete.txt`;do /bin/rm –rf /var/www/html/$i ;done
> /usr/local/subversion/myproject1/test.txt
> /usr/local/subversion/myproject1/upload.txt
> /usr/local/subversion/myproject1/delete.txt

 # chmod +x svnupload.sh           #give this file a execute permission   

4. Go to the hooks directory under repository.

#cd /usr/local/subversion/myproject1
#cd hooks
#cp post-commit.tmpl post-commit --- copy post-commit.tmpl as post-commit
#vim post-commit --- open post-commit file

#REPOS="$1"                                                                 #comment this line
#REV="$2"                                                                    #comment this line
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf     #comment this line
/bin/sh /usr/local/subversion/myproject1/svnupload.sh        #add this line

#chmod –R 775 hooks/                  #give hooks directory and sub-files 775 permission

Step 3: Configuring subversion.

#cd /etc/httpd/conf.d/
#vim svn.conf --- touch new file say svn.conf and add following line.

<Location /myproject1>
DAV svn
SVNPath /usr/local/subversion/myproject1
AuthType Basic
AuthName "Subversion repositories"
AuthUserFile /etc/httpd/conf.d/.htpasswd
Require valid-user
</Location>

Now create user for subversion say test1

#htpasswd –cm /etc/httpd/conf.d/.htpasswd test1

Now restart Apache service.

Take checkout of the repository on your local machine and try to upload code through local svn.

http://<server-ip-address>/myproject1


For More Details Refer Link What is SVN? && How does SVN work?

How to Create VPC, Subnets, Internet Gateway and Nat Gateway

1. Go to VPC Dashboard under AWS console after you log in.



2. Creating VPC.
Click on Create VPC on VPC dashboard.
Now provide the details as per the requirements.
Here I have Given Name Tag as “Pranav-VPC”.
CIDR block as 192.168.0.0/24 i.e. total 254 host IP’s possible.
Remaining field kept default as it is. 



3. Creating subnets.
Here I am going to create two subnets, one is for Public facing servers and the other one is for Private servers.
First, create a Public subnet with the details as follows.
Name Tag: Public-1a (Depends on you)
VPC: Select VPC created in above step
Availability Zone: us-east-1a (Depends on you)
CIDR block: 192.168.0.0/25 i.e. 128 host IP’s are possible starting from 192.168.0.0 - 192.168.0.127 



Now we create a Private subnet with the details as follows.
Name Tag: Private-1a (Depends on you)
VPC: Select VPC created in the previous step
Availability Zone: us-east-1a (Depends on you)
CIDR block: 192.168.0.64/25 i.e. 128 host IP’s are possible starting from 192.168.0.128 - 192.168.0.255




For More detailed subnet calculation refer http://www.subnet-calculator.com/ 

4. Creating Route tables.
When we create VPC, a default route table associated with that VPC gets created automatically and it is the main route table for that VPC.
Let’s name it as a “Private-RT”.



Create one more route table and name it as “Public-RT”
Associate this route table with our VPC “Pranav-VPC”.



5. Subnet association with route tables.
Under Route tables select “Public-RT”, go to Subnet Associations tab and select public subnet to associate with it.



Similarly select route table “Private-RT”, go to Subnet Associations tab and select private subnet to associate with it.



6. Creating Internet Gateway.
Internet Gateway will be used to route internet bound traffic from the VPC to the internet. 
Go to the Internet Gateway on left side of the dashboard.
Click on Create Internet Gateway and provide Name tag as say “Pranav-IGW”


After creation, click on to Attach to VPC in order to attach the Internet Gateway to VPC say “Pranav-VPC”.  



7. Associating Internet GW with a public subnet.
A public subnet is considered public when there is a route available in the associated route table to the Internet Gateway.
Go to the Route Tables on left side of the Dashboard.
Select Public subnet i.e. “Public-RT” in this case.
Go to the Routes tab and click on add another route.
Add 0.0.0.0/0 at the Destination field and select Internet GW i.e. “Pranav-IGW” at the Target field and then save.


8. Creating NAT Gateway.
Network address translation (NAT) gateway enables instances in a private subnet to connect to the Internet or other AWS services, but prevent the Internet from initiating a connection with those instances.
Go to the “Nat Gateways” on the left side of the VPC Dashboard, click on Create Nat Gateway.
Provide the necessary details, like subnet and Elastic IP, and create the NAT Gateway. 





9. Associating NAT Gateway with a private subnet.
Now go to the “Private-RT” route table and add the route for the traffic destined for the Internet toward the gateway. 





VsFTP Interview Questions

Q) Active and Passive FTP?
Active and Passive FTP are two modes of connection that FTP runs in. FTP uses two channels between client and server, the command channel and the data channel.
Typically command channel is on port no 21 and data channel is on port no 20. The command channel handles commands and responses, the data channel handles actually transferring files.
The difference between active and passive mode lies in whether server or client initiate the data connection.
In active mode, server initiate data connection with the client after the client has established a connection on command channel.
In passive mode, the client establishes the data connection with the server. 

Reference Link: stackoverflow.com

Active FTP:
1. The client connects to FTP server by establishing FTP control connection on port no 21 of the server.
2. Whenever the client requests data over the control connection the server initiates data transfer connection back to the client.
3. The source port for this data transfer connection is always on port no 20 of the server and destination port is the higher port (greater than 1024) on the client side.
4. Active mode is more secure for the server as it does not require an insecure port to be open at server end because it’s the server that initiates the data connection with the client side.

Passive FTP:
1. The client connects to FTP server by establishing FTP control connection on port no 21 of the server.
2. Whenever the client requests data over the control connection the client initiates data transfer connection back to the server.
3. The source port for this data transfer connection is always on the higher port on the client side and destination port is the higher port on the server side.

Reference Link: http://www.slacksite.com/other/ftp.html

Q) Restrict user to their home directory?