How can I disable automatic connections of IPFS nodes to other nodes, including bootstrap and mDNS connections, to create a private network?

I’m working with IPFS and need to ensure my IPFS daemon does not automatically connect to other nodes. I’ve managed to remove default bootstrap nodes by modifying the config file, but the daemon still connects to local nodes due to the mDNS function and other automatic connection behaviors.

Here’s what I’ve tried so far:

Removed default bootstrap nodes by modifying the config file.

Set Discovery.MDNS.Enabled to false in the config file.

However, these steps didn’t fully stop the automatic connections. I understand that changes in the config files alone are not sufficient and that further modification of the source code (specifically around go-libp2p-kad-dht) might be necessary to stop new connections without affecting the basic functionality of IPFS.

How can I modify the go-IPFS (KUBO) source code to achieve this? Are there specific files or functions I should look into? Any guidance on rebuilding the IPFS daemon to ensure no automatic connections are made would be greatly appreciated.

This is my .ipfs directory and This is my kubo directory

Have you tried kubo/docs/experimental-features.md at master · ipfs/kubo · GitHub ?

Or if you mean contacting absolutely no one, then ipfs --offline daemon.

I meant to absolutely connect to no one. I found a directory “go-libp2p-kad-dht,” which I added to make that possible. Now my next problem is to get the WebUI to work for my KUBO :frowning: .

I don’t understand your solution but it sounds wrong.

ok to explain in detail. I deleted the content of the Bootstrap in .ipfs config file and disabled the Discovery.MDNS. additional in the go-libp2p-kad-dht directory, i made some changes/configurations in the kubo/go-libp2p-kad-dht/internal/net/message_manager.go file. i replaced the line “nstr, err := ms.m.host.NewStream(ctx, ms.p, ms.m.protocols…)” with “nstr, err := ms.m.host.NewStream(network.WithNoDial(ctx, “Don’t auto dial”), ms.p, ms.m.protocols…)” and in the kubo/go-libp2p-kad-dht/rtrefresh/rt_refresh_manager.go file, i replaced
´if err := r.h.Connect(livelinessCtx, peer.AddrInfo{ID: ps.Id}); err != nil {
logger.Debugw(“evicting peer after failed connection”, “peer”, peerIdStr, “error”, err)
span.RecordError(err)
r.rt.RemovePeer(ps.Id)
return

with
´if err := r.refreshPingFnc(livelinessCtx, ps.Id); err != nil {
logger.Debugw(“evicting peer after failed ping”, “peer”, peerIdStr, “error”, err)
span.RecordError(err)
r.rt.RemovePeer(ps.Id)
return

and lastly, i added ´else {
return fmt.Errorf(“Connection Disallowed”)
}´ condition to the short circuit if we are connected already to have this

´if dht.host.Network().Connectedness(p) == network.Connected {
return nil
} else {
return fmt.Errorf(“Connection Disallowed”)

Now it is not stopping the auto connections yet and my Web UI is working again because i added the
´API": {
“HTTPHeaders”: {
“Access-Control-Allow-Methods”: [
“PUT”,
“POST”
],
“Access-Control-Allow-Origin”: [
My IP Address here with the relevant port
]
}
},´
and also added my IP address to the Addresses.API list too.

But now the Web UI is accessible when I run ´ipfs daemon,´ but I see peers connected. dynamic number of peers and not a fixed number of peers/nodes. I hope I did not confuse you more now and was able to explain everything in great detail enough.

Happy new year!

are you recompiling Kubo or why are you editing internal libraries?

Why not just ipfs --offline daemon ?

I’m not sure you know what you are doing

exactly i am recompling the kubo. It is a learning process. I have not done it before, but there is not also much information on how to do it online. I am trying to “customize the ipfs daemon”.

and I am not doing ipfs --offline daemon because it is not fulfilling my purpose.

The IPFS daemon can be configured by modifying the ”config” file in its
default directory. By doing this, you can remove those
default bootstrap nodes before the start of an IPFS daemon. Unfortunately, changes to config files are not enough to turn off the bootstrap function. The reason is that IPFS enables an mDNS function where nodes continuously find nodes in the local network and connect to them. Although you can set ”Discovery.MDNS.Enabled” to false in the config file, it does not stop the bootstrap function entirely. In some cases, a node is uploading a file for
For example, this node will still connect to other nodes in the local network. This is because every time a new uploading or downing event happens, the IPFS daemon will store the corresponding addresses in a local DHT table, and this DHT table is used to route contents. Hence, I am inspecting the source code of KUBO first to see where new connections are constructed. Then, i can
modify those codes carefully to stop the automatic new connection construction
without influencing the basic function of IPFS. After my modifications, I can rebuild the IPFS daemon from the source. At end, I should now have a version of the daemon that does not automatically start the bootstrapping process.

Does my goal make much sense now?

what is your purpose again?

To customize the ipfs daemon. I want to know how it can be done and understand why it works. :frowning:

  **Step-by-Step Solution for IPFS with Private Network Configuration**

I finally solved my problem. I will state a step-by-step guide here for those who have the same issue.
in the

.ipfs/config file

delete all nodes in the bootstraps list and change Discovery.MDNS.Enable from true to false.

install ipfs-swarm-key-gen using go install github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen@latest

and add GOPATH to your path if you have not done so, export PATH=$PATH:$(go env GOPATH)/bin.

To verify it works, run ipfs-swarm-key-gen --help and if you get no error, you are good to go.

Lastly, you can generate a swarm.key for your private network using ipfs-swarm-key-gen > ~/.ipfs/swarm.key

running “ipfs daemon”. and then ipfs swarm peers, should give you an empty list, meaning 0 peers.

If any node wants to connect to your node, the node will need exactly the same swarm key as you. Every node in the network will use the same key.