Tag: linux

How to test PHP installation

To test a successful php installation. We can check the PHP version in your machine or test the entire PHP setup using following

open a file in which your web directory name it anything like php_test.php and write the following content.

?php
phpinfo();
?>

Save the file and open the file in your browser like localhost/php_test.php.

Then you will notice that if php is installed the following image will be displayed.

PHP successful installation screenshot

There is a video tutorial that explains the same as above.

Fix ubuntu freezning or hard disk switching to read mode due to bad sector or file system error

This is one of the most common problems that one might encounter these days. This can be due to hardware of software.

If the issue is because of software we can fix it using below mentioned ways..

First print out the output of the command dmesg and see if there are any errors printed in red color which does symbolize that you have a file system error.

Note: sda5 means the disk that you want to check for errors. You can see the name of your disk by using the command df -h

Install smartctl a tool to check your hard disk for errors.

After installing smartctl run the following command.

smartctl -t long /dev/sda5

Then carefully see the output of above command it displays the time it will take to complete the test you have just started. To view the test run the below command.

smartctl -l selftest /dev/sda5

Now run below commands to check the health of your hard disk.

smartctl -a /dev/sda5
smartctl -H /dev/sda5

You can also add a line to your grub file located at /etc/default/grub or modify the line if it already exists.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash libata.force=noncq"

This will make sure that while rebooting you run a force check for errors in your file system. Be careful because you might need to back up your data as you may lose some of it.

Another way to go into recovery mode is to press ESC while rebooting and choose recovery mode. This will make sure to fix file system errors also you can choose to run fsck/resume normal boot in the options provided.

How to use the command CD in Linux Terminal – Ubuntu, Fedora, CentOS

The command CD stands for change directory. CD can be used in many ways to navigate between multiple directories just like we traverse in GUI.

cd <filename>

The above command will move to the directory specified in the angular brackets. But you have to make sure that the name given here is present in the current directory. You can see what are the folders/files in the current directory using the command LS. Also you can check your current working directory using the command PWD.

cd /path/to/another/folder

The above command will take to another folder that is far away from the current working directory. Make sure that the specified path exists. You can check the path of the current structure using the command TREE.

cd ..

The above command will move the current working directory to one level back.

cd 

The above command without any arguments will take you to the home directory no matter where your current working directory is.

There is a video example for the same, watch it below

Basics of vim – Create, edit, save and quit in vim.

How to use vim in Linux.

Vim has two modes. Command mode and insert mode. In command mode we can tell the editor to substitue, edit, insert, append, save, quit etc. By default vim opens in command mode. To switch to insert mode press “i” in command mode without quotes. Here you can edit the file with whatever content you want to.

To switch to command mode press “Esc” key. Here we can give commands to save and quit. To save press :w then the file will be saved and to save and quit press :wq.

Here is a video enplaning the same.

How to use “ls” command in Linux terminal

“ls” means list contents of a directory. We have various options with ls that can be used together to view files in a convenient way according to our requirement.

ls -a

ls with -a option shows all hidden files.

ls -l

ls with -l prints long listing format about each file. It consists of information like permissions(read/write/execute), owner, file size, file type(directory or file or symbolic link), last modified time and the name of the file.

ls -l can be used with another options like -t or/and -r. “-t” will sort files by modified time and “-r” reverses the order they are being sorted.

To view examples for above commands see the video below.

Setting Reverse Proxy in Apache Ubuntu

Suppose we have a web app running on a port. For example nodejs runs server on a port. For accepting incoming connections on this port without actually exposing the port number is what we look for.

To solve this we can add few lines in our apache configuration to tell our web server to accept connections on a port.

Lets assume we are trying to access this url

http://127.0.0.1:8080/mybankapp

Open the file /etc/apache2/sites-available/000-default.conf using any editor and paste the below lines. You may take a backup of the above file before doing so.

<VirtualHost *:80>
    ProxyPreserveHost On

    ProxyPass /mybankapp http://127.0.0.1:8080/mybankapp
    ProxyPassReverse /mybankapp http://127.0.0.1:8080/mybankapp
</VirtualHost>

Before we restart apache we may enable some modules.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

After we execute above command now we may restart apache.

sudo systemctl restart apache2

Now we can access our web app without the port number.

View Difference between Files and Folders Graphically in Linux With Meld

There are terminal utilities like ‘diff’ and ‘vimdiff’ in Linux to visualise the differences in two files. But sometimes this visual presentation can be difficult to be viewed and understanding it might take some time as it can be confusing to analyse.

Using meld we can visually the differences in a less confusing way and understand it more easily. Here is a screenshot of the same.

Bonus advantage is that we can view the graphical difference of two folders also.

To install Meld you can install from command line using the following command

sudo apt install meld

Swap two columns using Vim in Linux

Suppose you have a file seperated with a delimiter like tab or comma and you want to swap them manually it would take so much time if the file is huge. But if you want to do it using just a single command it would take 5 seconds.

Here is how it is done.

  • Open the file to be edited using following command.
  • vim file.txt
  • Then enter the following substitution commnad.
  • %s/\(.*\)\t(.*\)/\2\t\1/g
  • That’s it you have swapped two columns.
  • Now just save the file and quit vim using :wq

There is a video of the same below

How to use bc command for addition, subtraction, multiplication and division. Use Linux terminal as calculator

Linux command line is very powerful. Let us see how can we use it as a calculator.

Open a terminal window and write bc -ql.

Here there are two options. ‘q’ stands for quiet mode which means it doesnot print un necessary things. ‘l’ stands for mathlib which will operate mostly on decimals and other maths operations.

For addition.

For Subtraction

For Multiplication

For division

To quit the bc command type in ‘quit’.

To do more using bc command open man bc there you can see about more of its usage.

How to Scp, Ssh And Rsync Without Prompting For Password.

Whenever you need to use scp to copy files, it asks for passwords. Same with rsync as it (by default) uses ssh as well. Usually scp and rsync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying when the password is asked every time.

Lets say you want to copy between two hosts user_src and user_dest.user_src is the host where you would run the scp, ssh or rsync command, irrespective of the direction of the file copy!

1. On user_src,
run this command as the user that runs scp/ssh/rsync

 $ ssh-keygen -t rsa

This will prompt for a passphrase. Just press the enter key. It’ll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub: Your public key has been saved in <your_home_dir>/.ssh/id_rsa.pub

2. Transfer the id_rsa.pubfile to user_dest by either ftp, scp, rsync or any other method.

3. On user_dest, login as the remote user which you plan to use when you run scp, ssh or rsync on user_src.

4. Copy the contents of id_rsa.pub to ~/.ssh/authorized_keys

$ cat id_rsa.pub >>~/.ssh/authorized_keys 
$ chmod 700 ~/.ssh/authorized_keys

If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.

  1. Note that ssh by default does not allow root to log in. This has to be explicitly enabled on user_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin
    from no to yes. Don’t forget to restart sshd so that it reads the modified config file. Do this only if you want to use the root login.
  1. Note that ssh by default does not allow root to log in. This has to be explicitly enabled on user_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin
    from no to yes. Don’t forget to restart sshd so that it reads the modified config file. Do this only if you want to use the root login.

Well, that is it. Now you can run scp, ssh and rsync on user_src connecting to user_dest and it won’t prompt for the password. Note that this will still prompt for the password if you are running the commands on user_dest connecting to user_src. You can reverse the steps above (generate the public key on user_dest and copy it to user_src) and you have a two way transfer ready without prompting for password!