Archive

Archive for October, 2008

Linux Cached Memory

October 29, 2008 11 comments

In Linux, reading from a disk is very slow compared to accessing real memory. In addition, it is common to read the same part of a disk several times during relatively short periods of time. For example, one might first read an e-mail message, then read the letter into an editor when replying to it, then make the mail program read it again when copying it to a folder. Or, consider how often the command ls might be run on a system with many users. By reading the information from disk only once and then keeping it in memory until no longer needed, one can speed up all but the first read. This is called disk buffering, and the memory used for the purpose is called the buffer cache.

Unlike Windows other operating systems, Linux administers memory the smartest way it can.

Since unused memory is next to worthless, the filesystem takes whatever memory is left and caches it in order to speed up disk access. When the cache fills up, the data that has been unused for the longest time is discarded and the memory thus freed is used for the new data.

Whenever an application needs memory, the kernel makes the cache smaller; You do not need to do anything to make use of the cache, it happens completely automatically.

Freeing memory buffer does not make your programs faster… Actually it makes disk access slower.

BUT if for some reason (kernel debugging for example) you want to force the buffer to be freed, you need to set the drop_caches value to 1:

$ echo 1 > /proc/sys/vm/drop_caches

Issuing this command will release all the cached memory and also will stop collecting I/O buffer blocks. Let’s see an example of the effect:

Under normal situations, most of the memory is already cached by the system. But if we force the system to free the memory, you can see in the graph how the memory is suddenly dropped.

The technical details of how this works are explained on the Linux API

Categories: Memory Tags: ,

FTP Error Codes

October 25, 2008 3 comments

In case you are a die-hard FTP user, you should find helpful the error code reference. Normally, these codes appear in the system log files, or directly into your terminal.

120 Service ready in nnn minutes.
125 Data connection already open; transfer starting.
150 File status okay; about to open data connection.
200 Command okay.
202 Command not implemented, superfluous at this site.
211 System status, or system help reply.
212 Directory status.
213 File status.
214 Help message.On how to use the server or the meaning of a particular non-standard command. This reply is useful only to the human user.
120 Service ready in nnn minutes.
125 Data connection already open; transfer starting.
150 File status okay; about to open data connection.
200 Command okay.
202 Command not implemented, superfluous at this site.
211 System status, or system help reply.
212 Directory status.
213 File status.
214 Help message.On how to use the server or the meaning of a particular non-standard command. This reply is useful only to the human user.
215 NAME system type. Where NAME is an official system name from the list in the Assigned Numbers document.
220 Service ready for new user.
221 Service closing control connection.
225 Data connection open; no transfer in progress.
226 Closing data connection. Requested file action successful (for example, file transfer or file abort).
227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
230 User logged in, proceed. Logged out if appropriate.
250 Requested file action okay, completed.
257 “PATHNAME” created.
331 User name okay, need password.
332 Need account for login.
350 Requested file action pending further information
421 Service not available, closing control connection.This may be a reply to any command if the service knows it must shut down.
425 Can’t open data connection.
426 Connection closed; transfer aborted.
450 Requested file action not taken.
451 Requested action aborted. Local error in processing.
452 Requested action not taken. Insufficient storage space in system.File unavailable (e.g., file busy).
500 Syntax error, command unrecognized. This may include errors such as command line too long.
501 Syntax error in parameters or arguments.
502 Command not implemented.
503 Bad sequence of commands.
504 Command not implemented for that parameter.
530 Not logged in.
532 Need account for storing files.
550 Requested action not taken. File unavailable (e.g., file not found, no access).
551 Requested action aborted. Page type unknown.
552 Requested file action aborted. Exceeded storage allocation (for current directory or dataset).
553 Requested action not taken. File name not allowed.NAME system type. Where NAME is an official system name from the list in the Assigned Numbers document.
220 Service ready for new user.
221 Service closing control connection.
225 Data connection open; no transfer in progress.
226 Closing data connection. Requested file action successful (for example, file transfer or file abort).
227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
230 User logged in, proceed. Logged out if appropriate.
250 Requested file action okay, completed.
257 “PATHNAME” created.
331 User name okay, need password.
332 Need account for login.
350 Requested file action pending further information
421 Service not available, closing control connection.This may be a reply to any command if the service knows it must shut down.
425 Can’t open data connection.
426 Connection closed; transfer aborted.
450 Requested file action not taken.
451 Requested action aborted. Local error in processing.
452 Requested action not taken. Insufficient storage space in system.File unavailable (e.g., file busy).
500 Syntax error, command unrecognized. This may include errors such as command line too long.
501 Syntax error in parameters or arguments.
502 Command not implemented.
503 Bad sequence of commands.
504 Command not implemented for that parameter.
530 Not logged in.
532 Need account for storing files.
550 Requested action not taken. File unavailable (e.g., file not found, no access).
551 Requested action aborted. Page type unknown.
552 Requested file action aborted. Exceeded storage allocation (for current directory or dataset).
553 Requested action not taken. File name not allowed.

Categories: FTP Tags: ,

Using a Video Projector with Linux

October 20, 2008 1 comment

One of the first problems a user faces is the need to show topics and presentations with the help of a video projector and a laptop running Linux.

For some reason beyond me, Linux newcomers get turned off by the mere mention of a command line. The whole concept of using a console is like alien.

Okay, there are two options: You can try to modify your Xorg.conf file until you mess it up, destroy your computer’s GUI, and get attacked by sharks (Let’s be honest, this is not worth the effort for just a single presentation), or you can go for a fast, secure and temporary on-the-fly screen resizing.

If you chose the second options, keep reading. This was tested on my HP Pavilion dv2000, with a standard VGA port, running Fedora 9 x86_64.

First some easy stuff (OPTIONAL STEP): let’s disable the screensaver and as we want our screen to be ready and visible throughout the presentation.

$ xset s off

Next we have to take care of the external output. The command here is xrandr, which stands for “X Resize And Rotate” and is used to “allow clients to dynamically change X screens, so as to resize, rotate and reflect the root window of a screen“.

On your console type xrandr -v to check if the package is installed (99% chance it is).

$ xrandr -v
Server reports RandR version 1.2

Now, connect the video projector to the laptop. And automagically you should see… nothing, since the screens are not yet configured.

In order to verify it the system recognized the device, lets query the connected screens with the command xrandr -q

$ xrandr -q

You should see something like the following:

LVDS: minimum 320 x 200, current 1280 x 800, maximum 1280 x 800
VGA connected 1920x1440+0+0 (normal left inverted right x axis y axis) 380mm x 285mm

As you can see, there is a device named LVDS which is the “Low-voltage differential signaling” for the laptop panel, and there is also a VGA device which represents our video projector.

Here you can use the same resolution on the laptop screen and the projector or using two different resolutions. The first approach is a little less prone to errors so we are going to use it.

To clone the screen:
$ xrandr --output LVDS --auto --output VGA --auto --same-as LVDS

To extend the screen to the right in the VGA projector:
$ xrandr --output LVDS --auto --output VGA --auto --right-of LVDS

And finally, to turn off projection:
$ xrandr --output VGA --off

This method can also be used with DVI and S-VIDEO cables. Just be sure to type the correct name of the output device as it is showed with the xrandr command.

Categories: Screens Tags: , ,

Path of the Linux Jedi

October 17, 2008 1 comment

If you really want to get your hands on the “dirty job” and become a serious Linux programmer, I really recommend you to read the following book.

It’s called Advanced Linux Programming and is distributed under the GNU General Public License so you’re free to copy, share, and most important: read it.

BROWSE PDF FILES

As the publisher says, this book will help you to:

Develop GNU/Linux software that works the way users expect it to.
Write more sophisticated programs with features such as multiprocessing, multi-threading, interprocess communication, and interaction with hardware devices.
Improve your programs by making them run faster, more reliably, and more securely.
Understand the preculiarities of a GNU/Linux system, including its limitations, special capabilities, and conventions.

If you’re a developer already experienced with programming for the GNU/Linux system, are experienced with another UNIX-like system and are interested in developing GNU/Linux software, or want to make the transition for a non-UNIX environment and are already familiar with the general principles of writing good software, this book is for you. In addition, you will find that this book is equally applicable to C and C++ programming. Even those progamming in other languages will find this book useful since the C language APIs and conventions are the lingua franca of GNU/Linux.

As an advice, you will need a solid grasp of C programming knowledge.

Thruth be told, good luck.

Turn off echo in a terminal

October 13, 2008 4 comments

Imagine you are writing a shell script that requires the user to input confidential information, lets say a password.

The user wont feel comfortable if the password is echoed on the screen like a simple text. If you ever used expect interactive scripting, you know for sure this kind of problem.

Don’t worry, its amazingly easy to perform this trick and stop echoing whatever the user types on the screen.

Simply add the following line to your script:

stty -echo

and you are done. The stty output will go offline. Let’s see an example:

#!/bin/bash
echo Hello

stty -echo
# do what ever you want to do
echo 'I slept with your girlfriend'
stty echo
echo Bye
exit 0;

That’s it! Just remember to put it back to normal with the following command:

stty echo

Also, it would be helpful for you to read the whole man page for stty.

$ man stty

Categories: Shells Tags:

Connection closed by remote host: ssh_exchange_identification

October 9, 2008 1 comment

Often when a process that uses SSH runs in a regular basis, you may get a “Connection closed by remote host” error.

For example, in my case I was using a Nagios based monitor that needed to connect to a group of hosts and, from time to time I got bursts of this error when trying to access the monitored machines.

This is an example of the log file:

10 13:43:02  hoard04 [2]: Protocol error. ssh is complaining, see next
message. #d83bb35 (ssh_common.c 427)
10 13:43:02  hoard04 [2]: ssh_exchange_identification: Connection
closed by remote host

Even though the problem solves automatically by just ingnoring it for a while (really) I prefer to fix the problem rather than the symptoms, so with a little help from google I came up with the right solution.

This problem happens when the server hits the MaxStartups limit in the /etc/ssh/sshd_config file. This value acts as a security measure if for example someone tries to compromise your server with a DoS attack. By default its set to 10 so its relatively easy for SSH to get stuck at 10 connections.

Anyway, to solve the issue you just have to edit the mentioned file and bump the MaxStartups limit to, say 25 or 50 if you need a lot of connections.

Categories: networking, ssh Tags:

Custom Message for SSH logins

October 8, 2008 Leave a comment

If you ever wanted to show a message anytime someone logs to your server through SSH, here you have the how-to:

You will need root access to the server.

Login as root and use your favorite editor to modify the /etc/motd file. For this example I’m using vi.

# vi /etc/motd

Now type in the message you wish all users to see once they login to your server. Lets try something like this:

If you are not an authorized user for this server
or you are a hacker trying to access confidential info, think before you type.
Maybe join the human race and refrain from hacking it?  See
“Schlindler’s List” part where the German commandant is shooting
prisoners in the prsion year “because he can”.  Schlindler tells him
“true power is in not doing evil even though you can.”
You know, no one crushed you under their boot when you were a
baby, although they certainly could have…  With great power comes great responsibility.

OK, Now you’re done editing the file, type Esc to enter command mode and then type :wq to save the changes

Logout from SSH, then log back again and you will see your custom message.

Please note that the message will be displayed AFTER people log in. If you want to show a message BEFORE, you might want to edit your sshd_config file.

Categories: ssh Tags: