Showing posts with label Linux Commands. Show all posts
Showing posts with label Linux Commands. Show all posts

Wednesday, 7 March 2012

How to open a port in linux machine

You can use netcat (nc) to open a port in linux machines.

# nc -k -l 1000  -------- port number 1000 is open in local machine

it can manipulate, create or read/ write TCP/IP connections.

 File transfer

Copy a large folder over a raw tcp connection. The transfer is very quick (no protocol overhead) and you don't need to mess up with NFS or SMB or FTP or so, simply make the file available on the server, and get it from the client. Here 192.168.0.5 is the server IP address.

server#  tar -cf - -C VIDEO_TS . | nc -l -p 4444  
# Serve tar folder on port 4444
client#  nc 192.168.0.5 4444 | tar xpf - -C VIDEO_TS 
# Pull the file on port 4444

server#  cat largefile | nc -l 5678                # Server a single file
client#  nc 192.168.0.5 5678 > largefile      # Pull the single file

server#  dd if=/dev/da0 | nc -l 4444   # Server partition image
client#  nc 192.168.0.5 4444 |dd of=/dev/da0
# Pull partition to clone
client#  nc 192.168.0.5 4444 |dd of=da0.img   # Pull partition to file

Chat feature using nc

google and yahoo can chat over a simple TCP socket. The text is transferred with the enter key.

google# nc -lp 4444
yahoo # nc 192.168.1.1 4444





Monday, 5 March 2012

Useful Linux Commands

# ethtool eth0   -------- Show the ethernet status (replaces mii-diag)
# ethtool -s eth0 speed 100 duplex full # Force 100Mbit Full duplex
# ethtool -s eth0 autoneg off # Disable auto negotiation
# ethtool -p eth1 # Blink the ethernet led

# ip link show  # Display all interfaces on Linux
# ip link set eth0 up  # Bring device up
# ifconfig eth0 up
# ip addr show   # Display all IP addresses on Linux
# ip neigh show    # Similar to arp -a

#  arp -a  # Check the router (or host) ARP entry
# traceroute ctechz.blogspot.in  # Print the route path to destination
# netstat -s   # System-wide statistics for each network protocol
# arping 192.168.16.254  # Ping on ethernet layer

Routing

# route -n  # Linux or use "ip route"
#  netstat -rn # Linux, BSD and UNIX

# route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.16.254
#route add -net 192.168.20.0 netmask 255.255.255.0 dev eth0
# route add default gw 192.168.51.254
# route delete -net 192.168.20.0 netmask 255.255.255.0


add -p to make the route persistent.

Configure additional IP address

# ifconfig eth0 192.168.50.254 netmask 255.255.255.0 # first ip
# ifconfig eth0:0 192.168.51.254 netmask 255.255.255.0  
             # Second IP

Changing the MAC Address

# ifconfig eth0 down
# ifconfig eth0 hw ether 00:01:02:03:04:05

Port's In Use

# netstat -an | grep LISTEN 
# lsof -i  # List all Internet connections
# netstat -anp --udp --tcp | grep LISTEN 
# netstat -tup  # List active connections to/from system
# netstat -tupl # List listening ports from system
# netstat -ano  # Windows

IP Forwarding for routing in linux machines

# cat /proc/sys/net/ipv4/ip_forward  # Check IP forward 0=off, 1=on
# echo 1 > /proc/sys/net/ipv4/ip_forward
 or edit /etc/sysctl.conf with:
net.ipv4.ip_forward = 1

 

Sunday, 4 March 2012

How to check for open files in linux

This is useful to find out which file is blocking a partition which has to be unmounted and gives a typical error of:

# umount /home/
umount: unmount of /home failed: Device busy

  umount impossible because a file is locking home

Find opened files on a mount point with fuser or lsof:

# fuser -m /home   # List processes accessing /home

# lsof /home/ctechz/

About an application:
# lsof -p 3324 

About a single file:
# lsof /usr/local/src/firefox/firefox

FreeBSD and most Unixes

# fstat -f /home  # for a mount point
# fstat -p PID    # for an application with PID
# fstat -u user   # for a user name


To list all the open files on the var filesystem:
# lsof +D /var

To list all open files in your current directory only:
# lsof +d .

To list all open Internet files:
# lsof -i

To list all files currently open by user joe:
# lsof -u jake

 To list all files open by syslog-ng (this is a great quick way to find logs!):
# lsof -c syslog

To list all files open by pid:
# lsof -p PID

List sockets related to port 80
# lsof -i :80

Wednesday, 29 February 2012

How to get the Disk informations in linux


# hdparm -I /dev/sda  ------ information about the IDE/ATA disk (Linux) / disk performance
# fdisk /dev/sda6        ------ Display and manipulate the partition table
# smartctl -a /dev/sda1 --- Display the disk SMART info
# diskinfo -v /dev/ad2 ---- information about disk (sector/size) FreeBSD
# dmidecode               -------  dmidecode  is  a  tool for dumping a computer’s DMI(some say SMBIOS) table contents in a human-readable format. This table contains a description of the system’s hardware components.

To find the mounted file system in our system

# mount | column -t


Singnals in Linux

Terminate or send a signal with kill or killall

# kill -s TERM 4712  -------- same as kill -15 4712
# killall -1 httpd    -------- Kill HUP processes by exact name
# pkill -9 http          -------- Kill TERM processes by (part of) name
# pkill -TERM -u www ------- Kill TERM processes owned by www
# fuser -k -TERM -m /home ------ Kill every process accessing /home
                                                           (to umount)


Important signals are:

-1  HUP (hang up)
-2  INT (interrupt)
-3  QUIT (quit)
-9  KILL (non-catchable, non-ignorable kill)
-15 TERM (software termination signal)
















How to set a process Priority in Linux

Change the priority of a running process with nice and renice. Negative numbers have a higher priority, the lowest is -20 and "nice" have a positive value.

 # renice -5 586   ------ stronger priority

 Start the process with a defined priority with nice. Positive is "nice" or weak, negative is strong scheduling priority.

# nice -n -5 top   # Stronger priority (/usr/bin/nice)

# nice -n 5 top      # Weaker priority (/usr/bin/nice)
# nice +5 top

While nice changes the CPU scheduler, an other useful command ionice will schedule the disk IO. This is very useful for intensive IO application.


# ionice c3 -p123 # set idle class for pid 123

# ionice -c2 -n0 firefox # Run firefox with best effort and
                                             high priority
# ionice -c3 -p$$ # Set the actual shell to idle priority

nohup

Use nohup to start a process which has to keep running when the shell is closed

# nohup  sh run.sh  -b 0.0.0.0 &
# nohup ping -i 60 > ping.log &

 

 

 
















Monday, 27 February 2012

Useful linux commands

# id  ---------- Show the active user id with login and group

# id username ---------- Shows details for a particular user

last  ----------- Show last logins on the system

# who  --------- Show who is logged on the system

# who -u ------- shows the pid also of the logged in user, so easy to kill that particular user only. # kill -9 PID

# useradd -c "Colin Barschel" -g admin -m colin  ----- all in one line

ulimit -a
The shell limits are governed by ulimit. The status is checked with ulimit -a,
To change the open files limit from 1024 to 10240 do:
# ulimit -n 10240    , This is only valid within the shell.

 Login users and applications can be configured in /etc/security/limits.conf

# cat /etc/security/limits.conf
*  hard  nproc 250  # Limit user processes
asterisk  hard  nofile  409600 # Limit application open files

Use chkconfig to configure the programs that will be started at boot in a runlevel.

# chkconfig --list  ------- List all init scripts
# chkconfig --list sshd ----- Report the status of sshd
# chkconfig sshd --level 35 on ------ Configure sshd for levels 3 and 5
# chkconfig sshd off  -------- Disable sshd for all runlevels

Some Kernel Modules

#  lsmod  -------- List all modules loaded in the kernel
# rmmode --------  To remove a module
# modprobe usb-storage --------- To load a module
# insmod ----- To load a module 





Sunday, 26 February 2012

TOP Command Help in linux



# top

Line1: Gives System present time, up time of the machine, number of users logged in, Load average on system at 5, 10, 15 min interval.

Line2: Gives total number of process on the machine, number of running process, number of sleeping process, number of stopped process, number of Zambie process.

Line3: Gives you CPU details

Line4 & 5: Gives RAM and SWAP details.

Line6: To execute top command shortcuts

From Line7: dynamically displayed top process results.

l --To display or to hide load average line
t --To display or to hide task/cpu line
1 --To display or hide all other CPU's
m --to display or to hide RAM and SWAP details
s --To change the time interval for updating top results(value is in sec's)
R --To sort by PID number
u -- Press u then username to get only that user process details
P --To sort by CPU utilization
M --To sort by RAM utilization
c --To display or hide command full path
r --To renice a process, press r then the PID no then the renice value to renice a process.
k --To kill a process, press k then PID number then enter to kill a process
w --To save the modified configuration permanently.
q --To quit the top command.
h --for getting help on top command

System Load and Statistics

# top  -------- display and update the top cpu processes

# mpstat 1  ---------- display processors related statistics

# vmstat 2  ---------- display virtual memory statistics

# iostat 2    ---------- display I/O statistics

For checking system load we can also use

# uptime   and

# w

Hardware Informations

# dmesg  ---------- Detected hardware and boot messages

# dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8 ----------
 Read BIOS

cat /proc/cpuinfo  ---------- CPU model / informations

# cat /proc/meminfo ---------- Hardware memory

# watch -n1 'cat /proc/interrupts'  ---------- Watch changeable interrupts continuously

# free -m  ---------- Used and free memory
# vmstat

# cat /proc/devices  ---------- Configured devices

# lspci    ---------- list all PCI devices, it shows Ethernet card manufacturer etc.

lsusb  ---------- list USB devices.

lshal  ---------- Show a list of all devices with their properties

# dmidecode  ---------- Show DMI/SMBIOS: hw info from the BIOS

sysctl vm   ---------- Memory usage

# sysctl  ---------- read/write system parameters, configure kernel parameters at runtime.

# sysctl -a | grep mem  ---------- Kernel memory settings and info

# sysctl dev  ---------- Configured devices


System Related Informations

# uname -r    -------- print the kernel release
2.6.18-164.el5

# uname -i     --------- print the hardware platform
i386

# uname -o    --------- print the operating system
GNU/Linux

# uname -s    --------- print the kernel name
Linux

# cat /etc/issue -------- Show OS version
CentOS release 5.4 (Final)

# uptime           -------- Show how long the system has been running + load
15:52:07 up 5 days,  6:36,  3 users,  load average: 0.04, 0.06, 0.02

# hostname      ------- system's host name
cloudcc.ctechz.blogspot.com

# hostname -i   ------- Display the IP address of the host.

# man hier       ------- Description of the file system hierarchy

# last reboot  ------- Show system reboot history





Tuesday, 7 February 2012

Linux networking commands

Displaying IP address of an interface
# ifconfig eth0

Setting up IP address using ifconfig
# ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up

To up and down an ethernet device
# ifup eth0
# ifdown eth0

Showing status of ethernet interface eth0
# ethtool eth0

Showing status of wireless interface eth1
# iwconfig eth1

Listing network interfaces
# ip link show

List internet services on a system
# netstat -tupl

List active connections to/from system
#  netstat -tup






Encrypt a file in linux

To Encrypt a file in linux

# gpg -c file
The out put will be file.gpg

To Decrypt a file in linux

# gpg file.gpg




Saturday, 4 February 2012

How to add Route in linux


To check the routing table in linux we can use

# netstat -nr 

# route -n 

# ip route show

How to Change Your system's Default Gateway

Your server needs to have a single default gateway. DHCP servers will automatically assign a default gateway to DHCP configured NICs, but NICs with configured static IP addresses will need to have a manually configured default gateway.

 # route add default gw 192.168.1.1 eth0 
                                                                     wlan0

 Adding Temporary Static Routes

Reference to the 192.168.0.0 network has to be preceded with a -net switch and the subnet mask and gateway values also have to be preceded by the netmask and gw switches respectively. 

# route add -net 192.168.0.0 netmask 255.0.0.0 gw 192.168.1.254  eth0

If you wanted to add a route to an individual server, then the "-host" switch would be used with no netmask value.

# route add -host 10.0.0.1 gw 192.168.1.254 eth0


A universal way of making this change persistent after a reboot would be to place this route add command in the file /etc/rc.d/rc.local, which is always run at the end of the booting process.


 Adding Permanent Static Routes

permanent static routes are added on a per interface basis in files located in the /etc/sysconfig/network-scripts directory. The filename format is route-interface-name so the filename for interface wlan0 would be route-wlan0.

 we would have to configure file /etc/sysconfig/network-scripts/route-wlan0 to look like this:
#
# File /etc/sysconfig/network-scripts/route-wlan0
10.0.0.0/8 via 192.168.1.254


How to Delete a Route 
# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 eth0

 the file /etc/sysconfig/network-scripts/route-wlan0 will also have to be updated so that when you reboot the server will not reinsert the route. Delete the line that reads:

10.0.0.0/8 via 192.168.1.254



Displaying the routing cache with ip route show cache

The routing cache is used by the kernel as a lookup table analogous to a quick reference card.Routes existing in the route cache are periodically expired.

# ip route show cache 192.168.100.17


Displaying statistics from the routing cache with ip -s route show cache

# ip -s route show cache 192.168.100.17



Wednesday, 1 February 2012

Rename multiple file in linux


If a directory contains so many files with the extension .txt and if you want to rename it to some other extensions follow the step

# cd /directory/contains/file

shows result before renaming
# rename -n 's/\.txt$/\.bak/' *.txt (file we want to change)

# rename  's/\.txt$/\.html/' *.txt

# rename 's/.txt/.md/i' *

It is useful if the directory contains a lot of files to edit in a single step.

-n, --no-act
 No Action: show what files would have been renamed

* Also we can convert upper case letters to lowercase and  vice versa

# cd /directory/contains/file

# rename 'y/A-Z/a-z/' *

* Renaming file name from:

ctechz.txt0
ctechz.txt1
ctechz.txt2
ctechz.txt3
ctechz.txt4
ctechz.txt5

To file name:

ctechzblog.txt
ctechzblog.txt0
ctechzblog.txt1
ctechzblog.txt2

# rename 's/ctechz/ctechzblog/g' ctechz.txt*