Tuesday, February 9, 2010

Port Redirection: Howto?

You can easily redirect incoming traffic by inserting rules into PREROUTING chain of the nat table. You can set destination port using the REDIRECT target.
Syntax

The syntax is as follows to redirect tcp $srcPortNumber port to $dstPortNumber:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport $srcPortNumber -j REDIRECT --to-port $dstPortNumbe

The syntax is as follows to redirect udp $srcPortNumber port to $dstPortNumber:

iptables -t nat -A PREROUTING -i eth0 -p udp --dport $srcPortNumber -j REDIRECT --to-port $dstPortNumbe

Replace eth0 with your actual interface name. The following syntax match for source and destination ips:

iptables -t nat -I PREROUTING --src $SRC_IP_MASK --dst $DST_IP -p tcp --dport $portNumber -j REDIRECT --to-ports $rediectPort

Examples:

The following example redirects TCP port 25 to port 2525:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j REDIRECT --to-port 2525

In this example all incoming traffic on port 80 redirect to port 8123

iptables -t nat -I PREROUTING --src 0/0 --dst 192.168.1.5 -p tcp --dport 80 -j REDIRECT --to-ports 8123

Quoting from the iptables man page:

This target is only valid in the nat table, in the PREROUTING and OUTPUT
chains, and user-defined chains which are only called from those
chains. It redirects the packet to the machine itself by changing the
destination IP to the primary address of the incoming interface
(locally-generated packets are mapped to the 127.0.0.1 address). It
takes one option:

--to-ports port[-port]
This specifies a destination port or range of ports to use:
without this, the destination port is never altered. This is
only valid if the rule also specifies -p tcp or -p udp.

The OUTPUT chain example:

iptables -t nat -I OUTPUT --src 0/0 --dst 192.168.1.5 -p tcp --dport 80 -j REDIRECT --to-ports 8123

How Do I View NAT Rules?

Type the following command:

iptables -t nat -L -n -v

How Do I Save NAT Redirect Rules?

Type the following command:

iptables-save

Connecting RHEL to Active Directory Server through Winbind

You have a RHEL system and you want to authenticate it against your active directory. The good news is that Red Hat has made it easy for you to do this. The bad news is that they only get the most basic structure working for you.

Here I will show you how to get WinBind authentication working using Authconfig, and how make it a little more seamless than this utility leaves it off.

It should be noted that while this works perfectly well, it is really not the best way to authenticate users against a UNIX host. Given the option, having your users in OpenLDAP and PAM authenticating them against that would be a much better option. However, we don’t live in a perfect world, and sometimes we just have to make things work.

Let’s start by using authconfig to join your machine to the domain. This should all be done as the root user.

# authconfig

* Select “Use Winbind” and Use “Winbind Authentication”. Remember to leave “Cache Information”, “Use MD5 Passwords” and “Use Shadow Passwords” selected.
* Select “Next”
* Under “Security Model” select “ads”
* “Domains:” examplead (substatute with the name of your Active Directory)
* “Domain Controllers:” adserver.domain.com (Again, substitute with the name of your Active Directory server)
* “ADS Realm:” ADSERVER.DOMAIN.COM
* “Template Shell:” /bin/bash
* Select “Join Domain”
* Select “OK”

Now your machine should be be on the domain. Test it to make sure you can see your AD users:

# wbinfo -u

You should see your users in the list.

The only problem is that to do anything with them, you have to express their user name in that annoying way Windows likes you to. Something like this:

“EXAMPLEAD\\username”

Not very usefull. To get around this, simply edit “/etc/samba/smb.conf” and change this line:

winbind use default domain = no

to this:

winbind use default domain = yes

You should now be able to express AD usernames without the domain nonsense before it. Try it:

# finger username
Login: username Name: Username
Directory: /home/EXAMPLEAD/username Shell: /bin/bash
Never logged in.
No mail.
No Plan.

Finally check your “/etc/nsswhich.conf” file to make sure RHEL knows to use WinBind. Authconfig should have set this up for you, and it should have lines that look like this:

passwd: files winbind
shadow: files winbind
group: files winbind

Note:Follow these instructions to have your users directories automatically created….

http://kbase.redhat.com/faq/FAQ_43_5367.shtm

Tuesday, February 2, 2010

Linux: How to create a new Partition on Linux?

Linux Partitioning follows a simple step which can be executed post installation.Below mentioned steps creates a new partition called /u02 and needed to be mounted therein.

Hope it helps understandign clear cut steps to linux partitioning:

[root@ajeet ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 11G 479M 9.1G 5% /
/dev/sda7 4.9G 139M 4.5G 3% /home
/dev/sda6 4.9G 230M 4.4G 5% /var
/dev/sda5 7.6G 2.4G 4.9G 33% /usr
/dev/sda2 11G 155M 9.4G 2% /opt
/dev/sda1 99M 24M 71M 25% /boot
tmpfs 1010M 0 1010M 0% /dev/shm
/dev/sda9 9.2G 150M 8.6G 2% /u01

[root@ajeet ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1364 10851907+ 83 Linux
/dev/sda3 1365 2715 10851907+ 83 Linux
/dev/sda4 2716 9729 56339955 5 Extended
/dev/sda5 2716 3738 8217216 83 Linux
/dev/sda6 3739 4388 5221093+ 83 Linux
/dev/sda7 4389 5038 5221093+ 83 Linux
/dev/sda8 5039 5560 4192933+ 82 Linux swap / Solaris
/dev/sda9 5561 6777 9775521 83 Linux

Command (m for help): n
First cylinder (6778-9729, default 6778):
Using default value 6778
Last cylinder or +size or +sizeM or +sizeK (6778-9729, default 9729): +10000M

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1364 10851907+ 83 Linux
/dev/sda3 1365 2715 10851907+ 83 Linux
/dev/sda4 2716 9729 56339955 5 Extended
/dev/sda5 2716 3738 8217216 83 Linux
/dev/sda6 3739 4388 5221093+ 83 Linux
/dev/sda7 4389 5038 5221093+ 83 Linux
/dev/sda8 5039 5560 4192933+ 82 Linux swap / Solaris
/dev/sda9 5561 6777 9775521 83 Linux
/dev/sda10 6778 7994 9775521 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

[root@ajeet ~]# partprobe

[root@ajeet ~]# mkfs.ext3 /dev/sda10
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1224000 inodes, 2443880 blocks
122194 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2503999488
75 block groups
32768 blocks per group, 32768 fragments per group
16320 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

[root@ajeet ~]# e2label /dev/sda10 /u02

[root@ajeet ~]# vi /etc/fstab

[root@ajeet ~]# mkdir /u02

[root@ajeet ~]# mount -a

[root@ajeet ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 11G 479M 9.1G 5% /
/dev/sda7 4.9G 139M 4.5G 3% /home
/dev/sda6 4.9G 230M 4.4G 5% /var
/dev/sda5 7.6G 2.4G 4.9G 33% /usr
/dev/sda2 11G 155M 9.4G 2% /opt
/dev/sda1 99M 24M 71M 25% /boot
tmpfs 1010M 0 1010M 0% /dev/shm
/dev/sda9 9.2G 150M 8.6G 2% /u01
/dev/sda10 9.2G 150M 8.6G 2% /u02

[root@ajeet ~]# mount
/dev/sda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda7 on /home type ext3 (rw)
/dev/sda6 on /var type ext3 (rw)
/dev/sda5 on /usr type ext3 (rw)
/dev/sda2 on /opt type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sda9 on /u01 type ext3 (rw)
/dev/sda10 on /u02 type ext3 (rw)

[root@ajeet ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 11G 479M 9.1G 5% /
/dev/sda7 4.9G 139M 4.5G 3% /home
/dev/sda6 4.9G 230M 4.4G 5% /var
/dev/sda5 7.6G 2.4G 4.9G 33% /usr
/dev/sda2 11G 155M 9.4G 2% /opt
/dev/sda1 99M 24M 71M 25% /boot
tmpfs 1010M 0 1010M 0% /dev/shm
/dev/sda9 9.2G 150M 8.6G 2% /u01
/dev/sda10 9.2G 150M 8.6G 2% /u02
[root@ajeet ~]#

Saturday, January 30, 2010

RPM: Want to know the overall RPMs in your Linux Box ? Try Your Hands on !!

I was searching for a command which could provide me the overall RPMs in my CentOS 5.4 box.I did tried it several times but usually forget to collect it.
So this command is for all you guys (and me, of course) who may want to know the size of all the installed packages and sort them. Here we go:

Code:

$ rpm -qa --qf '%11{SIZE} %{NAME}\n' | sort -k1n

There you see..Sea of RPMs.

Happy LinuXing !!!

389-DS: Kickstarting CentOS with 389-DS Complete Setup

The Contents have been revised and redirected to new website.

Saturday, December 26, 2009

Apache:Start Apache as service in Linux (Fedora)

The Apache HTTP server is installed by default in all Linux distributions and it is configured to run as service. I usually prefer to install Apache from source instead of using the default one. After installing Apache I want to run that instance as service so that it can be restarted automatically whenever machine is rebooted. This can be achieved easily by modifying /etc/rc.d/init.d/httpd file, which is configured to run the Apache installed with OS.

Comment the following section, though it is not required but I prefer to keep all configuration at one place



if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
Modify the following lines-

apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
to point towards your Apache installation

apachectl=/opt/apps/httpd-2.2.14/bin/apachectl
httpd=${HTTPD-/opt/apps/httpd-2.2.14/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/opt/apps/httpd-2.2.14/logs/httpd.pid}

Now your custom Apache instance will be started by OS instead of the default one.

Wednesday, December 16, 2009

Cron: Understanding crontab deeply?

A crontab file contains instructions to the cron daemon of the general
form: `run this command at this time on this date'. Each user has
their own crontab, and commands in any given crontab will be executed as
the user who owns the crontab. Uucp and News will usually have their own
crontabs, eliminating the need for explicitly running su as part of a
cron command.

Blank lines and leading spaces and tabs are ignored. Lines whose first
non-space character is a pound-sign (#) are comments, and are ignored.
Note that comments are not allowed on the same line as cron commands,
since they will be taken to be part of the command. Similarly, comments
are not allowed on the same line as environment variable settings.

An active line in a crontab will be either an environment setting or a
cron command. An environment setting is of the form,

name = value

where the spaces around the equal-sign (=) are optional, and any subse-
quent non-leading spaces in value will be part of the value assigned to
name. The value string may be placed in quotes (single or double, but
matching) to preserve leading or trailing blanks. The name string may
also be placed in quote (single or double, but matching) to preserve
leading, traling or inner blanks.

Several environment variables are set up automatically by the cron
daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the
/etc/passwd line of the crontab's owner. HOME and SHELL may be overrid-
den by settings in the crontab; LOGNAME may not.

(Another note: the LOGNAME variable is sometimes called USER on BSD sys-
tems... on these systems, USER will be set also).

In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if
it has any reason to send mail as a result of running commands in
`this' crontab. If MAILTO is defined (and non-empty), mail is sent to
the user so named. If MAILTO is defined but empty (MAILTO=""), no mail
will be sent. Otherwise mail is sent to the owner of the crontab. This
option is useful if you decide on /bin/mail instead of /usr/lib/sendmail
as your mailer when you install cron -- /bin/mail doesn't do aliasing,
and UUCP usually doesn't read its mail.

The format of a cron command is very much the V7 standard, with a number
of upward-compatible extensions. Each line has five time and date
fields, followed by a user name (with optional `:' and
`/' suffixes) if this is the system crontab file, followed
by a command. Commands are executed by cron when the minute, hour,
and month of year fields match the current time, and when at least one of
the two day fields (day of month, or day of week) match the current time
(see `Note' below). cron examines cron entries once every minute.
The time and date fields are:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

A field may be an asterisk (*), which always stands for `first-last'.

Ranges of numbers are allowed. Ranges are two numbers separated with a
hyphen. The specified range is inclusive. For example, 8-11 for an
``hours'' entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by
commas. Examples: `1,2,5,9', `0-4,8-12'.

Step values can be used in conjunction with ranges. Following a range
with `/' specifies skips of the number's value through the
range. For example, `0-23/2' can be used in the hours field to specify
command execution every other hour (the alternative in the V7 standard is
`0,2,4,6,8,10,12,14,16,18,20,22'). Steps are also permitted after an
asterisk, so if you want to say `every two hours', just use `*/2'.

Names can also be used for the `month' and `day of week' fields. Use
the first three letters of the particular day or month (case doesn't mat-
ter). Ranges or lists of names are not allowed.

The `sixth' field (the rest of the line) specifies the command to be
run. The entire command portion of the line, up to a newline or % char-
acter, will be executed by /bin/sh or by the shell specified in the SHELL
variable of the cronfile. Percent-signs (%) in the command, unless
escaped with backslash (\), will be changed into newline characters, and
all data after the first % will be sent to the command as standard input.

The command can optionally be prefixed by `@AppleNotOnBattery ' to tell
cron not to run the command when functioning on battery power. For exam-
ple, the `sixth' field when using this option would appear something
like `@AppleNotOnBattery /usr/bin/touch /tmp/foo'

Note: The day of a command's execution can be specified by two fields --
day of month, and day of week. If both fields are restricted (ie, aren't
*), the command will be run when either field matches the current time.
For example, `30 4 1,15 * 5' would cause a command to be run at 4:30 am
on the 1st and 15th of each month, plus every Friday.

Instead of the first five fields, one of eight special strings may
appear:

string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (sames as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

EXAMPLE CRON FILE

# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
#
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
#
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
#
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Notes
When specifying day of week, both day 0 and day 7 will be considered Sunday. BSD and ATT seem to disagree about this.

Lists and ranges are allowed to co-exist in the same field. "1-3,7-9" would be rejected by ATT or BSD cron -- they want to see "1-3" or "7,8,9" ONLY.

Ranges can include "steps", so "1-9/2" is the same as "1,3,5,7,9".

Names of months or days of the week can be specified by name. Environment variables can be set in the crontab. In BSD or ATT, the environment handed to child processes is basically the one from /etc/rc.

Command output is mailed to the crontab owner (BSD can't do this), can be mailed to a person other than the crontab owner (SysV can't do this), or the feature can be turned off and no mail will be sent at all (SysV can't do this either).

All of the `@' commands that can appear in place of the first five fields are extensions.

If you're in one of the 70-odd countries that observe Daylight Savings Time, jobs scheduled during the rollback or advance will be affected. In general, it's not a good idea to schedule jobs during this period.

"Wisdom is the power to put our time and our knowledge to the proper use" - Thomas J. Watson