Find ports used by program

You are sometimes trying to install or configure a program and bind it to a port and it throws an error. That means some other program is using that particular port. Let’s see how you can check what program is using your system’s ports.
First if you know the number of the port you are interested in you can:
For example let’s check port number 631:
$ sudo lsof –i:631 –n –P
That should give you something like that:
COMMAND   PID USER   FD   TYPE  DEVICE  SIZE/OFF  NODE NAME
cupsd         776 root   10u  IPv6   9353       0t0  TCP [::1]:631 (LISTEN)
cupsd         776 root   11u  IPv4   9354       0t0  TCP 127.0.0.1:631 (LISTEN)
cups-brow      953 root    8u  IPv4   9930      0t0  UDP *:631
In the above, the “-n” parameter prevents automatic conversion of host IP address to host name, and “-P” parameter prohibits conversion of port number to port name. In our example, cupsd and cups-brow processes are using TCP and UDP port number 631, respectively.
If you want a list of all open TCP ports and the programs/processes associated you can type:
$ sudo lsof -i -n -P | grep TCP
On the table resulting from it, on the left column it has the processes and on the right the ports.
- See more at: http://www.unixmen.com/check-program-using-port-via-terminal/#sthash.RJSMJDQE.dpuf

Chef



Chef server part 1
===================
Enviroments
===============
think of it as development enviroment..development,test,staging,production
etc
model lif stages
may include url,location of your package repository,version of chef configuration

ROLES
=======
-->represents the type of servers in your infrastructure
like load balancer,app server ,monitoring server, DB server,DB cache server etc.

--allows you to define policy,may include list of chef config files (also called run list) to be applied.
--may include data attributes necessary for configuring your infrastructure, eg port that app server listens on,list of app to be deployed

Nodes
=============
nodes represents physical/virtual server in your infrastructure(may be in a public or pvt cloud).
each node will --belong to one organization,one enviroment,
have zero or more role.

Resources
===========
represents piece of system and its desired state eg. a package that should be installed,service to run,
file to be configured, cron job to run,a user which should be managed etc.
~ are fundamental building blocks of chef configuration.
~  are gathered into recipes...recipes ensure system is in desired state.

Recipe
========
collection of resources..help define the policy that should be  applied to node or in any component of  infrastructure,,it is configuration files that describe resources and its desired state
--it can install and configure s/w components.
--~ can manage files,deploy apps,execute other recipes


Cookbooks
==================
multiple recipes are taken and packaged into Cookbook.
recipes are stored in cookbooks.
contains recipes,templates,files,custom.
it makes use of code reuse and modularity.

Runlist
=======
runlist is the thing that is applied to the node.
it is marrying of policy and the node.
run list is composed of recipes and roles(application server role,database server role etc).

Search feature
=================
application server nodes can ask Chef server to search for other available servers with Roles,topology data
can use ip address,hostnames,FQDNs

NIC Bonding

eth2==
          ||||==> bond0---->>[]
eth3==


----------------------------------------------------------------------------------------------------
 [root@server2 network-scripts]#cat  ifcfg-eth2   
DEVICE=eth2
HWADDR=00:0c:29:5b:39:1d
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPV6INIT=no
USERCTL=no
##########################################
MASTER=bond0
SLAVE=yes

##########################################
#IPADDR=192.168.213.112
#NETMASK=255.255.255.0
#GATEWAY=192.168.213.2
#DNS1=8.8.8.8
[root@server2 network-scripts]#
[root@server2 network-scripts]# cat  ifcfg-eth3   
DEVICE=eth3
HWADDR=00:0c:29:5b:39:27
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
USERCTL=no
MASTER=bond0
SLAVE=yes

[root@server2 network-scripts]#
[root@server2 network-scripts]# cat ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPV6INIT=no
USERCTL=no
IPADDR=192.168.213.112
NETMASK=255.255.255.0
GATEWAY=192.168.213.2
DNS1=8.8.8.8

###below are setting for mode0 (mode zero)#####
#mode=0  for round robin and mode=1 for active backup
# miimon = n milliseconds  to check if any eth is down

BONDING_OPTS="mode=0  miimon=100"


 [root@server2 network-scripts]# watch -n .1 cat /proc/net/bonding/bond0

Changing passwords from scripts (docker file)

You can run the passwd command and send it piped input. So, do something like:
echo thePassword | passwd theUsername --stdin
 
You could also use chpasswd:
echo username:new_password | chpasswd   
echo username:newpassword   | chpasswd
so, you change password for user username to new_password.

Docker container commands

check docker version
------------------------
docker version    

Search for image


docker search urgensherpa/lamp6
redhat@test:~/ssh_image$ docker search urgensherpa
NAME                        DESCRIPTION   STARS     OFFICIAL   TRUSTED
urgensherpa/lamp6                                        0                   

Pulling image
you@tutorial:~$ docker pull learn/tutorial
Pulling repository learn/tutorial from https://index.docker.io/v1
Pulling image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c (precise) from ubuntu
Pulling image b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc (12.10) from ubuntu
Pulling image 27cf784147099545 () from tutorial
you@tutorial:~$

Run commands in container   
docker run learn/tutorial echo "hello world"

installing things in the container

docker run learn/tutorial apt-get install -y ping  

docker ps -l
docker ps
=== ========== ========= =        ====================
docker commit 698989sdafd learn/ping  
====meaning==
698989sd...= container id
learn/ping = learn -username and ping - image
-----------------------------------------------------------
docker run learn/ping ping google.com      
Find info about docker container
--------------------------------
docker inspect efesdlfk     

docker images   = This will show you which images are currently on your host

docker push learn/ping   

fail2ban on Ubuntu 12.04 LTS

Simple fail2ban config
- - - - -- - - - - -  - --  - -
#sudo apt-get install fail2ban

Now to fine tune default configs

#cd /etc/fail2ban

#sudo vim jail.conf
#it also help protect ssh ftp apache asterisk ...
.
.
bantime  = 989898     ##in seconds
maxretry = 3     ##place your threshold value

#search for jails
#JAILS

[ssh]
enabled=true
port =ssh
filter = sshd
logpath = var/log/auth.log
maxretry = 3

save/exit
................................
#service fail2ban restart

Change language setting os = ubuntu 13.04

# vim /etc/default/locale

 //change to below param's if its like eg. LANG="np_NP"                                    
LANG="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
.
..
...

# sudo locale-gen

logout and login again

Autostart script at runtime /etc/rc.local


The /etc/rc.local file is common to major Linux distributions.
This file contains BASH commands which will be run after run-level specific commands whenever the system is booted.
In a VPS environment, /etc/rc.local is commonly used to store the hostname <SERVER_HOSTNAME>command (where <SERVER_HOSTNAME> is the intended hostname for the VPS) to ensure that the VPS hostname is not reset when the VPS is rebooted.
                                                                         Method 1                                     
redhat@cloud01:/etc/init.d$ nano rc.local 


  GNU nano 2.2.6                                      File: rc.local                                                                                    


        if [ -x /etc/rc.local ]; then

                [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
                /etc/rc.local
                ES=$?
                [ "$VERBOSE" != no ] && log_end_msg $ES
                return $ES
        fi
}

case "$1" in

    start)
        do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
###########################at the end of file >>>>

/root/script/mystartupscript1.sh

/root/script/mystartupscript2.sh  &    #mystartupscript2  & ==the & sends the script to background 
chown wso2:docker /var/run/docker.sock
modprobe aufs
sudo killall dnsmasq


##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

                                                        Method 2  #vim   /root/scripts/mystartupscript1.sh
boot=$(date)
echo "the system boot at $boot " >> /var/log/custombootlog.log

#chmod u+x mystartupscript1.sh

#vim /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#put your script/command here

chown wso2:docker /var/run/docker.sock
modprobe aufs
sudo killall dnsmasq
/root/script/mystartupscript1.sh
/root/script/mystartupscript2.sh &

exit 0


docker1&2 setup note

FLOATING_RANGE=192.168.140.0/27
FIXED_RANGE=10.11.12.0/24
FIXED_NETWORK_SIZE=256
FLAT_INTERFACE=eth1
ADMIN_PASSWORD=g
MYSQL_PASSWORD=g
RABBIT_PASSWORD=g
SERVICE_PASSWORD=g
SERVICE_TOKEN=g
SCHEDULER=nova.scheduler.filter_scheduler.FilterScheduler
VIRT_DRIVER=docker
SCREEN_LOGDIR=$DEST/logs/screen
----------------------------------------------------------------
 cat install_docke0.sh
#!/usr/bin/env bash

# **install_docker.sh** - Do the initial Docker installation and configuration

# install_docker.sh
#
# Install docker package and images
# * downloads a base busybox image and a glance registry image if necessary
# * install the images in Docker's image cache


# Keep track of the current directory
SCRIPT_DIR=$(cd $(dirname "$0") && pwd)
TOP_DIR=$(cd $SCRIPT_DIR/../..; pwd)

# Import common functions
source $TOP_DIR/functions

# Load local configuration
source $TOP_DIR/stackrc

FILES=$TOP_DIR/files

# Get our defaults
source $TOP_DIR/lib/nova_plugins/hypervisor-docker

SERVICE_TIMEOUT=${SERVICE_TIMEOUT:-60}


# Install Docker Service
# ======================

# Stop the auto-repo updates and do it when required here
NO_UPDATE_REPOS=True

# Set up home repo
curl https://get.docker.io/gpg | sudo apt-key add -
install_package python-software-properties && \
    sudo sh -c "echo deb $DOCKER_APT_REPO docker main > /etc/apt/sources.list.d/docker.list"
apt_get update
install_package --force-yes lxc-docker-${DOCKER_PACKAGE_VERSION} socat
###############################################################################################
cat install_docke1.sh
#!/usr/bin/env bash

# **install_docker.sh** – Do the initial Docker installation and configuration

# install_docker.sh
#
# Install docker package and images
# * downloads a base busybox image and a glance registry image if necessary
# * install the images in DockerÂ’s image cache

# Keep track of the current directory
SCRIPT_DIR=$(cd $(dirname $0?) && pwd)
TOP_DIR=$(cd $SCRIPT_DIR/../..; pwd)

# Import common functions
source $TOP_DIR/functions

# Load local configuration
source $TOP_DIR/stackrc

FILES=$TOP_DIR/files

# Get our defaults
source $TOP_DIR/lib/nova_plugins/hypervisor-docker

SERVICE_TIMEOUT=${SERVICE_TIMEOUT:-60}

# Install Docker Service
# ======================

# Stop the auto-repo updates and do it when required here
NO_UPDATE_REPOS=True

# Start the daemon – restart just in case the package ever auto-starts…
#restart_service docker

echo Waiting for docker daemon to start
DOCKER_GROUP=$(groups | cut -d' ' -f1)
CONFIGURE_CMD="while ! /bin/echo -e 'GET /v1.3/version HTTP/1.0\n\n' | socat - unix-connect:$DOCKER_UNIX_SOCKET | grep -q '200 OK'; do

echo failure
sleep 1
done"
if ! timeout $SERVICE_TIMEOUT sh -c "$CONFIGURE_CMD"; then
die $LINENO docker did not start
fi

Changing Priority Values for process/scripts

Changing priority of process/or running script :
-------------------------------------------------
Use top or ps
------------------------------------------------
eg. ./myscript.sh
#ps -ef | grep myscript
get the process id...
eg.4545
#ps -o pid,comm,nice -p 4545            //pid,,comm=command,nice=nice value of process , -p = processid, -o = outputformat

#ps -efl     <displays nice values>
....
......
#renice -15 -p 4545

// / nice value ranges from -20 to 19 ..negative value have higher priority(eg.-20 is highest priority and 19 have lowest , default priority is 0)
==============================================
To set nice value/ priority for process/script/cmd to be started
======================================================
#nice -n 10 apt-get upgrade
#nice -n -11 ./myscript.sh