IPFS 安装配置

星期六, 7月 31, 2021 | 3分钟阅读 | 更新于 星期一, 8月 9, 2021

@

一、安装

可以使用Manjaro系统自带的包管理命令安装

sudo pacman -S go-ipfs

其他Linux系统可以用自带的包管理命令搜索ipfs关键字查找包再安装,如果不行就直接下载程序安装,安装可以参考ipfs的github仓库 https://github.com/ipfs/go-ipfs 如果系统有安装Docker,也可以使用ipfs的Docker镜像 https://hub.docker.com/r/ipfs/go-ipfs

docker pull ipfs/go-ipfs

二、启动

ipfs节点初始化

ipfs init

执行此命令使用默认配置初始化节点,将在家目录(用户目录)生成一个.ipfs的文件夹存储节点数据,其中config文件是节点的配置,可以修改。 再使用daemon命令启动

ipfs daemon

会启动默认的WEBUI为 http://127.0.0.1:5001/webui ,浏览器打开此链接可以查看本地ipfs节点的状态

Docker容器启动命令参考,可以使用-v选项挂载两个目录:

  1. /data/ipfs,ipfs存储数据的目录,挂载后容器销毁时可以保留ipfs数据
  2. /export,ipfs导出目录,容器不能访问主机的文件,挂载后可以通过此目录导入导出文件
docker run -d --name ipfs_host -v /root/ipfs/export:/export -v /root/ipfs/data:/data/ipfs -p 4001:4001 -p 8080:8080 -p 5001:5001 ipfs/go-ipfs

三、公开节点

默认启动是本地节点,如果是其他电脑要访问,需要配置跨域和绑定ip地址

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT","GET", "POST", "OPTIONS"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
ipfs config --json Addresses.API '"/ip4/0.0.0.0/tcp/5001"'
ipfs config --json Addresses.Gateway '"/ip4/0.0.0.0/tcp/8080"'

docker容器启动的ipfs需要使用docker exec <容器>来执行ipfs命令

docker exec ipfs_host ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT","GET", "POST", "OPTIONS"]'
docker exec ipfs_host ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
docker exec ipfs_host ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
docker exec ipfs_host ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
docker exec ipfs_host ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
docker exec ipfs_host ipfs config --json Addresses.API '"/ip4/0.0.0.0/tcp/5001"'
docker exec ipfs_host ipfs config --json Addresses.Gateway '"/ip4/0.0.0.0/tcp/8080"'

ip和端口可以按需配置,也可以直接去修改配置文件$HOME/.ipfs/config

...
  "API": {
    "HTTPHeaders": {
      "Access-Control-Allow-Credentials": [
        "true"
      ],
      "Access-Control-Allow-Headers": [
        "Authorization"
      ],
      "Access-Control-Allow-Methods": [
        "PUT",
        "GET",
        "POST",
        "OPTIONS"
      ],
      "Access-Control-Allow-Origin": [
        "*"
      ],
      "Access-Control-Expose-Headers": [
        "Location"
      ]
    }
  },
  "Addresses": {
    "API": "/ip4/0.0.0.0/tcp/5001",
    "Announce": [],
    "Gateway": "/ip4/0.0.0.0/tcp/8080",
    "NoAnnounce": [],
    "Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip6/::/tcp/4001",
      "/ip4/0.0.0.0/udp/4001/quic",
      "/ip6/::/udp/4001/quic"
    ]
  },
...

四、帮助手册

ipfs命令自带使用手册,在终端执行ipfs命令即可,子命令的帮助需要添加选项-h,例如ipfs init -h

USAGE
  ipfs  - Global p2p merkle-dag filesystem.

  ipfs [--config=<config> | -c] [--debug | -D] [--help] [-h] [--api=<api>] [--offline] [--cid-base=<base>] [--upgrade-cidv0-in-output] [--encoding=<encoding> | --enc] [--timeout=<timeout>] <command> ...

SUBCOMMANDS
  BASIC COMMANDS
    init          Initialize local IPFS configuration
    add <path>    Add a file to IPFS
    cat <ref>     Show IPFS object data
    get <ref>     Download IPFS objects
    ls <ref>      List links from an object
    refs <ref>    List hashes of links from an object
  
  DATA STRUCTURE COMMANDS
    dag           Interact with IPLD DAG nodes
    files         Interact with files as if they were a unix filesystem
    block         Interact with raw blocks in the datastore
    cid           Convert and discover properties of CIDs
  
  ADVANCED COMMANDS
    daemon        Start a long-running daemon process
    mount         Mount an IPFS read-only mount point
    resolve       Resolve any type of name
    name          Publish and resolve IPNS names
    key           Create and list IPNS name keypairs
    dns           Resolve DNS links
    pin           Pin objects to local storage
    repo          Manipulate the IPFS repository
    stats         Various operational stats
    p2p           Libp2p stream mounting
    filestore     Manage the filestore (experimental)
  
  NETWORK COMMANDS
    id            Show info about IPFS peers
    bootstrap     Add or remove bootstrap peers
    swarm         Manage connections to the p2p network
    dht           Query the DHT for values or peers
    ping          Measure the latency of a connection
    diag          Print diagnostics
    bitswap       Inspect bitswap state
    pubsub        Send and receive messages via pubsub
  
  TOOL COMMANDS
    config        Manage configuration
    version       Show IPFS version information
    update        Download and apply go-ipfs updates
    commands      List all available commands
    log           Manage and show logs of running daemon
  
  Use 'ipfs <command> --help' to learn more about each command.
  
  ipfs uses a repository in the local file system. By default, the repo is
  located at ~/.ipfs. To change the repo location, set the $IPFS_PATH
  environment variable:
  
    export IPFS_PATH=/path/to/ipfsrepo

© 2026 我的博客

🌱 Powered by Hugo with theme Dream.

关于这个博客

关于博客

记录软件开发过程的一些知识和技巧,提升自我

关于博主

除了才华一无所有~~ 薪尽自然凉~~ 强迫症患者~~ 热爱古风~~