I have a Red Hat server instance on AWS that I'm trying to make a nice AMI image of so I can test specific instance machine types with Apache Bench (ab).

I'm currently just trying to create the OS image with everything how I would like it. Currently I have httpd installed on the image. I want it so that when this image boots it automatically starts hosting its page.

Currently I'm starting the httpd service and stopping the iptables service to get the page to be served. Is there are way I can issue these commands on start up so that the page is served without having to SSH into the image and do it manually?

3 Answers 3


Sure! You don't say but if you're using Red Hat Server (probably CentOS) and probably versions 5 or 6, all you have to do is configure the 2 services httpd and iptables so that they either startup or shutoff when your server boots.

Service setup

There is a command line tool which you can use to do this configuration change, called chkconfig.

$ chkconfig --level 345 httpd on
$ chkconfig iptables off

Leaving the firewall up

Rather than disable your firewall, I'd encourage you to simply leave it up and add a rule allowing the port 80 traffic in and out so that it can reach the Apache web server.

$ iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
$ iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

These rules should open up 80, assuming the Ethernet device your VM uses is eth0. You might need to do some sleuthing to get this information, you can use the ip command to confirm.

$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 54:52:00:ff:ff:dd brd ff:ff:ff:ff:ff:ff

Making the FW rules stick!

To make these iptables rules permanent you can add them as I've described above and then tell iptables to save them for next time.

$ /etc/init.d/iptables save
  • So the command chkconfig --level 345 httpd on will set the httpd to turn on when the login level is at 3, 4, or 5? // Also thanls, good answer, I think it will help!
    – KDecker
    Feb 3, 2014 at 4:32
  • @BumSkeeter - correct. This just sets it up so that it's configured. If you don't do a reboot and the service was off, it is still off after running this command. Make sense?
    – slm
    Feb 3, 2014 at 4:36

Server-oriented RedHat Enterprise 6 (RHEL6) needs the file /etc/rc3.d/S85httpd when run in server mode to be a link to /etc/init.d/httpd and /etc/rc3.d/S08iptables be a link to /etc/init.d/iptables. If, by chance, its inital runlevel is 5 (unlikely), the links would have the same names, but in the /etc/rc5.d/ directory.

That in mind, as root I would:

cd /etc/rc3.d
ln -s ../init.d/httpd S85httpd
mv S08iptables K08iptables
cd /etc/rc5.d
ln -s ../init.d/httpd S85httpd
mv S08iptables K08iptables

To see the default run level in RHEL6:

grep initdefault /etc/inittab

Should this fail to start the services, one can add these lines to /etc/rc.local:

/etc/init.d/iptables stop
/etc/init.d/httpd start

Running these commands starts up or shuts down services on the server.

Note that the next release of RedHat (RHEL7), which is already in beta and will almost certainly be out later this year, changes how files are started up at boot time using a technology which I feel makes managing daemons (services) a lot easier called systemd ... and uses a very different way of setting up services to start up at system boot time.

  • Yeah don't do it this way, this is bad! RH distros provide tooling for manipulating the S & K files in the various rc directories.
    – slm
    Feb 3, 2014 at 3:47
  • There are still a lot of RHEL/CentOS 5 machines out there which may not work with the chkconfig tools and what not. I know RHEL5 -- which is supported for three more years -- has problems with service foo start/stop and needs to run /etc/init.d/foo start/stop instead
    – samiam
    Feb 3, 2014 at 4:37
  • I've worked with RHEL3, 4, 5, & 6 in enterprise scenarios for most of my career and I've never encountered any issues w/ chkconfig. Not doubting that it doesn't happen but if it does, it's likely that a novice was attempting to admin the box w/o fully understanding the tools and left it in a broken state. chkconfig keys off of the header info in the scripts under init.d, so it also could be the case that the script wasn't "setup" for chkconfig too.
    – slm
    Feb 3, 2014 at 4:45
  • Yeah, I see a RPM file for chkconfig included with RHEL3, so it's been out there for a while (it's service that's new for RHEL6). Anyway, it's going to be academic in a few years considering systemd does everything differently (albeit with a compatibility layer): fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet
    – samiam
    Feb 3, 2014 at 5:23
  • 1
    Yes all the knowledge will be relegated as useless 8-). I've been spending a fair amount of time getting experience with Systemd now in preparation.
    – slm
    Feb 3, 2014 at 5:25

Changing habits is always painful, but after using it with the RHEL 7 Beta for one month, I can say, it's the way to go: http://www.certdepot.net/rhel7-get-started-systemd/

  • 1
    It is better to provide the information here, and use the link as a reference for further details. That way your answer does not loose all of it value when the link becomes invalid.
    – Anthon
    Feb 3, 2014 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.