Tuesday, May 29, 2012

Linux X11 Connection Rejected Because of Wrong Authentication Error and Solution

Q. I'm trying to login to my remote Ubuntu Linux server from Mac OS X desktop using following command:
ssh -X user@vpn.officeserver.example.com xeyes
But I'm getting an error that read as follows:
X11 connection rejected because of wrong authentication.
How do I fix this error?

A. This error can be caused by various factors. Try following solutions:

Make sure you are not running out of disk space

Run df and make sure you have sufficient disk space:
$ df -H
If you are low on disk space remove unnecessary files from your system.

Make sure ~/.Xauthority owned by you

Run following command to find ownweship:
$ ls -l ~/.Xauthority
Run chown and chmod to fix permission problems
$ chown user:group ~/.Xauthority
$ chmod 0600 ~/.Xauthority

Replace user:group with your actual username and groupname.

Make sure X11 SSHD Forwarding Enabled

Make sure following line exists in sshd_config file:
$ grep X11Forwarding /etc/ssh/sshd_config
Sample output:
X11Forwarding yes
If X11 disabled add following line to sshd_cofing and restart ssh server:
X11Forwarding yes

Make sure X11 client forwarding enabled

Make sure your local ssh_config has following lines:
Host *
ForwardX11 yes

Finally, login to remote server and run X11 as follows from your Mac OS X or Linux desktop system:
ssh -X user@remote.box.example.com xeyes

FreeBSD: Apache httpready filter – Failed to enable the 'httpready' Accept Filter

Q. I've installed Apache 2.2 from FreeBSD 7.0 ports and when I try to start I get following warning on screen:
Performing sanity check on apache22 configuration:
Syntax OK
Starting apache22.
[Wed Sep 17 22:01:58 2008] [warn] (2)No such file or directory: Failed to enable the 'httpready' Accept Filter
How do I fix this error?

A. FreeBSD has special driver called accf_http. It will buffer incoming connections until a certain complete HTTP requests arrive to speed up and optimize performance.
The utility of accf_http is such that a server will not have to context switch several times before performing the initial parsing of the request. This effectively reduces the amount of required CPU utilization to handle incoming requests by keeping active processes in preforking servers such as Apache low and reducing the size of the file descriptor set that needs to be managed by interfaces such as select(), poll() or kevent() based servers.
Just open shell prompt and type the following command to load accf_http under FreeBSD :
# kldload accf_http
Restart apache:
# /usr/local/etc/rc.d/apache22 restart

Update /boot/loader.conf file

Type the following command so that driver get loaded at the time of booting system:
# echo 'accf_http_load="YES"' >> /boot/loader.conf

A note about FreeBSD jails (vps)

Under jail you cannot load this module. It needs to be loaded from host using above command.

Apache Error Client Denied By Server Configuration

Q. I'm running Apache 2 Web sever under CentOS Linux and getting an error that read as follows:
[Wed Sep 17 21:53:49 2008] [error] [client 122.1xx.y9.zzz] client denied by server configuration: /var/www/examples.com/
How do I fix this error?

A. By default Apache is configured as restrictive server. It will not allow end users (client) to do anything on default DocumentRoot. To fix this issue you need to add following lines to your VirtualHost configuration directives:
<Directory "/var/www/example.com">
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
'Order allow,deny' and 'Allow from all' will set appropriate permission for the directory. At the end it should look like as follows:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot "/var/www/example.com"
ServerName example.com
ServerAlias www.example.com
ErrorLog "/var/logs/httpd/example.com/error.log"
CustomLog "/var/logs/httpd/example.com/access.log" common
ScriptAlias /cgi-bin/ "/var/suexec/example.com/cgi-bin/"
 
<Directory "/var/www/example.com">
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
 
SuExecUserGroup user group
</VirtualHost>
Restart apache:
# service httpd restart

Shell: How To Remove Duplicate Text Lines

Q. I need to sort data from a log file but there are too many duplicate lines. How do I remove all duplicate lines from a text file under GNU/Linux?

A.. You need to use shell pipes along with following two utilities:
a] sort command - sort lines of text files
b] uniq command - report or omit repeated lines

Removing Duplicate Lines With Sort, Uniq and Shell Pipes

Use the following syntax:
sort {file-name} | uniq -u
sort file.log | uniq -u

Here is a sample test file called garbage.txt:
this is a test
food that are killing you
wings of fire
we hope that the labor spent in creating this software
this is a test
unix ips as well as enjoy our blog
Type the following command to get rid of all duplicate lines:
$ sort garbage.txt | uniq -u
Sample output:
food that are killing you
unix ips as well as enjoy our blog
we hope that the labor spent in creating this software
wings of fire
Where,
  • -u : check for strict ordering, remove all duplicate lines.

PHP: Verify And Sanitize Email Address

Q. How do I verify input data such as email address under PHP programming language?

A. You need to use PHP Filters designed for safely dealing with input parameters. It can validate and filter data coming from some insecure source, such as user input via filter extension. This extension is part of PHP Core version 5.20 and above, but you can always install it under Linux. You can use filters to validate following type of data:
=> regex
=> url
=> email
=> strings
=> magic_quotes
=> regular data types such as int, float etc

Validate email address

Here is a sample code:
<?
$email="vivek@nixcraft.com";
if ( filter_var($email, FILTER_VALIDATE_EMAIL) == TRUE) {
echo 'Valid Email Address';
}
else
{
echo 'Invalid Email Address';
}
?>
filter_var() will filter a variable with a specified filter. In this case you've used FILTER_VALIDATE_EMAIL filter. You may also want to sanitizes the e-mail using following code:
$out=filter_var($email, FILTER_SANITIZE_EMAIL);
Here is another sample:
<?php
// form.php
//....
//......
$_POST['email'] = stripslashes(trim($_POST['email']));
$tmpEmail=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if ( filter_var($tmpEmail, FILTER_VALIDATE_EMAIL) == TRUE) {
// callSmtp
fireSmtp();
}
else{
//show error
echo 'Invalid Input - an error has occurred when trying to send this email';
}
///....
 
?>

Linux / UNIX Crontab File Location

I login to my UNIX system as a normal user. However, I need to update my cronjob entry. But, I can't find where the crontab file is. How do I find out my crontab file location?

By default cron searches its spool area /var/spool/cron/crontabs directory for crontab files. All files which are named after username i.e. accounts in /etc/passwd file. So if your username is vivek, crontab file location should be /var/spool/cron/crontabs/$USER i.e. /var/spool/cron/crontabs/vivek. Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them as follows:
crontab -e
To view your crontab file (cron jobs) type
crontab -l

Linux / UNIX: Change Crontab Email Settings ( MAILTO )

Q. I'd like to send email to user@example.com instead of default root user. How do I change email settings under crontab file?

A. A crontab file contains instructions to the cron daemon.
An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form:
 name = value
where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks. The value string is not parsed for environmental substitutions, thus lines like
PATH = $HOME/bin:$PATH
will not work as you might expect.

MAILTO Variable

In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a result of running commands in "this" crontab. If MAILTO is defined (and non-empty), mail is sent to the user so named. First open your crontab file:
# vi /etc/crontab
OR
$ crontab -e
To send email to vivek@nixcraft.in, enter:
MAILTO=vivek@nixcraft.in
If MAILTO is defined but empty (MAILTO=""), no mail will be sent.
MAILTO=""
Otherwise mail is sent to the owner of the crontab.