Monday, July 28, 2014

How to Recover Mysql Root Password - 6 Steps

This post deals with recovering the Mysql root password. If you forget the root mysql password, following steps help us to recover it.





Step 1: 

First stop the mysql service.

[root@linuxstorages ~]# /etc/init.d/mysqld stop
Stopping mysqld:                                           [  OK  ]
Step 2:

Switch mysql service to safe mode using below command



[root@linuxstorages ~]# mysqld_safe --skip-grant-tables &
   

Login as root user

mysql -u root

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
 Step 3 : 

Change database to mysql


mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

Step 4 :

 Enter New Mysql root password by below command

mysql> update user set password=PASSWORD("NewPassword") where User='root';
Query OK, 3 rows affected (0.09 sec)Rows matched: 3  Changed: 3  Warnings: 0

Step 5 :

Update the new password

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
Exit the mysql safe mode connection

mysql> quit
Bye
[2]+  Exit 1                  mysqld_safe --skip-grant-tables
Step 6 :

Stop the mysql service

[root@linuxstorages ~]# /etc/init.d/mysqld stop
140723 06:52:37 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld:                                           [  OK  ]
[1]+  Done                    mysqld_safe --skip-grant-tables
Now login via new root password as we updated



         [root@linuxstorages ~]#  mysql -u root -p

Monday, July 21, 2014

User Modification Commands in Linux

The usermod command modifies the system account files to reflect the changes that are specified on the command line.



User Modification Commands :

#usermod -c <Add comment>              - To change the comment for user.

#usermod -d <Home directory>           - To change the user home directory.

#usermod -u <UID>                               - To change the user id.

#usermod -g <Initial group>                  - To change initial group.

#usermod -G <Supplementary group> - To add sub group.

#usermod -l <Login name >                 - To change the login name.

#usermod -s <shell>                             - To change the shell.

#usermod -L <User name>                  - To lock the user account.

#usermod -U <User name>                 - To unlock the user account.

SYNTAX

To Add the Comment Entry

root@linuxstorages:~#usermod -c <Comment> <User name>
 To change the home directory

root@linuxstorages:~#usermod -d <Home Directory> <User name>

To change the user ID

root@linuxstorages:~#usermod -u <New UID> <User name>

To change the group ID

root@linuxstorages:~#usermod -g <Existing GID> <User name>

To add users to a supplementary group

root@linuxstorages:~#usermod -G ls2  ls3  [Adding user ls3 to group ls2]

root@linuxstorages:~#usermod -G ls2  ls1  [Adding user ls1 to group ls2]

To change the user's login name

root@linuxstorages:~#usermod -l <New Login name> <User name>

To change a user's shell

root@linuxstorages:~#usermod -S /sbin/nologin <User name>

Alternate way of changing a shell is by giving command in prompt as chsh

root@linuxstorages:~#chsh <User name>

To lock user's password

root@linuxstorages:~#usermod -L <User name>

To unlock a user's password

root@linuxstorages:~#usermod -U <User name>

To delete a user [This will not remove the user's home directory]

root@linuxstorages:~#userdel <User name>

To delete a user including home directory

root@linuxstorages:~#userdel -r <User name>

Wednesday, July 16, 2014

Recover & Restore Mysql Database from Crashed Server

What can be done when Mysql Server got corrupted or crashed and can't able to boot mysql server, Which contain more important databases without proper backup. To avoid this situation take proper backup for mysql server in multiple ways which help you more.


                             


Follow the following steps to recover mysql database from crashed server

Step 1:

Setup a new Mysql Server with good configuration and start the service.

Step 2:

Compress the data from crashed mysql server.

First run the crashed server in recovery mode and find the data directory for mysql server with help of
mysql configuration file and by default /var/lib/mysql  is data directory for mysql server.

root@linuxstorages:~# vi /etc/my.cnf

root@linuxstorages:~#cd /var/lib

root@linuxstorages:~#zip -r mysql.zip mysql

root@linuxstorages:~#scp mysql.zip root@newserver:/root

Step 3:

Now return to the new server. Stop the mysql service

root@linuxstorages:~#service mysqld stop

Rename the new mysql data directory for backup process.

root@linuxstorages:~#cd /var/lib

root@linuxstorages:~#mv mysql mysql_backup

Step 4:

Now extract the crashed mysql.zip file in home directory and move it to default mysql data directory path.

root@linuxstorages:~#cd /root

root@linuxstorages:~#unzip mysql.zip

root@linuxstorages:~#cp -r mysql /var/lib

Step 5:

Change the ownership of the whole mysql directory.

root@linuxstorages:~#chown -R mysql:mysql  mysql/

Step 6:

Now we need to start the mysql service in safe mode to recover the mysql data in .sql format with help of mysqldump command

root@linuxstorages:~# mysqld_safe --skip-grant-tables &
 
 we can able to login mysql server as root without password in safe mode.

root@linuxstorages:~# mysql -u root

Now it's time to dump all databases. we can dump all database in single command but best way to dump is one by one so we can export and restore the important database first.

Create a new directory to save dumped database.

root@linuxstorages:~#mkdir /mysql_backup

root@linuxstorages:~#cd mysql_backup

root@linuxstorages:~#mysqldump -u root databasename > databasename.sql

After taking complete backup stop the mysql service.

Step 7:

Return new server to original mysql data setting.

root@linuxstorages:~#cd /var/lib

root@linuxstorages:~#mv mysql mysql_old

root@linuxstorages:~#mv mysql_backup mysql

Now reboot the server.

Step 8: 

Restore the database from dump.

root@linuxstorages:~#cd /mysql_backup

root@linuxstorages:~#mysql -u root -p

mysql> create database databasename;

mysql> use databasename;

mysql> source databasename.sql;

Now you have recovered your mysql data from crashed server to new server.

Monday, July 14, 2014

User Management in Linux

Each user is associated with a unique numerical identification number called a user ID (UID). Likewise, each group is associated with a group ID (GID). A user who creates a file is also the owner and group owner of that file. The file is assigned separate read, write, and execute permissions for the owner, the group, and everyone else. The file owner can be changed only by root, and access permissions can be changed by both the root user and file owner.


Now we can create a user named linuxstorages and can see what all steps and files getting involved for creating a new user in linux.  


root@linuxstorages:~# useradd linuxstorages

root@linuxstorages:~#passwd linuxstorages
New password:
Repeat password:

The username is the only information required to add a new user; however, for exam prospective you should know some additional command-line arguments for useradd. The useradd command creates the account, but the account is locked.
To unlock the account and create a password for the user, use the command passwd [username]. By default, the user's home directory is created and the files from /etc/skel/ are copied into it.

User Administration Commands:

  • Useradd   - To create a User
  • Usermod  - To Modify user
  • Userdel    - To delete a user

While using above commands of creating a user it reflects to three files

  •   /etc/passwd
  •   /etc/group
  •  /etc/shadow


# Vi /etc/passwd  

linuxstorages:x:500:500:linux:/home/linuxstorages:/bin/bash
      |                |   |      |                     |                         |
     1               2  3     4      5              6                       7



1 Username: It is used when user logs in. It should be between 1 and 32 characters in length.

2 Password: An x character indicates that encrypted password is stored in /etc/shadow file.

3 User ID:  Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UID’s 
1-99 reserved for other predefined accounts. Further UID 100-999 are reserved for administrative and system accounts/groups.

4 Group ID (GID): The primary group ID (stored in /etc/group file)
User ID info: The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc.

5 User ID Info: The comment field. it allow you to add extra informatio about the users such as user's full name, Phone number etc.
 
6 Home directory: The absolute path to directory the user will be in when they log in. If this directory does not exists then users directory becomes / .

7 Command / Shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell

# Vi /etc/shadow


linuxstorages:$!$nfsoadfnssdfasdfv*^$bosfs:14880:0:9999:7
     |                                     |                             |     |    |      |
    1                                    2                            3    4   5     6


1 User name: It is your login name.

2 Password: It your encrypted password. The password should be minimum 6-8 characters long including special characters/digits.

3 Last password change (lastchanged): Days since that password was last changed.

4 Minimum: The minimum number of days required between password change. The number of days left before the user is allowed to change password.

5 Maximum: The maximum number of days the password is valid (after that user is forced to change password)

6 Warn: The number of days before password is expire that user is warned that his/her password must be changed.

# Vi /etc/group

linuxstorages:x:503:linuxadmin,linuxusers
       |             |     |                 |      
      1            2    3                4     
 

1 Group Name: It is the name of the group. If you run ls –la command. You will see this name printed in the group field.

2 Password: Generally password is not used, hence it is empty. It can store encrypt password. This is useful to implement privileged groups.


3 Group ID (GID) : Each user must be assigned a group id. You can see this number in your /etc/passwd file.

4 Group list: It is a list user names of users who are members of the group. The user names must be separated by commas.

Whereas unprivileged users uid&gid will start from 500.

The uid&gid between 1-499 will be for system users and groups uid&gid for processing a system process.

Friday, July 11, 2014

Nginx vs Apache Architecture and Performance

 Nginx and Apache ( pronounced “engine-x”) are free, open-source, cross-platform web servers.





Nginx was developed by Igor Sysoev for use with one of Russia’s largest sites: Rambler (a web portal). He started developing the software in 2002, but the first public release wasn’t until 2004. The popularity of nginx has since exploded and it’s now used by millions of sites.

The Apache HTTP Server, commonly referred to as Apache, is a web server notable for playing a key role in the initial growth of the World Wide Web. In 2009 Apache became the first web server to surpass the 100 million web site milestone. Since April 1996 Apache has been the most popular HTTP server on the World Wide Web.

Nginx Architecture



Nginx is an event-based web server.

Nginx and Lighttpd are probably the two best-known asynchronous servers.The main advantage of the asynchronous approach is scalability.An asynchronous server, on the other hand, is event-driven and handles requests in a single (or at least, very few) threads.

Pulling numbers from thin air for illustrative purposes, serving 10,000 simultaneous connections would probably only cause Nginx to use a few megabytes of RAM.

Apache Architecture

 

Apache is a process-based server

In a process-based server, each simultaneous connection requires a thread which incurs significant overhead
and often perform on par with an asynchronous server under light loads, under heavier loads they usually consume far more RAM which significantly degrades performance. Also, they degrade much faster on less powerful hardware or in a resource-restricted environment such as a VPS. 

Pulling numbers from thin air for illustrative purposes, serving 10,000 simultaneous connections would  consume hundreds of megabytes.

Features:

Like Apache, Nginx has all the features you would expect from a leading Web server


  •  Ability to handle more than 10,000 simultaneous connections with a low memory footprint (~2.5 MB per 10k inactive HTTP keep-alive connections.
  •  Static file serving.
  • SSL/TLS support.
  • Virtual hosts.
  • Reverse proxying.
  • Load balancing.
  • Compression.
  • Access controls.
  • URL rewriting.
  • Custom logging.
  • Server-side includes.
  • Limited WebDAV.
  • FLV streaming.
  • FastCGI. 
Web Server Status



What is C10K

The C10k problem is the problem of optimizing network sockets to handle a large number of clients at the same time. The name C10k is a numeronym for concurrently handling ten thousand connections.

Adavantage of Nginx Over Apache

  • Nginx is one of a handful of servers written to address the C10K problem.
  • Performance and efficiency.
  • Nginx is a fast, light-weight web server that can also be used as a load balancer and caching server.
  • Memory usage is very low.
  • Asynchronously with one thread, rather than using multi-threaded programming.
Advantage of Apache over Nginx

  • Apache comes with built in support for PHP, Python, Perl, and other languages.  
  • digest access authentication
  • CGI
  • administrative console
  • .htaccess



Wednesday, July 9, 2014

How Renew DHCP IP in MAC - Command Line

The 'normal' way involves the Network preference pane in the System Preferences application. Click on the "Show" pull-down menu and select either 'Network Port Configurations' or 'Active Network Ports,' depending on whether you're using 10.2 or an earlier version. Click off the box next to your network port (usually 'Built-In Ethernet,' unless you're using AirPort), then hit 'Apply Now.' Then click it back on and hit 'Apply Now' again. And as Emeril might say ... BAM! New DHCP lease.





You can also do this through the Terminal. First type the Unix command

root@linuxstorages:~#  sudo ipconfig set en0 BOOTP

root@linuxstorages:~#  sudo ipconfig set en0 DHCP


If you're working with IP addresses through the terminal

root@linuxstorages:~# ipconfig getifaddr en0 

is another useful one; it should show you your current IP address.

Tuesday, July 8, 2014

RHEL 7 Installation - 6 Steps

Redhat has recently released RHEL7 Operating system.  Some of the Changes in RHEL7 are listed below as compare with RHEL 6 .


    
  • Default root file system is XFS
  • Linux 3.10 kernel.
  • A new systemctl command replaces chkconfig and service, although scripts allow you to still use the legacy commands.
  • Docker: The biggest new addition to RHEL 7 is tight integration of Docker, the explosively popular application-virtualization technology.
  • Boot-loader GRUB2
  • Samba 4.1 and BTRFS makes an appearance

In this article we will go through the installation steps of RHEL 7, we will be installating using DVD or ISO file .

Step:1 Download the Binary DVD ISO File


Only the registerd users & who have the subscription on redhat portal can download the iso file of RHEL7. 

Use the link " Download RHEL7 Binary Dvd ISO file" Once you have download the ISO file , Burn the ISO file to DVD media or create bootable USB media as per requirement.

Step:2 Boot the system from the bootable disk and select "Install Red Hat Enterprise Linux 7.0"  :


Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

Step:3 Select the language that will be used during installation

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

Click on Continue...

Step:4 Set Customize installation options:

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

  • Adjust Date & Time, Keyboard, and Language Support preferences under Localization.
  • Modify Installation Source, Software Selection, and Network Configuration  options   under Software.
  • Under System : I have created customize partition table by clicking on the Installation Destination , preview of my customize partition table shown below :

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

Now Finally click on "Begin Installation"

Step:5 Set the root password & Create a system user

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

As we can see above installation has started. Once the installation is completed it will ask fro reboot.

Step:6 Reboot the Machine , after installation is completed :

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

click on Reboot

Step:7 Console after OS Installation

Red Hat Enterprise Linux 7 (RHEL7) Installation Steps with Screenshots

Monday, July 7, 2014

Boot Process in Linux - 6 Stages

6 Stages of Linux Boot Process



1. BIOS

BIOS stands for Basic Input/Output System

Performs some system integrity checks

Searches, loads, and executes the boot loader program.

It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically F12 of F2, but it depends on your system) during the BIOS startup to change the boot sequence.

Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it.
So, in simple terms BIOS loads and executes the MBR boot loader.

2. MBR

MBR stands for Master Boot Record.

It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda

MBR is less than 512 bytes in size. This has three components

1) primary boot loader info in 1st 446 bytes
2) partition table info in next 64 bytes
3) mbr validation check in last 2 bytes.

It contains information about GRUB (or LILO in old systems).

So, in simple terms MBR loads and executes the GRUB boot loader.

3. GRUB

GRUB stands for Grand Unified Bootloader.

If you have multiple kernel images installed on your system, you can choose which one to be executed.

GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.

GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).
Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.
root@linuxstorages:~#cat /etc/grub.conf 
‪#‎boot‬=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-194.el5PAE)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-194.el5PAE ro root=LABEL=/
initrd /boot/initrd-2.6.18-194.el5PAE.img
As you notice from the above info, it contains kernel and initrd image.
So, in simple terms GRUB just loads and executes Kernel and initrd images.

4. Kernel

Mounts the root file system as specified in the “root=” in grub.conf

Kernel executes the /sbin/init program

Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.

initrd stands for Initial RAM Disk.

initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.

5. Init

Looks at the /etc/inittab file to decide the Linux run level.

Following are the available run levels
0 – halt
1 – Single user mode
2 – Multiuser, without NFS
3 – Full multiuser mode
4 – unused
5 – X11
6 – reboot
Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate program.

Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level

If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0 and 6 means, probably you might not do that.

Typically you would set the default run level to either 3 or 5.

6. Runlevel programs

When the Linux system is booting up, you might see various services getting started. For example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level directory as defined by your run level.

Depending on your default init level setting, the system will execute the programs from one of the following directories.
Run level 0 – /etc/rc.d/rc0.d/
Run level 1 – /etc/rc.d/rc1.d/
Run level 2 – /etc/rc.d/rc2.d/
Run level 3 – /etc/rc.d/rc3.d/
Run level 4 – /etc/rc.d/rc4.d/
Run level 5 – /etc/rc.d/rc5.d/
Run level 6 – /etc/rc.d/rc6.d/
Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.

Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.

Programs starts with S are used during startup. S for startup.

Programs starts with K are used during shutdown. K for kill.

There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.

For example, S12syslog is to start the syslog deamon, which has the sequence number of 12. S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog program will be started before sendmail.






 

© 2014 Linux Storages | Updated . All rights resevered. Designed by Templateism