Strange behavior for ipfs-init command

I have mounted external storage volume at location /mnt/disk1/ I am trying to install .ipfs at this location viz. /mnt/disk1/ipfs
As I set, here is the env variable IPFS_PATH for both the users, ubuntu and root
image

Both users root and ubuntu are administrators.

When I run command ipfs init with user ubuntu, the command runs well and IPFS is initiated at $IPFS_PATH location.

But the problem is, when I run the same ipfs init command but with SUDO power, the IPFS is installed at usual home directory of root viz. /root/.ipfs irrespective to the IPFS_PATH Following is the screenshot.
image

Why is there such inconsistency?

Also, I am facing another related issue. I have created a service daemon to keep ipfs service running. Following is the ipfs.service script
image

But When I start the ipfs.service, I am getting following error.

Here, even though, script is running as user ubuntu and actual executed command at service is ipfs daemon the IPFS service is failing to start.

However, when I run only simple command ipfs daemon as user ubuntu, the daemon is running without any error as expected. Here is screenshot…

Why there is so much inconsistencies here? I am unable to run ipfs. Please help.

This is all due to how Linux (and all Unixes) work.

The sudo command does not propagate all environment variables to its process, by default. If you want to do that you have to do sudo -E ipfs init.

The same thing happens with systemd. It will start the process as the given user but it will not read any of the environment variables you set in .bashrc or .profile.You have to set any environment variables in the unit file. You have to add the following in the [Service] section:

Environment=IPFS_PATH=/mnt/disk1/ipfs
1 Like

Thank you @mcamou This worked.

Added the suggested variable ‘Environment’ in systemd service file as below.

[Unit]
Description=IPFS Daemon

[Service]
Environment="IPFS_PATH=/mnt/disk1/ipfs"
ExecStart=ipfs daemon
User=ubuntu
Restart=always
LimitNOFILE=10240

[Install]
WantedBy=multi-user.target