Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

Friday, November 18, 2016

How to kill a process on a port on ubuntu

Sometimes, when we strying to up an service and get message like: the port *** is used by another service. And we are notable to start our app to use that port. In that case, we are in need to free the port by stopping the running process. For this purpose a first we need to find out the process which is using that port.

By using the below query we can get the process id using this port:

sudo lsof -t -i:8080 [For this case 8080 is our reference port number]

to get the process in details, we can use:
ps -ef | grep


And then we can kill the process by :
kill -9

We can use the below command at once to kill the process:
sudo kill $(sudo lsof -t -i:8080)

Thursday, May 26, 2016

Terminator Termina Shortcut in Ubuntu


Its very useful to use shortcut than moving cursor operate terminal. Here is the shortcut for operating Terminator terminal in Ubuntu :


Monday, November 2, 2015

Useful Sar (Sysstat) Examples for UNIX / Linux Performance Monitoring

SAR  stands for System Activity Reporter. The name indicates the functionality of it.
SAR is the system monitoring utility inside the sysstas.  Using sar you can monitor performance of various Linux subsystems (CPU, Memory, I/O..) in real time. We can easily install it using the following command:

sudo apt-get install sysstat
(or)
yum install sysstat
(or)
rpm -ivh sysstat-10.0.0-1.i586.rpm
After installing, verify the sar version using “sar -V”. Version 10 is the current stable version of sysstat.

$ sar -V
Finally, make sure sar works. For example, the following gives the system CPU statistics 3 times (with 1 second interval).

$ sar 1 3
Following are the other sysstat utilities.
  • sar collects and displays ALL system activities statistics.
  • sadc stands for “system activity data collector”. This is the sar backend tool that does the data collection.
  • sa1 stores system activities in binary data file. sa1 depends on sadc for this purpose. sa1 runs from cron.
  • sa2 creates daily summary of the collected statistics. sa2 runs from cron.
  • sadf can generate sar report in CSV, XML, and various other formats. Use this to integrate sar data with other tools.
  • iostat generates CPU, I/O statistics
  • mpstat displays CPU statistics.
  • pidstat reports statistics based on the process id (PID)
  • nfsiostat displays NFS I/O statistics.
  • cifsiostat generates CIFS statistics.
This article focuses on sysstat fundamentals and sar utility.

Collect the sar statistics using cron job – sa1 and sa2

Create sysstat file under /etc/cron.d directory that will collect the historical sar data.
# vi /etc/cron.d/sysstat
*/10 * * * * root /usr/local/lib/sa/sa1 1 1
53 23 * * * root /usr/local/lib/sa/sa2 -A

1. CPU Usage of ALL CPUs (sar -u)

This gives the cumulative real-time CPU usage of all CPUs. “1 3″ reports for every 1 seconds a total of 3 times. Most likely you’ll focus on the last field “%idle” to see the cpu load.
$ sar -u 1 3

2. CPU Usage of Individual CPU or Core (sar -P)

If you have 4 Cores on the machine and would like to see what the individual cores are doing, do the following.
“-P ALL” indicates that it should displays statistics for ALL the individual Cores.
In the following example under “CPU” column 0, 1, 2, and 3 indicates the corresponding CPU core numbers.
$ sar -P ALL 1 1
“-P 1″ indicates that it should displays statistics only for the 2nd Core. (Note that Core number starts from 0).
$ sar -P 1 1 1

3. Memory Free and Used (sar -r)

This reports the memory statistics. “1 3″ reports for every 1 seconds a total of 3 times. Most likely you’ll focus on “kbmemfree” and “kbmemused” for free and used memory.
$ sar -r 1 3

4. Swap Space Used (sar -S)

This reports the swap statistics. “1 3″ reports for every 1 seconds a total of 3 times. If the “kbswpused” and “%swpused” are at 0, then your system is not swapping.
$ sar -S 1 3

5. Overall I/O Activities (sar -b)

This reports I/O statistics. “1 3″ reports for every 1 seconds a total of 3 times.
Following fields are displays in the example below.
  • tps – Transactions per second (this includes both read and write)
  • rtps – Read transactions per second
  • wtps – Write transactions per second
  • bread/s – Bytes read per second
  • bwrtn/s – Bytes written per second
$ sar -b 1 3

6. Individual Block Device I/O Activities (sar -d)

To identify the activities by the individual block devices (i.e a specific mount point, or LUN, or partition), use “sar -d”
$ sar -d 1 1

7. Display context switch per second (sar -w)

This reports the total number of processes created per second, and total number of context switches per second. “1 3″ reports for every 1 seconds a total of 3 times.
$ sar -w 1 3

8. Reports run queue and load average (sar -q)

This reports the run queue size and load average of last 1 minute, 5 minutes, and 15 minutes. “1 3″ reports for every 1 seconds a total of 3 times.
$ sar -q 1 3

9. Report network statistics (sar -n)

This reports various network statistics. For example: number of packets received (transmitted) through the network card, statistics of packet failure etc.,. “1 3″ reports for every 1 seconds a total of 3 times.
sar -n KEYWORD
KEYWORD can be one of the following:
  • DEV – Displays network devices vital statistics for eth0, eth1, etc.,
  • EDEV – Display network device failure statistics
  • NFS – Displays NFS client activities
  • NFSD – Displays NFS server activities
  • SOCK – Displays sockets in use for IPv4
  • IP – Displays IPv4 network traffic
  • EIP – Displays IPv4 network errors
  • ICMP – Displays ICMPv4 network traffic
  • EICMP – Displays ICMPv4 network errors
  • TCP – Displays TCPv4 network traffic
  • ETCP – Displays TCPv4 network errors
  • UDP – Displays UDPv4 network traffic
  • SOCK6, IP6, EIP6, ICMP6, UDP6 are for IPv6
  • ALL – This displays all of the above information. The output will be very long.

10. Report Sar Data Using Start Time (sar -s)

When you view historic sar data from the /var/log/sa/saXX file using “sar -f” option, it displays all the sar data for that specific day starting from 12:00 a.m for that day.
Using “-s hh:mi:ss” option, you can specify the start time. For example, if you specify “sar -s 10:00:00″, it will display the sar data starting from 10 a.m (instead of starting from midnight) as shown below.
You can combine -s option with other sar option.
For example, to report the load average on 26th of this month starting from 10 a.m in the morning, combine the -q and -s option as shown below.
$ sar -q -f /var/log/sa/sa23 -s 10:00:01
There is no option to limit the end-time. You just have to get creative and use head command as shown below.
For example, starting from 10 a.m, if you want to see 7 entries, you have to pipe the above output to “head -n 10″.
$ sar -q -f /var/log/sa/sa23 -s 10:00:01 | head -n 10

Monday, October 19, 2015

Some Network Related Commands for Linux

TO resolve network related issue/connection issue in linux server, some commands are so much helpful.

• finding host/domain name and IP address - hostname
• test network connection – ping
• getting network configuration – ifconfig
• Network connections, routing tables, interface statistics – netstat
• query DNS lookup name – nslookup
• communicate with other hostname – telnet
• outing steps that packets take to get to network host – traceroute
• view user information – finger
• checking status of destination host - telnet





hostname
hostname with no options displays the machines host name

hostname –i displays the IP address for the current machine
hostname –d displays the domain name the machine belongs to
hostname –f displays the fully qualified host and domain name


ping
It sends packets of information to the user-defined source. If the packets are received, the destination device sends packets back. Ping can be used for two purposes

1. To ensure that a network connection can be established.
2. Timing information as to the speed of the connection.

If you do ping www.google.com it will display its IP address. Use ctrl+C to stop the test. 

ifconfig
View network configuration, it displays the current network adapter configuration. It is handy to determine if you are getting transmit (TX) or receive (RX) errors.

netstat
Most useful and very versatile for finding connection to and from the host. You can find out all the multicast groups (network) subscribed by this host by issuing "netstat -g"
netstat -nap | grep port will display process id of application which is using that port
netstat -a  or netstat –all will display all connections including TCP  and UDP  
netstat --tcp  or netstat –t will display only TCP  connection
netstat --udp or netstat –u will display only UDP  connection
netstat -g will display all multicast network subscribed by this host.

nslookup
If you know the IP address it will display hostname. To find all the IP addresses for a given domain name, the command nslookup is used. 
You can also use nslookup to convert hostname to IP Address and from IP Address from hostname.

traceroute
A handy utility to view the number of hops and response time to get to a remote system or web site is traceroute. Again you need an internet connection to make use of this tool.

finger
View user information, displays a user’s login name, real name, terminal name and write status. this is pretty old unix command and rarely used now days.
In ubuntu finger package is not installed by default. To install that, we need to install that using command:


      sudo apt-get install finger

telnet
Connects destination host via telnet protocol, if telnet connection establish on any port means connectivity between two hosts is working fine.
telnet hostname port   will telnet hostname with the port specified. Normally it is used to see whether host is alive and network connection is fine or not.

Wednesday, October 7, 2015

Installing Gradle in Ubuntu

Install Gradle on Ubuntu Linux
Step-by-step instructions
Gradle is a Java build system. It uses a clean, simple configuration syntax and is used by Android as the default build system. For API development, Gradle is supported by Spring.
Installation is quick and simple. We are going to install a private copy so that root permissions are not required during development.

Download Gradle

mkdir -p ~/opt/packages/gradle && cd $_
wget https://services.gradle.org/distributions/gradle-2.3-bin.zip
unzip gradle-2.3-bin.zip

Install and setup Gradle

Next, we will create a symlink that provides a shorter path to the specific Gradle version. The symlink will allow us to upgrade Gradle later without changing any other configuration.
ln -s ~/opt/packages/gradle/gradle-2.3/ ~/opt/gradle
Open your .profile file in vi, emacs, or gedit.
gedit ~/.profile
Paste the following at the bottom of your .profile file.
# Gradle
if [ -d "$HOME/opt/gradle" ]; then
    export GRADLE_HOME="$HOME/opt/gradle"
    PATH="$PATH:$GRADLE_HOME/bin"
fi
Finally, source your .profile and test gradle.
source
~/.profile
which gradle
gradle -version
Congratulations, you now have a working Gradle installation.


Saturday, September 12, 2015

To create and add Swap file in Ubuntu Linux after installation

What is a swap file on Ubuntu server or desktop system?

As a sysadmin it is necessary to add more swap space after installation on the server. Swap file allows Ubuntu Linux to use hard disk to increase virtual memory.
Virtual Memory = RAM + Swap space/file
Virtual Memory (1GB) = Actual RAM (512MB) + Swap space/file (512MB)
When the Ubuntu server runs low on memory, it swaps a section of RAM (say an idle program like foo) onto the hard disk (swap space) to free up memory for other programs. Then when you need that program (say foo again), kernel swapped out foo program, it changes places with another program in RAM.

Procedure to add a swap file on a Ubuntu Linux

Open the Terminal app or use the ssh client to get into the remote server. Login as a root user using sudo command:
 
sudo -s
 

Create a swap file command

Type the following command to create a 2GB swap file on Ubuntu:# dd if=/dev/zero of=/swapfile bs=1G count=2
Sample outputs:
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 20.2256 s, 106 MB/s
Verify that file has been created on the server:# ls -lh /swapfile
Sample outputs:
-rw-r--r-- 1 root root 2.0G Oct 29 14:07 /swapfile

Creating swap space using fallocate command instead of dd command

Instead of the dd command, you can use the the faster fallocate command to create swap file as follows:# fallocate -l 1G /swapfile-1
# ls -lh /swapfile-1

Sample outputs:
-rw-r--r-- 1 root root 1.0G Oct 29 14:11 /swapfile-1

Secure the swap file

Type the following chmod command and chown command to secure and set correct file permission for security reasons:# chown root:root /swapfile
# chmod 0600 /swapfile
# ls -lh /swapfile

Sample outputs:
-rw------- 1 root root 2.0G Oct 29 14:07 /swapfile
A world-readable swap file is a huge local vulnerability. The above commands make sure only root user can read and write to the file.

Turn on the swap file

First, use the mkswap command as follows to enable the swap space on Ubuntu:# mkswap /swapfile
Sample outputs:
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=10231c61-6e55-4dd3-8324-9e2a892e7137
Finally, activate the swap file, enter:# swapon /swapfile

Verify new swap file and settings on Ubuntu

Type the following command# swapon -s
Sample outputs:
Filename    Type  Size Used Priority
/dev/sda5                               partition 3998716 704 -1
/swapfile                               file  2097148 0 -2
You can also run the following commands to verify swap file and its usage:# grep -i --color swap /proc/meminfo
# top
# htop
# atop

How can I disable swapfile on Ubuntu?

You need to use the swapoff command as follows:# swapoff /swapfile
# swapon -s

Update /etc/fstab file

You need to make sure the swap file enabled when server comes on line after the reboot. Edit /etc/fstab file, enter:# vi /etc/fstab
Append the following line:
/swapfile none            swap    sw              0       0
Save and close the file.

Tuning the swap file i.e. tuning virtual memory

You can tune the following two settings:
  1. swappiness
  2. min_free_kbytes
  3. vfs_cache_pressure

How do I set swappiness on a Ubuntu server?

The syntax is:# sysctl vm.swappiness=VALUE
# sysctl vm.swappiness=20

OR
# echo VALUE > /proc/sys/vm/swappiness
# echo 30 > /proc/sys/vm/swappiness

The value in /proc/sys/vm/swappiness file controls how aggressively the kernel will swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60. To make changes permanent add the following line to/etc/sysctl.conf:
 
echo 'vm.swappiness=30' >> /etc/sysctl.conf
 
For database server such as Oracle or MySQL I suggest you set a swappiness value of 10. 

Sunday, August 23, 2015

To install subversion client in ubuntu

To install subversion client in ubuntu

  1. Install the subversion commandline tool using sudo apt-get install subversion.
  2. Use the client by typing the command svn command [options] [args].
  3. Do not do anything else. The server will not start and you can happily use the client without the server. You will also not have GUI access.
The subversion package
includes the Subversion client (svn), repository administration tools (svnadmin, svnlook) and a network server (svnserve).
Installing the whole packages (including both server and client) might seem like a lot of overhead, but it is the most easily maintainable solution. You easily get future updates, security releases and more. Also if ever one of your users desperately needs a subversion server, it is only a few commands away.
Use svn --version for the modules included:
* ra_neon : Module for accessing a repository via WebDAV protocol using Neon.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - with Cyrus SASL authentication
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - handles 'http' scheme
  - handles 'https' scheme
and use svn --help for more information. The available commands illustrate that the subversion package is the way to go.
   add
   blame (praise, annotate, ann)
   cat
   changelist (cl)
   checkout (co)
   cleanup
   ...

Sunday, August 16, 2015

Installing Oracle client libraries on Ubuntu

The latest RPM files can be downloaded from Oracle instant client download section.
The three minimal RPMs required are “Basic”, “SQLPlus” and the “SDK”.
In case of Ubuntu 14.04 LTS, Intel on 64-bit we will need to download the files mentioned below:
  • oracle-instantclient12.1-basic-*.x86_64.rpm
  • oracle-instantclient12.1-sqlplus-*.x86_64.rpm
  • oracle-instantclient12.1-devel-*.x86_64.rpm
Note: The asterisk (*) would be the latest version available for download.
Convert and Install
The RPM filess need to be converted from .rpm to .deb, you’ll need to use the commandline tool alien. I the tool is not already installed it needs to be installed as shown bellow:
Now convert and install the packages in this order:
Now test the client libs using the sqlplus commandline tool. Use either sqlplus or sqlplus64 depending on your platform:
Replace the connection string with the appropriate parameters.
If you get an error message “missing libaio.so.1 file”, run:
If you get an error message “sqlplus64: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory”, then you need to add it to the system library list. To do so you need to locate the Oracle liberaries path, which would be something like:
/usr/lib/oracle/12.1/client64/lib/
Now, add this path to the system library list. CREATE and ADD the line to a NEW file:
Add inside the path:
/usr/lib/oracle/12.1/client64/lib/
Run now the dynamic linker run-time bindings utility:
For any further errors or issues while trying to run sqlplus, please refer the Ubuntu help page.