IPFS 安装配置

admin
admin 2021年07月31日
  • 在其它设备中阅读本文章

一、安装

可以使用 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