Monday, April 22, 2013

Log-in as root on Linux Terminal

I am going to show you three most used methods to log-in as root.

First way :
If you have sudo privileges as a user, you can use following command to log-in as root on terminal.
$ sudo -i
It asks for your (user's) password. After enter your password, you can log-in as root on terminal.

Second way :
$ su - or $ su - root
It asks for root's password. If you know root's password, you can log-in as root.

Third way:
You can log-in as root using secure shell (ssh)
$ ssh ip-of-the-server

If client and server are two different systems, the first two methods do not work to log-in as root on server. Only third method will work.

If client and server are in same system (root and users are in same system) , the first two methods do not require internet to log-in as root on terminal.

Third method requires internet to log-in as root on terminal.

Disable sudo privileges for user

If you want, user should not execute command with sudo.
To remove sudo permissions for user, follow the process.

1. Change the group of user. To see how to change the group of a user click here.

2. Do not define the group in file /etc/sudoers
     A group-name is started with symbol % in  /etc/sudoers
     Comment the line in /etc/sudoers
    #%group-name ALL=(ALL) ALL
Above line without comment gives sudo permission to all users of the group.
If you comment this line, no user of the group can use sudo while executing any command.

3. Now whenever user tries to run command with sudo, it says
 Error - username is not in the sudoers file. This incident will be reported.

Add username & group in sudoers file

Error - username is not in the sudoers file. This incident will be reported.


Open the file /etc/sudoers, Add following line in it
username ALL=(ALL) ALL
If your username is john, the line should be
john ALL=(ALL) ALL

If you want to add whole group as sudoers.
%group-name ALL=(ALL) ALL
where % indicates the name is a group.

If your group-name is wheel, the line should be
%wheel ALL=(ALL) ALL

Run multiple commands in Linux Launcher

If you want to run multiple commands in launcher.




Example:- If you want to stop apache2 (httpd) & mysql (mysqld) simultaneously. You would use 

service apache2 stop && service mysql stop
or
service httpd stop && service mysqld stop

But unfortunately it won't work in Launcher. It runs the first command but does not execute second.

To run multiple commands as a Launcher, you should use
sh -c "service apache2 start && service mysql start"
Now when you run launcher, it executes all commands.

If you want to run three commands,
sh -c "service apache2 start && service mysql start && cat > redirect"
In above example, it would start both services as well as create a file redirect.

Run sudo without asking user password

No password for sudo


Open file /etc/sudoers  and uncomment following line.
%sudo ALL=NOPASSWD: ALL
Save the file.

Now if you run commands with sudo, it does not ask user password.

Friday, April 19, 2013

.htaccess - Error 500 - Internal Server Error

Error 500 - Internal Server Error



To remove this error, check the content of .htaccess. It should be like this. 

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /your-web-project-directory-name/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /your-web-project-directory-name/index.php [L] <IfModule>

If Apache can’t understand any line in your .htaccess, it will cause an error.
When you try to access your web page, it generates the error.

A garbage line in .htaccess can create Internal Server Error.

Example:
Create a file name .htaccess, write your name into it, save it and put it in your web project directory. Now try to access the web page, it will give this error.

Define rewrite rule carefully in your .htaccess. If you write anything in .htaccess which apache can't understand, it will be the reason of Internal Server Error.

If rewrite module is not enabled and you are using rewrite engine in the .htaccess, you may have this error. To find a solution, click here or you can find another solution here.

.htaccess issue - html links are not working

.htaccess - Menu Links are not working


.htaccess links defined in rewrite-rules are not working in apache2

1) Enable rewrite module
   $ sudo a2enmod rewrite    $ service apache2 restart


2) These lines should be in /etc/apache2/apache2.conf
AccessFileName .htaccess <Directory /var/www>     AllowOverride All     #AllowOverride None </Directory> 
Restart apache2.

3) Enter following lines in /etc/apache2/httpd.conf

<Directory /var/www/project-name>     AddHandler cgi-script .cgi     Options +Indexes +ExecCGI     AllowOverride all </Directory> 

4) "AllowOverride all" should be uncommented in apache2.conf or httpd.conf.

.htaccess issue - The character encoding of the HTML document was not declared

Error: The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.Source File: http://localhost/path/of/the/project/    Line: 0


The website dos not load the css and it looks distorted when .htaccess file is used for web project.

To solve this error:
Add following line in your apache configuration file.
/etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf (according to your linux distribution)
AddHandler application/x-httpd-php .htm

Restart Apache.

Enable authentication in apache2 using .htaccess

.htaccess is not working in apache2 :

If you have put .htaccess in your web project directory and it does not ask for username & password.
Follow this.

1) Check following directives in /etc/apache2/sites-available/default
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory>
   Where /var/www is path of the directory where you keep your web projects.

2) Add following directive in httpd.conf
<Directory /your/path/of/the/project> AllowOverride All </Directory>

3) Restart apache2.



Create .htaccess & .htpasswd files for your web projects


Make secure your web Project in Apache using .htaccess

Create a filename .htaccess and put following content in it.

AuthName "Restricted Area"  AuthType Basic  AuthUserFile /opt/lampp/.htpasswd  AuthGroupFile /dev/null  require valid-user

Create a filename .htpasswd and generate a password using following site.
http://www.htaccesstools.com/htpasswd-generator/

Enter Username and Password. Click on create.
Now copy the generated line and paste into your .htpasswd file.
The .htpasswd file contains only this, a username:encrypted password

root:$apr1$7I58.7SX$zkt0..fl0ZBwU3ShGpDjZ1

a) In .htaccess file, AuthUserFile is path of the directory, where .htpasswd file is placed.
In above example I am trying to make my phpmyadmin password protected that's why I have put my .htpasswd file in parent directory and .htaccess file in /opt/lampp/phpmyadmin.
Now when I try to access http://localhost/phpmyadmin , It asks password to open the phpmyadmin.

b) Put the .htpasswd file in parent directory of the password protected directory and place the .htaccess in directory which you want to protect.

c) In above example username is root & password is linux.

d) It asks for password when directory is accessed through browser.

e) Once you put the log-in details and open your website, you have to close the browser completely to see the log-in box again. It does not ask the password every time you refresh the page.


To protect a single file instead of whole directory


If you do not want to protect whole directory instead of you want to protect single file.
Example : If you or anyone in LAN access your phpinfo.php , you want it should ask password.
In this case the contents of .htaccess file is
AuthName "Name of Page" AuthType Basic AuthUserFile /opt/lampp/htdocs/.htpasswd <Files "phpinfo.php"> require valid-user </Files>

Put the .htaccess in the directory where file you want to  protect is placed.

In this example, I want to protect my phpinfo.php. This file is placed in /opt/lampp/htdocs/xampp that's why I have put the .htaccess in xampp directory and .htpasswd in parent directory of xampp.

It will ask for password when user tries to access that file using browser.
http://localhost/xampp/phpinfo.php
But it will not ask for password if you try to access the directory.

Saturday, April 13, 2013

XAMPP: Another web server daemon is already running / XAMPP: Another MySQL daemon is already running.


XAMPP: Another web server daemon is already running.
XAMPP: Another MySQL daemon is already running.



There are two ways to remove these errors.
1) Stop the running services
$ sudo service service-name stop
If you are using Debian or Ubuntu :
$ sudo service apache2 stop $ sudo service mysql stop
If you are using CentOS , RHEL or Fedora :
$ sudo service httpd stop $ sudo service mysqld stop

2) Kill the running services

Now here is the second method.

XAMPP-web server works on port 80 and XAMPP-MySQL works on port 3306.

We have to find the services of respective ports and kill them.

Run following command in terminal
$ sudo netstat -lnp | grep -e :80 -e :3306

It will show you all processes of port 80 & 3306 like this
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15268/apache2   
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      14699/mysqld 

Now kill them using
$ sudo killall -9 process-name

It shows the process name beside pid (process id). In above example pid is 15268 & process-name is apache2. Another pid is 14699 & process-name is mysqld.

To kill the above processes, Run
$ sudo killall -9 apache2 $ sudo killall -9 mysqld
Now if you start XAMPP, it does not give process running error.

But if it still gives, kill the running process again.

You can kill the processes using pid too.
$ sudo kill -9 pid

To kill the above processes, Run
$ sudo kill -9 15268 $ sudo kill -9 14699

#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

phpMyAdmin is giving error
#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

Probably there are two mysql running. Open the terminal and run
$ service mysql stop
or
$ service mysqld stop
(according to your Linux distribution)
Now restart lampp and try to open phpMyAdmin.

Make sure you have following code in your /opt/lampp/phpmyadmin/config.inc.php
$cfg['Servers'][$i]['connect_type'] = 'tcp';
If not, add above line in config.inc.php and Restart mysql.

If you are still facing the problem. Add this line in your config.inc.php too.
$cfg['Servers'][$i]['socket'] = '/var/run/mysql/mysql.sock';
and Restart mysql.

If you are still facing the problem, Make following changes in your config.inc.php

change 
$cfg['Servers'][$i]['host'] = 'localhost';
to 
$cfg['Servers'][$i]['host'] = '127.0.0.1';
Now restart mysql and open phpmyadmin.

If you are still facing the problem, check permission of the file /opt/lampp/phpmyadmin/config.inc.php, it should not be less than 755.
Set it to 755.
Run following command.
$ sudo chmod 755 /opt/lampp/phpmyadmin/config.inc.php
Now restart LAMPP and try to open phpmyadmin again.
http://localhost/phpmyadmin

XAMPP: Couldn't start MySQL

Remove Error : XAMPP: Couldn't start MySQL

Run following commands in terminal
$ sudo chown -hR root /opt/lampp $ sudo chmod -R 777 /opt/lampp $ sudo /opt/lampp/lampp restart
After running above commands when you try to start XAMPP and it gives following error :

"Warning: World-writable config file ‘/opt/lampp/etc/my.cnf’ is ignored"

To Remove above error, Run following command in terminal
$ sudo chmod 755 /opt/lampp/etc/my.cnf

After running above command when you try to start XAMPP again and it gives following error :

Wrong permissions on configuration file, should not be world writable!

To Remove above error, Run following command in terminal
$ sudo chmod 755 /opt/lampp/phpmyadmin/config.inc.php

Recover /etc/passwd file in Linux


/etc/passwd is deleted accidently, Recover the file.

There is a backup file of /etc/passwd in same directory with name passwd-
$ sudo cp /etc/passwd- /etc/passwd $ sudo chmod 644 /etc/passwd

Lock the file using
$ sudo pwck -q

If you are not root user and do not use sudo before pwck -q, it will give error
pwck: cannot lock /etc/passwd; try again later.


External USB Hard Disk (HDD) NTFS not detected in CentOS

External (USB) hard disk is not detected :

Install NTFS package in centOS


$ yum install fuse fuse-ntfs-3g

If it does not work, download rpm file and install using
ftp://ftp.muug.mb.ca/mirror/fedora/epel/6/x86_64/ntfs-3g-2011.4.12-5.el6.x86_64.rpm


$ rpm -i filename

Change htdocs path in XAMPP

Here in this example, I am showing you to change the php project directories path from /opt/lampp/htdocs to /var/www

1) Open file /opt/lampp/etc/httpd.conf and change following line

DocumentRoot "/opt/lampp/htdocs"
to
DocumentRoot "/var/www"

2) Now search following line in same file (/opt/lampp/etc/httpd.conf)
and change

# This should be changed to whatever you set DocumentRoot to.
<Directory "/opt/lampp/htdocs">

to
# This should be changed to whatever you set DocumentRoot to. <Directory "/var/www">
(Above line is commented, it has been mentioned to find directive easily)

3) Restart the XAMPP.

4) If virtual hosts have been defined, change the path in /opt/lampp/etc/extra/httpd-vhosts.conf too.
Otherwise above steps will not work.

or if you want to remove virtual hosts, just comment following line in /opt/lampp/etc/httpd.conf
# Include etc/extra/httpd-vhosts.conf
5) Restart the XAMPP.

6) After following above process, if it throws php code in browser.
Uncomment following line in the file /opt/lampp/etc/httpd.conf
Include etc/extra/httpd-xampp.conf
This line should not be commented. It means there should be no Hash '#' symbol at start of the line.

7) If it gives Internal Server Error or Server Error.
Comment following line in the file /opt/lampp/etc/httpd.conf
# Include etc/extra/httpd-vhosts.conf
& Restart the XAMPP.

Thursday, March 28, 2013

Linux File Permission Mechanism

Linux File Permissions :

Understanding Linux File & Directory Permissions :



Linux File and directory Permission mechanism

Read Write Execute
Owner 400 200 100
Group 40 20 10
Others 4 2 1


If you want to give Read, Write & Execute permission to Owner, Groups & Others, it means full permission.
400+200+100+40+20+10+4+2+1 = 777

If you do not want to give Write permission to Groups Others, it means 
400+200+100+40+10+4+1 = 755

The best thing of this calculation, you can not make sum of a number in two different ways .
It means 644 can be made in this way only that is 400+200+40+4
The meaning of 644 is Read & Write permission to Owner and Read permission to Group and Others only.

To give 777 to a directory :

# chmod -R 777 /path/of/the/directory

where -R means Recursive, it means give this permission to all sub-directories and files.

To give 777 to a file :
# chmod 777 /path/of/the/file
Now you can create & give permissions by your own.

Friday, March 15, 2013

Unable to boot : please use a kernel appropriate for your cpu

Virtual Box Error :
Unable to boot : please use a kernel appropriate for your cpu




Go on
Settings -> System -> Processor 

&

Make the Check-box PAE/NX Enable.

Make sure your virtual machine is not started otherwise it shows options disabled.


Wednesday, February 27, 2013

Copy Directory Structure without Files in Linux

If you want to copy the directory structure without any files, you can use following command.
rsync -av --include '*/' --exclude '*' /path/source/dir /path/destination/dir
'rsync' gives you utility to copy directory with files or without files.

If you want to copy the directory structure without any files from one host to another. You can use
rsync -av --include '*/' --exclude '*' /path/source/dir user@host:/path/destination/dir
Examples:
Suppose I want to copy the directory structure of /etc in /root/tmp on same host.
rsync -av --include '*/' --exclude '*' /etc /root/tmp
It will copy all directories of /etc in the /root/tmp without any files.
It will not create directory etc in the /root/tmp. It will start from sub-directories of etc.

Saturday, February 23, 2013

Compile & Run C Program on Linux Terminal



1) Write any C program in text editor and save as filename.c
In following example, I have saved this file on my Desktop.

2) To run this program, you have to compile it first.
$ gcc -o compiled-filename /home/user/Desktop/filename.c 
Where compiled-filename is the name of compiled file, it can be anything.
This will generate a compile copy of your program as a name compiled-filename in same directory where you ran the above command.

3) Now create output copy of your program.
Following command generate a output file of your program in same directory.
$ gcc /home/user/Desktop/filename.c
4) Now run your program.
$ ./a.out 
 or 
$ ./compiled-filename
where a.out is output copy of your program.
Alternatively you can run compiled copy of your program.

Friday, February 22, 2013

Make FTP server secure on Linux : RHEL, CentOS, UBUNTU

To install FTP on Linux Server, click here

1) Open vsftpd.conf (/etc/vsftpd.conf or/etc/vsftpd/vsftpd.conf)
To prevent anonymous log-in, Set anonymous_enable=NO
 In vsftpd.conf, if anonymous_enable=YES, There is no need to put username & password. If you put only hostname and try to log-in, you can log-in. This situation is very dangerous.
For security purpose, Always set this attribute NO. 

2) These are the harmful content for your vsftpd.
If your vsftpd file (/etc/pam.d/vsftpd) has following content, it will allow blank or wrong password. It is a threat for your security.
auth sufficient /lib/security/pam_pwdfile.so pwdfile /home/ftp/passwd
auth required pam_shells.so
account required pam_permit.so
Never make auth sufficient , it should be required always.

Never leave your vsftpd file with above content.

3) Never allow blank or wrong password for ftp user 
Make Sure your vsftpd file has following content

If it is Debian, Ubuntu
auth required pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
@include common-account
@include common-session
@include common-auth
auth required pam_shells.so

If it is CentOS, RHEL
session    optional     pam_keyinit.so    force revoke auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed auth       required pam_shells.so auth include password-auth session include password-auth session    required     pam_loginuid.so account include password-auth

4) When you make any changes in the file vsftpd.conf, make sure you have restarted the vsftpd.

Install FTP on Linux : CentOS, RHEL, UBUNTU

vsftpd Configuration :


1) Install vsftpd using following command :
   If it is Debian based (Ubuntu) Linux
$ sudo apt-get install vsftpd

   If it is RPM based (CentOS, RHEL) Linux
$ sudo yum install vsftpd

2) Make three changes in vsftpd.conf
   (Location of vsftpd.conf - /etc/vsftpd.conf or /etc/vsftpd/vsftpd.conf)

a)Uncomment line chroot_local_user=YES in vsftpd.conf
   This attribute will make jail for user to not move to the parent directory.

b)Define attribute local_root in vsftpd.conf
local_root=/opt/lampp/htdocs

This is the path of the directory where user should be jailed. User can not go to the parent directory.In this example user will be logged in htdocs and can not reach to the parent directory of htdocs. 

c)Allow users to upload file, set
local_enable=YES
write_enable_YES
Now user can log-in to the server by ftp using username and password.

host : IP of the server
username : user of the server except root
password : password of the user

3) To create user for ftp :
 

$ sudo adduser -c 'FTP USER ec2' -m ec2

   Where ec2 is the username for ftp. To set password for this user :
$ sudo passwd ec2

4) FTP works on port 21. you can close the port or stop the service using 
$ service vsftpd stop

   and open the port or start the service using
$ service vsftpd start

5) If you are using CentOS/RHEL servers, Add following line in your iptables file 
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

Path of iptables file :/etc/sysconfig/iptables

   Save and close the file & Restart the iptables. 
$ service iptables restart

6) To prevent anonymous log-in, Set anonymous_enable=NO
 In vsftpd.conf, if anonymous_enable=YES, There is no need to put username & password. If you put only hostname and try to log-in, you can log-in.
For security purpose, make sure you have set this attribute NO.

7) By default root user can not log-in by ftp but log-in through sftp.

8) When you make any changes in the file vsftpd.conf, make sure you have restarted the vsftpd.

Friday, February 15, 2013

oDesk Screenshot Time Hack


We all want to know when oDesk is going to take next screenshot.

I am NOT going to tell you any illegal method or force you to install any software.
It is all about simple and easy calculation.
I can't tell you the exact time but you can judge it after reading this.

Here I want to share with you some facts about oDesk, may be you already know this but please read once more.

 In every hour, oDesk takes only 6 screenshots.

  Every screenshot is taken in 10 min time interval. 
   It means 00 minute to 09 minute, only one screenshot is taken.
   After that 10 min to 19 minute only one screen shot will be taken.

  If you do not understand above point, Let me explain you with an example.
  Suppose it is 10:00 am, Now between 10:00 am and 11:00 am, 
only six screenshots will be taken by odesk.
   If first screen shot is taken on 10:02, 
it means next 07 minutes (till 10:09), no screenshot  will be taken.
   The second screenshot will be taken between 10:10 and 10:19.
   The third screen shot will be taken between 10:20 and 10:29

 If your screenshot is taken on XX:00 min, 
now the next screen shot will be taken between XX:10 and XX:19.
 It means you have at least nine minutes to do some other work.
Screenshot will not be taken in these 9 minutes.

 It does not depend on your system clock. Do not change the time.

 To check oDesk's screenshot system, check work diary of oDesk. 
Now you will understand everything. click here

We are NOT able to know the exact time when the next screenshot will be taken, but we can know How long the next screenshot will NOT be taken.

Google Chrome can not be run as root



Chrome Error in Linux
Google Chrome can not be run as root




1. First, Remove all chrome(stable/unstable) from synaptic package manager, Ubuntu software center or command line.
   Make your system chrome free.

2. Now download new chrome from Here . 

3. If it is deb file it means you are using debian Linux, if it is RPM it means you are using RPM (Red hat package Manager) Linux

4. Install the file.
   If you want to install it using command line, follow this.
   For Debian
   $ dpkg -i chrome_file_name.deb
   For RPM
   $ rpm -ivh chrome_file_name.rpm

5. Open /opt/google/chrome/google-chrome and find following line
  exec -a "$0" "$HERE/chrome" "$@"

6. Now replace above line with following line
  exec -a "$0" "$HERE/chrome" "$@" --user-data-dir
  
Now try to open chrome. You will not face the error anymore.

7. If you replace the line with following line
  exec -a "$0" "$HERE/chrome" "$@" --user-data-dir $HOME
  it will open root directory structure in chrome while opening new window.


Install or Upgrade Latest version of Mozilla Firefox On Ubuntu






Download latest stable version of firefox.
click here

If there is a directory name 'firefox' in /usr/lib, Rename it to old.
$ mv /usr/lib/firefox /usr/lib/firefox_old

 Extract your downloaded compress file and put it in /usr/lib
$ tar -xvf  firefox-new-version.tar.gz
$ mv  firefox-new-version /usr/lib

Now open it and make a link of filename 'firefox'.
   To make link, right click on the file and click 'Make Link'

 Now put this link file into /usr/bin and make sure it's name is 'firefox'.
$ sudo ln -s /usr/lib/firefox-new-version/firefox /usr/bin

 Now open firefox, it will open the latest version.

 Now you don't have to upgrade every time for latest version.
   Once you put the latest version , firefox is upgraded automatically, if it gets internet connection.

 If you want to know that your version is updated or not.
   In firefox menu, click on Help -> About firefox.
   If your version is not updated, it starts upgrading.

But above step does not help you in upgrading, if your version is too much old.
   Example: you are using 6 or 7 and current version is 15.

Enjoy firefox on Ubuntu