Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Change Default User Home Directory While Adding a New User:


You can add user using regular useradd command with  -d option:

#useradd –d /tmp/home/admin admin
#passwd admin

How Do I Change Existing User's Home Directory?

You need to use the usermod command to set the user's new login directory. The syntax is as follows:
#usermod -m -d /path_to_new_home_directory userName
Where,
1.       -d dirnanme : Path to new login (home) directory.

2.       -m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

Find out directory size in Linux:



du -ch | grep total                     (or)                 du --total | grep total       

This would display one line output showing the total size of the current directory including all the sub-directories.

Or

du -sk * | sort –n


The above command will sort the folders by size.

How to extend the ext4 primary partition

The below steps can be followed if you need to expand the ext4 primary partition of an Ubuntu 12.04 server partition. The same procedure can be followed to increase the primary partition on Ubuntu VMs running on VMware vSphere server or Citrix Xenserver.

Note:
You should have Ubuntu Live CD or GParted ISO to achieve this task.
1.    Make CD-ROM as the default boot device in the machine's BIOS settings and then boot with your Ubuntu LiveCD
2.    Run gParted as shown below

3.    Now you will see the GParted screen like this.

4.    Unmount the swap partition by right-clicking on lock icon and click swap off.
5.    Delete the swap partition.
6.    Delete the extended partition.
7.    Resize your primary partition to desired size.
8.    Allocate your swap back again to whatever you like - create an extended partition and then allocate as linux swap. Remember about click swapon.
And that's it. Reboot the machine and now you have a primary partition with more space!


How to Backup MySQL Database using mysqldump utility?

To take a backup of MySQL database or databases, the database must exist in the database server and you should have access to it.

The format of the command would be. 

# mysqldump -u [username] –p[password] [database_name] > [dump_file.sql]

How to Backup Remote MySQL Database

# mysqldump –h ipaddress -u [username] –p[password] [database_name] > [dump_file.sql]

How to Backup a Single MySQL Database?

# mysqldump -u root -proot123 MYDB1 > MYDB1.sql

How to Backup Multiple MySQL Databases?

# mysqldump -u root -proot123 --databases MYDB1 MYDB2 MYDB3 > MYDB1_MYDB2_MYDB3.sql

How to Backup All MySQL Databases?

# mysqldump -u root -proot123 --all-databases > alldb.sql

How to Backup MySQL Database Structure Only?

# mysqldump -u root -proot123 --no-data MYDB1 > MYDB1structure.sql

How to Backup MySQL Database Data Only?

# mysqldump -u root -proot123 --no-create-db --no-create-info MYDB1 > MYDB1data.sql

How to Backup Single Table of Database?

# mysqldump -u root -proot123 MYDB1 TABLE1 > TABLE1_MYDB1.sql

How to Backup Multiple Tables of Database?

# mysqldump -u root -proot123 MYDB1 TABLE1 TABLE2 > TABLE1_TABLE2_MYDB1.sql

Note:
To restore a MYSQL Database, you can refer my blog

Overview of Linux Filesystem Tree

Ubuntu adheres to the Filesystem Hierarchy Standard for directory and file naming. This standard allows users and software programs to predict the location of files and directories. The root level directory is represented simply by the slash /

At the root level, all Ubuntu systems include these directories:

/bin is a place for most commonly used terminal commands, like ls, mount, rm, etc.

/boot contains files needed to start up the system, including the Linux kernel, a RAM disk image and bootloader configuration files.

/dev contains all device files, which are not regular files but instead refer to various hardware devices on the system, including hard drives.

/etc contains system-global configuration files, which affect the system's behavior for all users.

/home home sweet home, this is the place for users' home directories.

/lib contains very important dynamic libraries and kernel modules

/media is intended as a mount point for external devices, such as hard drives or removable media (floppies, CDs, DVDs).

/mnt is also a place for mount points, but dedicated specifically to "temporarily mounted" devices, such as network filesystems.

/opt can be used to store addition software for your system, which is not handled by the package manager.

/proc is a virtual filesystem that provides a mechanism for kernel to send information to processes.

/root is the superuser's home directory, not in /home/ to allow for booting the system even if /home/ is not available.

/sbin contains important administrative commands that should generally only be employed by the superuser.

/srv can contain data directories of services such as HTTP (/srv/www/) or FTP.

/sys is a virtual filesystem that can be accessed to set or obtain information about the kernel's view of the system.

/tmp is a place for temporary files used by applications.

/usr contains the majority of user utilities and applications, and partly replicates the root directory structure, containing for instance, among others,/usr/bin/ and /usr/lib.

/var is dedicated variable data that potentially changes rapidly; a notable directory it contains is /var/log where system log files are kept.


Important considerations:

The following is a list of important considerations regarding directories and partitions. Note that disk usage varies widely given system configuration and specific usage patterns. The recommendations here are general guidelines and provide a starting point for partitioning.

·        The root partition / must always physically contain /etc, /bin, /sbin, /lib and /dev, otherwise you won't be able to boot. Typically 150–250MB is needed for the root partition.

·        /usr: contains all user programs (/usr/bin), libraries (/usr/lib), documentation (/usr/share/doc), etc. This is the part of the file system that generally takes up most space. You should provide at least 500MB of disk space. This amount should be increased depending on the number and type of packages you plan to install. A standard Ubuntu desktop requires a minimum of 1.5GB here. A generous workstation or server installation should allow 4–6GB.

·        /var: variable data like news articles, e-mails, web sites, databases, the packaging system cache, etc. will be placed under this directory. The size of this directory depends greatly on the usage of your system, but for most people will be dictated by the package management tool's overhead. If you are going to do a full installation of just about everything Ubuntu has to offer, all in one session, setting aside 2 or 3 GB of space for /var should be sufficient. If you are going to install in pieces (that is to say, install services and utilities, followed by text stuff, then X, ...), you can get away with 300–500 MB. If hard drive space is at a premium and you don't plan on doing major system updates, you can get by with as little as 30 or 40 MB.
·        
       /tmp: temporary data created by programs will most likely go in this directory. 40–100MB should usually be enough. Some applications — including archive manipulators, CD/DVD authoring tools, and multimedia software — may use/tmp to temporarily store image files. If you plan to use such applications, you should adjust the space available in /tmpaccordingly.
·        
       /home: every user will put his personal data into a subdirectory of this directory. Its size depends on how many users will be using the system and what files are to be stored in their directories. Depending on your planned usage you should reserve about 100MB for each user, but adapt this value to your needs. Reserve a lot more space if you plan to save a lot of multimedia files (pictures, MP3, movies) in your home directory.




Sources: Source1 and Sources2

How to find the age of a Linux process

To find out the age for any running process, you can use the following command:

#ps aux
……………..
nagios    1206  0.0  0.0  41184   584 ?        Ss   Mar11   2:28 /usr/sbin/nrpe
root      1227  0.0  0.0   7392   564 ?        Ss   Mar11   0:59 /usr/sbin/vnsta
root      1320  0.0  0.0      0     0 ?        S    Mar11   0:18 [flush-202:0]
root      1439  0.0  0.5 105616  5416 ?        Ss   Mar11   0:46 /usr/sbin/apach
root      1474  0.0  0.0  12744   492 hvc0     Ss+  Mar11   0:00 /sbin/getty -L
root      1476  0.0  0.0  14500   504 tty1     Ss+  Mar11   0:00 /sbin/getty -8
www-data  5295  0.0  0.7 121964  7584 ?        S    11:49   0:00 /usr/sbin/apach
root      5353  0.0  0.4  91168  4676 ?        Ss   11:50   0:00 sshd: root@nott
root      5539  0.0  0.0  12768   936 ?        Ss   11:50   0:00 /usr/lib/openss
root      5542  0.0  0.4  91168  4672 ?        Rs   11:50   0:00 sshd: root@pts/
root      5675  0.0  0.4  21624  4212 pts/0    Ss   11:50   0:00 -bash
backuppc  5838  0.1  2.8  84612 29260 ?        S    11:52   0:06 /usr/bin/perl /
backuppc  5841  0.0  0.0      0     0 ?        Z    11:52   0:00 [ssh] <defunct>
……………………….


In the above example, you can only see that process 1206 has been running since Mar 11, but there is no indication of the time it was started.
But the below command can give you more appropriate answer:
stat /proc/<pid> 

For example, here's an exact time-stamp for process 1206, which "ps" command shows only as Mar 11:
# stat /proc/1206

File: `/proc/1206'
  Size: 0               Blocks: 0          IO Block: 1024   directory
Device: 3h/3d   Inode: 10025       Links: 8
Access: (0555/dr-xr-xr-x)  Uid: (  113/  nagios)   Gid: (  121/  nagios)
Access: 2014-03-11 17:25:59.864000001 +0530
Modify: 2014-03-11 17:25:59.864000001 +0530
Change: 2014-03-11 17:25:59.864000001 +0530

 Birth: -

How to Repair GRUB2 in Ubuntu (Graphical method):

Ubuntu and many other Linux distributions use the GRUB2 as the default boot loader. Suppose, if you convert your Citrix Xenserver Ubuntu VM to VMWare ESXi VM (not always), or if you install Windows after installing Ubuntu, or if you overwrite your MBR, then you won’t be able to boot into Ubuntu.
In any of the above cases, you can use Boot Repair tool (which is a graphical tool) to easily restore GRUB2 from an Ubuntu live CD or USB drive.
Note: You need an internet connection in the server from live CD to achieve this.

Boot-Repair is free software, licensed under GNU-GPL. 
There are two ways to get Boot-Repair:
1st option: Get a CD including Boot-Repair
The easiest way to use Boot-Repair is to burn one of the following disks and boot on it.

1.      Boot-Repair-Disk is a CD starting Boot-Repair automatically.
2.      Boot-Repair is also included in Linux-Secure-Remix.

Remark: you can also install the ISO on a live-USB (eg via UnetBootin or LiliUSB or Universal USB Installer).
2nd option: install Boot-Repair in Ubuntu
- choose "Try Ubuntu"
- connect to the Internet
- open a new Terminal, then type:
sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
- Press ENTER.
- Then type:
sudo apt-get install -y boot-repair
- Press ENTER

Follow the below steps to repair GRUB2:
Step 1: If you have the media you installed Ubuntu from, insert it into your computer and restart.
If you don’t have, download a Ubuntu live CD and burn it to a disc or install it on a USB flash drive. You can also download a dedicated Boot Repair live CD.
Step 2: After booting into the live Ubuntu environment, open a terminal and run the following commands to install or open Boot Repair:
sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install -y boot-repair
boot-repair
Step 3: The Boot Repair window will appear once you run the boot-repair command. Click the “Recommended repair” button to repair GRUB2 with a single click.
http://pix.toile-libre.org/upload/original/1335260967.png
Step 4: Restart your computer after using the Boot Repair tool. Ubuntu should boot up normally.

 

How to install Java (JDK) on Ubuntu server

The following instructions will help you to install Oracle Java 7 JDK on Ubuntu 12.04 Server. Please follow these steps only when you install Java on a fresh server.
To remove the open JDK and to install the Oracle Java, you can refer my post here.
Step 1: First check whether the Ubuntu Linux operating system architecture is 32-bit or 64-bit. Run the following command,
root@ubuntusrv:~# uname -a
Linux ubuntusrv 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
So in my case it is 64 bit OS
Step 2: Check whether Java is installed or not.
root@ubuntusrv:~# java -version
The program 'java' can be found in the following packages:
 * default-jre
 * gcj-4.6-jre-headless
 * openjdk-6-jre-headless
 * gcj-4.5-jre-headless
 * openjdk-7-jre-headless
Try: apt-get install <selected package>
The above output tells us that java is not installed.

Step 3: Then create a directory called java in /ust/local. This directory will be used to hold all java binaries
root@ubuntusrv:~# sudo mkdir -p /usr/local/java
Step 4: Now download the latest version of java from here


Note:
You can download the Java from any windows machine and then you can use WinSCP to copy the same

Step 5: Now copy the downloaded file and extract the same using the following commands
root@ubuntusrv:/usr/local/java# cd /usr/local/java/
root@ubuntusrv:/usr/local/java# sudo tar xvzf server-jdk-7u51-linux-x64.tar.gz
Now remove the downloaded archive file
root@ubuntusrv:/usr/local/java# rm server-jdk-7u51-linux-x64.tar.gz

Step 6: Now add the java variables to /etc/environment file as follows

JAVA_HOME=/usr/local/java/jdk1.7.0_51
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/java/jdk1.7.0_51/bin"

Note:
          :/usr/local/java/jdk1.7.0_51/bin has to be appended to the existing line
          jdk1.7.0_51 is the directory extracted using tar command in the previous step

Step 7: Now restart the server and check the Java installation with the following command

root@ubuntusrv:~# java -version

You will get a response like this

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Step 8: Run the below commands to update the java installation
 sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_51/bin/java" 1

sudo update-alternatives --set java /usr/local/java/jdk1.7.0_51/bin/java
You are done. Your Server is ready with latest java.