# Install on Linux

`crushbalance_init.sh` is the control script for Linux and BSD. The host needs no Java: see step 3.

1. Copy `CrushBalance.jar` and `crushbalance_init.sh` into the install folder, `CRUSH_DIR` (the script's default is `/var/opt/CrushBalance/`).
2. Check the settings at the top of `crushbalance_init.sh`:
   - `CRUSH_DIR`: the install folder;
   - `CRUSH_USER`: the user CrushBalance runs as (`root` by default; see "Run as a non-root user" below);
   - `JAVA`: `java` means the folder `Java` in the install folder when there is one, otherwise the system's `java`. Give a full path (for example `JAVA="/usr/lib/jvm/temurin-25/bin/java"`) to use exactly that Java;
   - `JAVA_OPTS`: the Java options, the heap first (`-Xmx1024M`, [Capacity and tuning](Capacity-and-Tuning.md));
   - `MAX_OPEN_FILES`: the open-file limit CrushBalance gets (1048576 as delivered);
   - `OWN_JAVA` and `JAVA_API`: CrushBalance's own Java, below.
3. As root, run `./crushbalance_init.sh install`. It registers the service `crushbalance`, enables it at boot and starts it:
   - on a systemd host it writes `/etc/systemd/system/crushbalance.service`: Java runs as the service's own process, as `CRUSH_USER`, with `LimitNOFILE` from `MAX_OPEN_FILES`, `Restart=always` and `RestartSec=5`;
   - otherwise it links `/etc/init.d/crushbalance` to the script and registers it with `chkconfig` or `update-rc.d` (BSD: an `rc.d` script);
   - `INIT_SYSTEM` at the top of the script (`systemd`, `sysv` or `bsd`) decides where the script cannot tell.
   - **No Java needed.** When the host has no Java 21 or newer, the first start downloads Eclipse Temurin's JRE into `CRUSH_DIR/Java`, and `install` says so. It needs `curl` or `wget`, and HTTPS to `api.adoptium.net` and `github.com`.

   From then on:
   - `./crushbalance_init.sh start|stop|restart|status|info`. `status` prints the service file and `CrushBalance is running [pid …]`, or `CrushBalance is not running.` with exit code 3. `stop` waits until the process has exited, which normally takes about 4 seconds; one that is still there after `STOP_WAIT`, 30 s, is killed. `info` shows the system, the Java in use and the settings.
   - On a systemd host prefer `systemctl start|stop|restart crushbalance`, so systemd knows the state. A process stopped outside systemd is started again 5 seconds later (`Restart=always`).
   - `start` refuses a Java older than 21 ("CrushBalance needs Java 21 or later (Java 25 or later is recommended), but … is Java …") and warns below 25.
4. The script starts `java -Ddir=$CRUSH_DIR -Xmx1024M -XX:-UseBASE64Intrinsics -jar CrushBalance.jar -d` in `CRUSH_DIR`. On the first start CrushBalance creates `prefs.XML`, `crushbalance.key`, the admin certificate and the **setup token**.
   - The token is in `CRUSH_DIR/setup-token.txt`.
   - It is also in the console output with the admin URL and the certificate fingerprint: in the journal under systemd (`journalctl -u crushbalance`), or in `CRUSH_DIR/nohup.out` when the script was started from a terminal.
   - Continue with [First-run setup](First-Run-Setup.md).
- **Headless password instead of the token.** Run `java -jar CrushBalance.jar -set-admin-password` in `CRUSH_DIR`, as the user the service runs as. It prompts twice without echo (or reads one line from stdin when not on a terminal). It works before the first start (it creates `prefs.XML`) and while CrushBalance runs (applied within about 2 seconds). Setup then ends and the token file is deleted.
- **Run as a non-root user** (recommended). It is one setting, `CRUSH_USER`:
  1. Create the user, for example `useradd --system --shell /usr/sbin/nologin crushbalance`.
  2. Set `CRUSH_USER="crushbalance"` in the script.
  3. Give the user the folder: `chown -R crushbalance: /var/opt/CrushBalance` and `chmod 700 /var/opt/CrushBalance`.
  4. As root, run `./crushbalance_init.sh install`.
  - The systemd unit then has `User=crushbalance` and `AmbientCapabilities=CAP_NET_BIND_SERVICE`: CrushBalance runs as that user and may still open ports below 1024.
  - From then on, manage the service with `systemctl`. **Never run `crushbalance_init.sh` as root** while the folder belongs to the service user: that user can replace the script, and root would run whatever it then holds. To uninstall later, stop the service, give the folder back to root (`chown -R root: /var/opt/CrushBalance`), look at the script, then run `./crushbalance_init.sh uninstall`.
  - Keystores you add later (for example the certbot PKCS12 file of [Operating](Operating.md)) must be readable by that user.
  - Without systemd, `start` run by root starts CrushBalance as `CRUSH_USER` (with `su`, so the user needs a login shell), but nothing gives that user the ports below 1024: use high ports in CrushBalance and redirect to them (`iptables -t nat -A PREROUTING -p tcp -m tcp --dport 21 -j REDIRECT --to-ports 2121`).
  - Run every CLI command (`-set-admin-password`, `-check-config`) as that user too, so the files keep their owner.
- **A copy of the script from CrushBalance 11** still starts CrushBalance 27 when its `JAVA` is Java 21 or newer, but it downloads no Java and has the old `install` and settings. Replace it ([Upgrading](Upgrading.md)).

## CrushBalance's own Java

CrushBalance can bring its own Java and keep it up to date: Eclipse Temurin's JRE, the newest LTS, downloaded from Adoptium into the folder `Java` of the install folder. It is the same on Windows and macOS, where `CrushBalance.exe` and `CrushBalance.app` do what the script does here.

- **Whose Java.** Only a `Java` folder that CrushBalance downloaded is CrushBalance's: it holds the file `crushftp-java.txt`. A Java of the system, one you name with `JAVA=`, and a `Java` folder you put there yourself are used as they are and never changed.
- **The first start.** With no Java 21 or newer on the host, the script downloads the JRE, checks it against the SHA-256 that Adoptium publishes, and starts CrushBalance on it. With `OWN_JAVA="true"` at the top of the script it does so although the host has a Java.
- **Updates.** Running on its own Java, CrushBalance asks Adoptium for the newest LTS two minutes after its start and then every 12 hours. A newer one is downloaded into `Java.new` beside the one in use, gets your changes to `cacerts`, `java.security` and `krb5.conf`, and is started once as a test. The log says when it is ready.
- **CrushBalance never restarts itself for a new Java**, because a restart ends every connection. The new Java is put in place at the next start, whenever you make it; the one before stays as `Java.old` until CrushBalance is up.
- **A new Java that does not start** is taken back out, the Java before it is used, and that release is not tried again (it is written to `java_bad` in the install folder).
- **Network.** HTTPS to `api.adoptium.net` and `github.com` (and where GitHub sends its downloads). A host that reaches neither stays on the Java it has. `JAVA_API` in the script names a copy of Adoptium's API in your own network.
- **Turning it off.** Install Java 21 or newer on the host and remove the `Java` folder while CrushBalance is stopped, or name a Java with `JAVA=`. Keeping that Java current is then yours again.
