Monday, July 29, 2013

How to see disk space usage in linux

Sometimes we need to know the hard drive usage statistics.
In Linux we can use df command to find out how much disk space is used.

df command will show you Used and available space, File system mount point, File systen capacity, The number of inodes available,

The basic usage is follow:

df [options]

df [options] /path_to_dev

Example

df

df -h

The following command will show information only for the /home directory partiton

df /home

df -h /home

use Option -T to see file system type

df -T


Below is the list for df command options

  -a, --all             include dummy file systems
  -B, --block-size=SIZE  use SIZE-byte blocks
      --total           produce a grand total
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
  -H, --si              likewise, but use powers of 1000 not 1024
  -i, --inodes          list inode information instead of block usage
  -k                    like --block-size=1K
  -l, --local           limit listing to local file systems
      --no-sync         do not invoke sync before getting usage info (default)
  -P, --portability     use the POSIX output format
      --sync            invoke sync before getting usage info
  -t, --type=TYPE       limit listing to file systems of type TYPE
  -T, --print-type      print file system type
  -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE

Sunday, July 28, 2013

Enable mod_rewrite for apache in Ubuntu

For clean URL we need to use .htaccess and mod rewrite.
In ubuntu we can apply the following command to enable the rewrite modules.

sudo a2enmod rewrite

Change all occurrence of "AllowOverRide None" to "AllowOverRide All".

sudo vi /etc/apache2/sites-available/default

Now restart apache

/etc/init.d/apache2 restart

You are done.

Tuesday, July 23, 2013

CentOS Sync time from NTP server

First we need to install NTP service.

To install NTP

yum install ntp

Now Synchronize the system clock with pool.ntp.org server

ntpdate pool.ntp.org

You can auto start NTP service during system startup

chkconfig ntpd on

Finally Start the NTP service

service ntpd start

Monday, July 22, 2013

Delete files older than x days/minutes in Linux

Sometimes we need to delete older than x-days. In Linux we can use following command

find /path/ -mtime +5 -exec rm {} \;

The above command will delete all file older than 5 days.

If we need to remove all files older than 10 minutes, apply the following command

find /path/ -mmin +10 -exec rm {} \;

The above command will delete all files older than 5 minutes.

If you don't want to delete files but see the list, use ls instead of rm.

find /path/ -mtime +5 -exec ls {} \;

The above command will list all files older than 5 days.

Note that there are spaces between rm, {}, and \; 

Wednesday, July 17, 2013

Linux Basic - Part 2


If you want to see path settings in linux, apply the following command

echo $PATH

it will show current path settings.

if you run echo * command, it will show a list of files/folder in current directory.

Now if you want to see a list of files/folders which are starting with D, apply echo D*

If you need to see a list of files/folders which are ending with s, apply echo *s

If you need to know your home directory location, use echo ~

If you apply echo /usr/*/share it will show you a list which location match /usr/ then anything then /share

If you want to do match in linux terminal, use as echo $((2+2)) Here You must start with $(( and end with ))

You can also do a long match like echo $(( ((2+2)-(1+1))*2 )) it will output 4

Tuesday, July 16, 2013

Using alias in Linux - Temporary and Permanent

Sometimes we need to make alias of command or group of command including of any options, arguments and redirection.
We can pre-set a string to run any command using alias.

alias command allows a user to create simple name for commands including arguments or options.

The format is alias name='command'

For example:

alias install="sudo apt-get install"
alias poweroff="sudo shutdown -h now"

After making alias of above command, you can install package with install command instead of sudo apt-get install and you can shutdown the PC with simple command poweroff.

we can also use alias ls="ls -al" to create a alias of ls command with -al option.
now if we run ls command it will execute ls -al command.
Now if we need to run original ls command we can by adding a backslash before the command
example \ls it will execute the original ls command.

If we need to create permanent alias we have to put alias in .bashrc file under user home directory.

The alias should be typed in below the line that says # User specific aliases and functions.

System-wide aliases can be put in the /etc/bashrc file.

System must be restarted after creating permanent alias.

Monday, July 8, 2013

Changing Hostname in Linux

To see current hostname

# hostname

change hostname temporary

# hostname myhostname.com

Now see new hostname

# hostname

To change the hostname permanently

# vi /etc/sysconfig/network

change the hostname variable then save and exist

Now you have add your new hostname into /etc/hosts file

# vi /etc/hosts

Add your new hostname and If you want you can remove the old hostname from the file

Then save and exist

Now its time to restart your server.

You can also try restarting network service

In Ubuntu/Debian

# service network restart

In CentOS/Red Hat/Fedora

# service networking restart


If you want you can use GUI tool too

# system-config-network

And now follow the wizard

Finally Restart the server

Thursday, July 4, 2013

Install Apache MySQL PHP (LAMP stack) in ubuntu

LAMP stack is a group of Apache, MySQL, PHP for Linux.

To install LAMP stack you will need to have root privileges.
To get root privileges

sudo su

OR use sudo before every command

First we are updating apt-get databases

sudo apt-get update

Now we are installing apache 2 package

sudo apt-get install apache2

Apache is installed now. Try to access your website using localhost or server IP address.

To get the server IP

ifconfig eth0 | grep inet | awk '{ print $2 }'

Now we can proceed to install PHP 5

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

It may ask you some question, choose yes and continue

You can install other PHP externsions/modules which you need.
To see a list of PHP modules

apt-cache search php5-

It will show you a list of php5 moudules

Then you can install php5 modules as follow

sudo apt-get install name_of_the_module name_of_the_module_2 ....

You can install multiple modules by separating the name of each module with space

Now It's time to install MySQL

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

You will be asked to give a mysql root password. You can also set mysql root password later.

Once MySQL has been installed, we should activate it with this command

sudo mysql_install_db

Now Finish up it by running MySQL set up script

sudo /usr/bin/mysql_secure_installation

You will be asked to enter current mysql root password.
Then the prompt will ask you if you want to change the root password.
Choose N if you dont want to change current password and proceed to next step.
Complete all the steps and now you are done.

Start MySQL and Apache

sudo service mysql start
sudo service apache2 start

If its already started, restart them

sudo service mysql restart
sudo service apache2 restart

Wednesday, July 3, 2013

Resetting MySQL root Password

If you forget MySQL Root Password, you can reset it.
You need to have root access on the system OS (CentOS, Ubuntu etc..)

First Stop the current MySQL process.

service mysqld stop

OR

/etc/init.d/mysql stop

After stopping the current MySQL process, Start MySQL in safe mode with --skip-grant-tables option

mysqld_safe --skip-grant-tables &

After running MySQL in safe mode, you can run mysql update statement to change the root password.

Connect to MySQL server with root user

mysql -u root

Select/Connect to mysql database

use mysql;

Update root password

update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';

Flush MySQL privileges

flush privileges;

Exit MySQL

quit

You are all done now.

Stop MySQL safe mode instance

/etc/init.d/mysql stop

OR

service mysqld stop

Now It's time to start MySQL in normal mode.

service mysqld start

OR

/etc/init.d/mysql start