Search This Blog

Saturday, December 31, 2011

How To: Disable Firewall on RHEL / CentOS / RedHat Linux

don't want firewall because I only run one http (port 80) public service. How do I turn off or disable firewall permanently under RHEL / Fedora Linux / Red Hat Enterprise Linux and CentOS Linux?

iptables is administration tool / command for IPv4 packet filtering and NAT. You need to use the following tools:
[a] service is a command to run a System V init script. It is use to save / stop / start firewall service.
[b] chkconfig command is used to update and queries runlevel information for system service. It is a system tool for maintaining the /etc/rc*.d hierarchy. Use this tool to disable firewall service at boot time.

How Do I Disable Firewall?

First login as the root user.
Next enter the following three commands to disable firewall.
# service iptables save
# service iptables stop
# chkconfig iptables off

If you are using IPv6 firewall, enter:
# service ip6tables save
# service ip6tables stop
# chkconfig ip6tables off

Linux Disable / Remove The Iptables Firewall

Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.
If you are using RHEL (Redhat), Fedora core or Cent os Linux just type following commands to disable the iptables firewall:
# service iptables save
# service iptables stop
# chkconfig iptables off

If you are using any other Linux distribution type the following command to clear up firewall rules:
# iptables -F
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT

You may need to put above rules in a shell script and execute the same. Also remove your iptables startup script from your network configuration file such as /etc/network/interfaces. Look for post-up directive.

Howto disable the iptables firewall in Linux

A Linux firewall is software based firewall that provides protection between your server (workstation) and damaging content on the Internet or network.
It will try to guard your computer against both malicious users and software such as viruses/worms.

Task: Disable / Turn off Linux Firewall (Red hat/CentOS/Fedora Core)

Type the following two commands (you must login as the root user):
# /etc/init.d/iptables save
# /etc/init.d/iptables stop

Task: Enable / Turn on Linux Firewall (Red hat/CentOS/Fedora Core)

Type the following command to turn on iptables firewall:
# /etc/init.d/iptables start

Other Linux distribution

If you are using other Linux distribution such as Debian / Ubuntu / Suse Linux etc, try following generic procedure.
Save firewall rules
# iptables-save > /root/firewall.rules
OR
$ sudo iptables-save > /root/firewall.rules
Now type the following commands (login as root):
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -P INPUT ACCEPT
# iptables -P FORWARD ACCEPT
# iptables -P OUTPUT ACCEPT

To restore or turn on firewall type the following command:
# iptables-restore < /root/firewall.rules

GUI tools

If you are using GUI desktop firewall tools such as 'firestarter', use the same tool to stop firewall.
System > Administration > firestarter > Click on Stop Firewall button:
Howto disable the iptables firewall in Debian  / Ubuntu Linux

rand() examples in c++

C++ Syntax (Toggle Plain Text)
  1. #include
  2. #include
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int random_integer = rand();
  9. cout << random_integer << endl;
  10. }
     
     
    C++ Syntax (Toggle Plain Text)
    1. #include
    2. #include
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8. cout << "The value of RAND_MAX is " << RAND_MAX << endl;
    9. }
       
       
       
      C++ Syntax (Toggle Plain Text)
      1. #include
      2. #include
      3. #include
      4.  
      5. using namespace std;
      6.  
      7. int main()
      8. {
      9. srand((unsigned)time(0));
      10. int random_integer = rand();
      11. cout << random_integer << endl;
      12. }
         
         
         
         
        C++ Syntax (Toggle Plain Text)
        1. #include
        2. #include
        3. #include
        4.  
        5. using namespace std;
        6.  
        7. int main()
        8. {
        9. srand((unsigned)time(0));
        10. int random_integer;
        11. for(int index=0; index<20; index++){
        12. random_integer = (rand()%10)+1;
        13. cout << random_integer << endl;
        14. }
        15. }
           
           
           
          C++ Syntax (Toggle Plain Text)
          1. #include
          2. #include
          3. #include
          4.  
          5. using namespace std;
          6.  
          7. int main()
          8. {
          9. srand((unsigned)time(0));
          10. int random_integer;
          11. int lowest=1, highest=10;
          12. int range=(highest-lowest)+1;
          13. for(int index=0; index<20; index++){
          14. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
          15. cout << random_integer << endl;
          16. }
          17. }
             
             
             
             
             
           
         
       
     

rand() in c++

rand

function
int rand ( void );
Generate random number
Returns a pseudo-random integral number in the range 0 to RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in . Its default value may vary between implementations but it is granted to be at least 32767.

A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014

Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

Parameters

(none)

Return Value

An integer value between 0 and RAND_MAX.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* rand example: guess the number */
#include 
#include 
#include 

int main ()
{
  int iSecret, iGuess;

  /* initialize random seed: */
  srand ( time(NULL) );

  /* generate secret number: */
  iSecret = rand() % 10 + 1;

  do {
    printf ("Guess the number (1 to 10): ");
    scanf ("%d",&iGuess);
    if (iSecret"The secret number is lower");
    else if (iSecret>iGuess) puts ("The secret number is higher");
  } while (iSecret!=iGuess);

  puts ("Congratulations!");
  return 0;
}


Output:

Guess the number (1 to 10): 5
The secret number is higher
Guess the number (1 to 10): 8
The secret number is lower
Guess the number (1 to 10): 7
Congratulations!

In this example, the random seed is initialized to a value representing the second in which the program is executed (time is defined in the header ). This way to initialize the seed is generally a good enough option for most randoming needs.

Getting Random Values in C and C++ with Rand

At some point in any programmer's life, he or she must learn how to get a random value, or values, in their program. To some this seems involved, difficult, or even beyond their personal ability. This, however, is simply not the case.
Randomizing of values is, at its most basic form, one of the easier things a programmer can do with the C++ language.

I will first start with an introduction to the idea of randomizing values, followed by a simple example program that will output three random values. Once a secure understanding of these concepts is in place (hopefully it will be), I will include a short program that uses a range of values from which the random values can be taken.
Ok, now that you know why this tutorial was written, and what it includes, you are ready to learn how to randomize values! So without further ado, let's get started, shall we?
Many programs that you will write require the use of random numbers. For example, a game such as backgammon requires a roll of two dice on each move. Since there are 6 numbers on each die, you could calculate each roll by finding a random number from 1 to 6 for each die.
To make this task a little easier, C++ provides us with a library function, called rand that returns an integer between 0 and RAND_MAX. Let's take a break to explain what RAND_MAX is. RAND_MAX is a compiler-dependent constant, and it is inclusive. Inclusive means that the value of RAND_MAX is included in the range of values. The function, rand, and the constant, RAND_MAX, are included in the library header file stdlib.h.
The number returned by function rand is dependent on the initial value, called a seed that remains the same for each run of a program. This means that the sequence of random numbers that is generated by the program will be exactly the same on each run of the program.
How do you solve this problem you ask? Well I'll tell you! To help us combat this problem we will use another function, srand(seed), which is also declared in the stdlib.h header file. This function allows an application to specify the initial value used by rand at program startup.
Using this method of randomization, the program will use a different seed value on every run, causing a different set of random values every run, which is what we want in this case. The problem posed to us now, of course, is how to get an arbitrary seed value. Forcing the user or programmer to enter this value every time the program was run wouldn't be very efficient at all, so we need another way to do it.
So we turn to the perfect source for our always-changing value, the system clock. The C++ data type time_t and the function time, both declared in time.h, can be used to easily retrieve the time on the computers clock.
When converted to an unsigned integer, a positive whole number, the program time (at execution of program) can make a very nice seed value. This works nicely because no two program executions will occur at the same instant of the computers clock.
As promised, here is a very basic example program. The following code was written in Visual C++ 6.0, but should compile fine on most computers (given u have a compiler, which if your reading this I assume you do). The program outputs three random values.
/*Steven Billington
January 17, 2003
Ranexample.cpp
Program displays three random integers.
*/
/*
Header: iostream
Reason: Input/Output stream
Header: cstdlib
Reason: For functions rand and srand
Header: time.h
Reason: For function time, and for data type time_t
*/
#include 
#include 
#include 

using namespace std;

int main()
{
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Output random values.
*/
cout<< rand() << endl;
cout<< rand() << endl;
cout<< rand() << endl;
return 0;
}
Users of a random number generator might wish to have a narrower or a wider range of numbers than provided by the rand function. Ideally, to solve this problem a user would specify the range with integer values representing the lower and the upper bounds. To understand how we might accomplish this with the rand function, consider how to generate a number between 0 and an arbitrary upper bound, referred to as high, inclusive.
For any two integers, say a and b, a % b is between 0 and b - 1, inclusive. With this in mind, the expression rand() % high + 1 would generate a number between 1 and high, inclusive, where high is less than or equal to RAND_MAX, a constant defined by the compiler. To place a lower bound in replacement of 1 on that result, we can have the program generate a random number between 0 and (high - low + 1) + low.

/*
Steven Billington
January 17, 2003
exDice.cpp
Program rolls two dice with random
results.
*/
/*
Header: iostream
Reason: Input/Output stream
Header: stdlib
Reason: For functions rand and srand
Header: time.h
Reason: For function time, and for data type time_t
*/
#include 
#include 
#include 
/*
These constants define our upper
and our lower bounds. The random numbers
will always be between 1 and 6, inclusive.
*/
const int LOW = 1;
const int HIGH = 6;

using namespace std;

int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Get first and second random numbers.
*/
first_die = rand() % (HIGH - LOW + 1) + LOW;
sec_die = rand() % (HIGH - LOW + 1) + LOW;
/*
Output first roll results.
*/
cout<< "Your roll is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
/*
Get two new random values.
*/
first_die = rand() % (HIGH - LOW + 1) + LOW;
sec_die = rand() % (HIGH - LOW + 1) + LOW;
/*
Output second roll results.
*/
cout<< "My roll is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
return 0;
}

Generating Random Numbers

Generating Random Numbers
The key function in generating random numbers is;

 int random (int n);
which generates a random number in the range of 0 to n-1. For example;

 y = random(100);
y will be in the range of 0 though 99.
Note that if you run the following program, again and again, the same three random numbers will be generated. That is, TurboC always seeds the random number generator with the same starting number.
This feature is great for debugging. However, it would terrible if you were designing software for the gaming industry as everyone would know what numbers were going to come up.
Therefore, the function "randomize()" may be used to seed the random number generator with a number which is developed from the system clock, which of course, is always changing.

/*
** Program RANDOM1.C
**
** Generates three random numbers in range of 0 to 99 and
** reports as to which is the largest and which is the smallest.
**
** Intended to show the use of functions randomize(), random() anf the
** use of functions.
**
** Peter H. Anderson, MSU, Feb 6, '97
*/

#include 
#include  /* required for randomize() and random() */
#include   /* required for clrscr() */

int gen_rand(void);   /* note these are declarations of functions */
int find_max(int x, int y, int z);
int find_min(int x, int y, int z);

FILE *f1;

void main(void)
{
   int num1, num2, num3, max, min;

   clrscr();  /* clear the screen */

   f1=fopen("a:\\output.dta", "wt"); /* open a file for output */

   /* randomize(); */  /* uncomment this if you want a random start */

   num1=gen_rand();
   num2=gen_rand();
   num3=gen_rand();

   max=find_max(num1, num2, num3);
   min=find_min(num1, num2, num3);

   printf("Random numbers are %d, %d, and %d\n", num1, num2, num3);
   fprintf(f1, "Random numbers are %d, %d, and %d\n", num1, num2, num3);

   printf("Largest is %d.  Smallest is %d.\n", max, min);
   fprintf(f1, "Largest is %d.  Smallest is %d.\n", max, min);

   fclose(f1);
}

int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
   int n;
   n=random(100);  /* n is random number in range of 0 - 99 */
   return(n);
}

int find_max( int x, int y, int z)
/* returns largest number */
{
   int max;
   if ((x>=y) && (x>=z))
   {
  max = x;
   }
   else if ((y>=x) && (y>=z))
   {
  max = y;
   }
   else
   {
  max = z;
   }
   return(max);
}

int find_min( int x, int y, int z)
/* returns smallest number */
{
   int min;
   if ((x<=y) && (x<=z))
   {
  min = x;
   }
   else if ((y<=x) && (y<=z))
   {
  min = y;
   }
   else
   {
  min = y;
   }
   return(min);
}

How to generate a random number in C?

#include 
#include 

srand(time(NULL));
int r = rand();
 
 
/* random int between 0 and 19 */
int r = rand() % 20;