105

How can I network two Ubuntu computers, so that they can "see" each other at an IP address?

1

9 Answers 9

75

If you are using two computers with no router to connect them. To physically connect the computers you will either need a switch, a hub or a crossover cable(*). Then, you need to manually assign IP address in the same range.

In Ubuntu this is simple. right click on the network manager applet in the You need to do this on both computers

  1. edit connections
    • wired tab
      • add
  2. put the mac address of the interface you will be configuring. The ifconfig command can show you what the mac address is:

    $ ifconfig  
    eth0      Link encap:Ethernet  HWaddr 00:30:1b:b9:53:94 
    

    2.1. On newer version of Linux the network card names have changed. wlp2s0, enp1s0. Look for some thing with similar names to en being ethernet / wl being wireless.

    $ ip a l
    2: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 98:e7:f4:5d:59:90 brd ff:ff:ff:ff:ff:ff
    3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 84:ef:18:7b:cd:39 brd ff:ff:ff:ff:ff:ff
    

HWaddr 00:30:1b:b9:53:94 = mac address link/ether 84:ef:18:7b:cd:39 = mac address

  1. Then click the ipv4 settings tab. set method to manual.
  2. click add to add IP address on both Computer A and B.

example for computer A

address  | netmask       | gateway   
10.0.0.1 | 255.255.255.0 |  

example for computer B

address  | netmask       | gateway   
10.0.0.2 | 255.255.255.0 | 

see if you can ping each other
from computer A.

$ ping 10.0.0.2  
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=128 time=0.457 ms

from computer B.

$ ping 10.0.0.1  
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=128 time=0.457 ms

means everything is working.

(*) Most modern computers can use a normal cable instead of a crossover. Some old computers will require a crossover cable

11
  • 9
    I think most modern network cards are smart enough to detect whether a cross-over cable is being used or not. en.wikipedia.org/wiki/Medium_dependent_interface
    – Seanny123
    Sep 5, 2013 at 1:51
  • 3
    The answer using the "shared" method in NetworkManager at askubuntu.com/a/3117/6130 is much simpler, requires no configuration on one of the computers, and allows one of the computers to share an internet connection. It is the same as what @Robert Ancell is saying in his answer here.
    – nealmcb
    Nov 27, 2014 at 20:55
  • 2
    Does it matter which computer I do this from? Can I set it all up via one computer or do I have to do this process twice on each machine?
    – Matt Corby
    Jan 21, 2018 at 16:15
  • 2
    @nelaaro Can you explan what you mean by this: "put the mac address of the interface you will be configuring"" it is still very ambiguous. Which one are you configuring? Computer A or Computer B? Mar 8, 2018 at 20:04
  • 1
    @Meto you can not have two different hosts using the same IP address on the same network. An IP address conflict would occur. Imagine if every house in the street had the same number i.e. 1. If I wanted to deliver mail to house address 1 which house is it. Well, it must mean me, cause I am also using address 1. I would just send the mail to my self. Not quite what I was expecting if I wanted it to go to my nearby neighbour. This is essentially what happens using the same address. everything goes to yourself. So pings might work but you would not have a working network of two hosts.
    – nelaaro
    Sep 21, 2020 at 16:51
54

command line example cause it is so quick and simple.

On newer version of Linux the network card names have changed. wlp2s0, enp1s0. Look for something with similar names to en being ethernet / wl being wireless.

$ ip a l
2: enp1s0: 
3: wlp2s0: 

You would need to update the below command to use different dev enp1s0 etc.

On computer A

sudo ip ad add 10.0.0.10/24 dev eth0

On computer B

sudo ip ad add 10.0.0.20/24 dev eth0

To test from A to B

ping 10.0.0.20

To test from B to A

ping 10.0.0.10

Provided that a cable is connecting the two computers and the network interface on both of them is enabled and called eth0 this should work. The rest of this post is to help trouble shoot if there are problems.

10.0.0.xx is the ip address of the computers. /24 tells the computers that the last 8 bits of the 32 bit ip address can change. It is similar to saying netmask 255.255.255.0. This set ups the routes that the computer / network has available to it.

you should see some thing similar to this using

ip route
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.20

If you don't see some thing like above in the route table but see some thing like this when you use ip ad. You need add the address again using /24 subnet syntax.

ip ad
inet 172.16.1.40/32 scope global eth0

Notice the /32 which effectively is saying that is the only computer that exists on a network of one computer. Which is its self. Net mask 255.255.255.255. Hence no routes and the network does not work.

To find out which network interfaces you have and which are working

# ip ad
1: lo:  mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 46:fd:51:f9:f5:2e brd ff:ff:ff:ff:ff:ff
    inet 172.16.0.186/24 brd 172.16.0.255 scope global eth0
    inet 172.16.1.10/24 scope global eth0
    inet 172.16.1.40/32 scope global eth0
    inet6 fe80::44fd:51ff:fef9:f52e/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1:  mtu 1500 qdisc noop qlen 1000
    link/ether 72:3f:92:eb:a4:cc brd ff:ff:ff:ff:ff:ff

If you need to delete an ip address cause you made a mistake

ip ad del 10.0.0.40/32 dev eth0 ip ad del 10.0.0.40/24 dev eth0

This will remove the routes as well as the ip address.

6
  • This worked initially, but seemed to be less reliable than the graphical method.
    – 8128
    Jul 16, 2012 at 7:50
  • 5
    This setup does not persist. It will be lost after a reboot. It is a simple and quick method to set up a working network connection between two computers.
    – nelaaro
    Oct 17, 2012 at 7:23
  • Did not work on Ubuntu 14.04 between a ThinkPad T430 and T400 with a 26awg cable. Sep 10, 2015 at 10:38
  • 1
    Works great connecting a 17.04 Ubuntu computer (A) to a second Linux box (B) which has no keyboard or screen. Before disconnecting B from the network, ssh into it and execute "sudo ip ad add 10.0.0.20/24 dev eth0" as shown above. Then you can go ahead and use the patch cable to connect B to A. Also, on newer version of Ubuntu, use "ip ad" as shown above to get the interface name (since it is not always eth0).
    – Enterprise
    Oct 20, 2017 at 21:48
  • Could someone tell me please if you need to use a crossover ethernet cable to use this method?
    – user151027
    Aug 6, 2020 at 12:42
39

Pick one of the computers to be the server. If one computer has a connection to the Internet use that as the server.

On the server click the network indicator and chose Edit Connections. Select the Ethernet connection then click the Edit button. Go to the IPv4 tab and change Method from Automatic (DHCP) to Shared to other computers.

Connect the two computers together using an Ethernet cable. The second computer will get assigned an IP address from the server and get access to the Internet.

If you have old hardware you may need to make sure the Ethernet cable is a crossover cable. Modern hardware automatically does the crossover.

Tested on Ubuntu 14.04 LTS.

1
  • 1
    This also works on Linux Mint 18, just so you know. Jan 6, 2017 at 19:34
13

If you are connecting them directly using an ethernet cable alone, and at least since 16.04, you can do this easily by going to network settings:

  1. Edit the wired connection
  2. Go to the IPv4 tab
  3. Select Link-Local Only

Do this on both computers and you're finished.

5

Below is a reprise of nelaar's answer, updated for Ubuntu 14.04.

Hardware Requirements

In order to connect two computers without a router, you will need one of the following:

  • A standard Ethernet cable, which should work with most modern hardware, or
  • An Ethernet crossover cable, in lieu of a modern NIC, or
  • A simple Ethernet hub (and two Ethernet cables).

In Ubuntu 14.04 LTS

You will need to manually assign IP address in the same range. The following steps assume you are using the standard Unity interface. Repeat these steps for each computer.

  1. Click the Network indicator on the Unity panel (top right).
  2. From the drop-down, select Edit Connections...
  3. In the Network Connections dialog box, click the Add button.
  4. When prompted to choose a connection type, choose Ethernet, and click the Create... button.
  5. Name your connection "Direct to [other hostname]" to differentiate from a typical Ethernet connection.
  6. In the Device MAC address drop-down, select the one corresponding to the interface you plan to use.
  7. Switch to the IPv4 Settings tab.
  8. Change the Method to Manual.
  9. click the Add button to add an IP address.
    • Example settings for Computer #1:
      • Address: 10.0.0.1
      • Netmask: 255.255.255.0
      • Gateway:
    • Example settings for Computer #2:
      • Address: 10.0.0.2
      • Netmask: 255.255.255.0
      • Gateway:

Once that's set up, and the computers are wired together, try pinging each other.

user@computer1:~$ ping 10.0.0.2

The output should look something like this:

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=128 time=0.457 ms
2
  • You don't need to manually assign IP addresses as shown in my answer. Nov 24, 2014 at 23:38
  • Arrgh, the old-fashioned telephone cable looks like ethernet cable, and fits perfectly to the ethernet connector on PC... Just nothing works... Beware! :-)
    – Ott Toomet
    Aug 6, 2021 at 20:25
3

Command-line (nmcli), reboot-persistent solution

Connect the two hosts with the Ethernet cable. Determine the Ethernet interface of the computers running

ip addr

on each one. It should start with en or eth. Below, replace {enX} and {enY} by the corresponding interface names just retrieved.

On host X, issue

nmcli c add type ethernet ifname {enX} con-name MyWired ip4 10.0.0.1/24

On host Y, issue

nmcli c add type ethernet ifname {enY} con-name MyWired ip4 10.0.0.2/24

Now you should be able to ping from Y to X and vice-versa with

ping 10.0.0.2 #From host X
ping 10.0.0.1 #From host Y

If you want to ssh into the machines, install openssh-server.

2

Your question is not quite brief, I am going to assume you just want 2 Ubuntu computers in network.

Connecting 2 Ubuntu computers is easy enough, just get a Networking cable (Cat 5e Cross Cable) and connect both computers using that cable and Ubuntu should be able to get both of them an "Auto Eth" (Automatic IP) connection. This should be pretty much it to get 2 ubuntu computers in the network.

Later on If you want to see the Automatic IP assigned to the computer, run the command ifconfig or right click on the 'NetworkManager applet' and click on connection information

enter image description here

1
  • 2
    I have tried this but the connection just goes crazy trying to connect and failing every time. What could the problem be? I have set the IPv4 method to "Shared with other computers"
    – Severo Raz
    Jan 28, 2012 at 23:39
0

I liked nelaaro's solution (https://askubuntu.com/a/116680/22307): It is nice and short and does no permanent change. If you reboot everything is back to normal.

But my NeworkManager would not let me: It wanted to control the interfaces and when I set the address by hand it would reset them.

I found a way to cheat it, though: Virtual interfaces.

Identify your physical interface by running this on both computers:

$ ip a l
1: lo: << nope: this is loopback interface
2: enp0s25: << THIS IS THE ONE YOU WANT
3: wlp3s0: << nope: this is wireless
4: docker0: << nope: used by docker

The trick is now to use a virtual interface instead of the physical one. You simply add :0 to the name:

computer1$ sudo ip ad add 10.1.1.1 dev enp0s25:0
computer2$ sudo ip ad add 10.2.2.2 dev enp0s25:0
computer1$ ping 10.2.2.2
computer2$ ping 10.1.1.1

NetworkManager apparently does not see this so it does not reset your interface.

0

Ubuntu 23.10 GUI

Just to provide some screenshots of what https://askubuntu.com/a/553374/52975 said.

Computer 1

Open Settings -> Network.

Click the cog under Wired:

enter image description here

I don't think I changed anything here:

enter image description here

Select "Shared to other computers" (TODO how to do this from CLI? https://unix.stackexchange.com/questions/243408/share-wlan-connection-to-ethernet-using-command-line )

enter image description here

And then "Apply".

Computer 2

I don't think I changed anything in Computer 2, it's just set at "Automatic (DHCP)"

enter image description here

Outcome

In Computer 1:

ip a

shows:

2: enp1s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fc:5c:ee:24:fb:b4 brd ff:ff:ff:ff:ff:ff
    inet 10.42.0.1/24 brd 10.42.0.255 scope global noprefixroute enp1s0f0
       valid_lft forever preferred_lft forever

and Computer 2:

2: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 54:e1:ad:b5:5b:08 brd ff:ff:ff:ff:ff:ff
    inet 10.42.0.70/24 brd 10.42.0.255 scope global dynamic noprefixroute enp0s31f6
       valid_lft 2914sec preferred_lft 2914sec
    inet6 fe80::a64f:794b:b8fa:5501/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

so we understand that these IPs were automatically negotiated with DHCP.

We can now connect from one computer to the other with those IPs, e.g. from 1 to 2:

ping 10.42.0.70

and from 2 to 1:

ping 10.42.0.1

Tested with computer 1 = Lenovo ThinkPad P14s, Computer 2 = Lenovo ThinkPad P51, both on Ubuntu 23.10 and connected with regular Ethernet Cat 5e cable (not crossover, the network cards of such relatively modern computers can handle that).

If you also want computer 2 to access the Internet through computer 1, then you also need to play with sudo sysctl net.ipv4.ip_forward=1 as mentioned at: Share Wireless connection with Wired Ethernet Port

You must log in to answer this question.