ipfs私网搭建

  1. 1,单机配置
  2. 2,多机配置
  3. 3,使用密钥加入ipfs网络

使用centos7

1,单机配置

下载go版本ipfs,解压:

https://dist.ipfs.io/go-ipfs/v0.13.0/go-ipfs_v0.13.0_linux-amd64.tar.gz

tar -zxf go-ipfs_v0.13.0_linux-amd64.tar.gz 

安装

cd go-ipfs
./install.sh 
显示:Moved ./ipfs to /usr/local/bin

执行ipfs初始化:

[root@ch134 go-ipfs]# ifps init
-bash: ifps: command not found
[root@ch134 go-ipfs]# ipfs init
generating ED25519 keypair...done
peer identity: 12D3KooWJZbzzmz3GBXbczGvcHBUyk1xnj8oyCU7cfa3bF9BicuC
initializing IPFS node at /root/.ipfs
to get started, enter:

        ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme

2022-06-15T11:31:49.522+0800    ERROR   provider.queue  queue/queue.go:124      Failed to enqueue cid: leveldb: closed

报错可以暂时忽略

查看本机id

[root@ch134 go-ipfs]# ipfs id
{
        "ID": "12D3KooWEz4muJsD6AiF12txrJ7HgBE1hh845S9eoddSp6X159oH",
        "PublicKey": "CAESIEzGk1GSYQt13e3gcsbHtAY3Yu7OEJ9bs64KWMgtX2K8",
        "Addresses": null,
        "AgentVersion": "go-ipfs/0.13.0/",
        "ProtocolVersion": "ipfs/0.1.0",
        "Protocols": null
}

IPFS初始化后会在~/.ipfs/config生成默认的配置文件,其中BootStrap是定义了一些附近的节点

默认配置:

 "Bootstrap": [
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
    "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
    "/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa"
  ],

搭建私有链时需要把该项设置为空

允许其他机器访问:

ipfs config --json Addresses.API '"/ip4/0.0.0.0/tcp/5001"'

跨资源共享CORS配置

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'

也可以直接在配置文件里面修改,效果是一样的

启动服务:

[root@ch134 ~]# ipfs daemon
Initializing daemon...
go-ipfs version: 0.13.0
Repo version: 12
System version: amd64/linux
Golang version: go1.18.3
2022/06/16 11:23:37 failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/127.0.0.1/udp/4001/quic
Swarm listening on /ip4/172.16.164.134/tcp/4001
Swarm listening on /ip4/172.16.164.134/udp/4001/quic
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/::1/udp/4001/quic
Swarm listening on /p2p-circuit
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/127.0.0.1/udp/4001/quic
Swarm announcing /ip4/172.16.164.134/tcp/4001
Swarm announcing /ip4/172.16.164.134/udp/4001/quic
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/::1/udp/4001/quic
API server listening on /ip4/0.0.0.0/tcp/5001
WebUI: http://0.0.0.0:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

另起一个窗口,使用ipfs add命令向网络中写入一个文件

[root@ch134 ~]# vi test
this is a test file

[root@ch134 ~]# ipfs add test
added QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp test
 20 B / 20 B [===========================================================================================] 100.00%

打印的 QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp即该文件的hash,可以在命令行查看文件:

[root@ch134 ~]# ipfs cat QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp 
this is a test file

也可以通过此hash下载文件,hash即是文件名

[root@ch134 ~]# ipfs get QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp
Saving file(s) to QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp
 20 B / 20 B [========================================================================================] 100.00% 0s

[root@ch134 ~]# ls
QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp  test
[root@ch134 ~]# more QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp 
this is a test file

还可以通过浏览器查看该文件,访问路径是http://172.16.164.134:8080/ipfs/QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp

172.16.164.134为我的虚拟机ip地址

如果访问不通,配置防火墙规则后,还要设置配置文件中的Gateway外网访问权限,使用下面命令设置

ipfs config --json Addresses.Gateway '"/ip4/0.0.0.0/tcp/8080"'

ipfs服务后台启动:

ipfs daemon &
2,多机配置

以上是在虚拟机172.16.164.134上,下面在另一台虚拟机172.16.164.135上也做同样的配置,并将个服务连接起来,共享文件

重复上面的步骤,启动ipfs服务

使用ipfs id命令查看两个节点的id

134: 12D3KooWEz4muJsD6AiF12txrJ7HgBE1hh845S9eoddSp6X159oH
135: 12D3KooWNdAiRfvxJ3vMTNb4bxuCW5vEkS19YAG9NjYnEZffF8cn

相互添加:

134:

ipfs bootstrap add /ip4/172.16.164.135/tcp/4001/ipfs/12D3KooWNdAiRfvxJ3vMTNb4bxuCW5vEkS19YAG9NjYnEZffF8cn

135:

ipfs bootstrap add /ip4/172.16.164.134/tcp/4001/ipfs/12D3KooWEz4muJsD6AiF12txrJ7HgBE1hh845S9eoddSp6X159oH

添加之后就可以相互访问彼此的文件,在135上面执行

[root@ch135 ~]# ipfs cat QmQ6zADQZGASCnYxXfFkGHUF5Fc7ibrtjGWaxjY8w9nANp
this is a test file
3,使用密钥加入ipfs网络

同一个IPFS私链内的所有节点通过共享同一个密钥组网

需要有golang环境

安装密钥生成工具:

go get -u github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen

手动下载:

地址:https://github.com/Kubuxu/go-ipfs-swarm-key-gen

编译

go build -o ipfs-swarm-key-gen go-ipfs-swarm-key-gen/ipfs-swarm-key-gen/main.go

生成key:

./ipfs-swarm-key-gen > swarm.key

移动到ipfs配置目录下

mv swarm.key ~/.ipfs/

通过scp命令,将swarm.key文件发送到其他ipfs节点的配置目录下,启动各节点即可完成组网


转载请注明来源

×

喜欢就点赞,疼爱就打赏