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.




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