Friday, June 28, 2013

Run php script on command line in Linux

Run php files on terminal :

First make sure you should have following packages installed on your system.
  • php5 
  • libapache2-mod-php5
  • php5-cli
Method : I
Now run the script on the terminal
# php /path/of/the/phpfile.php
Disadvantage :
If your php file in the xampp and if it requires database connectivity, the file will not be run successfully because it will not be able to connect to database. It will try to connect to default mysql server. It does not connect to xampp's mysql server. You will have to change the socket path to connect to xampp's mysql server.

Method : II
You have another way to run php files on terminal but you should be able to run them successfully on your browser.
I mean, you should have a web-server (apache) and a database-server (mysql) on your system. Now the same way you run your php file on browser, we can run it on terminal too.

Suppose you can run your php file using following url in your browser.
http://localhost/project-directory/phpfile.php

Run following command on terminal.
# wget http://localhost/project-directory/phpfile.php
Disadvantage :
The disadvantage of this method is, every time when you run the command , it downloads the php file in current directory where you run the command.

Advantage :
Method Ist's disadvantage is Method IInd's advantage.

Connect XAMPP's mysql on Linux terminal

Access XAMPP's mysql on Linux command line :

If you want to operate xampp's mysql on Linux terminal, follow the procedure.

By default, if you run
# mysql
It tries to connect to default mysql of the system and unfortunately default mysql is not XAMPP's mysql.

To open the xampp's mysql prompt on terminal, Run following command.
# /opt/lampp/bin/mysql or # /opt/lampp/bin/mysql -u root or # /opt/lampp/bin/mysql -h localhost -u root -p Default Password is blank, just press enter key.
/opt/lampp/bin/mysql is the path of the mysql binary file of xampp.

If you want to run XAMPP's mysql using 
# mysql
You should follow these steps.

1) Stop xampp & copy file /opt/lampp/bin/mysql
2) Rename the file /usr/bin/mysql to /usr/bin/mysql_default
    If you haven't installed mysql on your system, you will not get the file.
3) Paste file  /opt/lampp/bin/mysql into /usr/bin
4) Now whenever you will run mysql on terminal, it will open XAMPP"s mysql prompt.

Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

If you have installed apache2 and while starting or restarting apache2, you get this error. Try following steps to remove it.

Solution :

Add following line in /etc/apache2/apache2.conf
ServerName localhost
Restart apache2
# service apache2 restart
Now try again.

Thursday, June 27, 2013

ssh Error : scp: ambiguous target

scp: ambiguous target

If you are transferring files from your host to another host and there are spaces in the path, you could get this error.

If there are spaces in the directory name of source (your system) and you have used double quotes in the path, you will not get the error but if there are spaces in the directory name of destination (remote host) and you have used double quotes in the path, you could still get the error.

To remove this error
Use backslash before spaces.

Suppose there is a directory "linux host" in the root of Remote Host and you want to transfer a file test.txt from your root directory to the remote host directory  "linux host". You should use this command.
# scp "/root/test.txt" 192.168.xx.xx:"/root/linux\ host"
Since there is space in the directory name "linux host" that's why I have used backslash before space.
Now you will not get the error while file transfer.

If you put just double quotes in the remote host path and you do not put backslash before space, you will get the error scp: ambiguous target

One more example :
# scp "/root/Documents/file.txt" 192.168.xx.xx:"/root/untitled\ folder/red\ hat/linux\ host"
where
192.168.xx.xx is IP of Remote Host

Remove start up applications using command line

Remove/Delete Start up applications using command line :

When you want to run a command or a script automatically just after log-in, you create a start up application using Startup applications preferences. If you want to remove them, you use remove button, placed at bottom of the add button, to remove them.



But suppose you have created a application and because of some bad code, it is not able to let you reach on the desktop. You log-in and and your system is turned off automatically or you face any other similar problem.

Now you want to delete the command or the script from start up applications. But How?
You are not able to reach on Desktop.

Here is good news.
You can delete the application from command line.

You can reach on command line using recovery mode or you can press key combination ctrl+alt+f1 when log-in screen is appeared and it takes you on command line.

All startup applications are stored in the directory /root/.config/autostart.
If you are logged in as a user, you can find your all start up applications in /home/username/.config/autostart

Just reach to the directory using
# cd /root/.config/autostart or # cd /home/username/.config/autostart
list the applications using ls command and delete your corrupt application.

The file extension of the application is .desktop

Alternatively if you know the application name, you can delete it using
# rm /root/.config/autostart/application-name.desktop
or
# rm /home/username/.config/autostart/application-name.desktop

ssh Error : /usr/bin/ssh-copy-id: ERROR: No identities found

Error : /usr/bin/ssh-copy-id: ERROR: No identities found :

Solution :

Run following command on the client
# ssh-add

If it gives following error.
 Could not open a connection to your authentication agent
To remove this error, Run following command.
# eval `ssh-agent`
Now try to run ssh-add again.

If you are still getting this error, check if you have generated the keys using ssh-keygen.
Read here.

Show GUI alert boxes on remote host

Show popup/alert box on other system :

Sometime you want to give just a message to your colleague, you do not want reply. 

If you do not want to send messages on terminal or chat-box, you can use alert box to display the message but you should be able to log into your colleague's system.



Requirements:
ssh should be installed on both systems.
You should be able to log into your colleague's system.

Steps:
Log into the Remote Host (your colleague's system) using ssh.
# ssh 192.168.xx.xx
where 192.168.xx.xx is IP of the Remote Host.
Run following command on his/her command line
# DISPLAY=:0 zenity --info --text="your-message-should-be-here"
If you want to give the title for the message box.
# DISPLAY=:0 zenity --title="your-title" --info --text="your-message-should-be-here"
If you would not use DISPLAY=:0 in your command, it would show a display error.
Gtk-WARNING **: cannot open display: 
If you open the display using -X in your ssh command like ssh -X 192.168.xx.xx and run the command without using  DISPLAY=:0, it opens the alert box but on your system. Your colleague would not be able to see this.
You have to set DISPLAY on remote host.


Wednesday, June 26, 2013

Track the users who are using sudo

List of users who are using sudo :

If you are root of the server and there are several users who are logged in to the server, you should know your user activities.

There can be some circumstances that you just can't disable sudo facility and the reason is not that you don't know how but you can't because you have to give the functionality to the users.

You must know that how to find number of users, current logged in users and all that stuff.

But some of your user can misuse the sudo facility. You should track the record that  who are currently using sudo.

Fortunately Linux can track the record for you. To read this log file, you can know the list of the users who are using sudo.

If you are using Debian or ubuntu, the location of the file is
/var/log/auth.log

If you are using CentOS, Fedora or RHEL, the location of the file is
/var/log/secure

If user tries to remove his/her entry from the file, it enters this act in the file too so you can know that user has edited the log file and he has done something nasty.


Add one file's contents in other file on Remote host using command line

Add one file's text in another file on remote host :

If you have a file A and you want to add this whole file's text in file B but the problem is file B is on remote host.

You may be think to download the file B, update it and upload it again.

But you can do this using command line in a single command.

Suppose file A which is on your system is in  /root directory and file B which is on Remote host is in /root/Documents.

Now run following commands to add file A's text in file B.
# cat /root/fileA.txt | ssh 192.168.xx.xx "cat >> /root/Documents/fileB.txt"
or
# ssh 192.168.xx.xx "cat >> /root/Documents/fileB.txt" </root/fileA.txt
or
# scp /root/fileA.txt 192.168.xx.xx: /root/fileA.txt && ssh 192.168.xx.xx "cat /root/fileA.txt >>  /root/Documents/fileB.txt"
where:
192.168.xx.xx is IP of Remote Host

The commands do not remove the text of fileB instead they add the text at bottom of the fileB.

ssh Error : Could not open a connection to your authentication agent

Could not open a connection to your authentication agent :

If you are trying to execute ssh-add -l or ssh-add and it gives following error.
 Could not open a connection to your authentication agent
To remove this error, Run following command.
# eval `ssh-agent`
Now try to tun ssh-add again.

Change hostname of your Linux machine without Reboot

Change hostname of your Linux machine without Reboot :

You must fill hostname while installation. It is your machine name.

If you want a definition of host-name or you want to understand what exactly is it, you should click here .

But if you know about it, you should read ahead.

You must have seen your host name in terminal after symbol '@'.
But if you want to know your host-name using command line, you should run
# hostname
If you want to change it, follow this.

If you are using CentOS, Fedora or Redhat

1) Open file /etc/sysconfig/network
2) Now change HOSTNAME=old-host-name to HOSTNAME=new-host-name
3) Save the file
4) Restart network using
# service network restart
Now open terminal, you will see the host name has been changed after symbol '@'
If you are not satisfied enough. Run command
# hostname

If you are using Debian or Ubuntu

1) Open file /etc/hostname
2) Change the old hostname into new hostname.
3) Save the file.
OR
Run the following command
# echo new-hostname > /etc/hostname
Now open terminal, you will see the host name has been changed after symbol '@'
If you are not satisfied enough. Run command
# hostname

Friday, June 14, 2013

Enable & Disable SELinux

Enable & Disable SELinux on CentOS :

Disable SELinux :
Run on terminal
# echo 0 > /selinux/enforce
or
# setenforce 0
No need to reboot using above commands but when system will reboot SELinux will be enabled again.

Enable SELinux :
# echo 1 > /selinux/enforce
or
# setenforce 1
No need to reboot using above commands.

Permanent Solution (Disable SELinux ):
Open file /etc/selinux/config
change SELINUX=enforcing to SELINUX=permissive 
or 
SELINUX=enforcing to SELINUX=disabled
Reboot the system.
It will disable the SELinux permanently.

Check status of SELinux
# getenforce
Output is :
Disabled

Another command :
# sestatus
Output is :
SELinux status:                 disabled

Could not chdir to home directory /home/user: Permission denied -bash: /home/user/.bash_profile: Permission denied

Error : Could not chdir to home directory /home/user: Permission denied -bash: /home/user/.bash_profile: Permission denied

The error opens the bash prompt instead of user prompt when user tries to login using ssh.
Set the home directory permission to 775
$ chmod 775 /home/

It sets the permission of directory /home 775. Do not give recursive permission, it means the permission of directories inside /home (sub-dirs of /home) should not be 775. They should be 700.

/home should be 775 & directories inside /home should be 700.

If you think it will increase the number of hazardous attempts, then try second solution. Although it is about security too. You should disable the SELinux.

SSH : agent admitted failure to sign using the key

Error : Agent admitted failure to sign using the key :

Solution :

Run following command on the client
# ssh-add



Permissions for .ssh directory and authorized_keys file

Permissions for .ssh directory and authorized_keys file

The permission for directory .ssh should be 700. It means drwxr_xr_x
# chmod 700 ~/.ssh

The permission for file authorized_keys should be 600. 
It means -rw_____
# chmod 600 ~/.ssh/authorized_keys

Generate ssh keys

Setting up SSH public/private keys

There are two types of keys in ssh :

a) Public keys &
b) Private keys

RSA & DSA are two most popular public key cryptography algorithm.
RSA was named after the initials of its co-founders, Rivest, Shamir and Adleman.
DSA is Digital Signature Algorithm.

Difference between DSA & RSA :
DSA is faster in signing, but slower in verifying. A DSA key of the same strength as RSA (1024 bits) generates a smaller signature. An RSA 512 bit key has been cracked, but only a 280 DSA key.

Path of Public & Private keys file :
If you are root
Public key file :  /root/.ssh/id_dsa.pub or /root/.ssh/id_rsa.pub
Private key file : /root/.ssh/id_dsa or /root/.ssh/id_rsa

If you are user
Public key file :  /home/user/.ssh/id_dsa.pub or /home/user/.ssh/id_rsa.pub
Private key file : /home/user/.ssh/id_dsa or /home/user/.ssh/id_rsa

Generate Public keys : dsa

# ssh-keygen -t dsa



Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
c9:a7:51:72:2b:e1:58:be:2a:12:53:41:28:03:e2:3e root@user-To-be-filled-by-O-E-M
The key's randomart image is:
+--[ DSA 1024]----+
|+  o.            |
|= . .            |
| +   .  + o      |
|.   .  * * .     |
| E .  . S o      |
|  +      *       |
|   o    o        |
|  . .  .         |
|   . ..          |

+-----------------+

In your .ssh directory
id_dsa.pub is public key file & id_dsa is private key file.

Generate Public keys : rsa

# ssh-keygen -t rsa


Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
44:ba:dc:d2:e1:f8:27:27:db:c0:e8:e5:21:31:43:2d root@user-To-be-filled-by-O-E-M
The key's randomart image is:
+--[ RSA 2048]----+
|        .        |
|       +         |
|      E +        |
|     o O .       |
|      O S        |
|       O         |
|      o O o      |
|     . + X       |
|      . o .      |
+-----------------+



In your .ssh directory
id_rsa.pub is public key file & id_rsa is private key file

NOTE :

You need either RSA or DSA key. You do not need to generate both.


Linux server authentication without password

Log into Linux server without password :
ssh login without password :

If you log into Linux server frequently using ssh and every time it asks for password. Now you have been sick to type server's password every time, read ahead.

This process is easy as well as secure. It does not leave any loop hole in your security.

Just follow these steps to get rid of typing password.

1. Open your .ssh directory inside your home directory.
2. There should be a pub file inside it, either it is id_dsa.pub or id_rsa.pub according to your key type. If you do not have keys in the directory, generate them. Read this.
3. Now open your server's ssh directory and save your public file ( id_dsa.pub or id_rsa.pub )as filename authorized_keys inside ssh directory of server.
4. If there is already a file authorized_keys inside server's ssh directory, just copy your public file content and paste in the file authorized_keys at last.
5. Now save the file.

Bottom line is client's public file content should be saved as authorized_keys file in the server.

To do this process using command line :
There can be multiple test cases that's why I wrote the process in steps.
1. Client is a user and server is a user or client is a root and server is a user or client is a user and server is a root or client is a root and server is a root.
2. If client is a user, the path of public file is /home/username/.ssh/id_dsa.pub or /home/username/.ssh/id_rsa.pub
If client is a root, the path of public file is /root/.ssh/id_dsa.pub or /root/.ssh/id_rsa.pub
3.  If server is a user, the path of authorized_keys is /home/username/.ssh or /home/username/.ssh
If server is a root, the path of authorized_keys is /root/.ssh or /root/.ssh
4. The authorized_keys file can exist already inside .ssh directory, if you replace it with your's authorized_keys, another user who is using this non-password process will lose the facility that's why you should add your public file content inside existing authorized_keys file without removing anything.

I am giving some examples of test cases and you will understand how to do this.
Case : 1
Suppose you are user and trying to log into server as a root. There is no authorized_keys file inside .ssh directory of server. You have id_dsa.pub file in your .ssh directory.
Just run following command.
# scp /home/user/.ssh/id_dsa.pub 192.168.xx.xx:/root/.ssh/authorized_keys
Case : 2
Suppose you are user and trying to log into server as a root. There is an existing authorized_keys file inside .ssh directory of server. You have id_dsa.pub file in your .ssh directory.
Just run following command.
# scp /home/username/.ssh/id_dsa.pub 192.168.xx.xx:/root/.ssh/ak && ssh 192.168.xx.xx "cat /root/.ssh/ak >> /root/.ssh/authorized_keys"
where
192.168.xx.xx : IP of the server
id_dsa.pub : public key file on client's system

Permissions for .ssh directory and authorized_keys file
The permission for directory .ssh should be 700. It means drwxr_xr_x
# chmod 700 ~/.ssh
The permission for file authorized_keys should be 600. 
It means -rw_____
# chmod 600 ~/.ssh/authorized_keys

NOTE :
You need either RSA or DSA key. You do not need to generate both.

Thursday, June 13, 2013

XAMPP : Fatal error: Call to undefined function __() in /opt/lampp/phpmyadmin/libraries/Config.class.php on line 755

XAMPP ERROR - phpmyadmin is not opened
 Fatal error: Call to undefined function __() in /opt/lampp/phpmyadmin/libraries/Config.class.php on line 755




Check the permission of the file /opt/lampp/phpmyadmin/config.inc.php
The file should not be writable. Make it read only using right click -> permissions.
or
Run following command.
$ sudo chmod 755 /opt/lampp/phpmyadmin/config.inc.php
Now restart LAMPP and try to open phpmyadmin again.
http://localhost/phpmyadmin