Category: Linux & Open Source

Linux administration on Ubuntu and CentOS: commands, scripting, networking, services (DNS, DHCP, Samba, NFS, Apache, Squid) and LPIC study notes.

  • Change MAC address on the interface in case the virtual box clone gave you the same MAC addresses or MAC address is causing a problem

    ip link show ethX

    ip link set dev ethX down

    ip link set dev ethX address XX:XX:XX:XX:XX:XX

    ip link set dev ethX up

  • Duplex and Speed on Linux

    To check duplex and speed on the Smoothwall:

    #ethtool ethA (always recommended to set the auto-negotiate on the switch side for the interface where Smoothwall is connected)

    (for example to change the MTU size on the fly for testing purposes) (Shows the network speed and information about the interface)

    To change the speed, duplex and the auto-negotiate for an interface:

    #ethtool -s ethX speed 100 duplex full autoneg off (to turn off the auto negotiate)

    OR

    #vi /etc/actions/secondboot/9999custom

    (insert the following in the file)

    #! /bin/sh

    /usr/sbin/ethtool -s ethC duplex full speed 100 autoneg off

    (now press escape :wq! )

    #chmod +x /etc/actions/secondboot/9999custom

    #/etc/actions/secondboot/9999custom

    (Finally, you need to reboot to exectute the startup script created, if required)

  • Speed test (speedtest) / slow speed issue

    Install iperf client on windows from:

    https://iperf.fr/

    or

    Ubuntu using:

    sudo apt-get install iperf

    Try the following from the client machines inside the network and/or from the Smoothwall:

    (using speedtest sites through the Smoothwall are not recommended)

    iperf -c iperf.securityinspection.com

    iperf -c iperf.saratov.ertelecom.ru

    iperf -c st2.nn.ertelecom.ru

    iperf -c iperf.ips.versatel.de

    iperf -c iperf.testdebit.info

    iperf -c iperf.cds.com

    iperf -c iperf.lan.kth.se

    (More servers can be found here: http://en.wikipedia.org/wiki/Iperf)

  • To see live traffic on an interface especially external interface (updates every second)

    watch -n 1 /sbin/ifconfig ethC

    To check the latest NIC drivers in the database for the Smoothwall’s linux kernel

    #lspci -nnn (to check the ethernet controllers and the hardware detected by smoothwall)(To check the company ID and the unique hardware ID i.e. PCI-ID) (to check the ethernet controllers and the hardware detected by smoothwall)

    or # lspci -nn | grep Eth

    #cat /lib/modules/3.13.0-1-smp/modules.pcimap | grep 1533 | grep 8086

    (you can put the kernel you are looking the drivers in instead of 3.13.0-1-smp and the current kernel can be checked using #uname -r)

  • To enable or disable the network interface

    #ip link set ethX up

    #ip link set ethX down

  • CPU usage Linux scripts and commands

    Script to run top command after every 5 minutes CPU utilization:

    #!/bin/sh

    top -b -n 1 | head -17 >> /var/log/logging.txt

    echo “=========” >> /var/log/logging.txt

    To show CPU and process information (press q to quit):

    #top (resource usage and running processes)

    (uptime and the load average)

    (CPU usage: us for users, sy for system and ni for nice processes)(id is idle time, wa is wait state)

    (if the wa is high that means the smoothwall is busy with extensive logs or reports)

    (press ‘F’ to set the fields and ‘d’ to set it)

    #htop (resource usage and running processes)(same as top but better layout)

  • Swap usage Linux scripts and commands

    Swap usage Linux scripts and commands

    To see SWAP usage by processes script:

    #!/bin/sh

    # Get current swap usage for all running processes

    # Erik Ljungstrom 27/05/2011

    SUM=0

    OVERALL=0

    for DIR in `find /proc/ -maxdepth 1 -type d | egrep “^/proc/[0-9]”` ; do

    PID=`echo $DIR | cut -d / -f 3`

    PROGNAME=`ps -p $PID -o comm –no-headers`

    for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk ‘{ print $2 }’`

    do

    let SUM=$SUM+$SWAP

    done

    echo “PID=$PID – Swap used: $SUM – ($PROGNAME )”

    let OVERALL=$OVERALL+$SUM

    SUM=0

    done

    echo “Overall swap used: $OVERALL”

    ./getswap.sh | egrep -v “Swap used: 0” |sort -n -k 5

    To see free memory and swap space:

    free -hm

    Top command to calculate swap usage:

    top

    (SWAP = VIRT – RES)

  • Linux scripts and commands to see RAM/memory utilization

    Script to run top command after every 5 minutes RAM utilization:

    #!/bin/sh
    top -b -n 1 -o %MEM  | head -17 >> /var/log/logging.txt
    echo “=========” >> /var/log/logging.txt

    Change the permissions to execute,
    chmod 777 script1

    move the script to cronfrequently to run it after every 5 mins,
    mv script1 /etc/actions/cronfrequently/

    (use cronhourlybyfour directory for 15 mins execution)

    To see the RAM usage by processes:

    ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort

    top -b -n 1 -o %MEM  | head -17

    To see RAM stats:

    vmstat

    To see free memory and cached space:

    free -hm

    To see a specific process CPU or RAM utilization using top::

    top -p `pgrep PROCESS_NAME | tr “\\n” “,” | sed ‘s/,$//’`
    (e.g. datastore.pl)

    Script to see what processes are using  max RAM every 5 minutes:

    /var/log/9999monitorram

    #!/bin/sh

    OutDir=/var/log/monitoring

    File=RamUsage$(date +%d-%m-%y)

    mkdir -p $OutDir

    echo “——————————–$(date +%d-%m-%y)—–$(date +%H:%M:%S)—————————————“>> $OutDir/$File

    ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort | tail -n20>> $OutDir/$File

    ln -s 9999monitorram /etc/actions/cronfrequently/

  • 5 Commands to Get Public IP using Linux Terminal

    Public IP are used for communication between computers over internet. A computer running with public ip is accessible all over world using internet. So we can say that it is the identity of computer on internet. Now the question is how do we know our public ip. For computers having GUI can easily get there ips using web tools like this but how we get ips of computers having terminal access only. Solution is here – use one of the following commands to find public ip of your system using Linux terminal. These are also useful to use in shell script.

    Get Public IP using Linux Terminal

    Command 1. Using wget

    # wget http://ipecho.net/plain -O - -q ; echo
    

    Command 2: Using curl

    # curl ipecho.net/plain; echo
    

    Command 3: Using wget

    # wget http://observebox.com/ip -O - -q ; echo
    

    Command 4: Using curl

    # curl icanhazip.com
    

    Command 5: Using curl

    # curl ifconfig.me
    

    Get Public IP in Shell Script

    We can simply use following commands in our shell script get our computers public ip and store them in a variable to use anywhere in shell script.

    #!/bin/bash
    
    PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
    echo $PUBLIC_IP
    
    

    You could use a DNS request instead of HTTP request to find out your public IP:

    C:\> nslookup myip.opendns.com resolver1.opendns.com

    It uses resolver1.opendns.com dns server to resolve the magical myip.opendns.com hostname to your ip address.

    Unix version:

    $ dig +short myip.opendns.com @resolver1.opendns.com
  • Martian Packet Messages

    Occasionally, you might see messages like the following in your Linux kernel messages:

    martian source 192.168.1.1 from 127.0.0.1, on dev eth1<br />
            ll header: 52:54:00:98:99:d0:52:54:00:de:d8:10:08:00

    There’s a lot of discussion out there about what this means, but not a lot about how to trace down the source.  Hopefully this will provide some insight into what the messages actually mean, and how to understand them.

    What are martian packets?

    A martian packet is one that comes from an “impossible source”.  For example, this might be from an RFC 1918 reserved netblock routed across the internet, or any packet with a “localnet” (127.0.0.1, etc.) IP on an interface other than the loopback interface.  In other words, martian packets mean either something is misconfigured, or someone is trying to do something very sneaky.  RFC 1812 provides more detail about what is and is not a valid routable IPv4 address.

    So what do those messages mean?

    Let’s start with the first message: it contains the destination and source IP addresses, as set in the IPv4 packet, and the network interface on which the packet arrived.  Despite how it reads, the first address is NOT the source address in the packet — it is, in fact, the destination address!  The 2nd IP is the IP that was contained in the IPv4 packet, but this isn’t of very much help in finding the sending host — remember, the whole point of this message is an indication that the source of the packet is “suspect.”

    Fortunately, the 2nd line contains a lot of information for us, but it’s printed in a format that’s very hard to read.  This is the link-layer header of the packet that contained the erroneous source address.  Almost all machines these days use physical connectivity that looks like ethernet, and the log entry above is based on an Ethernet Frame.  (PPP connections will probably look different, but I’ve never seen this message on anything but ethernet.)  The ethernet header is at least 14 bytes long, and most of us will only see 14 byte headers.  The longer headers are used with 802.1q VLANs, but most people will never see those, so we’re going to discuss only the 14 byte headers.

    The 14 bytes above, printed in colon-separated hex, contain the destination MAC address, the source MAC address, and either the length or the ethertype code.  The MAC addresses are in standard notation, and the first 6 octets are the destination, with octets 7-12 representing the source.  These will help you track down where the packet came from (though it might be your upstream router, in which case you’ll need to do more investigative work).

    The last 2 bytes take more explaining.  If the value is less than 0x0600, then it represents the length of the data field for 802.3 compliant ethernet frames.  Greater values, as seen above, are actually EtherType codes.  This will mostly be 0x0800, which means an IPv4 packet is contained in the data segment.  (After all, this error does come from an IPv4 issue.)  Other values might be 0x8100 (802.1q VLAN-tagged packet) or 0x0806 (ARP packet).  There are many more, but you’re unlikely to see them in this context.

    Long Story Short

    Look at bytes 7-12 of the link-layer header.  That’s the MAC of the last machine to touch the packet.  Look there for the cause (if it’s a router, look upstream from there, and then figure out why you’re not filtering the martian packets on your router).  Yes, the source MAC could be spoofed, but then someone’s playing games on your local network segment.  (Which probably means they have physical access, or you have a terribly designed network.  Remember, physical access=pwned.)