Friday, June 30, 2017

how to find physical location of mysql database under linux?

If you are using MySQL, and want to know the actual physical path of MySQL, where MySQL stores the db, and its information files and the amount of data for MySQL data and MySQL index files i.e. table_name.MYD and table_name.MYI respectively, below is the easy solution.

Default MySQL Physical Directory Path is
/var/lib/mysql/ or /var/db/mysql

You can open it in Terminal  (using sudo or root access). Once you get into the folder, you can easily find the folders which are actually your database names, and in the database name folders, you can find the table names, having MYD and MYI extensions, which are actually your data and index files.




You can use the following command to locate MySQL datadir:

Method 1:

deba@linuxforfreshers.com>>grep datadir /etc/mysql/my.cnf

Sample output
datadir                        = /var/lib/mysql

Method 2:


deba@linuxforfreshers.com>>ps -eo cmd,args | grep mysql

Sample output:

/bin/sh /usr/bin/mysqld_saf /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
/usr/libexec/mysqld --based /usr/libexec/mysqld --basedir=/usr
--datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock



Method 3:

mysql -u root -p

mysql> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)


This output confirms that MySQL is configured to use the default data directory, /var/lib/mysql/.

Wednesday, June 28, 2017

how to Allow A Normal User To Run Commands As root Under Linux ?

You need to use the sudo command which is use to execute a command as another user. It allows a permitted user to execute a command as the superuser or another user, as specified in the /etc/sudoers (config file that defines or list of who can run what) file. The sudo command allows users to do tasks on a Linux system as another user.

sudo command

sudo is more more secure than su command. By default it logs sudo usage, command and arguments in /var/log/secure (Red Hat/Fedora / CentOS Linux) or /var/log/auth.log (Ubuntu / Debian Linux).
If the invoking user is root or if the target user is the same as the invoking user, no password is required. Otherwise, sudo requires that users authenticate themselves with a password by default. Once a user has been authenticated, a timestamp is updated and the user may then use sudo without a password for a short period of time (15 minutes unless overridden in sudoers).

/etc/sudoers Syntax

Following is general syntax used by /etc/sudoers file:
USER HOSTNAME=COMMAND
Where,
      USER: Name of normal user
      HOSTNAME: Where command is allowed to run. It is the hostname of the system where this rule applies. sudo is designed so you can use one sudoers file on all of your systems. This space allows you to set per-host rules.
      COMMAND: A simple filename allows the user to run the command with any arguments he/she wishes. However, you may also specify command line arguments (including wildcards). Alternately, you can specify “” to indicate that the command may only be run without command line arguments.

How do I use sudo?

If there are multiple matching entries in /etc/sudoers, sudo uses the last one. Therefore, if you can execute any command with a password prompt, and you want to be able to execute a particular command without a password prompt, you need the exception last.
myusername ALL = (ALL) ALL
myusername ALL = (root) NOPASSWD: /path/to/my/program

Give user ravi access to halt/shutdown command and restart Apache web server. First, Login as root user. Use visudo command edit the config file:
# visudo
Append the following lines to file:
ravi localhost=/sbin/halt
ravi dbserver=/etc/init.d/apache restart
Save and close file . Now ravi user can restart Apache web server by typing the following command:
$ sudo /etc/init.d/apache restart
Output:
Password:
Restarting apache web server....
The sudo command has logged the attempt to the log file /var/log/secure or /var/log/auth.log file:
# tail -f /var/log/auth.log
Sample outputs:
May 17 08:37:43 debian sudo:       ravi : TTY=pts/4 ; PWD=/home/ravi ; USER=root ; COMMAND=/etc/init.d/apache restart
If ravi want to shutdown computer he needs to type command:
$ sudo /sbin/halt
Output:
Password:
Before running a command with sudo, users usually supply their password. Once authenticated, and if the /etc/sudoers configuration file permits the user access, then the command is run. sudo logs each command run.

Examples:

a) Allow admin to run various commands:
admin ALL=/sbin/halt, /bin/kill, /etc/init.d/httpd
b) Allow user admin to run /sbin/halt without any password i.e. as root without authenticating himself:
admin ALL= NOPASSWD: /sbin/halt,/etc/init.d/mysqld
c) Allow user admin to run any command from /usr/bin directory on the system dev02:
admin dev02 = /usr/bin/*
Example:2
The following steps will help you achieve the desired output:
Create a new script file (replace dir.sh  with your desired script name):
vim ~/dir.sh
The script will be created in the user’s home directory
Add some commands that only a root or sudo user can execute like creating a folder at the root directory level:
mkdir /ravi
Note: Don’t add sudo to these commands. Save and exit (using :wq!)
Assign execute permissions to it using:
sudo chmod u+x dir.sh
Make changes so that this script doesn’t require a password.
Open the sudoers file:
sudo visudo
Add the following line at the end:
ravi ALL=(root) NOPASSWD: /home/ravi/dir.sh
Replace ravi with whatever your username is. Also make sure this is the last line. Save and exit.
Now when running the command add sudo before it like:
sudo ./dir.sh
This will run the commands inside the script file without asking for a password.




Friday, June 23, 2017

how to check list of opened and closed port in linux ?

There are different commands on both Linux and UNIX server to see what TCP/UDP ports are listening or open on your server. You can use netstat command, which prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships, etc.

Method 1:
netstat command to find open ports

Syntax :

netstat --listen

Or

netstat -l

Example:

ravi@linuxforfreshers.com>>sudo netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 *:1234                  *:*                     LISTEN    
tcp        0      0 *:8084                  *:*                     LISTEN    
tcp        0      0 192.168.122.1:domain    *:*                     LISTEN    
tcp        0      0 *:ssh                   *:*                     LISTEN    
tcp        0      0 *:ipp                   *:*                     LISTEN    
tcp        0      0 *:microsoft-ds          *:*                     LISTEN    
tcp        0      0 *:7070                  *:*                     LISTEN    
tcp        0      0 localhost:mysql         *:*                     LISTEN    
tcp        0      0 *:netbios-ssn           *:*                     LISTEN       
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN    
tcp6       0      0 [::]:ipp                [::]:*                  LISTEN    
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN    
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN    
udp        0      0 *:39505                 *:*                               
udp        0      0 *:ipp                   *:*                               
udp        0      0 *:mdns                  *:*                               
udp        0      0 *:mdns                  *:*                               

Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12950    /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     18259042 @atpl-com.canonical.Unity.Scope.rhythmbox.T516689809663571
unix  2      [ ACC ]     STREAM     LISTENING     19096    /run/user/1000/keyring-n7CcyZ/control
unix  2      [ ACC ]     STREAM     LISTENING     22589    @/tmp/.ICE-unix/3779
unix  2      [ ACC ]     STREAM     LISTENING     21540    @/tmp/dbus-u6IauIGH5I

To display open ports and established TCP connections, enter:

netstat -vatn

Example:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 0.0.0.0:1234            0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:8084            0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:631             0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:7070            0.0.0.0:*               LISTEN    

To display only open UDP ports try the following command:

netstat -vaun

Example:

ravi@linuxforfreshers.com>>sudo netstat -vaun
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
udp        0      0 0.0.0.0:39505           0.0.0.0:*                         
udp        0      0 0.0.0.0:631             0.0.0.0:*   

Using netstat -lntu

Where
     -l = only services which are listening on some port
     -n = show port number, don't try to resolve the service name
     -t = tcp ports
     -u = udp ports
     -p = name of the program          


Method 2:

Using lsof Command

To display the list of open ports, enter:
# lsof -i
To display all open files, use:
# lsof

To display all open IPv4 network files in use by the process whose PID is 10050, use:
# lsof -i 4 -a -p 10050

Another example:
# lsof -iTCP -sTCP:LISTEN

Method 3:

Using telnet

Quickest way to test if a TCP port is open (including any hardware firewalls you may have), is to type, from a remote computer (e.g. your desktop):

Syntax:

telnet hostip port_number

Example 1:

ravi@linuxforfreshers.com>>telnet 192.168.101.156 22
Trying 192.168.101.156...
Connected to 192.168.101.156
Escape character is '^]'.

Example 2:

ravi@linuxforfreshers.com>>telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.

Method 3:

Using ss command

Syntax:
ss -lntu
Example:

ravi@linuxforfreshers.com>>ss -lntu
Netid State      Recv-Q Send-Q                                                                                     Local Address:Port                                                                                       Peer Address:Port
tcp   UNCONN     0      0                                                                                                      *:39505                                                                                                 *:*    
tcp   UNCONN     0      0                                                                                                      *:631                                                                                                   *:*    
tcp   UNCONN     0      0                                                                                                      *:5353                                                                                                  *:*



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.