Navigation Bar

Tuesday, June 11, 2024

Connect two linux machines with ethernet cable or a LAN crossover cable

We first need to assign a static ip address to both the machines.

You can do this through the command line or using the GUI.

Go to Setting -> Network -> Wired (Here click on the gear wheel).



Here in the IPv4 settings, set the IPv4 method to manual and assign an address, netmask and gateway.


Click on Apply once the changes are done.
For the changes to take effect restart the network on command prompt as follows.

$ nmcli networking off
$ nmcli networking on
Make the same network changes on the other linux machine also. So you will have the following.
Machine 1
IP Address : 192.168.178.36
Netmask    : 255.255.255.0
Gateway    : 192.168.178.1
Machine 2
IP Address : 192.168.178.37
Netmask    : 255.255.255.0
Gateway    : 192.168.178.1
To enable both machines to talk to each other, we have to install the following rpms 
ifplugd and openssh on both the machines 
rsync on the master 

If the rpms are already there in the yum repository you can install them directly.
# yum install openssh
# yum install rsync
Any missing rpms can be downloaded from the rpmfind repository
https://rpmfind.net/linux/rpm2html/search.php

https://rpmfind.net/linux/rpm2html/search.php?query=ifplugd 
# rpm -ivh ifplugd-0.27-1.2.el4.rf.x86_64.rpm
To start the ifplugd service you need to add the following 2 scripts. 
To edit run
# systemctl --full --force edit ifplug@.service 
or you can 
# vi /etc/systemd/system/ifplug@eth0.service
[Unit]
Description=Interface plug monitor (interface-specific version)
Documentation=https://raspberrypi.stackexchange.com/a/96605/79866
After=sys-subsystem-net-devices-%i.device

[Service]
ExecStart=/usr/sbin/ifplugd --iface=%i --no-daemon --no-auto --no-syslog --no-beep --no-shutdown --run=/etc/ifplugs/ifplugs.action

[Install]
WantedBy=sys-subsystem-net-devices-%i.device
Create the action script for the ifplugd service to run 
# mkdir /etc/ifplugs 
# vi /etc/ifplugs/ifplugs.action
#!/bin/bash
# redirect all output into a logfile for debug
#exec 1>> /tmp/ifplugs-debug.log 2>&1

INTERFACE="$1"
EVENT="$2"
IPADDR=''
get_ipaddr () {
    if [ "$EVENT" = "down" ]; then
        IPADDR=$(/sbin/ip -4 -br addr show $INTERFACE | /bin/grep -Po "\d+\.\d+\.\d+\.\d+")
        return 0
    fi
    # check 10 times with 1 sec delay if ip address is available
    for ((i=10; i>0; i--)); do
        IPADDR=$(/sbin/ip -4 -br addr show $INTERFACE | /bin/grep -Po "\d+\.\d+\.\d+\.\d+")
        [ $? -eq 0 ] && break
        /bin/sleep 1
    done
}
case "$INTERFACE" in
eth0)
    case "$EVENT" in
    up)
        # do stuff on connect with interface
        get_ipaddr
        echo "$INTERFACE" up with ip address \""$IPADDR"\"
        ;;
    down)
        # do stuff on disconnect with interface
        get_ipaddr
        echo "$INTERFACE" down with ip address \""$IPADDR"\"
        ;;
    *)
        >&2 echo empty or undefined event for "$INTERFACE": \""$EVENT"\"
        exit 1
        ;;
    esac
    ;;
wlan0)
    case "$EVENT" in
    up)
        # do stuff on connect with interface
        get_ipaddr
        echo "$INTERFACE" up with ip address \""$IPADDR"\"
        ;;
    down)
        # do stuff on disconnect with interface
        get_ipaddr
        echo "$INTERFACE" down with ip address \""$IPADDR"\"
        ;;
    *)
        >&2 echo empty or undefined event for "$INTERFACE": \""$EVENT"\"
        exit 1
        ;;
    esac
    ;;

*)
    >&2 echo empty or unknown interface: \""$INTERFACE"\"
    exit 1
    ;;
esac
make it executable
chmod +x /etc/ifplugs/ifplugs.action
/usr/sbin/ifplugd needs an own instance for every interface. So we start them with the interface-specific version:
# systemctl start ifplug@eth0.service
If you want to start the ifplug.service on boot up, just enable it
# systemctl enable ifplug@eth0.service
Connect the ethernet cable connectors to the ethernet port on each machine.

Test your network connection with the following command

# ping -c3 192.168.178.37

PING 192.168.178.37 (192.168.178.37 56(84) bytes of data
64 bytes from 192.168.178.37: icmp_seq=0 ttl=128 time=726 usec
64 bytes from 192.168.178.37: icmp_seq=1 ttl=128 time=403 usec
64 bytes from 192.168.178.37: icmp_seq=2 ttl=128 time=402 usec

--- 192.168.178.37 ping statistics
3 packets transmitted, 3 packets received, 0% packet loss









References 


Thought for the day
Listen, my son, to your father’s instruction and do not forsake your mother’s teaching. 
They are a garland to grace your head and a chain to adorn your neck.
Proverbs 1:8-9

No comments:

Post a Comment