postfix part 6 maildir and mailbox

#setenforce 0  (set selinux in permissive mode, i had problems with maildir with selinux on)

Postfix supports 2 types of Mail Box formats
1. MBOX Format
2. MailDir Format
MBOX (/var/spool/mail/$user)
    MBOX is the default storage method used in the Postfix. This is also the traditional Unix format to store the msgs. This appends the mails to a single file in sequential fashion. This file needs to be locked by any application for writing into it. In a high utilized servers there may be issues of locking and performance if you are using the MBOX format. Because only one application at a time is able to read and write the file same time. By default postfix delivers mail to the file object in the spool directory. (For Eg: For user root the  MBOX file is /var/spool/mail/root). Most of the mail retrieving technologies such as imap and pop3 base servers are following this directory structure by default.
Spooling mails in same MBOX format to users home directory.
    This will results the mail delivery in users home directory. There is a MAIL variable in users shell that defines the default location of the mails for the MUA. The following command shows the mail variable.
# echo $MAIL
# set |grep -i mail
Moving the MBOX to users home
# vim  /etc/postfix/main.cf
home_mailbox = Mailbox
# postfix reload
    The default behavior of postfix is to spool the mail to the /var/spool/mail directory. By defining the home_mailbox postfix will delivers the mail to the users home directory. The file named “Mailbox” will be created by the Postfix daemon.
Now change the mail variable for the user(recommended when localy installed MUA such as mutt, mail etc used).
# export MAIL=~/Mailbox
Make it permanent (following shows for bash shell)
# vim /etc/bashrc
export     MAIL=~/Mailbox
Now source the file and check the mail variable
#  .   /etc/bashrc
# echo $MAIL
    Now the MUA will be able to get the mail from exact location.
Maildir
    This is newer Unix standard to route the mail to a directory struchure. Maildir provides the superior scaling as well as “no locking issues”.
Implementing Maildir
# vim /etc/postfix/main.cf
home_mailbox = Maildir/
# postfix reload
     The above process will create a sub-directory in each users home directory called Maildir. Beneath this directory contains the structure that contains the msgs. Maildir is introduced by Qmail and recognized and supported by almost all the MUAs. Test sending a mail to any user in the system and trace the newly created directory inside the home.
# ls ~/Maildir
cur
new
tmp
    These are the three sub directories created by postfix. When a msg is spooled typically copied in to the “tmp” directory. “new” directory contains the unread mails. The mails containing in the directory “new”  has a typical nomenclature for the identification of the msgs.
Eg:- 2214525412.v80osui654.destinedhost.
    In the above file name the initial prefix (2214525412) is the unique identified that corresponds to the time after the epoc time 1970 (command “date +%s” shows the current epoc time). “v80osui654″ is the identified added by postfix and followed by the destination host name of the mailbox.“cur” (current) directory contains the read mails
The MAILDIR variable has to be set and MAIL variable has to be unset
# unset MAIL
# export MAILDIR=~/Maildir
    The variable change need to be specified globally. if we are using any MUAs depends upon this variable, else the mails wont be able to process by MUAs.
Set the variables globally
# vim /etc/bashrc
unset MAIL
export   MAIL=~/Mailbox
 
# .    /etc/bashrc

Mounting windows share on linux

Mount windows share on linux (centos 6.5)

# yum install cifs-utils
[to unmount # umount /mounteddir   OR incase of lock errors #umount -l    (small 'L') ]

//server/share /mnt/mountname cifs username=server_user,password=user_password,iocharset=utf8,file_mode=0777,dir_mode=07‌​77 0 0

Where,

server = your server that you are trying to access
share = mapped share on that server
/mnt/mountname = any folder that you created with sudo /mnt/<folder_name>
username = the name of the user on that server that can access the mount
password = the password for that user

After that you can test with sudo mount -a, if you dont get any faults you can safely reboot to find your shares mounted in /mnt/<folder_name>.

To use a credentials file instead of the username and password parameters on the fstab you can create a file with those 2 lines so that your username and password are not explicitly shown in the fstab

sudo nano /etc/cifspwd

Add these lines to the file

username=<username on server>
password=<password for that username>

Press ctrl+x keys and when asked press y to save the file.

Secure it with

sudo chmod 600 /etc/cifspwd

Use this line instead of the previous

//server/share /mnt/mountname cifs credentials=/etc/cifspwd,iocharset=utf8,file_mode=0777,dir_mode=07‌​77 0 0


myexample
cat /etc/fstab
..
//172.69.2.250/mrhost /backup cifs credentials=/unc.txt,filemode=0660,dirmode=0770,iocharset=utf8,sec=ntlm  0  0

#chmod 660 /unc.txt
#cat /unc.txt
username=user234
password=p@ssw0rd
 

Mysql master slave replication



Mysql server replication(centos 6.4)
steps for mysql db replication
1)
#vim /etc/my.cnf
[mysqld]
log-bin
binlog-ignore-db=mysql            #ignores the databases selected
binlog-ignore-db=whmxfer
binlog-ignore-db=information_schema
binlog-ignore-db=cphulkd
binlog-ignore-db=eximstats
binlog-ignore-db=horde
binlog-ignore-db=leechprotect
binlog-ignore-db=logaholicDB_web01
binlog-ignore-db=performance_schema
binlog-ignore-db=roundcube
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
2)
#service msqld restart
[root@server1 ~]# ls -l /var/lib/mysql/
total 20492
-rw-rw----. 1 mysql mysql 10485760 Jun 11 20:57 ibdata1
-rw-rw----. 1 mysql mysql  5242880 Jun 11 20:57 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 Jun 11 20:01 ib_logfile1
drwx------. 2 mysql mysql     4096 Jun 11 20:01 mysql
-rw-rw----. 1 mysql mysql      106 Jun 11 20:57 mysqld-bin.000001
-rw-rw----. 1 mysql mysql       20 Jun 11 20:57 mysqld-bin.index
srwxrwxrwx. 1 mysql mysql        0 Jun 11 20:57 mysql.sock
##the index file tells which log file mysql is using and the position  where it left off                   .         

[root@server1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.66-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysqld-bin.000001 |         106   |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
note: the file and position info highlighted green above is very important to be supplied to slave mysql server
3)Creating Replication user
mysql>  GRANT REPLICATION SLAVE ON *.* TO 'replica'@'192.168.213.11' IDENTIFIED BY 'p@ssw0rd';
Query OK, 0 rows affected (0.01 sec) #192.168.213.11 is salve server IP # ip canbe = '%' for all IP
mysql> GRANT SELECT,SUPER,RELOAD ON *.* TO 'replica'@'192.168.213.10' IDENTIFIED by 'p@ssw0rd';
Query OK, 0 rows affected (0.01 sec)
mysql > show grants for replica;   ##to view privileges for user replica


Flush all tables and block writes to restrict changes during replication configuration:
mysql> flush tables with read lock;

To make sure that replica user can access master .. login to slave server
[root@mail ~]# mysql -h 192.168.213.10 -u replica -p'p@ssw0rd'
you get a MySQL prompt? If so, you're successfully logged into your Master server using the new 'repl' user you just created. Alright, we're about half way there.
Now at Master server in mysql command<This is optional> :
mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE my_db;
Query OK, 1 row affected (0.00 sec)

mysql> use my_db;
Database changed
mysql> CREATE TABLE Persons
    -> (
    -> PersonID int,
    -> LastName varchar(255),
    -> FirstName varchar(255),
    -> Address varchar(255),
    -> City varchar(255)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-----------------+
| Tables_in_my_db |
+-----------------+
| Persons         |
+-----------------+
1 row in set (0.00 sec)

mysql> insert into Persons values ('1','johnson','jack','ktm','bagmati');
Query OK, 1 row affected (0.01 sec)

mysql> insert into Persons values ('2','daniel','mohan','pokhara','Testing');
Query OK, 1 row affected (0.00 sec)

mysql>flush tables with read lock;
[root@server1 mysql]# pwd
/var/lib/mysql
[root@server1 mysql]# tar -cjvf my_db.bz2 my_db
my_db/
my_db/Persons.frm
my_db/Persons.MYI
my_db/db.opt
my_db/Persons.MYD
[root@server1 mysql]# scp my_db.bz2 192.168.213.11:/var/lib/mysql/
root@192.168.213.11's password: **********
my_db.bz2                                                                                                       100%


Now at slave
[root@mail ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
[root@mail ~]# cd /var/lib/mysql/
[root@mail mysql]# tar xjvf my_db.bz2
my_db/
my_db/Persons.frm
my_db/Persons.MYI
my_db/db.opt
my_db/Persons.MYD
[root@mail mysql]# ll
total 8
drwx------. 2 mysql mysql 4096 Jun 11 21:57 my_db
-rw-r--r--. 1 root  root   713 Jun 11 22:09 my_db.bz2

note: you can remove bz2 file

Now at Master mysql
mysql> unlock tables;
configure server ID for both MASTER and Slave  servers
vim /etc/my.cnf                   #(at master mysql server)
[mysqld]
log-bin
server-id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
                      #master server-id=1 (can be up to 2^32 -1)
#service mysqld restart

At slave server
#vim /etc/my.cnf
[mysqld]
server-id=100
replicate-ignore-db=mysql            #slave ignores DB selected
replicate-ignore-db=information_schema
replicate-ignore-db=cphulkd
replicate-ignore-db=eximstats
replicate-ignore-db=horde
replicate-ignore-db=leechprotect
replicate-ignore-db=logaholicDB_web01
replicate-ignore-db=performance_schema
replicate-ignore-db=roundcube
replicate-ignore-db=whmxfer

 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
#service msqld start
mysql> change master to master_host='192.168.213.10',master_user='replica',master_password='p@ssw0rd',master_log_file='mysqld-bin.000002',master_log_pos=106;
the master_host='canbe FQDN '
mysql>start slave;
mysql> show slave status; OR
 mysql> SHOW SLAVE STATUS\G;          

mysql> show processlist;
[root@mail mysql]# ls -ltrh /var/lib/mysql/
total 21M
drwx------. 2 mysql mysql 4.0K Jun 11 21:57 my_db
-rw-r--r--. 1 mysql mysql  713 Jun 11 22:09 my_db.bz2
drwx------. 2 mysql mysql 4.0K Jun 11 22:27 mysql
-rw-rw----. 1 mysql mysql 5.0M Jun 11 22:27 ib_logfile1
-rw-rw----. 1 mysql mysql  10M Jun 11 22:28 ibdata1
-rw-rw----. 1 mysql mysql 5.0M Jun 11 22:28 ib_logfile0
srwxrwxrwx. 1 mysql mysql    0 Jun 11 22:28 mysql.sock
-rw-rw----. 1 mysql mysql  156 Jun 11 22:35 mysqld-relay-bin.000001
-rw-rw----. 1 mysql mysql   52 Jun 11 22:35 mysqld-relay-bin.index
-rw-rw----. 1 mysql mysql  252 Jun 11 22:35 mysqld-relay-bin.000002
-rw-rw----. 1 mysql mysql   74 Jun 11 22:35 master.info
-rw-rw----. 1 mysql mysql   52 Jun 11 22:35 relay-log.info



postfix part 5 Virtual domains

By default postfix has setup to handle few domains, defined by $mydestination. The idea of the virtual domain is to  map the multiple domains to the same server. ” hostname -f ” shows the FQDN also known as canonical domain used by Postfix.
Note: The IP address also considers as domain. For eg:- A message To: user@[10.0.0.1] (The “[]” is must). So in this case the IP address is also considered as the domain. This domain is also considered as a part of the canonical domain.

1) Basic Virtual Domain Configuration



# vim /etc/postfix/main.cf
mydestination = $myhostname, localhost, $mydomain, anewdomain.com, someotherdomain.com
relay_domains = $mydestination
# postfix reload
    Now the messages that destined to the domains listed in the $mydestination will be handled by the server. So messages send to a user at domain that defined at $mydestination will be delivered locally.
For Eg:- Mail send to user123@anewdomain.com and user123@someotherdomain.com will get deliver to the same user in the host. In other words the list of domains that defined in the $mydestination will be considered to be local and delivers the mail locally.

2) Virtual Domains Using Maps For Single Domain

    This scenario is used ideally in a Linux mail server where the local users need to share the different domains, (used in ISP environment).
Splitting Local users in to separate domains
    To do so we have to setup the virtual aliases maps
# vim /etc/postfix/main.cf
virtual_alias_domains = example.com
virtual_alias_maps = hash:/etc/postfix/virtual
     virtual_alias_domains tells what domains are needed to be supported by virtaul alias maps
# vim /etc/postfix/virtual
##LHS (email addr)      --->   RHS(Local and/or remote address)   
apple@himali.external          apple
orange@himali.external       orange

##any thing else destined to ...@himali.external will be sent to internal/external-user on the RHS

@himali.external            catchit

# postmap /etc/postfix/virtual
   The format is as same as in the transport table. In the Left Hand Side we mention the address that need to be mapped and in Right Hand Side we mention the local or remote user mail address to which mail has to be delivered.
# postfix reload
Reload the postfix service

# postconf  | grep virtual_alias_
    Now test the setting by composing the msg to the user userdd@example.com and dduser@example.com. The msg will be delivered to the local users apple and orange respectively.
3) Virtual Domains Using catch all features.                  
    In a virtual alias map environment if mail is send to a non-existing Local user in a postfix server the mail will be rejected with the error “recipient address rejected” in log file. This situation could be overcome by defining the catchall address for the domain. But these feature will be subjected to catch all the mails that coming to the domain and obviously the server will be filled up with spam mails. So it is not at all considered to be used at production environment.
Defining the catch all
# vim /etc/postfix/main.cf
##LHS (email addr)    --->   RHS(Local and/or remote address)   
apple@himali.external        apple
testing99@himali.external       orange

##any thing else destined to ...@himali.external will be sent to internal/external-user on the RHS

@himali.external            catchit

# postmap /etc/postfix/virtual
   now email destined to apple@himali.external will be routed to apple  (local user)
and email destined to testing99@himali.external  will  be routed to orange(local user)

The following virtual map file will send the all mails coming to the domain example.com to multiple recipients.
# vim /etc/postfix/virtual
@example.com    catchit,user1, user2, user3
The following virtual map will send the mails coming to user don@example.com to remote domain nepal@secureserver.com.
# vim /etc/postfix/virtual
DON@example.com       nepal@secureserver.com
   
4) Virtual alias Maps For Multiple Domains              
    The following example shows the configuration of the multiple domains
# vim  /etc/postfix/main.cf
virtual_alias_domains = firstdomain.com, seconddomain.net, thirddomain.org, fourthdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual


# vim /etc/postfix/virtual
sales@firstdomain.com             satriani
hr@seconddomain.net              joe
finance@thirddomain.org       david
project@fourthdomain.com      paul
abuse@seconddomain.net        abuse


# postmap /etc/postfix/virtual
    This finishes the configuration of the Virtual alias maps

#postmap -q abuse@seconddomain.net /etc/postfix/virtual
    The above command will query the virtual map file for the mapped address.
example:
postmap -q  user45@samsung.ext   virtual
user45                                                   #result
postmap -q  nep123@sky.net virtual
somebody@google.com                        #result

# postfix reload
    Test the settings by sending mails to each and every users in the new domains.
While testing this configuration make sure that the proper DNS entry in place.

                                                    SOME LOGS Generated at my test enviroment                                    
LOG MESSAGE @SENDING SERVER-->nepal.external
sender user = hellboy     destination = nonexistent user (which is redirected to hellboy itself for test purpose)
Jun 10 23:38:20 mailvm postfix/pickup[1582]: 8F5F9FF345: uid=500 from=<hellboy>
Jun 10 23:38:20 mailvm postfix/cleanup[1636]: 8F5F9FF345: message-id=<20140610175320.GA1631@mailvm.nepal.external>
Jun 10 23:38:20 mailvm postfix/qmgr[1583]: 8F5F9FF345: from=<hellboy@nepal.external>, size=472, nrcpt=1 (queue active)
Jun 10 23:38:20 mailvm postfix/smtp[1638]: 8F5F9FF345: to=<nonexistuser@himali.external>, relay=mailvm.himali.external[192.168.140.200]:25, delay=0.1, delays=0.05/0.01/0.02/0.02, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 910FAFF644)
Jun 10 23:38:20 mailvm postfix/qmgr[1583]: 8F5F9FF345: removed
Jun 10 23:38:20 mailvm postfix/smtpd[1639]: connect from mailvm.urgens.com[192.168.140.200]
Jun 10 23:38:20 mailvm postfix/smtpd[1639]: A3682FF328: client=mailvm.urgens.com[192.168.140.200]
Jun 10 23:38:20 mailvm postfix/cleanup[1636]: A3682FF328: message-id=<20140610175320.GA1631@mailvm.nepal.external>
Jun 10 23:38:20 mailvm postfix/qmgr[1583]: A3682FF328: from=<hellboy@nepal.external>, size=846, nrcpt=2 (queue active)
Jun 10 23:38:20 mailvm postfix/smtpd[1639]: disconnect from mailvm.urgens.com[192.168.140.200]
Jun 10 23:38:20 mailvm postfix/local[1641]: A3682FF328: to=<hellboy@nepal.external>, relay=local, delay=0.01, delays=0/0.01/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Jun 10 23:38:20 mailvm postfix/local[1641]: A3682FF328: to=<root@nepal.external>, relay=local, delay=0.02, delays=0/0.01/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Jun 10 23:38:20 mailvm postfix/qmgr[1583]: A3682FF328: removed

LOG @RECEIVING SERVER-->urgens.internal
  Jun 10 17:59:37 mailvm postfix/smtpd[2119]: connect from unknown[192.168.140.20]
Jun 10 17:59:37 mailvm postfix/smtpd[2119]: 2D277FF644: client=unknown[192.168.140.20]
Jun 10 17:59:37 mailvm postfix/cleanup[2122]: 2D277FF644: message-id=<20140610180013.GB1631@mailvm.nepal.external>
Jun 10 17:59:37 mailvm postfix/qmgr[2104]: 2D277FF644: from=<hellboy@nepal.external>, size=636, nrcpt=2 (queue active)
Jun 10 17:59:37 mailvm postfix/smtpd[2119]: disconnect from unknown[192.168.140.20]
Jun 10 17:59:37 mailvm postfix/smtp[2123]: 2D277FF644: to=<hellboy@nepal.external>, orig_to=<noexistuser@himali.external>, relay=mailvm.nepal.external[192.168.140.20]:25, delay=0.04, delays=0.01/0.01/0.02/0.01, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 3F8E9FF328)
Jun 10 17:59:37 mailvm postfix/smtp[2123]: 2D277FF644: to=<root@nepal.external>, orig_to=<noexistuser@himali.external>, relay=mailvm.nepal.external[192.168.140.20]:25, delay=0.04, delays=0.01/0.01/0.02/0.01, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 3F8E9FF328)
Jun 10 17:59:37 mailvm postfix/qmgr[2104]: 2D277FF644: removed

Postfix part 3 Transport tables

    This feature allows to route messages to additional domains according to the Map defined. Here Postfix accepts the message and rather than consulting the DNS for MX record it routes the mail by checking the transport map. In sendmail this feature is called mailertable

Transport Map
The map format is as follows
Left Handside                    Right Hand Side
user@domain.com                 transport:nexthop  
domain.com                       transport:nexthop
host1.domain.com                 transport:nexthop
   In the above format the Right Hand side defines the list of the domains that Postfix accepts the messages and routes. In Left Hand Side defines the transport mechanism that used and the destination of the mails.
Setting up the Transport Mechanism to Route the messages:
Check the derivative that supports transport map
# postconf   |  grep transport_maps
    transport_maps is the derivative that enables the transport map. By default the derivative will be undefined. The types of transport that supported by the Postfix can be determined by examining the “/etc/postfix/master.cf” file. smtp, local, error etc are the types of the transport defined.
Create the transport map file
# vim /etc/postfix/transport
sales.domain.com           smtp:[internalhost1.domain.com]
tax.domain.com              smtp:[internalhost2.domain.com]
finance.domain.com       smtp:newdomain.com
    The Left hand side we have mentioned the “mails coming from domains” which has to be routed (sales.domain.com) and in the Right hand side we have mentioned the transported which has to be use (smtp) and the destination machine (internalhost1.domain.com).
Note: “While using the transport table, when mails subjected to route, to disable the MX lookup by postfix for the destination domains we have to add the recieving domains inside the square bracket (“[]“). This will tell Postfix to “not perform” the MX lookup of the destined domains.
# postmap /etc/postfix/transport
   Now we have generated the transport map file named transport.db
Enable the transport map feature in the postfix
# vim  /etc/postfix/main.cf
transport_maps = hash:/etc/postfix/transport
# postfix reload
    Now we have integrated the transport map with the postfix.
Testing the configuration
# postconf |grep transport_maps
    Check the maps are defined correctly.
Make sure that the MX record for all the domains that needs to route the mail to another server points to the Mail Routing Server. Now try sending mails destined to domains sales.domain.com, tax.domain.com, finance.domain.com. According to the map defined the Mail Routing server will route the mails to respective host. It is understood that the receiving host has configured to accept the mails from the Mail Router.

Example log ()sending server{where transport_maps is defined}
=========

Jun 10 20:26:03 server1 postfix/pickup[1780]: 227ED9F8EA: uid=0 from=<root>
Jun 10 20:26:03 server1 postfix/cleanup[1798]: 227ED9F8EA: message-id=<20140610144103.GA1793@server1.laptop.internal>
Jun 10 20:26:03 server1 postfix/qmgr[1781]: 227ED9F8EA: from=<root@laptop.internal>, size=476, nrcpt=1 (queue active)
Jun 10 20:26:03 server1 postfix/smtp[1791]: 227ED9F8EA: to=<root@spider.cbt>, relay=192.168.213.12[192.168.213.12]:25, delay=0.12, delays=0.07/0/0.01/0.04, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as EA65520091)
Jun 10 20:26:03 server1 postfix/qmgr[1781]: 227ED9F8EA: removed
example log (AT the receiving end)
= = = = = = = = =
Jun 10 20:25:49 mail postfix/smtpd[1380]: connect from server1.laptop.internal[192.168.213.10]
Jun 10 20:25:49 mail postfix/smtpd[1380]: EA65520091: client=server1.laptop.internal[192.168.213.10]
Jun 10 20:25:49 mail postfix/cleanup[1383]: EA65520091: message-id=<20140610144103.GA1793@server1.laptop.internal>
Jun 10 20:25:49 mail postfix/qmgr[1191]: EA65520091: from=<root@laptop.internal>, size=680, nrcpt=1 (queue active)
Jun 10 20:25:49 mail postfix/smtpd[1380]: disconnect from server1.laptop.internal[192.168.213.10]
Jun 10 20:25:49 mail postfix/local[1384]: EA65520091: to=<root@spider.cbt>, relay=local, delay=0.04, delays=0.04/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Jun 10 20:25:49 mail postfix/qmgr[1191]: EA65520091: removed

Postfix part 4 SMART HOST and NULL CLIENT





The main.cf in remaining server should contain  valid myhostname, mydomain values.+ iptables should allow port 25


========================================================================
Defining SmartHost
Initially check that what domains the postfix will accept the mails for.
# postconf | grep mydestination
    The output shows the list of the domains that postfix accepts mails for. After defining the SmartHost postfix will forward all the mails to the SmartHost server which are not destined to $mydestination.
# vim /etc/postfix/main.cf
relayhost = smarthost.server.com
    In this scenario postfix forwards all the mails that are not destined local to smarthost.server.com.
Note: In this case the postfix will perform DNS MX record query to the domain smarthost.server.com. Adding square brackets “[]” will skip the MX record query and delivers the msgs directly.
eg: relay_host = [mail1.smarthost.server.com]
eg, for different ports
relayhost = 5.9.240.232:2525 
# postconf |grep relayhost
# postfix reload
Verify the configuration and reload the postfix service
    Test the configuration by sending mails to outside using this Local Server.Examine the Logs in the Local server as well as in the SmartHost server.

Routing all mails for a specific domain to a smarthost :

Note: The information below relates to a very specific customer requirement and is not recommended for most deployments. A configuration like this can cause problems which will be hard to troubleshoot. Unless you are sure you need to use this setup, please explore the other available options for routing mail.

Add a line to /etc/postfix/transport:

example.com smtp:mxsrv1.spamgateway.comodo.com:587



generate a postmap file :

postmap hash:/etc/postfix/transport

To use the transport file, add or edit a line in /etc/postfix/main.cf:

transport_maps = hash:/etc/postfix/transport


Restart Postfix and all mail. The mail for selected domains should go trough the Smarthost.

setting up Fail2ban on centos 6.x/RHEL 6.x/fedora

Machine used = centos 6.5 x64bit



Fail2ban setup Centos 6.x/RHEL 6.x    (centos 6.5)
# yum install fail2ban
Copy /etc/fail2ban/jail.conf to jail.local
# vi /etc/fail2ban/jail.conf
Now, you will see default section with some basic rules that are followed by fail2ban itself. If you want to add some extra layer of protection to your server, then you can customize the each rule section as per your needs.
[DEFAULT]
 
# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1
 
# "bantime" is the number of seconds that a host is banned.
bantime = 600
 
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 600
 
# "maxretry" is the number of failures before a host get banned.
maxretry = 3


Let me describe each rule section with their description and what purpose we use these rules.
  1. ignoreip : IgnoreIP section allows you to white list certain IP addresses from blocking. Here, you can specify list of IP addresses with space separated and make sure you include your address.
  2. bantime : The number of seconds that a host would be banned from the server. The default is set for 600 (600 seconds = 10 minutes), you may increase this to an hour or higher if you like.
  3. findtime : The amount of time that a host has to log in. The default is set to 10 minutes, it means that if a host attempts, and fails, to log in more than the maxretry number of times, they will be banned.
  4. maxretry : The number of failed login attempts before a host is blocked for the length of the ban time.
Configuring ssh-iptables section for Fail2Ban
The following section is the default ssh-iptables section and it is turned on by default. So, you don’t need to make any changes to this section,
[ssh-iptables]

enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com]
logpath  = /var/log/secure
maxretry = 5
You can find the details of each rule described below.
  1. enabled : This section refers that SSH protection is on. You can turn it off by changing the word “true” to “false“.
  2. filter : This section by default set to sshd and refers the config file (/etc/fail2ban/filter.d/sshd.conf) containing the rules that fail2ban uses to find matches.
  3. action : This action tells the fail2ban to ban a matching IP address once a filter matches in the /etc/fail2ban/action.d/iptables.conf file. If your server have mail setup, you can add email address, where fail2ban sends you a email alerts whenever it bans an IP address. The sender section refers to file /etc/fail2ban/action.d/sendmail-whois.conf file.
  4. logpath : The log path is the location of logs where fail2ban will track.
  5. maxretry : The max retry section is the same definition as the default option that we discussed above.
Restarting Fail2Ban Service
Once you’ve made the changes to the fail2ban config file, then always make sure to restart Fail2Ban service.
# chkconfig --level 23 fail2ban on
# service fail2ban start
Starting fail2ban:                                         [  OK  ]
Verifying Fail2Ban iptables rules
Check the rules that fail2ban added in effect within the IP table section.
# iptables -L
I have made some failed login attempts from one of our server to the server where fail2ban installed and it works. You see the banned IP address of my server.

Watch Failed SSH login attempts

To see the current ssh failed login attempts, run the following command it will display a list of failed attempts attempted by hosts.
# cat /var/log/secure | grep 'Failed password' |  sort | uniq -c

Remove IP Address from Fail2Ban

To remove the banned IP address from the fail2ban iptable rules. Run the following command.
# iptables -D fail2ban-ssh 1