Monday, June 19, 2017

How create Mysql User, Database and set privileges to user on linux?

How create Mysql User, Database and set privileges to user

[ravi@linuxforfreshers.com~]$ mysql –u root –p
password:

mysql> create user 'ravi'@'localhost' identified by '123456';

mysql> create database if not exists `ravidb`;

mysql> grant all on ravidb.* to ravi@linuxforfreshers.com identified by "123456" with grant option;

[ravi@linuxforfreshers.com~]$ mysql ravidb -u ravi -p

mysql> show databases;


mysql> use ravidb;

Friday, June 9, 2017

how to convert rpm to deb and deb to rpm using alien command on linux ?

You can use alien command to convert *.deb to *.rpm file. Also, if you have a *.rpm file that you want to install on a Debian or Ubuntu, you can convert the *.rpm to *.deb file using alien command as explained in this article.

Install alien command on Debian / Ubuntu

Install alien command on Ubuntu as shown below.
# sudo apt-get install alien
Install alien command on RHEL
 You should check http://li.nux.ro/download/nux/dextop/el7/x86_64/ to see whether there’s a newer version before proceeding further:
#rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
then do,
# yum update && yum install alien
1. Convert RPM to DEB
Use alien command to convert rpm to deb file
The following example converts the linuxconf-devel rpm file to linuxconf-devel deb file. Once you generate the deb file, you can install it on Ubuntu or Debian.
Example:
ravi@linuxforfreshers.com:~$ sudo alien -d --script -k anydesk-2.9.1-2.x86_64.rpm
anydesk_2.9.1-2_amd64.deb generated

Note: You'll also notice that alien has counted up the version number. If you want to keep the original version number, you must use the -k switch:
2. Convert DEB to RPM
Use alien to convert deb to rpm file
Use alient -r option to convert a deb file to rpm file. The following example converts libsox deb file to libsox rpm file. Once you generate the rpm file, you can install it on Red Hat, or CentOS.
Example:
ravi@linuxforfreshers.com:~/Downloads$ sudo alien -r anydesk_2.9.1-1_amd64.deb
Warning: Skipping conversion of scripts in package anydesk: postinst postrm
Warning: Use the --scripts parameter to include the scripts.
anydesk-2.9.1-2.x86_64.rpm generated

In above example i am not used -k so it will update the version automatically.

Example2:

ravi@linuxforfreshers.com:~$sudo  alien -r --scripts -k anydesk_2.9.1-3_amd64.deb
anydesk-2.9.1-3.x86_64.rpm generated
Where  -r, --to-rpm                Generate a Red Hat rpm package
             --scripts                        Include scripts in package.
              -d, --to-deb                  Generate a Debian deb package (default).
             -k, --keep-version        Do not change version of generated package.

If u want more help check man alien.




Wednesday, May 24, 2017

how to find number of threads in a process on linux ?

The proc pseudo file system, which resides in /proc directory, is the easiest way to see the thread count of any active process. The /proc directory exports in the form of readable text files a wealth of information related to existing processes and system hardware such as CPU, interrupts, memory, disk, etc.

cat /proc/<pid>/status
The proc pseudo file system, which resides in /proc directory, is the easiest way to see the thread count of any active process. The /proc directory exports in the form of readable text files a wealth of information related to existing processes and system hardware such as CPU, interrupts, memory, disk, etc.
Threads: <N>
For example Find the pid of the google chrome and find the each process using how many Threads.

root@linuxforfreshers.com:~# ps -ef | grep chrome
root      28168 20969  1 15:45 ?        00:00:17 /opt/google/chrome/chrome

Where 28168 is pid of google chrmoe.

Or

root@linuxforfreshers.com:~# pidof chrome
28168


Example :

root@linuxforfreshers.com:~# cat /proc/28168/status
Name: chrome
State:   S (sleeping)
Tgid:    28168
Ngid:    0
Pid:      28168
PPid:    20969
TracerPid:       0
Uid:      1000    1000    1000    1000
Gid:      1000    1000    1000    1000
FDSize: 256
Groups:            4 24 27 30 46 108 124 128 1000
NStgid: 28168  12008  1
NSpid:  28168  12008  1
NSpgid:            3938    0          0
NSsid:  3938    0          0
VmPeak:          1087572 kB
VmSize:           1028908 kB
VmLck:                   0 kB
VmPin:        0 kB
VmHWM:          311332 kB
VmRSS:              212288 kB
VmData:            606664 kB
VmStk:      136 kB
VmExe:              109204 kB
VmLib:    45784 kB
VmPTE:                1676 kB
VmPMD:               620 kB
VmSwap:             8520 kB
HugetlbPages:        0 kB
Threads:          15
SigQ:    0/15451
SigPnd:            0000000000000000
ShdPnd:           0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001002
SigCgt: 00000001c0014efd
CapInh:            0000000000000000
CapPrm:          0000000000000000
CapEff:            0000000000000000
CapBnd:           0000003fffffffff
CapAmb:         0000000000000000
Seccomp:         2
Cpus_allowed: 3
Cpus_allowed_list:      0-1
Mems_allowed:          00000000,00000001
Mems_allowed_list:   0
voluntary_ctxt_switches:        32357
nonvoluntary_ctxt_switches:  18926

Or

root@linuxforfreshers.com:~# cat /proc/28168/status | grep Threads
Threads:          15

Method 2: Using ls command

syntax:
ls /proc/<pid>/task | wc -l

This is because, for every thread created within a process, there is a corresponding directory created in /proc/<pid>/task, named with its thread ID. Thus the total number of directories in /proc/<pid>/task represents the number of threads in the process.

Example:

root@linuxforfreshers.com:~# ls /proc/28168/task/ | wc -l
15

Method 3: Using ps command

Syntax:
ps huH p <PID_OF_U_PROCESS> | wc -l
Example :
root@linuxforfreshers.com:~# ps huH p 28168 | wc -l
15
Method 4:

Syntax: ps -eT | grep <PID_of_process> | wc -l

Example :

root@linuxforfreshers.com:~# ps -eT | grep 28168 | wc -l
15








how to find which process is using highest memory(RAM) in linux ?

If you are running out of RAM on your Linux system, you will want to find the culprit in order to solve the problem, either by reconfiguring the RAM-hungry application or by stopping it.

Method 1:

ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20
Example :

root@liuxforfrehers.com:~# ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20
1551 12.7 /opt/google/chrome/chrome
21268 11.1 /opt/google/chrome/chrome
2068 7.1 /opt/google/chrome/chrome
21416 6.4 /opt/google/chrome/chrome
28168 6.2 /opt/google/chrome/chrome
21564 5.8 /opt/google/chrome/chrome
26696 5.5 /opt/google/chrome/chrome
20802 5.2 /opt/google/chrome/chrome
14806 4.9 /opt/google/chrome/chrome
2223 4.8 /opt/google/chrome/chrome
4134 4.5 compiz
17267 3.7 /opt/google/chrome/chrome
21231 3.1 /opt/google/chrome/chrome
28135 2.4 /opt/google/chrome/chrome
21025 2.2 /opt/google/chrome/chrome
4369 2.0 /usr/lib/x86_64-linux-gnu/zeitgeist-fts
17330 1.5 /opt/google/chrome/chrome
21126 1.4 /opt/google/chrome/chrome
1324 1.1 /usr/lib/policykit-1/polkitd
3932 1.0 /usr/lib/x86_64-linux-gnu/hud/hud-service

Method 2:
Show the processes memory in megabytes and the process path.

ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n
Method 3:

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

Example:

root@liuxforfrehers.com:~# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
  PID  PPID CMD                         %MEM %CPU
26696 20969 /opt/google/chrome/chrome - 21.6  1.8
 1551 20969 /opt/google/chrome/chrome -  9.2  0.5
21268 20969 /opt/google/chrome/chrome -  9.0  1.4
 2068 20969 /opt/google/chrome/chrome -  6.2  3.7
28168 20969 /opt/google/chrome/chrome -  5.0  1.1
20802  3680 /opt/google/chrome/chrome    4.8  3.9
21564 20969 /opt/google/chrome/chrome -  4.2  0.9
 2223 20969 /opt/google/chrome/chrome -  4.0  5.1
 4134  3938 compiz                       3.8  2.3

Where    pid is Process Id
                Ppid is Parent Process Pid
               %mem is Memory usage
                %cpu is CPU usage

    

Thursday, April 27, 2017

locate command examples in linux ?

When you need to search for some files, you might typically use find command. find is a good search utility but it is slow.
However locate can search for files very quickly.
Though the locate command works very fast, it still has not out-thrown the find command because it has some limitations.
This article explains everything you need to know about locate command .

How Locate Command Works? – updatedb and updatedb.conf

When we say that locate searches very quickly, then the first question that comes into mind is that what makes locate so fast?.
Well, locate does not search the files on disk rather it searches for file paths in a database.
The database is a file that contains information about the files and their path on your system. The locate database file is located at:
/var/lib/mlocate/mlocate.db
The next logical question is, what keeps this mlocate database updated?
Well, there is another utility known as updatedb. When you execute updatedb, it scans the whole system and updates the mlocate.db database file.
So one limitation of the ‘locate’ command is its dependency on the database which can be updated by another utility ‘updatedb’. Hence, in order to get the latest and reliable results from ‘locate’ command the database on which it works should be updated at regular intervals.
We can also configure the ‘updatedb’ utility as per our needs. This can be achieved by updating the updatedb.conf. This is a configuration file that updatedb reads before updating the database. updatedb.conf is located under /etc/ :

# cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"

updatedb.conf file contains information in the form of VARIABLES=VALUES. These variables can be classified into :
    PRUNEFS : A  whitespace-separated  list of file system types (as used in /etc/mtab) which should not be scanned by updatedb.  The file system type matching is case-insensitive.  By default, no file system types are skipped. When scanning a file system is skipped, all file systems mounted in the subtree are skipped too, even if their  type  does  not  match  any entry in PRUNEFS.
    PRUNENAMES : A  whitespace-separated list of directory names (without paths) which should not be scanned by updatedb.  By default, no directory names are skipped. Note that only directories can be specified, and no pattern mechanism (e.g.  globbing) is used.
    PRUNEPATHS : A whitespace-separated list of path names of directories which should not be scanned by updatedb.  Each path name must be exactly in the form in which the directory would be reported by locate.  By default, no paths are skipped.
    PRUNE_BIND_MOUNTS :  One  of  the  strings  0,  no,  1  or yes.  If PRUNE_BIND_MOUNTS is 1 or yes, bind mounts are not scanned by updatedb.  All file systems mounted in the subtree of a bind mount are skipped as well, even if they are not bind mounts. By default, bind mounts are not skipped.
Note that all of the above configuration information can also be changed or updated through the command line options to the utility ‘updatedb’.


Practical Examples of Locate Command

1. Search a File using locate

To search a particular file using locate, just do the following
srini@linuxforfreshers.com:~$ locate mysql.conf
/etc/init/mysql.conf

The following command searches for apache2.conf in the entire system.
srini@linuxforfreshers.com:~$ locate apache2.conf
/etc/apache2/apache2.conf

You can also use “locate -0” to display all the output in one line. For example:
 Locate -0 apache2.conf

2. Display only the Count

To get the count of number of matching entry, use locate -c as shown below.
srini@linuxforfreshers.com:~$ locate -c apache2.conf
1

3.Restrict the Locate Output

In the following example, locate command displayed several entries.
$ locate passwd
/etc/passwd
/etc/passwd-
/etc/dovecot/conf.d/auth-passwdfile.conf.ext
/etc/pam.d/passwd
/etc/security/opasswd
/etc/vsftpd/passwd
/lib64/security/pam_unix_passwd.so
/usr/bin/gpasswd
/usr/bin/htpasswd
/usr/bin/ldappasswd
/usr/bin/mksmbpasswd.sh
/usr/bin/passwd
If you want to display only certain number of records, use locate -l option and specify how many records you want to see in the locate command output.
For example, the following displays only 5 records (Even when locate command finds several records..)
srini@linuxforfreshers.com:~$ locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/alternatives/vncpasswd
/etc/alternatives/vncpasswd.1.gz
/etc/cron.daily/passwd

4. Ignore Case in Locate Output

The locate command by default is configured to accept the file name in a case sensitive manner. In order to make the results case insensitive, we can use the -i option :
In the following example, we created two files with both lowercase and uppercase.
# cd /tmp
# touch new.txt NEW.txt

# updatedb
If you use the locate command only with the lowercase, it will find only the lowercase file.
# locate new.txt
/tmp/new.txt
Use locate -i, which will ignore case, and look for both lowercase and uppercase file.
$ locate -i new.txt
/tmp/NEW.txt
/tmp/new.txt
/usr/share/doc/samba-common/WHATSNEW.txt.gz