Showing posts with label network. Show all posts
Showing posts with label network. Show all posts

Friday, June 1, 2012

Debian APC UPS client-server monitoring

Apcupsd is a UPS control system that permits orderly shutdown of your computer in the event of a power failure. We will take a look at NIS (Network Information Server) server and client configuration (this is the case when a single UPS powers several computers).

Server

NIS (Network Information Server) mode allows communication between different hosts. Only one of those hosts, the server, needs to talk to the UPS directly.
  1. Ensure device is connected and recognized. In most cases your UPS is connected to server via USB cable. In Linux you can check this by listing USB devices (provided by usbhid driver):
    deby1:~# ls /dev/usb/
    hiddev0
    
    If your device is no connected, most likely, you will get a message like this:
    ls: cannot access /dev/usb/: No such file or
    directory
    
    Note, in our case the UPS device is available at /dev/usb/hiddev0.
  2. Install UPS monitoring software. Apcupsd is a software designed to control APC UPS devices, let get it installed:
    apt-get install apcupsd
    
  3. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE usb
    
    UPSTYPE  usb
    DEVICE   /dev/usb/hiddev0
    
    NISIP    0.0.0.0
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  4. Start apcupsd service:
    /etc/init.d/apcupsd start
    
    Check UPS status:
    apcaccess status <server name>
    
    Take a look at any errors reported (file /var/log/apcupsd.events):
    ...  apcupsd 3.14.10 (...) debian startup succeeded
    

Client

The client computer will communicate with server via network.
  1. Install UPS monitoring software.
    apt-get install apcupsd
    
  2. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE ether
    
    UPSTYPE net
    #DEVICE  hostname:port
    DEVICE  deby1.dev.local:3551
    
    NETSERVER off
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  3. Start apcupsd service:
    /etc/init.d/apcupsd start
    

Notifications

You are able receive a number of notification events, e.g. power failure, etc (see a complete list of events here). By default apcupsd calls script located at /etc/apcupsd/apccontrol. This script echo some events to user console, as well as shuts down host per doshutdown event. You can easily extend this script to email you events. Here is the script (file /usr/local/sbin/notify.sh):
#!/bin/sh

domain=`hostname -d`
mail=root@$domain
msg=Test

if [ ! -z "$2" ]; then
    mail=$1; msg=$2
    if ! echo $mail | grep -q "$domain"; then
        mail=$mail@$domain
    fi
else
    if [ ! -z "$1" ]; then msg=$1; fi
fi

# strip whitespace at the end of message
msg=`echo "$msg" | sed 's/ *$//g'`

echo $msg | mail -s "$msg" $mail
echo $msg | wall
Ensure the following in apc event handler (file /etc/apcupsd/apccontrol):
#WALL=wall
WALL="xargs -0 notify.sh ups@dev.local"
This will email all events handled by apccontrol to ups@dev.local.

Saturday, May 7, 2011

Debian Link Aggregation (Network Bonding)

Here we are going setup link aggregation for Debian. Your system must have at least 2 network interfaces (but not limited to).

Bonding

  1. Install ifenslave-2.6 package:
    apt-get install ifenslave-2.6
    
  2. Load bonding kernel module:
    modprobe bonding
    
  3. Note that you shouldn't have any configuration for eth0 and eth1, they are attached as slaves for bond0 interface. Network configuration (file /etc/network/interfaces):
    auto bond0
    iface bond0 inet static
         address 192.168.10.14
         netmask 255.255.255.0
         network 192.168.10.0
         broadcast 192.168.10.255
         gateway 192.168.10.1
    
         slaves eth0 eth1
    
         # Transmit packets in sequential order 
         # from the first available slave through 
         # the last. This mode provides load 
         # balancing and fault tolerance.
         bond-mode balance-rr
    
         # Only one slave in the bond is active. 
         # A different slave becomes active if, 
         # and only if, the active slave fails. 
         #This mode provides fault tolerance.
         #bond-mode active-backup
    
         bond-miimon 100
         bond-downdelay 200
         bond-updelay 200
    
  4. Once above is complete you need to reboot your server so the network changes take place (that should be faster than bringing up/down network interfaces and restarting networking service).

Bridge

You can use link aggregation with network bridge, for example if you are using lxc / kvm virtualization. Here is network configuration (file /etc/network/interfaces):
auto bond0
iface bond0 inet manual
     slaves eth0 eth1
     bond-mode balance-rr
     bond-miimon 100
     bond-downdelay 200
     bond-updelay 200

auto br0
iface br0 inet static
     address 192.168.10.14
     netmask 255.255.255.0
     network 192.168.10.0
     broadcast 192.168.10.255
     gateway 192.168.10.1
     bridge_ports bond0
     bridge_fd 0
     bridge_maxwait 0
     bridge_stp off

Network Switch

If your switch supports port trunking you should add the switch ports used by eth0 and eth1 to a single trunk.

Thursday, April 14, 2011

Debian KVM

Kernel-based Virtual Machine (KVM) is a virtual machine implementation using the operating system's kernel (read more here). Here are few steps to install kvm in debian:

Server

  1. Setup SSH. Read more here.
  2. Setup bridge-utils package...
    apt-get install bridge-utils
    
    ... and configure network interface (restart computer so network changes take place):
    auto eth0
    iface eth0 inet manual
    
    auto br0
    iface br0 inet static
         address 192.168.10.11
         netmask 255.255.255.0
         network 192.168.10.0
         broadcast 192.168.10.255
         gateway 192.168.10.1
         bridge_ports eth0
         bridge_stp off
         # 1.
         bridge_fd 0
         bridge_maxwait 0
         # 2.
         #bridge_fd 9
         #bridge_hello 2
         #bridge_maxage 12
    
  3. Install qemu-kvm and libvirt-bin packages:
    apt-get -y install qemu-kvm libvirt-bin
    
  4. Add a user that will be managing kvm to group libvirt (e.g. user1):
    adduser user1 libvirt
    

Client

  1. Setup Password-less ssh login to kvm server. Read more here.
  2. Install virt-manager package:
    apt-get -y install virt-manager
    
  3. If your client is not going to host kvm virtual machines you can disable the following daemons:
    update-rc.d ebtables disable
    update-rc.d libvirt-bin disable
    update-rc.d libvirt-guests disable
    update-rc.d lvm2 disable
    
  4. Open Virtual Machine Manager from Applications > System Tools.
  5. In File menu select Add Connection. In dialog that appears ensure method ssh and user that you added on server to group libvirt).

Performance Tuning

  1. The KVM host can take benefit of KSM by finding and sharing memory blocks between vitual machines (add the following to /etc/rc.local).
    echo 100 > /sys/kernel/mm/ksm/sleep_millisecs
    echo 1 > /sys/kernel/mm/ksm/run
    
    You can take a look at pages sharing / shared:
    cat /sys/kernel/mm/ksm/pages_sharing
    cat /sys/kernel/mm/ksm/pages_shared
    
    Another useful thing is to use vhost-net kernel module to boost virtual machine network performance (ensure guest vm uses virtio network device).
    echo vhost-net >> /etc/modules
    
  2. The KVM linux guest IO performance can be improved by:
    • using virtio as disk bus
    • setting virtual disk performance options to: cache mode - none, IO mode - native
    • using noop IO scheduler for each guest (file /etc/default/grub):
    GRUB_CMDLINE_LINUX_DEFAULT="quiet elevator=noop"
    
    Update grub by issuing update-grub command.

Monday, April 4, 2011

How to chroot ntp in Debian

Here are few simple steps to chroot ntp in debian. Add the following to file /usr/local/sbin/chroot-ntp and execute (alternatively you can download script from bitbucket site here):
#!/bin/bash

/etc/init.d/ntp stop

rootfs=/var/chroot/ntp
mkdir -p $rootfs/{etc,var/lib/ntp,var/log}

mv /etc/ntp.conf $rootfs/etc
ln -s $rootfs/etc/ntp.conf /etc/ntp.conf

if [ -e /var/lib/ntp/ntp.drift ]; then
    mv /var/lib/ntp/ntp.drift $rootfs/var/lib/ntp
fi
ln -s $rootfs/var/lib/ntp/ntp.drift \
    /var/lib/ntp/ntp.drift
chown -R ntp:ntp $rootfs/var/lib/ntp

mv /var/log/ntpstats $rootfs/var/log
ln -s $rootfs/var/log/ntpstats /var/log/ntpstats
chown -R ntp:ntp $rootfs/var/log/ntpstats

sed -e "s,'-g','-4 -i /var/chroot/ntp -g'," \
    /etc/default/ntp > /tmp/x && \
    mv /tmp/x /etc/default/ntp

sed -e "s,restrict -6,#restrict -6," \
    -e "s,restrict ::1,#restrict ::1," \
    /etc/ntp.conf > /tmp/x && \
    mv /tmp/x /etc/ntp.conf

/etc/init.d/ntp start
Verify that ntp uses the chroot (file /etc/default/ntp):
NTPD_OPTS='-4 -i /var/chroot/ntp -g'

Debian NTP Server

Let start by installing few packages:
apt-get -y install ntp ntpdate
The options passed to ntp daemon are set in /etc/default/ntp file. We are interested to turn off ipv6 for now:
NTPD_OPTS='-4 -g'
As well we are going restrict ntp daemon for use ipv4 only (file /etc/ntp.conf):
# By default, exchange time with everybody, but don't 
# allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
#restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more 
# closely.
restrict 127.0.0.1
#restrict ::1
Restart ntp daemon so the changes take place:
/etc/init.d/ntp restart
Look which servers it uses for synchronization:
ntpq -4p

Client

Install ntpdate package:
apt-get -y install ntpdate
You can sync the client with you ntp server by issuing the following command (I assume your local htp server resolves to ntp.dev.local):
ntpdate -p 2 ntp.dev.local
Consider have a look at the following post (you just need to substitute the ntp server name with yours).

Friday, December 24, 2010

Configuring OpenLDAP with SSL/TLS on Debian

It is recommended that communication between clients and ldap server be encrypted. Before we enable encryption for ldap server we need SSL private key and certificate signed by certificate authority. Have a look at OpenSSL Certificates. Suppose here are your files: ldap.dev.local-key.pem and ldap.dev.local-cert.pem.

Server

  1. Install CA certificate:
    cp ~/ca/demoCA/cacert.pem /etc/ssl/certs/
    chmod go+r /etc/ssl/certs/cacert.pem
    
  2. Copy ldap key and certificate files to /etc/ldap/ssl
    mkdir /etc/ldap/ssl/
    cp ~/ca/ldap.dev.local-*.pem /etc/ldap/ssl/
    
  3. Secure certificates:
    ldap1:~# chown -R root:openldap /etc/ldap/ssl
    ldap1:~# chmod -R o-rwx /etc/ldap/ssl
    
  4. Enable ldaps protocol (file /etc/default/slapd)
    LAPD_SERVICES="ldap://127.0.0.1:389/ ldaps:/// ldapi:///"
    
  5. Create tls configuration file (tls-config.ldif):
    dn: cn=config
    add: olcTLSCACertificateFile
    olcTLSCACertificateFile: /etc/ssl/certs/cacert.pem
    -
    add: olcTLSCertificateFile
    olcTLSCertificateFile: /etc/ldap/ssl/ldap.dev.local-cert.pem
    -
    add: olcTLSCertificateKeyFile
    olcTLSCertificateKeyFile: /etc/ldap/ssl/ldap.dev.local-key.pem
    
  6. Apply it:
    ldapmodify -QY EXTERNAL -H ldapi:/// -f tls-config.ldif
    
  7. Restart slapd:
    /etc/init.d/slapd restart
    
  8. Ensure started:
    netstat -tunlp | grep slapd
    tcp        0      0 0.0.0.0:636             0.0.0.0:*               LISTEN      2462/slapd      
    tcp        0      0 127.0.0.1:389           0.0.0.0:*               LISTEN      2462/slapd  
    

Client

  1. Install ldap-utils package:
    apt-get install ldap-utils
    
  2. Configure (file /etc/ldap/ldap.conf)
    BASE    dc=dev,dc=local
    URI     ldaps://ldap.dev.local
    
    TLS_CACERT /etc/ssl/certs/cacert.pem
    TLS_REQCERT demand
    
  3. Ensure working:
    ldapsearch -x
    
  4. Have a look at server log file, the communication must go through port 636 now
    ldap1 slapd[2462]: conn=1005 fd=15 ACCEPT from IP=192.168.10.8:38344 (IP=0.0.0.0:636)
    ldap1 slapd[2462]: conn=1005 fd=15 TLS established tls_ssf=128 ssf=128
    ldap1 slapd[2462]: conn=1005 op=0 BIND dn="" method=128
    ldap1 slapd[2462]: conn=1005 op=0 RESULT tag=97 err=0 text=
    ldap1 slapd[2462]: conn=1005 op=1 SRCH base="dc=dev,dc=local" scope=2 deref=0 filter="(objectClass=*)"
    ldap1 slapd[2462]: conn=1005 op=1 SEARCH RESULT tag=101 err=0 nentries=6 text=
    ldap1 slapd[2462]: conn=1005 op=2 UNBIND
    ldap1 slapd[2462]: conn=1005 fd=15 closed
    

Thursday, December 23, 2010

Debian OpenLDAP

OpenLDAP is a free, open source implementation of the Lightweight Directory Access Protocol (LDAP).

Install OpenLDAP Server

  1. Ensure the host name is FQDN:
    ldap1:~# hostname 
    ldap1.dev.local
    
    If it is not, issue the following:
    echo "ldap1.dev.local" > /etc/hostname
    hostname -F /etc/hostname
    
  2. Install necessary packages (during a package configuration phase set admin password and accept all default options):
    apt-get -y install rsyslog slapd ldap-utils
    
  3. Setup system-wide defaults for LDAP clients (file /etc/ldap/ldap.conf):
    BASE    dc=dev,dc=local
    URI     ldap://ldap1.dev.local
    
  4. Disable ipv6 support for slapd (file /etc/default/slapd):
    # Additional options to pass to slapd
    SLAPD_OPTIONS="-4"
    
    Restart slapd:
    /etc/init.d/slapd restart
    netstat -tunlp | grep slapd
    
    Output:
    tcp        0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      1557/slapd
    

Logging

  1. Create a file that enable ldap logging (file log-stats.ldif):
    # Enable LDAP logging
    dn: cn=config
    changetype: modify
    replace: olcLogLevel
    olcLogLevel: stats
    
  2. ... disable ldap logging (file log-none.ldif):
    # Disable LDAP logging
    dn: cn=config
    changetype: modify
    replace: olcLogLevel
    olcLogLevel: none
    
  3. And here is a command (changes are applied immediately, no need to restart slapd):
    ldapmodify -QY EXTERNAL -H ldapi:/// -f log-stats.ldif
    

What to index

  1. Create indexes to match the actual filter terms used in search queries. Read more here. We are going to add the following indexes: uid, cn. So here is our index file (file db-index.ldif):
    dn: olcDatabase={1}hdb,cn=config
    changetype: modify
    add: olcDbIndex
    olcDbIndex: uid eq
    -
    add: olcDbIndex
    olcDbIndex: cn eq
    -
    add: olcDbIndex
    olcDbIndex: ou eq
    -
    add: olcDbIndex
    olcDbIndex: dc eq
    -
    add: olcDbIndex
    olcDbIndex: uniqueMember eq
    -
    add: olcDbIndex
    olcDbIndex: uidNumber eq
    -
    add: olcDbIndex
    olcDbIndex: gidNumber eq
    
    Apply changes:
    ldapmodify -QY EXTERNAL -H ldapi:/// -f db-index.ldif
    

Reindex database

  1. Here is a simple script to reindex database (file /usr/local/sbin/slap-reindex). You do not need to run it often, that is depends how big is your database and how many changes occur, consider run it monthly:
    #!/bin/sh
    /etc/init.d/slapd stop > /dev/null
    su openldap -c "slapindex"
    /etc/init.d/slapd start > /dev/null
    

Simple tree structure

  1. Here is our simple structure:
    dev.local
    |--people
    `--groups
    
  2. It correspond to the following (file init-tree.ldif):
    dn: ou=people,dc=dev,dc=local
    ou: people
    objectClass: organizationalUnit
    
    dn: ou=groups,dc=dev,dc=local
    ou: groups
    objectClass: organizationalUnit
    
  3. Add it to ldap:
    ldapadd -cxWD cn=admin,dc=dev,dc=local -f init-tree.ldif
    
  4. Test if we can find it:
    ldapsearch -x ou=people
    
    Here is search result:
    # extended LDIF
    #
    # LDAPv3
    # base  (default) with scope subtree
    # filter: ou=people
    # requesting: ALL
    #
    
    # people, dev.local
    dn: ou=people,dc=dev,dc=local
    ou: people
    objectClass: organizationalUnit
    
    # search result
    search: 2
    result: 0 Success
    
    # numResponses: 2
    # numEntries: 1
    

Wednesday, December 22, 2010

Debian Kerberos Slave

Slave KDCs provide an additional source of Kerberos ticket-granting services in the event of inaccessibility of the master KDC. It recommended that your KDCs have a predefined set of CNAME records (DNS hostname aliases), such as krb for the master KDC and kdc1, kdc2, ... for the slave KDCs. This way, if you need to swap a machine, you only need to change a DNS entry, rather than having to change hostnames.

Master (Primary) Kerberos Server

  1. Add a new slave (kdc2.dev.local) to file /etc/krb5.conf (for the master and any other slaves):
    [realms]
            DEV.LOCAL = {
                    kdc = kdc1.dev.local
                    kdc = kdc2.dev.local
                    admin_server = krb.dev.local
            }
    
    Alternatively (preferred way) consider setup DNS discovery. Read here how.
  2. Add slave host principal:
    kadmin.local -q "addprinc -randkey host/kdc2.dev.local"
    
    kadmin.local -q "ktadd host/kdc2.dev.local"
    
  3. Create database propagation host list (file /etc/krb5kdc/kpropd.acl):
    host/kdc1.dev.local@DEV.LOCAL
    host/kdc2.dev.local@DEV.LOCAL
    
  4. Create a dump of the kerberos database (that is a default path for kprop utility):
    kdb5_util dump /var/lib/krb5kdc/slave_datatrans
    

Secondary (Slave, Read-Only) Kerberos Server

  1. Install Kerberos Server and xinetd (to be used for database propagation):
    apt-get install krb5-kdc xinetd
    
  2. Copy (a) realm configuration (file /etc/krb5.conf), (b) database propagation list (file /etc/krb5kdc/kpropd.acl), (c) keytab (file /etc/krb5.keytab), (d) logrotate settings from master, e.g. using ssh copy:
    scp kdc1:/etc/krb5.conf /etc
    scp kdc1:/etc/krb5kdc/kpropd.acl /etc/krb5kdc
    scp kdc1:/etc/krb5.keytab /etc
    scp kdc1:/etc/logrotate.d/krb5 /etc/logrotate.d
    mkdir /var/log/krb5
    
  3. Setup database propagation service (file /etc/xinetd.d/krb_prop):
    service krb_prop
    {
            disable         = no
            socket_type     = stream
            protocol        = tcp
            user            = root
            wait            = no
            server          = /usr/sbin/kpropd
    }
    
    Restart xinetd service:
    /etc/init.d/xinetd restart
    

Propagate database

  1. Propagate database from Master to Slave
    kdc1:~# kprop kdc2.dev.local
    Database propagation to kdc2.dev.local: SUCCEEDED
    
  2. Create database stash key on slave
    kdb5_util stash
    
  3. Start Kerberos Slave service:
    /etc/init.d/krb5-kdc start
    

Automate database propagation

  1. Here is a script that populates master database to all slaves (run on master, file /usr/local/sbin/krb5-prop):
    #!/bin/sh
    
    #slaves="kdc2.dev.local kdc3.dev.local"
    slaves="kdc2.dev.local"
    
    /usr/sbin/kdb5_util dump /var/lib/krb5kdc/slave_datatrans
    error=$?
    if [ $error -ne 0 ]; then
      echo "Kerberos database dump failed."
      exit 1
    fi
    
    for slave in $slaves; do
      /usr/sbin/kprop $slave > /dev/null
      error=$?
      if [ $error -ne 0 ]; then
        echo "Kerberos propagation to host $slave failed."
      fi
    done
    exit 0
    
    Ensure the file is executable:
    chmod +x /usr/local/sbin/krb5-prop
    
  2. Schedule a cron job (/usr/local/sbin/cron-krb5-prop):
    #
    # Regular cron job for Kerberos database propagation
    #
    PATH=/usr/local/sbin
    HOME=/
    LOG=/dev/null
    
    # Every 53 minutes
    53 * * * * root test -x /usr/local/sbin/krb5-prop && krb5-prop >> $LOG
    
    .. and let cron know about it:
    ln -s /usr/local/sbin/cron-krb5-prop /etc/cron.d/cron-krb5-prop
    
Finally here is how to test it is working:
  1. Stop Master Kerberos server:
    /etc/init.d/krb5-kdc stop
    
  2. Open log file on Slave:
    tail -f /var/log/krb5/kdc.log
    
  3. Login to kerberos client:
    ssh user1@deby01
    
  4. Watch the log on Slave, you should see authentication messages.
Read more about kerberos here.

Tuesday, December 21, 2010

How to setup Kerberos DNS discovery

Kerberos DNS discovery can simplify the client hosts setup. The following need to be added to zone file.
$ORIGIN dev.local.
_kerberos-adm._tcp      SRV     0 0 749 kdc1
$ORIGIN _udp.dev.local.
_kerberos               SRV     10 0 88 kdc1.dev.local.
_kerberos               SRV     20 0 88 kdc2.dev.local.
_kerberos-master        SRV     0 0 88 kdc1.dev.local.
_kpasswd                SRV     0 0 464 kdc1.dev.local.
The client configuration can now look like this (file /etc/krb5.conf):
[libdefaults]
        default_realm = DEV.LOCAL
# ...
[realms]
        DEV.LOCAL = {
        }

[domain_realm]
Let test this:
deby01:~$ host -t SRV _kerberos._udp
_kerberos._udp.dev.local has SRV record 10 0 88 kdc1.dev.local.

Debian Kerberos Client

You must have Kerberos server running on the network, read here how to get it up. We are going to add host deby01 as a client for dev.local Kerberos realm.
  1. Ensure the host name is FQDN:
    ldap1:~# hostname -f
    deby01.dev.local
    
    If it is not, issue the following:
    echo "deby01" > /etc/hostname
    hostname -F /etc/hostname
    
  2. Install Kerberos client:
    apt-get -y install krb5-user libpam-krb5
    
  3. Configure client (file /etc/krb5.conf):
    [libdefaults]
            default_realm = DEV.LOCAL
    # ...
    [realms]
            DEV.LOCAL = {
                    # The entry below can be commented 
                    # out in case there is dns 
                    # resolution for kdc
                    kdc = kdc1.dev.local
                    admin_server = krb.dev.local
            }
    
    [domain_realm]
    
    
  4. Add host principal:
    kadmin -p admin -q "addprinc -randkey host/deby01.dev.local"
    
    kadmin -p admin -q "ktadd host/deby01.dev.local"
    
Let verify it:
  1. List kerberos principals:
    deby01:~# kadmin -p admin -q "list_principals"
    ...
    host/deby01.dev.local@DEV.LOCAL
    ...
    user1@DEV.LOCAL
    ...
    
  2. List keys in keytab:
    klist -ke
    
  3. Now you can login to deby01 as user1.
  4. Have a look at log on kerberos server (file /var/log/krb5/kdc.log):
    kdc1.dev.local krb5kdc[988](info): AS_REQ (4 etypes {18 17 16 23}) 192.168.10.41: NEEDED_PREAUTH: user1@DEV.LOCAL for krbtgt/DEV.LOCAL@DEV.LOCAL, Additional pre-authentication required
    kdc1.dev.local krb5kdc[988](info): AS_REQ (4 etypes {18 17 16 23}) 192.168.10.41: ISSUE: authtime 1293635137, etypes {rep=18 tkt=18 ses=18}, user1@DEV.LOCAL for krbtgt/DEV.LOCAL@DEV.LOCAL
    kdc1.dev.local krb5kdc[988](info): TGS_REQ (4 etypes {18 17 16 23}) 192.168.10.41: ISSUE: authtime 1293635137, etypes {rep=18 tkt=18 ses=18}, user1@DEV.LOCAL for host/deby01.dev.local@DEV.LOCAL
    
The pam authentication by default is configured to authenticate user with kerberos with fallback to local authentication, that is fine so nothing need to be configured there.

How to add a new user to Kerberos

The Kerberos is used only for authentication purpose that means that user we are going to add must exists as a normal unix account (or ldap account).
root@kdc1:~# kadmin.local -q "addprinc user1"
...
Principal "user1@DEV.LOCAL" created.
Let test it out:
root@kdc1:~# kinit user1 && klist && kdestroy 
...
Default principal: user1@DEV.LOCAL
...
The operations must be performed on kdc1 that is Kerberos administrative server.

How to add a new host to Kerberos

Each host (a client computer) that need to be a part of kerberos realm must have principal and keytab. Let do that for host deby01. The command must be invoked on Kerberos administrative server (kdc1):
kadmin.local -q "addprinc -randkey host/deby01.dev.local"
This is run on client (deby01):
kadmin -p admin -q "ktadd host/deby01.dev.local"
or consider using the following script (file /usr/local/sbin/kdc-add):
#!/bin/sh

type=host
if [ ! -z $1 ]; then type=$1; fi

sh -c "`cat /etc/hostname | xargs -t -I {} echo \
"kadmin -p admin -q \\\"addprinc -randkey $type/{}\\\""`"

sh -c "`cat /etc/hostname | xargs -t -I {} echo \
"kadmin -p admin -q \\\"ktadd $type/{}\\\""`"
You can run it on a machine you wish to add (you will be prompted to enter password two times):
kdc-add
Please note that each host added to kerberos must have fully qualified hostname. Both forward and reverse mapping must work properly. Here are few simple tests:
deby01:~# hostname
deby01.dev.local

deby01:~# dig deby01.dev.local +short
192.168.2.41

deby01:~# dig -x 192.168.2.41 +short
deby01.dev.local.
Read more here.

Debian Kerberos Master

Kerberos is a network authentication protocol. The idea is to be a secure, single sign-on authentication provider.
  1. Install Kerberos Server.
    apt-get -y install rsyslog krb5-{admin-server,user,doc}
    
  2. Create realm (this may take a long time, up to few minutes).
    krb5_newrealm
    
  3. Activate Kerberos administration by authorizing admin access (file /etc/krb5kdc/kadm5.acl).
    # ...
    */admin *
    admin *
    
  4. Setup logging (new file /etc/logrotate.d/krb5):
    /var/log/krb5/kadmin.log /var/log/krb5/kdc.log {
            daily
            missingok
            rotate 7
            compress
            delaycompress
            notifempty
    }
    
    Create log directory
    mkdir /var/log/krb5
    
  5. Realm Configuration (file /etc/krb5.conf). In our case the kerberos server name is kdc1 and there is alias to it krb (used for administration purpose).
    [libdefaults]
            default_realm = DEV.LOCAL
    
    [realms]
            DEV.LOCAL = {
                    kdc = kdc1.dev.local
                    # kdc = kdc2.dev.local
                    admin_server = krb.dev.local
            }
    
    [domain_realm]
            .dev.local = DEV.LOCAL
            dev.local = DEV.LOCAL
    
    [logging]
            kdc = FILE:/var/log/krb5/kdc.log
            admin_server = FILE:/var/log/krb5/kadmin.log
    
  6. Restart kerberos services.
    invoke-rc.d krb5-admin-server restart ; \
    invoke-rc.d krb5-kdc restart
    
  7. Open another console and have a look at log files.
    cd /var/log/krb5/ ; \
    tail -f kadmin.log kdc.log
    
  8. Ensure services are running.
    root@kdc1:~# netstat -tunlp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:464             0.0.0.0:*               LISTEN      840/kadmind     
    tcp        0      0 0.0.0.0:749             0.0.0.0:*               LISTEN      840/kadmind     
    tcp6       0      0 :::464                  :::*                    LISTEN      840/kadmind     
    udp        0      0 0.0.0.0:464             0.0.0.0:*                           840/kadmind     
    udp        0      0 0.0.0.0:88              0.0.0.0:*                           861/krb5kdc     
    udp        0      0 0.0.0.0:750             0.0.0.0:*                           861/krb5kdc     
    
So far we have the services up and running, however in order to administer it we need create an administrative account:
  1. Add admin principal:
    kadmin.local -q "addprinc admin"
    
  2. Add host (kdc1) principal:
    kadmin.local -q "addprinc -randkey host/kdc1.dev.local"
    
    kadmin.local -q "ktadd host/kdc1.dev.local"
    
Now let test it:
root@kdc1:~# kinit admin && klist && kdestroy 
Password for admin@DEV.LOCAL: 
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: admin@DEV.LOCAL

Valid starting     Expires            Service principal
12/21/10 18:05:14  12/22/10 04:05:14  krbtgt/DEV.LOCAL@DEV.LOCAL
 renew until 12/22/10 18:05:11
Read more here.

Thursday, December 16, 2010

How to edit Dynamic DNS zone

All changes made to a zone using dynamic update are stored in the zone's journal file. The zone file is updated every 15 min. The zone files of dynamic zones cannot normally be edited by hand because they are not guaranteed to contain the most recent dynamic changes (those are only in the journal file). Here are few steps that let you edit entries in dynamic dns zone:
  1. Suspend updates to all dynamic zones.
    rndc freeze
    
  2. Edit zone file
  3. Enable updates to all dynamic zones and reload them.
    rndc thaw
    
Read more about advanced dns features here.

Debian DHCP server failover

Before we start I assume you followed previous two posts: setup and dynamic-dns. Our primary dhcp server located at 192.168.10.4 and secondary at 192.168.10.5.

Primary DHCP Server

  1. You need declare failover section that identifies the primary dhcp server (file /etc/dhcp/dhcpd.conf).
    failover peer "dhcp-failover" {
      primary; # declare this to be the primary server
      address 192.168.10.4;
      port 647;
      peer address 192.168.10.5;
      peer port 647;
      max-response-delay 30;
      max-unacked-updates 10;
      load balance max seconds 3;
      mclt 1800;
      split 128;
    }
    
  2. Failover peer needs to be referenced by concrete subnet:
    subnet 192.168.10.0 netmask 255.255.255.0 {
      pool {
        # In order to turn off failover just comment out
        # the line below
        failover peer "dhcp-failover";
        range 192.168.10.41 192.168.10.254;
      }
      option subnet-mask 255.255.255.0;
      option broadcast-address 192.168.10.255;
      option routers gw1.dev.local;
    }
    

Secondary DHCP Server

  1. Failover secondary peer declaration (file /etc/dhcp/dhcpd.conf):
    failover peer "dhcp-failover" {
      secondary; # declare this to be the secondary server
      address 192.168.10.5;
      port 647;
      peer address 192.168.10.4;
      peer port 647;
      max-response-delay 30;
      max-unacked-updates 10;
      load balance max seconds 3;
    }
    
  2. subnet 192.168.10.0 netmask 255.255.255.0 {
      pool {
        # In order to turn off failover just comment out
        # the line below
        failover peer "dhcp-failover";
        range 192.168.10.41 192.168.10.254;
      }
      option subnet-mask 255.255.255.0;
      option broadcast-address 192.168.10.255;
      option routers gw1.dev.local;
    }
    
That pretty much you need to do. Read more here.

Dynamic DNS update with DHCP on Debian

If you have many dhcp clients it is much convenient to find them by name than remember ip addresses. This is what dynamic dns update with dhcp is for. I assume you followed few previous posts on dns and dhcp topic.

Configure DNS server

  1. We would like to accept only authorized secure updates, so let generate a secure key:
    dnssec-keygen -r /dev/urandom -a hmac-md5 -b 256 -n host key
    cat Kkey.*.private
    rm Kkey*
    
    Here is sample output:
    Private-key-format: v1.3
    Algorithm: 157 (HMAC_MD5)
    Key: 9rHjOgEuZ8O8LpsoJcl4zORqbeOCaPc3WfYDd5Mq3FHI=
    ...
    
  2. Add the following (replace md5 key with the one you generated) to a new file /etc/bind/dynamic-dns.key
    key DYNAMICDNS {
            algorithm hmac-md5;
            secret "9rHjOgEuZ8O8LpsoJcl4zORqbeOCaPc3WfYDd5Mq3FHI=";
    };
    
  3. Secure key:
    chmod o-r /etc/bind/dynamic-dns.key
    
  4. Ensure bind is the owner of the configucation directory, since it save some files there during dynamic updates:
    chmod -R g+w /etc/bind/
    
  5. Update zone registration file to allow dynamic updates (file /etc/bind/named.conf.local):
    include "/etc/bind/dynamic-dns.key";
    
    zone "dev.local" IN {
           type master;
           file "/etc/bind/db.dev.local";
           allow-update { key DYNAMICDNS; };
    };
    
    zone "10.168.192.IN-ADDR.ARPA" IN {
           type master;
           file "/etc/bind/db.10.168.192";
           allow-update { key DYNAMICDNS; };
    };
    
  6. Restart bind9

Test DNS Settings

  1. Let configure DNS for a new host test with ip 192.168.10.7:
    root@ns1:/etc/bind# nsupdate 
    > server 127.0.0.1
    > key DYNAMICDNS 9rHjOgEuZ8O8LpsoJcl4zORqbeOCaPc3WfYDd5Mq3FHI=
    > zone dev.local
    > update add test.dev.local. 600 IN A 192.168.10.7
    > send
    > zone 10.168.192.IN-ADDR.ARPA
    > update add 7.10.168.192.in-addr.arpa 600 IN PTR test.dev.local.
    > send
    
  2. And now verify:
    user1@deby01:~$ host test
    test.dev.local has address 192.168.10.7
    
    user1@deby01:~$ host 192.168.10.7
    7.10.168.192.in-addr.arpa domain name pointer test.dev.local.
    

Configure DHCP server

  1. Add the following (replace md5 key with the one you generated) to a new file /etc/dhcp/dynamic-dns.key
    key DYNAMICDNS {
            algorithm hmac-md5;
            secret "9rHjOgEuZ8O8LpsoJcl4zORqbeOCaPc3WfYDd5Mq3FHI=";
    };
    
  2. Create a new file /etc/dhcp/dhcpd.conf.local with the following content:
    include "/etc/dhcp/dynamic-dns.key";
    
    zone dev.local. {
            primary ns1.dev.local;
            key DYNAMICDNS;
    }
    
    zone 10.168.192.IN-ADDR.ARPA. {
            primary ns1.dev.local;
            key DYNAMICDNS;
    }
    
  3. Open file /etc/dhcp/dhcpd.conf and ensure:
    ddns-update-style interim;
    include "/etc/dhcp/dhcpd.conf.local";
    
  4. Restart dhcp server so our change take effect.
    /etc/init.d/isc-dhcp-server restart
    

Test DHCP server with Debian client

  1. First of all in order to identify your debian client by name you must ensure it send host name to dhcp server. You can check this in file /etc/dhcp/dhclient.conf:
    send host-name "deby01";
    
  2. Assuming the dhcp client interface is configured for eth1, here is a command to re-new ip address from server:
    dhclient -v eth1
    

Debian DHCP Server Setup

Dynamic Host Configuration Protocol (DHCP) is a protocol. It gives client machines "leases" for IP addresses and can automatically set their network configuration.
apt-get -y install rsyslog isc-dhcp-server
Before we start configuring the dhcp server let set our requirements:
  • Domain name: dev.local
  • Network: 192.168.10.0/24
  • DNS Servers: ns1.dev.local, ns2.dev.local
  • Gateway: gw1.dev.local
  • First 40 ip addresses are reserved for servers
  • DHCP pool is 41 - 254

Server Configuration

  1. The server will be listening on eth0 interface (file /etc/default/isc-dhcp-server):
    # On what interfaces should the DHCP server (dhcpd) 
    # serve DHCP requests? Separate multiple interfaces 
    # with spaces, e.g. "eth0 eth1".
    INTERFACES="eth0"
    
  2. Configure DHCP per our requirements (file /etc/dhcp/dhcpd.conf)
    # The ddns-updates-style parameter controls whether or
    # not the server will attempt to do a DNS update when 
    # a lease is confirmed. We default to the behavior of 
    # the version 2 packages ('none', since DHCP v2 didn't
    # have support for DDNS.)
    ddns-update-style none;
    
    # option definitions common to all supported networks
    option domain-name "dev.local";
    option domain-name-servers ns1.dev.local, ns2.dev.local;
    option ip-forwarding off;
    
    # This way you can specify multiple search domains.
    # For Windows clients it doesn't work and need to be
    # setup manually
    option domain-search "dev.local", "corp.local";
    
    # Lease time is in seconds
    default-lease-time 600;
    max-lease-time 7200;
    
    # If this DHCP server is the official DHCP server for 
    # the local network, the authoritative directive should 
    # be uncommented.
    authoritative;
    
    # Use this to send dhcp log messages to a different log 
    # file (you also have to hack syslog.conf to complete 
    # the redirection).
    log-facility local7;
    
    subnet 192.168.10.0 netmask 255.255.255.0 {
      pool {
        range 192.168.10.41 192.168.10.254;
      }
      option subnet-mask 255.255.255.0;
      option broadcast-address 192.168.10.255;
      option routers gw1.dev.local;         
    }
    
    

How to test from Debian client

  1. You need a network interface configured for dhcp (file /etc/network/interfaces):
    allow-hotplug eth0
    iface eth0 int dhcp
    
  2. Obtain ip address and check your up:
    root@dh1:~# dhclient eth0 && ifconfig eth0 | grep inet
              inet addr:192.168.10.41 ...
    
  3. Try some lookups (notice multiple dns search list; in order to use host command you need to install dnsutils package):
    root@dh1:~# host ns1
    ns1.dev.local has address 192.168.10.2
    
    root@dh1:~# host mail
    mail.corp.local has address 192.168.11.10
    

How to enable multi-domain search in Windows client

  1. Choose Advanced TCP/IP Settings
  2. In DNS tab choose "Append these DNS suffixes (in order)"
  3. Add as many as you need domain to search
  4. Try some lookups (notice multiple dns search list):
    C:\>nslookup ns1
    ...
    C:\>nslookup mail
    ...
    
Read more here.

Wednesday, December 15, 2010

Debian Slave DNS Server Setup

The setup of Slave (Secondary) DNS Server is pretty easy. You need to follow two previous posts of setting up a simple DNS server and chroot bind9.

Master (Primary) DNS Server

  1. Add the following to /etc/bind/named.conf.options
    dnssec-enable yes;
    
  2. Generate MD5 hash key:
    dnssec-keygen -r /dev/urandom -a hmac-md5 \
     -b 256 -n host rndc ; cat Krndc.*.private \
     | grep Key ; rm Krndc*
    
    Here is output:
    Key: 9EKQM+7+EnJzO7TWyayUf0vks4k+SZPf9DAs8fOeREM=
    
  3. Add the following (replace md5 key with the one you generated) to a new file /etc/bind/transfer.key
    key TRANSFER {
            algorithm hmac-md5;
            secret "9EKQM+7+EnJzO7TWyayUf0vks4k+SZPf9DAs8fOeREM=";
    };
    
  4. Secure key:
    chmod o-r /etc/bind/transfer.key
    
  5. Add the following to a new file /etc/bind/named.conf.transfer
    include "/etc/bind/transfer.key";
    
    // Slave IP Address
    server 192.168.10.3 {
            keys {
            TRANSFER;
        };
    };
    
  6. Add the following to file /etc/bind/named.conf
    include "/etc/bind/named.conf.transfer";
    

Slave (Secondary) DNS Server

  1. Add the following to /etc/bind/named.conf.options
    dnssec-enable yes;
    
  2. Add the following (replace md5 key with the one you generated) to file /etc/bind/transfer.key
    key TRANSFER {
            algorithm hmac-md5;
            secret "9EKQM+7+EnJzO7TWyayUf0vks4k+SZPf9DAs8fOeREM=";
    };
    
  3. Secure key:
    chmod o-r /etc/bind/transfer.key
    
  4. Add the following to file /etc/bind/named.conf.transfer
    include "/etc/bind/transfer.key";
    
    // Master IP Address
    server 192.168.10.2 {
            keys {
            TRANSFER;
        };
    };
    
  5. Add the following to file /etc/bind/named.conf
    include "/etc/bind/named.conf.transfer";
    
  6. Specify slave zones in file /etc/bind/named.conf.local:
    zone "dev.local" IN {
           type slave;
           file "/etc/bind/db.dev.local";
           masters { 192.168.10.2; };
           allow-notify { 192.168.10.2; };
    };
    
    zone "10.168.192.IN-ADDR.ARPA" IN {
           type slave;
           file "/etc/bind/db.10.168.192";
           masters { 192.168.10.2; };
           allow-notify { 192.168.10.2; };
    };
    
  7. Copy forwards to slave (file /etc/bind/named.conf.forward):
    zone "corp.local" IN {
           type forward;
           forwarders { 192.168.11.2; 192.168.11.3; };
    };
    
  8. Ensure bind:bind is the owner of the configuration so it can update the files received from master.
    chown -R bind:bind /var/chroot/bind9/etc/*
    
In order to keep both servers in sync, setup ntpdate (on master and slave dns servers):
root@ns2:/etc/bind# apt-get install ntpdate
...
root@ns2:/etc/bind# ntpdate pool.ntp.org
Now you can restart bind9 on both servers and ensure that slave received zone files.

Troubleshooting

Have a look at system log file (/var/log/syslog) for any errors reported by named. If you will see something telling you permission denied while dumping a file, ensure bind:bind is the owner as following:
chown -R bind:bind /var/chroot/bind9/etc/*
/etc/init.d/bind9 restart
ls -l /ent/bind/db.*

Debian LAN DNS setup

We are going setup a LAN DNS server for a dev.local domain. I assume you already have dns server up and running, if not please follow previous post here.

Configure Forward Lookup Zone

First of all let start from forward lookup zone (file /etc/bind/db.dev.local):
$TTL 2d
dev.local.    IN     SOA     ns1.dev.local. hostmaster.dev.local. (
                             2010122201 ; se = serial number
                             6h         ; ref = refresh
                             15m        ; ret = update retry
                             3w         ; ex = expiry
                             3h         ; min = minimum
                             )
              IN     NS      ns1.dev.local.
              IN     NS      ns2.dev.local.
              IN     MX  10  mail.dev.local.
              IN     A       192.168.10.5
; hosts
gw1           IN     A       192.168.10.1
ns1           IN     A       192.168.10.2
ns2           IN     A       192.168.10.3
mail          IN     A       192.168.10.4
www           IN     CNAME   dev.local.

Configure Reverse Lookup Zone

Each forward lookup zone can have a reverse lookup zone, here is ours (file /etc/bind/db.10.168.192):
$ORIGIN .
$TTL 2d
10.168.192.IN-ADDR.ARPA      IN   SOA   ns1.dev.local. hostmaster.dev.local. (
                             2010122201 ; se = serial number
                             6h         ; ref = refresh
                             15m        ; ret = update retry
                             3w         ; ex = expiry
                             3h         ; min = minimum
                             )
              IN     NS      ns1.dev.local.
              IN     NS      ns2.dev.local.
$ORIGIN 10.168.192.IN-ADDR.ARPA.
1             IN     PTR     gw1.dev.local.
2             IN     PTR     ns1.dev.local.
3             IN     PTR     ns2.dev.local.
4             IN     PTR     mail.dev.local.
5             IN     PTR     www.dev.local.

Add Zone to DNS server

Now that we have both forward and reverse dns lookup zones for dev.local domain, we need to let dns server to know about it. All we need to do is add the following to /etc/bind/named.conf.local:
//
// Do any local configuration here
//

zone "dev.local" IN {
       type master;
       file "/etc/bind/db.dev.local";
};

zone "10.168.192.IN-ADDR.ARPA" IN {
       type master;
       file "/etc/bind/db.10.168.192";
};

// Consider adding the 1918 zones here, if they are not 
// used in your organization
include "/etc/bind/zones.rfc1918";

Forwarding to other LAN DNS Servers

Let do DNS forwarding for corp.local LAN domain with dns server on 192.168.11.2 (create a new file /etc/bind/named.conf.forward).
zone "corp.local" IN {
       type forward;
       forwarders { 192.168.11.2; 192.168.11.3; };
};
zone "11.168.192.IN-ADDR.ARPA" IN {
       type forward;
       forwarders { 192.168.11.2; 192.168.11.3; };
};
Let include it into the /etc/bind/named.conf
include "/etc/bind/named.conf.forward";
We need to ask bind9 reload the changes:
/etc/init.d/bind9 reload

Forwarding to IPS DNS Servers

You can optimize the dns queries to use dns servers supplied by your ISP (they are much closer to you than any others). In case your dns server can not resolve some domains, instead of contacting root servers it will contact ISP's servers first. This can be configured in /etc/bind/named.conf.options file:
// forwarders {
//      0.0.0.0;
// };
forwarders {
        192.168.123.123;    # ns1.your-isp.net
        192.168.321.321;    # ns2.your-isp.net
};

Client Configuration

Our dns server for dev.local is up and running, so now it is time configure client machines to use it. Ensure the following in /etc/resolv.conf:
search dev.local
nameserver 192.168.10.2
Please read more here.

Debian simple DNS server setup

We are going setup a simple Debian DNS server for local purpose using bind9.
apt-get install -y rsyslog bind9 bind9-doc dnsutils
Once the server installed let our system know which dns server to use (a one we just installed), ensure that 127.0.0.1 is the first nameserver in the list (file /etc/resolv.conf):
nameserver 127.0.0.1
In case you do no need the server to listen on ipv6 set the following option (file /etc/bind/named.conf.options):
listen-on-v6 { none; };
Restart bind9 daemon:
/etc/init.d/bind9 restart
and verify with:
root@ns1:~# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.10.2:53         0.0.0.0:*               LISTEN      816/named       
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      816/named       
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      816/named       
udp        0      0 192.168.10.2:53         0.0.0.0:*                           816/named       
udp        0      0 127.0.0.1:53            0.0.0.0:*                           816/named       
That pretty it, let ensure its working. First we need install dnsutils package that comes with dig command, so here we go:
root@ns1:~# dig debian.org
; <<>> DiG 9.7.2-P3 <<>> debian.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64434
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 3, ADDITIONAL: 3

;; QUESTION SECTION:
;debian.org.   IN A

;; ANSWER SECTION:
debian.org.  3600 IN A 128.31.0.51
debian.org.  3600 IN A 206.12.19.7

;; AUTHORITY SECTION:
debian.org.  28606 IN NS ns2.debian.org.
debian.org.  28606 IN NS ns4.debian.com.
debian.org.  28606 IN NS ns1.debian.org.

;; ADDITIONAL SECTION:
ns1.debian.org.  28606 IN AAAA 2607:f8f0:610:4000:214:38ff:feee:b65a
ns4.debian.com.  28606 IN A 194.177.211.209
ns4.debian.com.  28606 IN AAAA 2001:648:2ffc:deb::10:10

;; Query time: 96 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Dec 15 21:47:12 2010
;; MSG SIZE  rcvd: 196
Notice the server responded to our request was 127.0.0.1. Read more here and here. Consider chroot your dns server, details here.