Skip to content

Instantly share code, notes, and snippets.

@atweiden
Created March 19, 2016 21:30
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save atweiden/397288a285cd7479ca5c to your computer and use it in GitHub Desktop.
Save atweiden/397288a285cd7479ca5c to your computer and use it in GitHub Desktop.
directly connect two linux machines using an ethernet cable

ssh/rsync from one Linux machine to another with a standard ethernet cable

Overview

  • Install ifplugd and openssh on both machines.
  • Optionally install inxi on both machines (recommended).
  • Install rsync on master.
  • Start ifplugd service on both machines.
  • Start sshd service on slave.
  • Plug in the ethernet cable from master to slave.
  • SSH from master to slave.

Procedure

prepare master

Install dependencies on master

pacman -S ifplugd inxi openssh rsync

Obtain ethernet interface name on master

$ inxi -i
Network:   Card-1: Your PCI Express Gigabit Ethernet Controller driver: r8169
           IF: enp4s0f2 state: down mac: 30:02:06:bc:ab:14

MASTER_INTERFACE=enp4s0f2

Ethernet interface name is traditionally eth0. On systemd machines, the default differs. Find it by running inxi -i as above.

Alternatively, run ip addr and pick the second result. The name will look similar to enp4s0f2.

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp4s0f2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 30:02:06:bc:ab:14 brd ff:ff:ff:ff:ff:ff

Start ifplugd service on master

With the ethernet interface name on master obtained, start the ifplugd service:

systemctl start ifplugd@enp4s0f2

or

systemctl start ifplugd@${MASTER_INTERFACE}

prepare slave

Install dependencies on slave

pacman -S ifplugd inxi openssh

Obtain ethernet interface name on slave

$ inxi -i
Network:   Card-1: Intel Gigabit Network Connection driver: e1000e
           IF: enp0s25 state: down mac: 73:2c:4b:e8:7b:76

SLAVE_INTERFACE=enp0s25

Alternatively, run ip addr and pick the second result. The name will look similar to enp0s25.

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s25: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 73:2c:4b:e8:7b:76 brd ff:ff:ff:ff:ff:ff

Start ifplugd service on slave

With the ethernet interface name on slave obtained, start the ifplugd service:

systemctl start ifplugd@enp0s25

or

systemctl start ifplugd@${SLAVE_INTERFACE}

Start sshd on slave

systemctl start sshd

connect ethernet cable

Directly connect master and slave ethernet ports with cat cables (such as standard CAT5e).

Check to make sure both ends of the ethernet cable are plugged in all the way. Run this command on both master and slave:

ifplugstatus -v

obtain slave ip address

Obtain the IPv6 address on slave ethernet interface:

ifconfig | grep inet6 | head -n 1 | awk '{print $2}'

Alternatively, use inxi:

$ inxi -ix
    WAN IP: None Detected! IF: enp0s25 ip-v4: N/A ip-v6: fe80::6497:17de:fee3:1942

In the inxi output, look for IF: (your ethernet interface name), then copy the ip-v6 value.

SLAVE_IPV6=fe80::6497:17de:fee3:1942

ssh from master to slave

example:

ssh user@fe80::6497:17de:fee3:1942%enp4s0f2

or

SLAVE_USER_NAME=user
ssh ${SLAVE_USER_NAME}@${SLAVE_IPV6}%${MASTER_INTERFACE}

rsync from master to slave

example:

rsync \
  --recursive \
  --perms \
  --human-readable \
  --progress \
  --verbose \
  -e ssh \
  /path/to/src \
  user@[fe80::6497:17de:fee3:1942%enp4s0f2]:/path/to/dest

or

rsync \
  --recursive \
  --perms \
  --human-readable \
  --progress \
  --verbose \
  -e ssh \
  /path/to/src \
  ${SLAVE_USER_NAME}@[${SLAVE_IPV6}%${MASTER_INTERFACE}]:/path/to/dest

Putting brackets around the slave IPv6 address is required.

@shivajichalise
Copy link

worked perfectly! thanks!

@danielgomez3
Copy link

danielgomez3 commented Jul 29, 2022

This helped a lot. Any way to shorten do this using the 'scp' command?

@monomycelium
Copy link

Is there a way to use IPV4 instead of IPV6?

@atweiden
Copy link
Author

Is there a way to use IPV4 instead of IPV6?

On IPv4, it has to be done manually with the ip utility (from package: iproute2), e.g.

on master:

ip addr add 10.0.1.2/24 dev eth0

on slave:

ip addr add 10.0.1.1/24 dev eth0

ssh from master to slave:

ssh user@10.0.1.1%eth0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment