tags:

views:

151

answers:

1

Hi,

I'm creating a simple web server in Ruby, which display's the text LOLZ in the browser. I have this now:

#!/usr/bin/ruby
require 'socket'

server = TCPServer.open(2000)
loop do

client = server.accept
client.puts "HTTP/1.1 200 OK\r\n"
client.puts "Content-type: text/plain\r\n"
client.puts "\r\n"
client.puts "LOLZ"
client.close

end

This works as expected. However, I want it to work on port 80. Whenever I change 2000 to 80, and start the server using bash, I get this error:

unknown-00-25-4b-8c-b9-b3:rServe koningbaardxiv$ ./rServe.rb
    ./rServe.rb:4:in `initialize': Permission denied - bind(2) (Errno::EACCES)
        from ./rServe.rb:4:in `open'
        from ./rServe.rb:4

Can anyone help me? Thanks

EDIT: I just figured out that this is for all ports within a range of 0 to 999 :S

+4  A: 

The ports below 1024 are reserved (also called well-known ports). You can access them only as root.

$ sudo ./rServe.rb


From http://www.iana.org/assignments/port-numbers:

The port numbers are divided into three ranges: the Well Known Ports, the Registered Ports, and the Dynamic and/or Private Ports.

The Well Known Ports are those from 0 through 1023.

From http://www.linuxquestions.org/linux/articles/Technical/Why_can_only_root_listen_to_ports_below_1024:

I do not blame those who invented the port 1024 limit, it was a natural and important security feature given how UNIX machines were used in the 1970's and 1980's. A typical UNIX machine allowed a bunch of not necessarily fully trusted people to log in and do stuff. You don't want these untrusted users to be able to install a custom daemon pretending to be a well-known service such as telnet or ftp since that could be used to steal passwords and other nasty things.

The MYYN
That did the trick! Thanks!
Time Machine