Apache webserver 2.2.15.el6 (centos 6.4 x86) part1



Apache
yum install httpd -y

inside /etc/httpd/conf/httpd.conf
line 57
ServerRoot   /etc/httpd  ##ServerRoot is the path to the server’s configuration, error and log                                      files.. it can be changed [ no trailing / at end of path e.g /etc/httpd/  ]
line 65
PidFile  run/httpd.pid
##PidFile is the process identification number for the httpd. This process number
is important, because Apache spawns numerous child processes when running
to accommodate the web traffic. It allows you to monitor and manipulate your
server processes

Line 276
ServerName www.ninja.com:80
this setting you will have to change to get your server running. This
is where you declare the name of your website. I will use www.ninja.com
Line 292
DocumentRoot "/var/www/html"
Line 484
ErrorLog logs/error_log
Line 136
Listen 80

Virtual Hosts

Running several name-based web sites on a single IP address.

You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing,

Listen 80 #this is default

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
</VirtualHost>

or  

NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>   ##172.20.30.40 is real ip of system
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here
</VirtualHost> 
 
The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.example1.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.
------------------------------------------------------------------------------------------------
For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
You can alternatively specify an explicit IP address in place of the * in both the NameVirtualHost and <VirtualHost> directives. For example, you might want to do this in order to run some name-based virtual hosts on one IP address, and either IP-based, or another set of name-based virtual hosts on another address.
Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the <VirtualHost> section. For example in the first <VirtualHost> block above, the ServerAlias directive indicates that the listed names are other names which people can use to see that same web site:
ServerAlias domain.tld *.domain.tld
then requests for all hosts in the domain.tld domain will be served by the www.domain.tld virtual host. The wildcard characters * and ? can be used to match names. Of course, you can't just make up names and place them in ServerName or ServerAlias. You must first have your DNS server properly configured to map those names to an IP address associated with your server.
The complete list of names in the VirtualHost directive are treated just like a (non wildcard) ServerAlias.
Finally, you can fine-tune the configuration of the virtual hosts by placing other directives inside the <VirtualHost> containers. Most directives can be placed in these containers and will then change the configuration only of the relevant virtual host. To find out if a particular directive is allowed, check the Context of the directive. Configuration directives set in the main server context (outside any <VirtualHost> container) will be used only if they are not overridden by the virtual host settings.
Now when a request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then it will look at each <VirtualHost> section with a matching IP address and try to find one where the ServerName or ServerAlias matches the requested hostname. If it finds one, then it uses the configuration for that server. If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a <VirtualHost> container and list it first in the configuration file.


Name-based hosts on more than one IP address.
# This is the other address
NameVirtualHost 172.20.30.50

<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here ...

</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here ...

</VirtualHost> 

Any request to an address other than 172.20.30.50 will be served from the main server. A request to 172.20.30.50 with an unknown hostname, or no Host: header, will be served from www.example1.com. 

Serving the same content on different IP addresses (such as an internal and external address).

The server machine has two IP addresses (192.168.1.1 and 172.20.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server.example.com resolves to the external address (172.20.30.40), but inside the network, that same name resolves to the internal address (192.168.1.1).
The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.

Server configuration

NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40

<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
</VirtualHost>
Now requests from both networks will be served from the same VirtualHost.

Note:

On the internal network, one can just use the name server rather than the fully qualified host name server.example.com.
Note also that, in the above example, you can replace the list of IP addresses with *, which will cause the server to respond the same on all addresses.

Running different sites on different ports.

You have multiple domains going to the same IP and also want to serve multiple ports. By defining the ports in the "NameVirtualHost" tag, you can allow this to work. If you try using <VirtualHost name:port> without the NameVirtualHost name:port or you try to use the Listen directive, your configuration will not work.

Server configuration

Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>

<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

IP-based virtual hosting

The server has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively.

Server configuration

Listen 80

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost> 


Mixed port-based and ip-based virtual hosts

The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively. In each case, we want to run hosts on ports 80 and 8080.

Server configuration

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost> 


Mixed name-based and IP-based vhosts

Server configuration

Listen 80

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>

<VirtualHost 172.20.30.60>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost> 

 





 

Multiple SSL Certificate install on apache2 server  [Ubuntu 12.04]
Enable SSL on your webserver
sudo a2enmod ssl
Follow up by restarting Apache.
sudo service apache2 restart
Copy your SSL certificate and key to any location
Eg. Here we copied to /etc/apache2/ssl  
Make sure permission of those files are 400 or 600 [root only read/write]
In   sites/sites-available/<your domains>

vim btlg.com.au
<VirtualHost *:80>
ServerName btlg.com.au 
ServerAlias 
www.btlg.com.au
DocumentRoot /home/bootleg/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName btlg.com.au
ServerAlias 
www.btlg.com.au
DocumentRoot /home/bootleg/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/btlg/281027f6c6f445.crt
SSLCertificateKeyFile /etc/apache2/ssl/btlg/www_btlg_com_au.key 
SSLCertificateChainFile /etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>

vim example.com.au
<VirtualHost *:80>
ServerName btlg.com.au 
ServerAlias
example.com.au
DocumentRoot /home/example.com/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName
example.com.au
ServerAlias www.example.com.au
DocumentRoot /home/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/example/281027f6c6f445.crt
SSLCertificateKeyFile /etc/apache2/ssl/example/www_example_com_au.key 
SSLCertificateChainFile /etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>


Restart apache2 and browse to your domain with http or https

Restricting  virtual host by ip address example

<virtualhost *:80>
ServerName mail.rpmanpower.com
ServerAlias mail.rpmanpower.com
ServerAdmin zadmin@localhost
DocumentRoot "/etc/zpanel/panel/etc/apps/webmail"
<Location />
Order deny,allow
Deny from all
Allow from 182.93.95.225 198.50.239.107
</Location>
</virtualhost>

this will restrict access to all ip address to mail.rpmanpower.com
EXCEPT 182.93.95.225 198.50.239.107  thest TWO CAN ACCESS

Deny only one ip 192.168.16.100
<Location /server-status>
    SetHandler server-status
    Order Allow,Deny
    Deny from  192.168.16.100
    Allow from 192.168.16.0/24
</Location>
Deny from all is not needed. In fact it will screw up because everything will match all, and thus denied (and I think Apache is trying to be smart and do something stupid). I have always found Apache's Order, Allow and Deny directives confusing, so always visualize things in a table (taken from the docs):
Match      | Allow,Deny result   | Deny,Allow result
-------------------------------------------------------
Allow only | Allowed             | Allowed
Deny only  | Denied              | Denied
No match   | Default: Denied     | Default: Allowed
Match both | Final match: Denied | Final match: Allowed
With the above settings:
  • Requests from 192.168.16.100 get "Match both" and thus denied.
  • Requests from 192.168.16.12 get "Allow only" and thus allowed.
  • Requests from 123.123.123.123 get "No match" and thus denied.

Apache 2.4.X  restricting URLS (ubuntu 14.04.X)

open up the apache host ffile and add below to allow only listed IP to access this url

 Example 2:
===========

<VirtualHost *:80>
...
ServerName hottiehunter.com
        ServerAlias  www.hottiehunter.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/hottiehunter/public
<Location /admin>
             Require ip 202.51.76.236 110.44.116.70 202.51.76.238

</Location>

 <Directory /var/www/hottiehunter/public>
        Options -Indexes
        </Directory>



Example 2
========

Alias /PMA /usr/share/phpmyadmin
<Location /PMA>
Require ip 202.51.76.236 110.44.116.70 202.51.76.238
</Location>

<Directory /usr/share/phpmyadmin>
        Options FollowSymLinks
        DirectoryIndex index.php

<IfModule mod_php5.c>
                AddType application/x-httpd-php .php
....
......


NICE RENICE

The process scheduling priority range is from -20 to 19. We call this as nice value.
A nice value of -20 represents highest priority, and a nice value of 19 represent least priority for a process.
By default when a process starts, it gets the default priority of 0.
# nice -5 perl script.pl    {for 5}
# nice --10 perl test.pl {for -10}
 
$ ps -fl -C "perl test.pl" 
 
Change running pprocesses  

# renice -n -19 -p 3534
 
# renice -n 5 -g apache  (of group apache) 
 
# renice -n 5 -u hari    {of user hari}


Traffic control and HTB

"tc" is the command-line tool to interact with the Linux kernel's traffic control subsystem, and the "htb" is queuing disciplines that can be used within the "tc" command to shape and prioritize network traffic.

primary egress(outgoing traffic) queuing discipline on any device is known as the root qdisc.

HTB make sure that each class gets at least the minimum of the amount of service assigned to it when requested by that class. When a class requests less than the amount assigned, the remaining (excess) bandwidth is distributed to other classes which request service

# Create the root qdisc, here eth0 is internet link
tc qdisc add dev eth0 root handle 1: htb default 1000
The default 1000 means that any traffic that is not otherwise classified will be assigned to class 1:1000



handles are written X : Y where x is an integer identifying a qdisc(queuing discipline) and y is an integer identifying a class belonging to that qdisc. The handle for a qdisc must have zero for its y value and the handle for a class must have a non-zero value for its y value. The "1:" above is treated as "1:0".tc class add dev eth0 parent 1: classid 1:1 htb rate 2mbit ceil 2mbit burst 1k


here burst=1kilobyte (burst < mtu=1500 B) to avoid fragmentation# Define variables


### Define variables
INTERNET_INTERFACE="eth1" ### Interface connected to the internet
LOCAL_NETWORK_INTERFACE="eth0" ### Interface connected to the local network
GENERAL_RATE="5mbit" ### General rate for all machines except specific ones
SPECIFIC_MAC_PC="aa:bb:cc:11:22:33" ### Specific MAC address of the PC with 10Mbps rate
SPECIFIC_MAC_LAPTOP="dd:ee:ff:44:55:66" ### Specific MAC address of the laptop with 15Mbps rate

### Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

### Clear existing tc configuration
tc qdisc del dev $INTERNET_INTERFACE root
tc qdisc del dev $LOCAL_NETWORK_INTERFACE root
tc qdisc del dev ifb0 root

### Create ifb device
modprobe ifb numifbs=1
ip link set dev ifb0 up

### Configure HTB qdisc on the internet interface (upload)
tc qdisc add dev $INTERNET_INTERFACE root handle 1: htb default 9999

### Create default class for general rate (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k

### Create class for PC with specific MAC address (10Mbps) (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:11 htb rate 10mbit ceil 10mbit burst 5k

### Create class for laptop with specific MAC address (15Mbps) (upload)
tc class add dev $INTERNET_INTERFACE parent 1: classid 1:12 htb rate 15mbit ceil 15mbit burst 5k

### Create filters to match specific MAC addresses and assign them to their respective classes (upload)
tc filter add dev $INTERNET_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:11 match mac $SPECIFIC_MAC_PC flowid 1:11
tc filter add dev $INTERNET_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:12 match mac $SPECIFIC_MAC_LAPTOP flowid 1:12

### Configure HTB qdisc on the local network interface (download)
tc qdisc add dev $LOCAL_NETWORK_INTERFACE root handle 1: htb default 9999

### Create default class for general rate (download)
tc class add dev $LOCAL_NETWORK_INTERFACE parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k

### Create filter to match all traffic and assign it to the default class (download)
tc filter add dev $LOCAL_NETWORK_INTERFACE parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:9999

### Redirect incoming traffic to ifb0 for shaping (download)
iptables -A FORWARD -i $INTERNET_INTERFACE -j ifb0

### Configure HTB qdisc on ifb0 (download)
tc qdisc add dev ifb0 root handle 1: htb default 9999

### Create default class for general rate on ifb0 (download)
tc class add dev ifb0 parent 1: classid 1:9999 htb rate $GENERAL_RATE ceil $GENERAL_RATE burst 5k

### Create filter to match all traffic on ifb0 and assign it to the default class (download)
tc filter add dev ifb0 parent 1:0 protocol ip prio 1 u32 match u32 0 0 flowid 1:9999

+-------+   +------+                 +------+
|ingress|   |egress|   +---------+   |egress|
|qdisc  +--->qdisc +--->netfilter+--->qdisc |
|eth1   |   |ifb1  |   +---------+   |eth1  |
+-------+   +------+                 +------+


INTERNET_INTERFACE="eth1" LOCAL_NETWORK_INTERFACE="eth0" iptables -F iptables -t nat -F # Enable IP forwarding echo 1 > /proc/sys/net/ipv4/ip_forward # Set up iptables rules to redirect traffic to tc classes iptables -t mangle -A PREROUTING -i $INTERNET_INTERFACE -j MARK --set-mark 1 iptables -t nat -A POSTROUTING -o $INTERNET_INTERFACE -j MASQUERADE iptables -A FORWARD -i $INTERNET_INTERFACE -o $LOCAL_NETWORK_INTERFACE -m mark --mark 1 -j ACCEPT iptables -A FORWARD -i $LOCAL_NETWORK_INTERFACE -o $INTERNET_INTERFACE -j ACCEPT



Reference: http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm ,















PPTP

download pptpd for redhat based 64bit
http://poptop.sourceforge.net/yum/stable/packages/pptpd-1.4.0-1.el6.x86_64.rpm

download pptpd for redhat based 32bit
http://poptop.sourceforge.net/yum/stable/packages/pptpd-1.4.0-1.el6.i686.rpm

OS: CentOS 6 64bit
VPN server:  192.168.0.2
VPN client IP: 192.168.10.10 - 192.168.10.70
VPN username: vpnuser
Password: myVPN$99
1. Install ppp via yum:
$ yum install ppp -y
2. Download and install pptpd (the daemon for point-to-point tunneling). You can find the correct package at this website http://poptop.sourceforge.net/yum/stable/packages/  OR go above link at top of page:
$ cd /usr/local/src
$ wget http://poptop.sourceforge.net/yum/stable/packages/pptpd-1.3.4-2.el6.x86_64.rpm
$ rpm -Uhv pptpd-1.3.4-2.el6.x86_64.rpm
3. Once installed, open /etc/pptpd.conf using text editor and add following line:
localip 192.168.10.1        //your ip 
remoteip 192.168.10.10-70    //ip for remote distribution
4. Open /etc/ppp/options.pptpd and add  authenticate method, encryption and DNS resolver value:
require-mschap-v2
require-mppe-128
ms-dns 8.8.8.8
5. create user to access the VPN server. Open /etc/ppp/chap-secrets and add the user as below:
vpnuser pptpd myVPN$99 *
The format is: [username] [space] [server] [space] [password] [space][IP addresses]
6. We need to allow IP packet forwarding for this server. Open /etc/sysctl.conf via text editor and change line below:

net.ipv4.ip_forward = 1

7. Run following command to take effect on the changes:
$ sysctl -p
8. Allow IP masquerading in IPtables by executing following line:
$ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ service iptables save
$ service iptables restart
 
Update: Once you have done with step 8, check the rules at /etc/sysconfig/iptables. Make sure that the POSTROUTING rules is above any REJECT rules.
9. Turn on the pptpd service at startup and reboot the server:
$ chkconfig pptpd on
$ init 6
Once the server is online after reboot, you should now able to access the PPTP server from the VPN client. >> monitor /var/log/messages for ppp and pptpd related log

If you would also like your PPTP clients to talk to each other, add the following iptables rules:
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
iptables -I INPUT -s 10.0.0.0/8 -i ppp0 -j ACCEPT
iptables -A FORWARD --in-interface eth0 -j ACCEPT













PAM - pluggable authentication module part 1

we begin by considering an example. We take an application that grants some service to users; login is one such program. Login does two things, it first establishes that the requesting user is whom they claim to be and second provides them with the requested service: in the case of login the service is a command shell (bash, tcsh, zsh, etc.) running with the identity of the user.
Traditionally, the former step is achieved by the login application prompting the user for a password and then verifying that it agrees with that located on the system; hence verifying that as far as the system is concerned the user is who they claim to be. This is the task that is delegated to Linux-PAM.
From the perspective of the application programmer (in this case the person that wrote the login application), Linux-PAM takes care of this authentication task -- verifying the identity of the user.
The flexibility of Linux-PAM is that you, the system administrator, have the freedom to stipulate which authentication scheme is to be used. You have the freedom to set the scheme for any/all PAM-aware applications on your Linux system. That is, you can authenticate from anything as naive as simple trust (pam_permit) to something as paranoid as a combination of a retinal scan, a voice print and a one-time password! 
Linux-PAM deals with four separate types of (management) task. These are: authentication management; account management; session management; and password management.
Here is a figure that describes the overall organization of Linux-PAM:
  +----------------+
  | application: X |
  +----------------+       /  +----------+     +================+
  | authentication-[---->--\--] Linux-   |--<--| PAM config file|
  |       +        [----<--/--]   PAM    |     |================|
  |[conversation()][--+    \  |          |     | X auth .. a.so |
  +----------------+  |    /  +-n--n-----+     | X auth .. b.so |
  |                |  |       __|  |           |           _____/
  |  service user  |  A      |     |           |____,-----'
  |                |  |      V     A
  +----------------+  +------|-----|---------+ -----+------+
                         +---u-----u----+    |      |      |
                         |   auth....   |--[ a ]--[ b ]--[ c ]
                         +--------------+
                         |   acct....   |--[ b ]--[ d ]
                         +--------------+
                         |   password   |--[ b ]--[ c ]
                         +--------------+
                         |   session    |--[ e ]--[ c ]
                         +--------------+
      If a program is going to use PAM, then it has to have PAM
      functions explicitly coded into the program. If you have
      access to the source code you can add the appropriate PAM
      functions. If you do not have access to the source code, and
      the binary does not have the PAM functions included, then
      it is not possible to use PAM.
    
 More @ http://www.linux-pam.org/Linux-PAM-html/sag-overview.html

In a nutshell, PAM the Pluggable Authentication Modules.
As its name implies it uses a plug-in style architecture. PAM makes it so that each program does not have to implement its own authentication mechanism. Instead it just checks the PAM stack.
Because PAM is pluggable you can configure the authentication stack however you like. Authenticating against LDAP, NIS, RADIUS, MySQL, Oracle, BerkelyDB, SQLite, RSA tokens, x509 certificates, flat files, one time passwords, Google two-factor, phases of the moon, etc. can all be configured in any combination.
PAM can also trigger actions such as mounting filesystems, creating directories, logging, or any other action when the stack is activated, fails or succeeds.
PAM is the authentication Swiss Army Knife, giving infinite flexibility to custom tailor authentication in any way necessary, for any application.
Without it every program would have to independently support all of these features, and that would be a sad world indeed.

Configure Multiple IP Adresses on single interface

#cd /etc/sysconfig/network-scripts
step1 --> #cp  ifcfg-eth0   ifcfgeth0:1

#cat ifcfg-eth0

DEVICE="eth0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.0.100
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
HWADDR=00:0C:29:28:FD:4C

step 2 -->
#vim ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.0.101
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes

To setup up a range of ip address range assigned to n.w interface
#cp  ifcfg-etho ifcfg-etho-range0

                        #vim ifcfg-eth0-range0

#DEVICE="eth0"
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
TYPE="Ethernet"
IPADDR_START=192.168.0.120
IPADDR_END=192.168.0.190
NETMASK=255.255.255.0
IPV6INIT=no
USERCTL=no
CLONENUM_START=22   #{if   eth0:1,eth0:2 ..eth0:21 are already occupied} it will start from eth0:22
#GATEWAY=192.168.0.1


#service network reload
  you can also give different range/n.w address ..see the example below

Poor man's VPN / SSH SOCKS proxy config / Dynamic port forwarding

Scenario :  LAN1 = 192.168.0.0/24
#Machine1 doesnt have access to internet & LAN1

#Machine2 RHEL6 SSH srv has access to internet and LAN1

Task :
browse internet securely through ssh connection/SOCKS proxy/Dynamic port forwarding
--------------------------In server-----------------------
enable packet forwarding/ Masquerading 
<for temporary>
@SRV #echo 1 > proc/sys/net/ipv4/ip_forward

<for permanent>
@SRV #vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

@SRV #iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE

@SRV #service iptables save

 ------------------------In client --machine 1------------------------

 #ssh  -D  9001  user@192.168.0.100      
9001 is machine1 local port (you can put any free/un-used port)


In firefox(chrome or other browsers ) go to settings, network,manual proxy settings --> 
socks host = Localhost
port = 9001  (defined above)
socksv5

see below for working screen shot + configuration is same if you are connecting to internet from unsecure connections ..(your ssh server need to have public ip address) --this method is also known as poor mans VPN








 






Swap part 2 How to create swap

To see what swap space you have, use the command swapon -s. The output will look something like this:

Filename  Type       Size       Used Priority
/dev/sda5 partition  859436  0       -1


Each line lists a separate swap space being used by the system. Here, the 'Type' field indicates that this swap space is a partition rather than a file, and from 'Filename' we see that it is on the disk sda5. The 'Size' is listed in kilobytes, and the 'Used' field tells us how many kilobytes of swap space has been used (in this case none). 'Priority' tells Linux which swap space to use first. One great thing about the Linux swapping subsystem is that if you mount two (or more) swap spaces (preferably on two different devices) with the same priority, Linux will interleave its swapping activity between them, which can greatly increase swapping performance.
To add an extra swap partition to your system, you first need to prepare it. Step one is to ensure that the partition is marked as a swap partition and step two is to make the swap filesystem. To check that the partition is marked for swap, run as root:

 If the partition isn't marked as swap you will need to alter it by running fdisk and using the 't' menu option. Be careful when working with partitions -- you don't want to delete important partitions by mistake or change the id of your system partition to swap by mistake.

Once a partition is marked as swap, you need to prepare it using the mkswap (make swap) command as root:

# mkswap /dev/hdb1
 
If you see no errors, your swap space is ready to use. To activate it immediately, type:

# swapon /dev/hdb1

To verify your config run  

# swapon -s 

To mount swap partition automatically at boot time 

#vim /etc/fstab

/dev/hdb1 none swap sw 0 0
============================================================OR example 

/dev/hdb1 swap swap defaults 0 0


To add a swap file:
  1. Determine the size of the new swap file in megabytes and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.
  2. At a shell prompt as root, type the following command with count being equal to the desired block size:
  3. dd if=/dev/zero of=/swapfile bs=1024 count=<required size in KB>
    dd if=/dev/zero of=/swapfile bs=1024 count=65536 
    ------------------------------------------------------------
    for 1GB swapfile
    dd if=/dev/zero of=/swapfile bs=1M count=1024

  4. Setup the swap file with the command:
    mkswap /swapfile
  5. To enable the swap file immediately but not automatically at boot time:
    swapon /swapfile
  6. To enable it at boot time, edit /etc/fstab to include:

    /swapfile swap swap defaults 0 0

    The next time the system boots, it enables the new swap file.
  7. After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command  
    #cat /proc/swaps 
    or

    #free


 
 
 

Swap part1 Basics of swap

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Need for swap when :
1)  system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately.

2) a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.

Disadvantages of SWAP memory :

Compared to memory, disks are very slow. Memory speeds can be measured in nanoseconds, while disks are measured in milliseconds, so accessing the disk can be tens of thousands times slower than accessing physical memory. The more swapping that occurs, the slower your system will be. Sometimes excessive swapping or thrashing occurs where a page is swapped out and then very soon swapped in and then swapped out again and so on. In such situations the system is struggling to find free memory and keep applications running at the same time. In this case only adding more RAM will help.

Linux has two forms of swap space: 
1) swap partition 
The swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there.

2) swap file.  The swap file is a special file in the filesystem that resides amongst your system and data files. 

How big should my swap space be?

It is possible to run a Linux system without a swap space, and the system will run well if you have a large amount of memory -- but if you run out of physical memory then the system will crash, as it has nothing else it can do, so it is advisable to have a swap space, especially since disk space is relatively cheap.

Recommended System Swap Space (By Redhat Customer Portal)
Amount of RAM in the System Recommended Amount of Swap Space
4GB of RAM or less a minimum of 2GB of swap space
4GB to 16GB of RAM a minimum of 4GB of swap space
16GB to 64GB of RAM a minimum of 8GB of swap space
64GB to 256GB of RAM a minimum of 16GB of swap space
256GB to 512GB of RAM a minimum of 32GB of swap space

The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."
One downside to Morton's idea is that if memory is swapped out too quickly then application response time drops, because when the application's window is clicked the system has to swap the application back into memory, which will make it feel slow.
The default value for swappiness is 60. You can alter it temporarily (until you next reboot) by typing as root:
echo 50 > /proc/sys/vm/swappiness
If you want to alter it permanently then you need to change the vm.swappiness parameter in the /etc/sysctl.conf        file.