Latest Post

Showing posts with label IMG. Show all posts
Showing posts with label IMG. Show all posts

Linux Servers

Written By Sajib Barua on Monday, August 20, 2012 | 10:14 PM

Managing Internet Services
The Internet is a world of clients and servers. Clients make requests to servers, and servers respond to the requests. For example, your Web browser is a client that downloads information from Web servers and displays it to you. Of course, the clients and servers are computer programs that run on a wide variety of computers. A Linux system is an ideal system to run different types of servers — from a Web server to a Windows file and print server. This chapter provides an overview of a typical Internet service and its client/server architecture, and discusses how to manage the servers in Linux. You can use the information in this chapter to manage any server running on your Linux system.
Understanding Internet Services
Internet services are network applications designed to deliver information from one system to another. By design, each Internet service is implemented in two parts — a server that provides information and one or more clients that request information.
Such a client/server architecture is the most common way to build distributed information systems. The clients and servers are computer programs that run on these computers and communicate through the network. The neat part is that you can run a client at your desktop computer and access information from a server running on a computer anywhere in the world (as long as it’s on the Internet).
The Web itself, e-mail, and FTP (File Transfer Protocol) are examples of Internet services that use the client/server model. For example, when you use the Web, you use the Web browser client to download and view Web pages from the Web server.
Client/server architecture requires clients to communicate with the servers. That’s where the Transmission Control Protocol/Internet Protocol — TCP/IP — comes in. TCP/IP provides a standard way for clients and servers to exchange packets of data. The next few sections explain how TCP/IP-based services communicate.
TCP/IP and sockets
Client/server applications such as the Web and FTP use TCP/IP for data transfers between client and server. These Internet applications typically use TCP/IP communications utilizing the Berkeley sockets interface (so named because the socket interface was introduced in Berkeley UNIX around 1982). The sockets interface is nothing physical — it’s simply some computer code that a computer programmer can use to create applications that can communicate with other applications on the Internet.
Even if you don’t write network applications using sockets, you may have to use or set up many network applications. Knowledge of sockets can help you understand how network-based applications work, which in turn helps you find and correct any problems with these applications.
Socket definition
Network applications use sockets to communicate over a TCP/IP network. A socket represents one end-point of a connection. Because a socket is bidirectional, data can be sent as well as received through it. A socket has three attributes:
  • The network address (the IP address) of the system
  • The port number, identifying the process (a process is a computer program running on a computer) that exchanges data through the socket
  • The type of socket, identifying the protocol for data exchange
Essentially, the IP address identifies a computer (host) on the network; the port number identifies a process (server) on the node; and the socket type determines the manner in which data is exchanged — through a connectionoriented (stream) or connectionless (datagram) protocol.
Connection-oriented protocols
The socket type indicates the protocol being used to communicate through the socket. A connection-oriented protocol works like a normal phone conversation. When you want to talk to your friend, you have to dial your friend’s phone number and establish a connection before you can have a conversation. In the same way, connection-oriented data exchange requires both the sending and receiving processes to establish a connection before data exchange can begin.
In the TCP/IP protocol suite, TCP — Transmission Control Protocol — supports a connection-oriented data transfer between two processes running on two computers on the Internet. TCP provides reliable two-way data exchange between processes.
As the name TCP/IP suggests, TCP relies on IP — Internet Protocol — for delivery of packets. IP does not guarantee delivery of packets; nor does it deliver packets in any particular sequence. IP does, however, efficiently move packets from one network to another. TCP is responsible for arranging the packets in the proper sequence, detecting whether errors have occurred, and requesting retransmission of packets in case of an error.
TCP is useful for applications intended to exchange large amounts of data at a time. In addition, applications that need reliable data exchange use TCP. (For example, FTP uses TCP to transfer files.)
In the sockets model, a socket that uses TCP is referred to as a stream socket.
Connectionless protocols
A connectionless data-exchange protocol does not require the sender and receiver to explicitly establish a connection. It’s like shouting to your friend in a crowded room — you can’t be sure that your friend hears you.
In the TCP/IP protocol suite, the User Datagram Protocol (UDP) provides connectionless service for sending and receiving packets known as datagrams. Unlike TCP, UDP does not guarantee that datagrams ever reach their intended destinations. Nor does UDP ensure that datagrams are delivered in the order they’re sent.
UDP is used by applications that exchange small amounts of data at a time or by applications that don’t need the reliability and sequencing of data delivery. For example, SNMP (Simple Network Management Protocol) uses UDP to transfer data.
In the sockets model, a socket that uses UDP is referred to as a datagram socket.
Sockets and the client/server model
Two sockets are needed to complete a communication path. When two processes communicate, they use the client/server model to establish the connection. Figure 1-1 illustrates the concept. The server application listens on a specific port on the system — the server is completely identified by the IP address of the system where it runs and the port number where it listens for connections. The client initiates a connection from any available port and tries to connect to the server (identified by the IP address and port number).
When the connection is established, the client and the server can exchange data according to their own protocol.
Client and server processes use two sockets to communicate
Figure 1-1: Client and server processes use two sockets to communicate.
The sequence of events in socket-based data exchanges depends on whether the transfer is connection-oriented (TCP) or connectionless (UDP).
For a connection-oriented data transfer using sockets, the server listens on a specific port, waiting for clients to request connection. Data transfer begins only after a connection is established.
For connectionless data transfers, the server waits for a datagram to arrive at a specified port. The client does not wait to establish a connection; it simply sends a datagram to the server.
Regardless of whether it’s a server or a client, each application first creates a socket. Then it associates (binds) the socket with the local computer’s IP address and a port number. The IP address identifies the machine (where the application is running), and the port number identifies the application using the socket.
Servers typically listen to a well-known port number so that clients can connect to that port to access the server. For a client application, the process of binding a socket to the IP address and port is the same as that for a server, but the client can use 0 as the port number — the sockets library automatically uses an unused port number for the client.
For a connection-oriented stream socket, the communicating client and server applications have to establish a connection. The exact steps for establishing a connection depend on whether the application is a server or a client.
In the client/server model, the server has to be up and running before the client can run. After creating a socket and binding the socket to a port, the server application sets up a queue of connections, which determines how many clients can connect to the server. Typically, a server listens to anywhere from one to five connections. However, the size of the listen queue is one of the parameters you can adjust (especially for a Web server) to ensure that the server responds to as many clients as possible. After setting up the listen queue, the server waits for a connection from a client.
Establishing the connection from the client side is somewhat simpler. After creating a socket and binding the socket to an IP address, the client establishes a connection with the server. To make the connection, the client must know the host name or IP address of the server, as well as the port on which the server accepts connection. All Internet services have well-known standard port numbers.
After a client establishes a connection to a server via a connection-oriented stream socket, the client and server can exchange data by calling the appropriate sockets’ API functions. Like a conversation between two persons, the server and client alternately send and receive data — the meaning of the data depends on the message protocol that the server and clients use. Usually, a server is designed for a specific task; inherent in that design is a message protocol that the server and clients use to exchange necessary data. For example, the Web server and the Web browser (client) communicate using HTTP (HyperText Transfer Protocol).
Internet services and port numbers
The TCP/IP protocol suite is the lingua franca of the Internet because the Internet services speak TCP/IP. These services make the Internet tick by making possible the transfer of mail, news, and Web pages. Each Internet service has its own protocol that relies on TCP/IP for the actual transfer of the information. Each service also has one or more assigned port numbers that it uses to do whatever it’s designed to do. Here are some well-known Internet services and their associated protocols:
  • DHCP (Dynamic Host Configuration Protocol): Dynamically configures TCP/IP network parameters on a computer. DHCP is used, primarily, to assign dynamic IP addresses and other networking information such as name server, default gateway, and domain names that are needed to configure TCP/IP networks. The DHCP server listens on port 67.
  • FTP (File Transfer Protocol): Transfers files between computers on the Internet. FTP uses two ports: Data is transferred on port 20, and control information is exchanged on port 21.
  • HTTP (HyperText Transfer Protocol): Sends documents from one system to another. HTTP is the underlying protocol of the Web. By default, the Web server and client communicate on port 80.
  • SMTP (Simple Mail Transfer Protocol): Exchanges e-mail messages between systems. SMTP uses port 25 for information exchange.
  • NNTP (Network News Transfer Protocol): Distributes news articles in a store-and-forward fashion across the Internet. NNTP uses port 119.
  • SSH (Secure Shell): Enables secure remote login and other secure network services over an insecure network. SSH uses port 22.
  • TELNET: Enables a user on one system to log into another system on the Internet. (The user must provide a valid user ID and password to log into the remote system.) TELNET uses port 23 by default, but the TELNET client can connect to any specified port.
  • NFS (Network File System): Shares files among computers. NFS uses Sun’s Remote Procedure Call (RPC) facility, which exchanges information through port 111.
  • NTP (Network Time Protocol): Synchronizes the system time on a client computer with that on a server with a more accurate clock. NTP uses port 123.
  • SNMP (Simple Network Management Protocol): Manages all types of network devices on the Internet. Like FTP, SNMP uses two ports: 161 and 162.
  • TFTP (Trivial File Transfer Protocol): Transfers files from one system to another. (It’s typically used by X terminals and diskless workstations to download boot files from another host on the network.) TFTP data transfer takes place on port 69.
Each service is provided by a server process — a computer program that runs on a system awaiting client requests that arrive at the well-known port associated with its service. Thus, the Web server expects client requests at port 80, the standard port for HTTP service.
The /etc/services text file on your Linux system stores the association between a service name and a port number (as well as a protocol). Here is a small subset of entries in the /etc/services file from a Linux system:
ftp-data 20/tcp
ftp 21/tcp
fsp 21/udp fspd
ssh 22/tcp # SSH Remote Login Protocol
ssh 22/udp
telnet 23/tcp
smtp 25/tcp mail
time 37/tcp timserver
time 37/udp timserver
rlp 39/udp resource # resource location
nameserver 42/tcp name # IEN 116
whois 43/tcp nicname
tacacs 49/tcp # Login Host Protocol (TACACS)

A quick look through the entries in the /etc/services file shows the breadth of networking services available under TCP/IP.
Port number 80 is designated for Web services. In other words, if you set up a Web server on your system, that server listens to port 80. By the way, IANA — the Internet Assigned Numbers Authority (www.iana.org) — is the organization responsible for coordinating the assignment of port numbers below 1,024.
Using the Internet Super Server
The client/server architecture of Internet services requires that the server be up and running before a client makes a request for service. It’s probably a bad idea to run all the servers all the time — doing so is impractical because each server process uses up system resources in the form of memory and processor time. Besides, you don’t really need all the services up and ready at all times. Instead, run a single server that listens to all the ports and then starts the appropriate server when a client request comes in. Such a server is known as the Internet super server because it starts various services on demand.
The two Internet super servers are inetd and xinetd. The inetd server is the older one and is still used in some Linux distributions such as Debian, Knoppix, Ubuntu, and Xandros. The xinetd server is a replacement for inetd, offering improved access control and logging. The name xinetd stands for extended inetd. Distributions such as Fedora and SUSE use xinetd.
Using inetd
In Linux distributions that use inetd, the system starts inetd when the system boots. The inetd server reads a configuration file named /etc/ inetd.conf at startup. This file tells inetd which ports to listen to and what server to start for each port. For example, the entry in the /etc/ inetd.conf file that starts the IMAP (Internet Message Access Protocol) on one server looks like this:
imaps stream tcp nowait root /usr/sbin/tcpd /usr/sbin/imapd
The first item on this line, imaps, tells inetd the name of the service. inetd uses this name to look up the port number from the /etc/services file. If you type grep imaps /etc/services, you find that the port number of the IMAP service is 993. This tells inetd to listen to port 993 for FTP service requests.
The rest of the fields on the IMAP entry have the following meanings:
  • The second and third fields of the entry, stream and tcp, tell inetd that the FTP service uses a connection-oriented TCP socket to communicate with the client. For services that use the connectionless UDP sockets, these two fields are dgram and udp.
  • The fourth field, nowait, tells inetd to start a new server for each request. If this field is wait, inetd waits until the server exits before starting the server again.
  • The fifth field provides the user ID that inetd uses to run the server. In this case, the server runs the FTP server as root.
  • The sixth field specifies the program to run for this service and the last field is the argument that inetd passes to the server program. In this case, the /usr/sbin/tcpd program is provided /usr/sbin/imapd as an argument.
The /usr/sbin/tcpd program is an access control facility, or a TCP wrapper, for Internet services. Because unnecessary Internet services are often the sources of security vulnerabilities, you may want to turn off any unneeded services or at least control access to the services. The tcpd program can start other services, such as FTP and TELNET, but before starting the service, tcpd consults the /etc/hosts.allow file to see if the host requesting service is allowed that service. If nothing is in /etc/hosts. allow about that host, tcpd checks the /etc/hosts.deny file to see if the service should be denied. If both files are empty, tcpd allows the host access to the requested service. You can place the line ALL:ALL in the /etc/hosts.deny file to deny all hosts access to any Internet services.
Browse through the /etc/inetd.conf file on your system to find out the kinds of services that inetd is set up to start. Nowadays, most inetd services are turned off, and many others, such as FTP, are started by standalone servers. In any case, if you should see any services that you want to turn off, simply place a hash mark (#) at the beginning of the lines that start these services. When you make such a change to the /etc/inetd.conf file, type /etc/init.d/inetd restart to restart the inetd server.
Using xinetd
Linux distributions that use xinetd start xinetd when the system boots. The xinetd server reads a configuration file named /etc/xinetd.con fat startup. This file tells xinetd which ports to listen to and what server to start for each port. The file can contain instructions that include other configuration files. In Linux, the /etc/xinetd.conf file looks like the following:
# Simple configuration file for xinetd
#
# Set some defaults and include /etc/xinetd.d/
defaults
{
instances = 30
log_type = FILE /var/log/xinetd.log
log_on_success = HOST EXIT DURATION
log_on_failure = HOST ATTEMPT
cps = 50 10
}
includedir /etc/xinetd.d

Comment lines begin with the hash mark (#). The defaults block of attributes, enclosed in curly braces ({ . . . }), specifies default values for some attributes. These default values apply to all other services in the configuration file. The instances attribute is set to 30, which means that no more than 30 servers can be simultaneously active for any service.
last line in the /etc/xinetd.conf file uses the includedir directive to include all files inside the /etc/xinetd.d directory, excluding files that begin with a period (.). The idea is that the /etc/xinetd.d directory contains all service-configuration files — one file for each type of service the xinetd server is expected to manage. Type ls /etc/xinetd.d to see the xinetd configuration files for your system. Each file in /etc/xinetd.d specifies attributes for one service that xinetd can start.
For example, SUSE Linux uses xinetd to start some services, including the vsftpd (Very Secure FTP daemon) server. (A daemon is a process that runs continuously and never dies.) Type cat /etc/xinetd.d/vsftpd to see the xinetd configuration for the vsftpd service. Here’s a typical listing of that file on a SUSE system:
# default: off
# description:
# The vsftpd FTP server serves FTP connections. It uses
# normal, unencrypted usernames and passwords for authentication.
# vsftpd is designed to be secure.
service ftp
{
socket_type = stream
protocol = tcp
wait = no
user = root
server = /usr/sbin/vsftpd
}

The filename (in this case, vsftpd) can be anything; what matters is the service name that appears next to the service keyword in the file. In this case, the line service ftp tells xinetd the name of the service. xinetd uses this name to look up the port number from the /etc/services file.
The attributes in /etc/xinetd.d/vsftpd, enclosed in curly braces ({ . . . }), have the following meanings:
  • The socket_type attribute is set to stream, which tells xinetd that the FTP service uses a connection-oriented TCP socket to communicate with the client. For services that use the connectionless UDP sockets, this attribute is set to dgram.
  • The wait attribute is set to no, which tells xinetd to start a new server for each request. If this attribute is set to yes, xinetd waits until the server exits before starting the server again.
  • The user attribute provides the user ID that xinetd uses to run the server. In this case, the server runs the TELNET server as root.
  • The server attribute specifies the program to run for this service. In this case, xinetd runs the /usr/sbin/vsftpd program to provide the FTP service.
Browse through the files in the /etc/xinetd.d directory on your Linux system to find out the kinds of services xinetd is set up to start. If you want to turn off any service (many are already disabled), you can do so by editing the configuration file for that service and adding the following line inside the curly braces that enclose all attributes:
disable = yes
When you make such a change to the xinetd configuration files, you must restart the xinetd server by typing the following command:
/etc/init.d/xinetd restart
You can typically configure services to run under xinetd or as a standalone service. For example, SUSE starts the Very Secure FTP daemon (vsftpd) under the control of xinetd. Debian and Fedora, however, run vsftpd as a standalone server.
Running Standalone Servers
Starting servers through inetd or xinetd is a smart approach but not always efficient. A Web server controlled by inetd or xinetd, would be started often because every time a user clicks a link on a Web page, a request arrives for the Web service. For such high-demand services, starting the server in a standalone manner is best. In standalone mode, the server can run as a daemon. That means the server listens on the assigned port, and whenever a request arrives, the server handles it by making a copy of itself. In this way, the server keeps running as long as the machine is running — in theory, forever. A more efficient strategy, used for Web servers, is to run multiple copies of the server and let each copy handle some of the incoming requests.
You can easily configure your Linux system to start various standalone servers automatically, as shown in this section.
Starting and stopping servers manually
To start a service that’s not running, use the server command. For example, if the Web server (called httpd in Fedora) isn’t running, you can start it by issuing a special shell script with the following command:
/etc/init.d/httpd start
That command runs the /etc/init.d/httpd script with start as the argument. If the httpd server is already running and you want to stop it, run the same command with stop as the argument, like this:
/etc/init.d/httpd stop
To stop and start a server again, just use restart as the argument:
/etc/init.d/httpd restart
You can also check the status of any service by using status as the argument:
/etc/init.d/httpd status
In Debian, Ubuntu, and SUSE, where the Web server program is called apache2, type /etc/init.d/apache2 start to start the Web server. In Knoppix and Xandros, type /etc/init.d/apache start. Use that same command with arguments stop or restart to stop the Web server or restart it.
What are all the services that you can start and stop? Well, the answer is in the files in the /etc/init.d directory. To get a look at it, type the following command:
ls /etc/init.d
All the files you see listed in response to this command are the services installed on your Linux system — and you can start and stop them as needed. You typically find 65 to 70 services listed in the /etc/init.d directory.
Starting servers automatically at boot time
You can start, stop, and restart servers manually by using the scripts in the /etc/init.d directory, but you want some of the services to start as soon as you boot the Linux system. You can configure servers to start automatically at boot time by using a graphical server-configuration utility or a command.
The command for configuring services to start automatically depends on the distribution. In Debian, Knoppix, Ubuntu, and Xandros, use the updaterc.d command. In Fedora and SUSE, use the chkconfig command. Both commands are explained in the following sections.
Using the chkconfig command in Fedora and SUSE
The chkconfig program is a command-line utility in Fedora and SUSE for checking and updating the current setting of servers in Linux. Various combinations of servers are set up to start automatically at different run levels. Each run level represents a system configuration in which a selected set of processes runs. You’re usually concerned about run levels 3 and 5 because run level 3 is for text mode login and run level 5 is for logging in through a graphical interface.
The chkconfig command is simple to use. For example, suppose that you want to automatically start the named server at run levels 3 and 5. All you have to do is log in as root and type the following command at the shell prompt:
chkconfig --level 35 named on
To see the status of the named server, type the following command:
chkconfig --list named
You see a line of output similar to the following:
named 0:off 1:off 2:off 3:on 4:off 5:on 6:off
The output shows you the status of the named server at run levels 0 through 6. As you can see, named is set to run at run levels 3 and 5.
If you want to turn off named, you can do so with this command: chkconfig --level 35 named off
You can use chkconfig to see the status of all services, including the ones started through xinetd. For example, you can view the status of all services by typing the following command:
chkconfig --list | more
The output shows the status of each service for each of the run levels from 0 through 6. For each run level, the service is either on or off. At the very end of the listing, chkconfig displays a list of the services that xinetd controls. Each xinetd-based service is also marked on or off, depending on whether xinetd is configured to start the service.
Using the update-rc.d command in Debian, Knoppix, Ubuntu, and Xandros
In Debian, Knoppix, Ubuntu, and Xandros, you can use the update-rc.d command to set up services that should start when the system boots at specific boot levels. The easiest way to set up is to use the defaults option in a command of this form:
update-rc.d service defaults
where service is the name of the script file in the /etc/init.d directory that starts and stops the service, among other things.
you use the defaults option, update-rc.d sets up symbolic links — shortcuts, in other words — to start the service in run levels 2, 3, 4, and 5 and stop the service in run levels 0, 1, and 6. A sequence number controls the order in which each service is started. (Services with smaller sequence numbers start before those with larger sequence numbers, and the numbers typically range from 00 through 99.) If you do not specify a sequence number explicitly, update-rc.d uses a sequence number of 20 when you use the defaults option.
You can also start and stop a service at specific run levels as well as in a specific sequence. For example, to start a service at run levels 2 and 5 at a sequence number of 85 and stop it at run levels 0, 1, and 6 at a sequence of 90, use the following command:
update-rc.d service start 85 2 5 . stop 90 0 1 6 .
Remember that service must be the name of a script file in the /etc/ init.d directory.
If you need to stop a service from starting at system startup, type updaterc.d -f service remove in a terminal window, where service is the name of the script file in /etc/init.d that starts or stops that service.
Using a GUI service configuration utility
If you don’t like typing commands, you may be able to use a GUI tool to configure the services. Fedora and SUSE include such tools to manage the services.
In Fedora, choose System»System Settings»Service Manager from the KDE desktop and enter the root password when prompted. You can then turn on or off services from the Service Configuration window, as shown in Figure 1-2.
In Fedora, this is where you set services to start automatically at boot time Figure 1-2: In Fedora, this is where you set services to start automatically at boot time.
The Service Configuration utility shows the names of services in a scrolling list. Each line in the list shows the name of a service with a check box in front of the name. A check mark in the box indicates that the service is selected to start at boot time for the current run level. When the dialog box first appears, many services are already selected.
You can scroll up and down the list and click the check box to select or deselect a service. If you click the check box, the check mark alternately turns on and off. Additionally, the utility also shows you whether the selected service is currently running.
After you select all the servers you want to start when the system boots, click the Apply button on the toolbar to save the changes.
By default, the service configuration utility configures the selected services for the current run level. That means if you’re selecting services from the graphical desktop, the system is in run level 5 and the services you configure are set to start at run level 5. If you want to set up the services for a different level, select that run level from the Edit Runlevel menu.
In SUSE, you can configure the services from YaST Control Center by opening the main menu and choosing System➪Control Center (YaST). Click System in the left window and then click System Services (Runlevel). YaST opens the System Services (Runlevel) window, as shown in Figure 1-3. To enable or disable services at specific run levels, click the Expert Mode button and edit the services in the new list that appears.
In SUSE, use this YaST window to specify services that start at boot time Figure 1-3: In SUSE, use this YaST window to specify services that start at boot time.
Table 1-1 shows a list of the services, along with a brief description of each one. The first column shows the name of the service, which is the same as the name of the program that has to run to provide the service. You may not see all these services listed when you run the GUI service configuration utility on your system because the exact list of services depends on what is installed on your Linux system.
Table 1-1
Some Common Services in Linux
Service Name
Description
Acpid
Listens to Advanced Configuration and Power Interface(ACPI) events from the kernel and notifies other programs when such events occur. ACPI events can occur when the kernel puts the computer into a low-power state (for example, standby mode) to save energy.
apache, apache2, or httpd
Runs the Apache World Wide Web (WWW) server.
Apmd
Monitors the Advanced Power Management (APM) BIOS and logs the status of electric power (AC or battery backup).
Atd
Runs commands scheduled by the at and cron commands.
Autofs
Automatically mounts file systems (for example, when you insert a CD-ROM in the CD-ROM drive).
cron or crond
Runs user-specified programs according to a periodic schedule set by the crontab command.
Gpm
Enables the use of the mouse in text mode screens.
Innd
The InterNetNews daemon — the Internet news server you can use to support local newsgroups on your system.
Isdn
Starts and stops ISDN (Integrated Services Digital Network) services — a digital communication service over regular phone lines (enable only if you have ISDN service).
Named
Translates host names into IP addresses. named is a server for the Domain Name System (DNS). You can run a copy on your system if you want.
network or networking
Enables you to activate or deactivate all network interfaces configured to start at system boot time.
nfs or nfsserver
Enables sharing of file systems specified in the /etc/ exports file using the Network File System (NFS) protocol.
Nfslock
Provides file-locking capability for file systems exported using the Network File System (NFS) protocol, so other systems (running NFS) can share files from your system.
Pcmcia
Provides support for PCMCIA devices.
Portmap
Works with any software that relies on Remote Procedure Calls (RPC). For example, NFS requires the portmap service.
samba, smb, or smbfs
Starts and stops the Samba smbd and nmbd services, which support LAN Manager services on a Linux system.
Sendmail
Moves mail messages from one machine to another. Start this service if you want to send mail from your Linux system. If you don’t plan to use your Linux system as a mail server, don’t start the sendmail server because it can slow down the booting process and consume unnecessary resources.
Snmpd
Manages networks. snmpd stands for Simple Network Management Protocol daemon.
Spamassassin
Runs spamd — the SpamAssassin mail filter program.
ssh or sshd
Runs the OpenSSH (Secure Shell) secure remote login facility.
syslog or syslogd
Logs various error and status messages in a log file (usually, the /var/log/messages file). This service is used by many other programs (including other services). Always run this service.
vsftpd
Transfers files using the File Transfer Protocol (FTP). vsftpd stands for Very Secure FTP daemon.
winbind
Starts and stops the Samba winbindd server, which provides a name-switch capability similar to that provided by the /etc/nsswitch.conf file.
xfs
Starts and stops the X font server.
xinetd
Starts other Internet services, such as TELNET and FTP, whenever they are needed. This server is the Internet super server, a replacement for the older inetd.
ypbind
Runs on Network Information System (NIS) clients and binds the clients to an NIS domain. You don’t have to start ypbind unless you’re using NIS.
next Managing Mail and News Servers in Linux

Using Secure Shell (SSH) for Remote Logins

Linux comes with the Open Secure Shell (OpenSSH) software, a suite of programs that provides a secure replacement for the Berkeley r commands: rlogin (remote login), rsh (remote shell), and rcp (remote copy). OpenSSH uses public key cryptography to authenticate users and to encrypt the communication between two hosts, so users can securely log in from remote systems and copy files securely.
This section briefly describes how to use the OpenSSH software in Linux. To find out more about OpenSSH and read the latest news about it, visit www. openssh.com or www.openssh.org.
The OpenSSH software is installed during Linux installation. Table 2-2 lists the main components of the OpenSSH software.
Table 2-2
Components of the OpenSSH Software
Component
Description
/usr/sbin/sshd
This Secure Shell daemon must run on a host if you want users on remote systems to use the ssh client to log in securely. When a connection from the ssh client arrives, sshd performs authentication using public key cryptography and establishes an encrypted communication link with the ssh client.
/usr/bin/ssh
Users can run this Secure Shell client to log in to a host that is running sshd. Users can also use ssh to execute a command on another host.
/usr/bin/slogin
This component is a symbolic link to /usr/bin/ssh
/usr/bin/scp
This secure-copy program works like rcp but securely. The scp program uses ssh for data transfer and provides the same authentication and security as ssh.
/usr/bin/ssh-keygen
You use this program to generate the public and private key pairs you need for the public key cryptography used in OpenSSH. The ssh-keygen program can generate key pairs for both RSA and DSA (Digital Signature Algorithm) authentication. (RSA comes from the initial of the last Book VI name of Ron Rivest, Adi Shamir, and Leonard Adleman — Chapter 2 the developers of the RSA algorithm.)
/etc/ssh/sshd_config
This configuration file for the sshd server specifies many parameters for sshd, including the port to listen to, the protocol to use, and the location of other files. (There are two versions of SSH protocols: SSH1 and SSH2, both supported by OpenSSH.)
/etc/ssh/ ssh_config
This configuration file is for the ssh client. Each user can also have an ssh configuration file named config in the .ssh subdirectory of the user’s home directory.
OpenSSH uses public key encryption, in which the sender and receiver both have a pair of keys — a public key and a private key. The public keys are freely distributed, and each party knows the other’s public key. The sender encrypts data by using the recipient’s public key. Only the recipient’s private key can then decrypt the data.
To use OpenSSH, you first need to start the sshd server and then generate the host keys. Here’s how:
  • If you want to support SSH-based remote logins on a host, start the sshd server on your system. Type ps ax | grep sshd to see if the server is already running. If not, log in as root and turn on the SSH service.
    In Fedora and SUSE, type chkconfig --level 35 sshd on. In Debian and Xandros, type update-rc.d ssh defaults. To start the sshd server immediately, type /etc/init.d/ssh start in Debian and Xandros, or type /etc/init.d/sshd start in Fedora and SUSE.
  • Generate the host keys with the following command:
    ssh-keygen -d -f /etc/ssh/ssh_host_key -N ‘’
    The -d flag causes the ssh-keygen program to generate DSA keys, which the SSH2 protocol uses. If you see a message saying that the file /etc/ssh/ssh_host_key already exists, that means that the key pairs were generated during Linux installation. You can use the existing file without having to regenerate the keys.

A user who wants to log in using SSH can simply use the ssh command. For example:
ssh 192.168.0.4 -l kdulaney
where 192.168.0.4 is the IP address of the other Linux system. SSH then displays a message:
The authenticity of host ‘192.168.0.4 (192.168.0.4)’ can’t be established.
RSA key fingerprint is 7b:79:f2:dd:8c:54:00:a6:94:ec:fa:8e:7f:c9:ad:66.
Are you sure you want to continue connecting (yes/no)?
Type yes and press Enter. SSH then adds the host to its list of known hosts and prompts you for a password on the other Linux system:
kdulaney@192.168.0.4’s password:
After entering the password, you have a secure login session with that system. You can also log in to this account with the following equivalent command:
ssh kdulaney@192.168.0.4
If you simply want to copy a file securely from another system on the LAN (identified by its IP address, 192.168.0.4), you can use scp like this:
scp 192.168.0.4:/etc/X11/xorg.conf
This command prompts for a password and securely copies the /etc/X11/ xorg.conf file from the 192.168.0.4 host to the system from which the scp command was typed, as follows:
kdulaney@192.168.0.4’s password: (type the password.)
xorg.conf 100% 2814 2.8KB/s 00:00

Setting Up Simple Firewalls
A firewall is a network device or host with two or more network interfaces — one connected to the protected internal network and the other connected to unprotected networks, such as the Internet. The firewall controls access to and from the protected internal network.
If you connect an internal network directly to the Internet, you have to make sure that every system on the internal network is properly secured — which can be nearly impossible because a single careless user can render the entire internal network vulnerable. A firewall is a single point of connection to the Internet: You can direct all your efforts toward making that firewall system a daunting barrier to unauthorized external users. Essentially, a firewall is like a protective fence that keeps unwanted external data and software out and sensitive internal data and software in. (See Figure 2-3.)
A firewall protects hosts on a private network from the Internet
Figure 2-3: A firewall protects hosts on a private network from the Internet.
The firewall runs software that examines the network packets arriving at its network interfaces and takes appropriate action based on a set of rules. The idea is to define these rules so that they allow only authorized network traffic to flow between the two interfaces. Configuring the firewall involves setting up the rules properly. A configuration strategy is to reject all network traffic and then enable only a limited set of network packets to go through the firewall. The authorized network traffic would include the connections necessary to enable internal users to do things such as visit Web sites and receive electronic mail.
To be useful, a firewall has the following general characteristics:
  • It must control the flow of packets between the Internet and the internal network.
  • It must not provide dynamic routing because dynamic routing tables are subject to route spoofing — the use of fake routes by intruders. Instead, the firewall uses static routing tables (which you can set up with the route command on Linux systems).
  • It must not allow any external user to log in as root. That way, even if the firewall system is compromised, the intruder is blocked from using root privileges from a remote login.
  • It must be kept in a physically secure location.
  • It must distinguish between packets that come from the Internet and packets that come from the internal protected network. This feature allows the firewall to reject packets that come from the Internet but have the IP address of a trusted system on the internal network.
  • It acts as the SMTP mail gateway for the internal network. Set up the sendmail software so that all outgoing mail appears to come from the firewall system.
  • Its user accounts are limited to a few user accounts for those internal users who need access to external systems. External users who need access to the internal network should use SSH for remote login (see “Using Secure Shell (SSH) for Remote Logins,” earlier in this chapter).
  • It keeps a log of all system activities, such as successful and unsuccessful login attempts.
  • It provides DNS name-lookup service to the outside world to resolve any hostnames that are known to the outside world.
  • It provides good performance so that it doesn’t hinder the internal users’ access to specific Internet services (such as HTTP and FTP).
A firewall can take many different forms. Here are three common forms of a firewall:
  • Packet filter firewall: This simple firewall uses a router capable of filtering (blocking or allowing) packets according to a number of their characteristics, including the source and destination IP addresses, the network protocol (TCP or UDP), and the source and destination port numbers. Packet filter firewalls are usually placed at the outermost boundary with an untrusted network, and they form the first line of defense. An example of a packet filter firewall is a network router that employs filter rules to screen network traffic.
    Packet filter firewalls are fast and flexible, but they can’t prevent attacks that exploit application-specific vulnerabilities or functions. They can log only a minimal amount of information, such as source IP address, destination IP address, and traffic type. Also, they’re vulnerable to attacks and exploits that take advantage of flaws within the TCP/IP protocol, such as IP address spoofing, which involves altering the address information in network packets to make them appear to come from a trusted IP address.
  • Stateful inspection firewall: This type of firewall keeps track of the network connections that network applications are using. When an application on an internal system uses a network connection to create a session with a remote system, a port is also opened on the internal system. This port receives network traffic from the remote system. For successful connections, packet filter firewalls must permit incoming packets from the remote system. Opening up many ports to incoming traffic creates a risk of intrusion by unauthorized users who abuse the expected conventions of network protocols such as TCP. Stateful inspection firewalls solve this problem by creating a table of outbound network connections, along with each session’s corresponding internal port. This “state table” is then used to validate any inbound packets. This stateful inspection is more secure than a packet filter because it tracks internal ports individually rather than opening all internal ports for external access.
  • Application-proxy gateway firewall: This firewall acts as an intermediary between internal applications that attempt to communicate with external servers such as a Web server. For example, a Web proxy receives requests for external Web pages from Web browser clients running inside the firewall and relays them to the exterior Web server as though the firewall was the requesting Web client. The external Web server responds to the firewall, and the firewall forwards the response to the inside client as though the firewall was the Web server. No direct network connection is ever made from the inside client host to the external Web server.
    Application-proxy gateway firewalls have some advantages over packet filter firewalls and stateful inspection firewalls. First, application-proxy gateway firewalls examine the entire network packet rather than only the network addresses and ports. This enables these firewalls to provide more extensive logging capabilities than packet filters or stateful inspection firewalls. Another advantage is that application-proxy gateway firewalls can authenticate users directly whereas packet filter firewalls and stateful inspection firewalls normally authenticate users based on the IP address of the system (that is, source, destination, and protocol type). Given that network addresses can be easily spoofed, the authentication capabilities of application-proxy gateway firewalls are superior to those found in packet filter and stateful inspection firewalls.The advanced functionality of application-proxy gateway firewalls, however, results in some disadvantages when compared with packet filter or stateful inspection firewalls. First, because of the full packet awareness found in application-proxy gateways, the firewall is forced to spend significant time reading and interpreting each packet. Therefore, application-proxy gateway firewalls are generally not well suited to highbandwidth or real-time applications. To reduce the load on the firewall, a dedicated proxy server can be used to secure less time-sensitive services, such as e-mail and most Web traffic. Another disadvantage is that application-proxy gateway firewalls are often limited in terms of support for new network applications and protocols. An individual applicationspecific proxy agent is required for each type of network traffic that needs to go through the firewall. Most vendors of application-proxy gateways provide generic proxy agents to support undefined network protocols or applications. However, those generic agents tend to negate many of the strengths of the application-proxy gateway architecture, and they simply allow traffic to tunnel through the firewall.
Most firewalls implement a combination of these firewall functionalities. For example, many vendors of packet filter firewalls or stateful inspection firewalls have also implemented basic application-proxy functionality to offset some of the weaknesses associated with their firewalls. In most cases, these vendors implement application proxies to provide better logging of network traffic and stronger user authentication. Nearly all major firewall vendors have introduced multiple firewall functions into their products in some manner.
In a large organization, you may also have to isolate smaller internal networks from the corporate network. You can set up such internal firewalls the same way that you set up Internet firewalls.
Using NAT
Network Address Translation (NAT) is an effective tool that enables you to hide the network addresses of an internal network behind a firewall. In essence, NAT allows an organization to use private network addresses behind a firewall while maintaining the ability to connect to external systems through the firewall.
Here are the three methods for implementing NAT:
  • Static: In static NAT, each internal system on the private network has a corresponding external, routable IP address associated with it. This particular technique is seldom used because unique IP addresses are in short supply.
  • Hiding: With hiding NAT, all systems behind a firewall share the same external, routable IP address, while the internal systems use private IP addresses. Thus, with a hiding NAT, a number of systems behind a firewall still appear to be a single system.
  • Port address translation: With port address translation, you can place hosts behind a firewall system and still make them selectively accessible to external users.
In terms of strengths and weaknesses, each type of NAT — static, hiding, or port address translation — is applicable in certain situations; the variable is the amount of design flexibility offered by each type. Static NAT offers the most flexibility, but it’s not always practical because of the shortage of IP addresses. Hiding NAT technology is seldom used because port address translation offers additional features. Port address translation is often the most convenient and secure solution.
Enabling packet filtering on your Linux system
The Linux kernel has built-in packet filtering software in the form of something called netfilter. You use the iptables command to set up the rules for what happens to the packets based on the IP addresses in their header and the network connection type.
To find out more about netfilter and iptables, visit the documentation section of the netfilter Web site at www.netfilter.org/documentation.
The built-in packet filtering capability is handy when you don’t have a dedicated firewall between your Linux system and the Internet. This is the case, for example, when you connect your Linux system to the Internet through a DSL or cable modem. Essentially, you can have a packet filtering firewall inside your Linux system, sitting between the kernel and the applications.
Using the security level configuration tool
Some Linux distributions, such as Fedora and SUSE, include GUI tools to turn on a packet filtering firewall.
In Fedora, you can turn on different levels of packet filtering through the graphical Firewall Configuration tool. To run the tool, log in as root and choose System»Administration»Firewall. The Firewall Configuration window appears (see Figure 2-4) along with an authentication window.
In Fedora, you can configure the firewall with this toolFigure 2-4: In Fedora, you can configure the firewall with this tool.
From the Firewall Configuration dialog box, you can select two predefined levels of simple firewalling (more precisely, packet filtering):
  • Disabled: This option doesn’t perform any filtering and allows all connections. (You can still turn off Internet services by not running the servers or disabling them in the xinetd configuration files.) This security level is fine if your Linux system is inside a protected local area network or if you have a separate firewall device.
  • Enabled: This option turns on packet filtering. You can then select the services that you want to allow and the network devices that you trust.
You can allow incoming packets meant for specific Internet services such as SSH, TELNET, and FTP. If you select a network interface such as eth0 (the first Ethernet card) as trusted, all network traffic over that interface is allowed without any filtering.
In SUSE, to set up a firewall, choose Main Menu»System»YaST. In the YaST Control Center window that appears, click Security and Users on the left side of the window and then click Firewall on the right side. YaST opens a window that you can use to configure the firewall.
You can designate network interfaces (by device name, such as eth0, ppp0, and so on) to one of three zones: internal, external, or demilitarized zone. Then for that zone, you can specify what services (such as HTTP, FTP, and SSH) are allowed. If you have two or more network interfaces and you use the Linux system as a gateway (a router), you can enable forwarding packets between network interfaces (a feature called masquerading). You can also turn on different levels of logging (for example, logging all dropped packets that attempt connection at specific ports). If you make changes to firewall settings, click the Startup category and click Save Settings and Restart Firewall Now.
Using the iptables command
The GUI firewall configuration tools use the iptables command to implement the firewall. If your Linux system doesn’t have a GUI tool, you can use iptables directly to configure firewalling on your Linux system.
Using the iptables command is somewhat complex. The iptables command uses the concept of a chain, which is a sequence of rules. Each rule says what to do with a packet if the header contains certain information (such as the source or destination IP address). If a rule doesn’t apply, iptables consults the next rule in the chain. By default, there are three chains:
  • INPUT chain: The first set of rules against which packets are tested. The packets continue to the next chain only if the INPUT chain doesn’t specify DROP or REJECT.
  • FORWARD chain: Contains the rules that apply to packets attempting to pass through this system to another system (when you use your Linux system as a router between your LAN and the Internet, for example).
  • OUTPUT chain: Includes the rules applied to packets before they are sent out (either to another network or to an application).
When an incoming packet arrives, the kernel uses iptables to make a routing decision based on the destination IP address of the packet. If the packet is for this server, the kernel passes the packet to the INPUT chain. If the packet satisfies all the rules in the INPUT chain, the packet is processed by local processes such as an Internet server that is listening for packets of this type.
If the kernel has IP forwarding enabled and the packet has a destination IP address of a different network, the kernel passes the packet to the FORWARD chain. If the packet satisfies the rules in the FORWARD chain, it’s sent out to the other network. If the kernel doesn’t have IP forwarding enabled and the packet’s destination address isn’t for this server, the packet is dropped.
If the local processing programs that receive the input packets want to send network packets out, those packets pass through the OUTPUT chain. If the OUTPUT chain accepts those packets, they’re sent out to the specified destination network.
You can view the current chains, add rules to the existing chains, or create new chains of rules by using the iptables command. When you view the current chains, you can also save them to a file. For example, if you had configured nothing else and your system has no firewall configured, typing iptables -L should show the following:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

In this case, all three chains — INPUT, FORWARD, and OUTPUT — show the same ACCEPT policy, which means everything is wide open.
If you’re setting up a packet filter, the first thing you do is specify the packets that you want to accept. For example, to accept packets from the 192.168.0.0 network address, add the following rule to the INPUT chain:
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
Now add a rule to drop everything except local loopback (the lo network interface) traffic and stop all forwarding with the following commands:
iptables -A INPUT -i ! lo -j REJECT
iptables -A FORWARD -j REJECT

The first iptables command, for example, appends to the INPUT chain (-A INPUT) the rule that if the packet does not come from the lo interface (-i ! lo), iptables rejects the packet (-j REJECT).
Before rejecting all other packets, you may also add more rules to each INPUT chain to allow specific packets in. You can select packets to accept or reject based on many parameters, such as IP addresses, protocol types (TCP, UDP), network interface, and port numbers.
You can do all sorts of specialized packet filtering with iptables. For example, suppose you set up a Web server and want to accept packets meant for only HTTP (port 80) and secure shell (SSH) services. The secure shell service (port 22) is for you to securely log in and administer the server. Suppose the server’s IP address is 192.168.0.10. Here is how you might set up the rules for this server:
iptables -P INPUT DROP
iptables -A INPUT -s 0/0 -d 192.168.0.10 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -s 0/0 -d 192.168.0.10 -p tcp --dport 22 -j ACCEPT

In this case, the first rule sets up the default policy of the INPUT chain to DROP, which means that if none of the specific rules match, the packet will be dropped. The next two rules say that packets addressed to 192.168.0.10 and meant for ports 80 and 22 are accepted.
Don’t type iptables commands from a remote login session. A rule that begins denying packets from all addresses can also stop what you type from reaching the system; if that happens, you may have no way of accessing the system over the network. To avoid unpleasant surprises, always type iptables rules at the console — the keyboard and monitor connected directly to your Linux PC that is running the packet filter. If you want to delete all filtering rules in a hurry, type iptables -F to flush them. To change the default policy for the INPUT chain to ACCEPT, type iptables -t filter -P INPUT ACCEPT. This causes iptables to accept all incoming packets by default.
Not every iptables command is discussed in this section. You can type man iptables to read a summary of the commands. You can also read about netfilter and iptables at www.iptables.org.
After you define the rules by using the iptables command, they’re in memory and are gone when you reboot the system. Use the iptablessave command to store the rules in a file. For example, you can save the rules in a file named iptables.rules by using the following command:
# Generated by iptables-save v1.3.0 on Sun Aug 28 16:10:12 2010
*filter
:FORWARD ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [6:636]
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 192.168.0.0/255.255.255.0 -j ACCEPT
-A INPUT -i ! lo -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Sun Aug 28 16:10:12 2010

These rules correspond to the following iptables commands used to configure the filter:
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -i ! lo -j REJECT
iptables -A FORWARD -j REJECT

If you want to load these saved rules into iptables, use the following command:
iptables-restore < iptables.rules
next Performing Computer Security Audits






























































 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger