Set Custom Port
This guide explains how to use a shell script to quickly configure a Cosmos chain node with custom ports, minimum gas price, pruning, and monitoring settings.
Prerequisites
- You have a Cosmos chain node initialized (with
config/app.toml
andconfig/config.toml
in place). - You have Bash and
sed
installed on your system.
Usage
Run the script with the following arguments:
./set-custom-port.sh <NODE_DIR> [MINIMUM_GAS_PRICES] [PORT_PREFIX] [ENABLE_INDEXER] [ENABLE_ABCI_RESPONSE]
<NODE_DIR>
: Path to your node's base directory (required)[MINIMUM_GAS_PRICES]
: Minimum gas price (default:0uinit
)[PORT_PREFIX]
: Port prefix for all services (default:2
)[ENABLE_INDEXER]
: Set totrue
to enable tx_index (default:false
)[ENABLE_ABCI_RESPONSE]
: Set totrue
to keep ABCI responses (default:false
)
If you need to query transactions by hash, enable the transaction indexer. If you want to save disk space, keep ABCI response discarding enabled.
What the Script Does
- Validates input arguments
- Sets minimum gas price in
app.toml
- Configures pruning for efficient storage
- Disables state sync snapshots
- Enables Prometheus monitoring
- Enables or disables transaction indexer (configurable)
- Enables or disables ABCI response discarding (configurable)
- Sets custom ports for all major services
- (Optional) Cleans up backup files created by
sed
(see below)
By default, the script disables the transaction indexer and enables ABCI response discarding for performance and disk usage. You can override these behaviors with the optional arguments.
Script
Always back up your configuration files before running scripts that modify them!
#!/bin/bash
set -euo pipefail
DIR="${1:-}"
MINIGAS="${2:-0uinit}"
PORT="${3:-2}"
ENABLE_INDEXER="${4:-false}"
ENABLE_ABCI_RESPONSE="${5:-false}"
if [[ -z "$DIR" ]]; then
echo "[ERROR] No base directory provided."
echo "Usage: $0 <NODE_DIR> [MINIMUM_GAS_PRICES] [PORT_PREFIX] [ENABLE_INDEXER] [ENABLE_ABCI_RESPONSE]"
exit 1
fi
if [[ ! -d "$DIR/config" ]]; then
echo "[ERROR] Directory $DIR/config does not exist."
exit 1
fi
# Set minimum gas price
grep -q '^minimum-gas-prices' "$DIR/config/app.toml" && \
sed -i.bak -e "s|^minimum-gas-prices *=.*|minimum-gas-prices = \"${MINIGAS}\"|" "$DIR/config/app.toml"
# Set pruning
sed -i.bak \
-e 's|^pruning *=.*|pruning = "custom"|' \
-e 's|^pruning-keep-recent *=.*|pruning-keep-recent = "100"|' \
-e 's|^pruning-keep-every *=.*|pruning-keep-every = "0"|' \
-e 's|^pruning-interval *=.*|pruning-interval = "10"|' \
"$DIR/config/app.toml"
# Disable state sync snapshots
sed -i.bak \
-e 's|^snapshot-interval *=.*|snapshot-interval = 0|' \
-e 's|^snapshot-keep-recent *=.*|snapshot-keep-recent = 2|' \
"$DIR/config/app.toml"
# Enable Prometheus monitoring
sed -i.bak \
-e 's|^prometheus *=.*|prometheus = true|' \
"$DIR/config/config.toml"
# Enable or disable tx_index
if [[ "$ENABLE_INDEXER" == "true" ]]; then
sed -i.bak -e 's|^indexer *=.*|indexer = "kv"|' "$DIR/config/config.toml"
else
sed -i.bak -e 's|^indexer *=.*|indexer = "null"|' "$DIR/config/config.toml"
fi
# Enable or disable ABCI response discarding
if [[ "$ENABLE_ABCI_RESPONSE" == "true" ]]; then
sed -i.bak -e 's|^discard_abci_responses *=.*|discard_abci_responses = false|' "$DIR/config/config.toml"
else
sed -i.bak -e 's|^discard_abci_responses *=.*|discard_abci_responses = true|' "$DIR/config/config.toml"
fi
# Set custom ports
sed -i.bak -e "s%^proxy_app = \"tcp://.*:26658\"%proxy_app = \"tcp://127.0.0.1:${PORT}6658\"%; s%^laddr = \"tcp://.*:26657\"%laddr = \"tcp://0.0.0.0:${PORT}6657\"%; s%^pprof_laddr = \".*:6060\"%pprof_laddr = \"127.0.0.1:${PORT}6060\"%; s%^laddr = \"tcp://.*:26656\"%laddr = \"tcp://0.0.0.0:${PORT}6656\"%; s%^prometheus_listen_addr = \".*:26660\"%prometheus_listen_addr = \":${PORT}6660\"%" "$DIR/config/config.toml"
sed -i.bak -e "s%^metrics-address = \".*:6065\"%metrics-address = \"127.0.0.1:${PORT}6065\"%; s%^ws-address = \".*:8546\"%ws-address = \"tcp://127.0.0.1:${PORT}8546\"%; s%^address = \".*:8545\"%address = \"127.0.0.1:${PORT}8545\"%; s%^address = \"tcp://.*:1317\"%address = \"tcp://127.0.0.1:${PORT}1317\"%; s%^address = \".*:8080\"%address = \"127.0.0.1:${PORT}8080\"%; s%^address = \".*:9090\"%address = \"127.0.0.1:${PORT}9090\"%; s%^address = \".*:9091\"%address = \"127.0.0.1:${PORT}9091\"%" "$DIR/config/app.toml"
Final Step: Clean Up Backup Files
After running the script, you may want to remove the .bak
backup files created by sed
.
find <NODE_DIR>/config -name '*.bak' -delete
This step is optional, but helps keep your config directory tidy. Only run this after you have confirmed your configuration is correct!