> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to remove the default user in a cluster and use a cluster secret for inter-node authentication

> In a self-managed cluster, Distributed queries connect to the other servers as the default user. Replace it with an administrator account and a cluster secret before you remove it.

In a self-managed cluster, a server connects to the other servers for `Distributed` queries as the user configured in `remote_servers`. Unless you configured something else, that is `default` with an empty password. Before you remove the `default` user, create another administrator on every server and switch the cluster to a shared `<secret>`. Then remove `default` in the users configuration.

This article applies to self-managed ClickHouse. For a single server, see [How do I remove the default user?](https://clickhouse.com/docs/resources/support-center/knowledge-base/security/remove-default-user.md).

## Step 1: create a replacement administrator on every server

Connect as `default` (the `users.xml` file that ships with ClickHouse [enables SQL access management](https://clickhouse.com/docs/concepts/features/security/access-rights.md#enabling-access-control) for it) and create a role and a user on all servers of the cluster. Replace `my_cluster` with the name of your cluster:

```sql theme={null}
CREATE ROLE admin_role ON CLUSTER my_cluster;

CREATE USER admin ON CLUSTER my_cluster IDENTIFIED WITH sha256_password BY '<password>';

GRANT ON CLUSTER my_cluster admin_role TO admin;
```

Then copy the privileges of `default` to the role. Run this statement on each server, connected as `default`:

```sql theme={null}
GRANT CURRENT GRANTS ON *.* TO admin_role WITH GRANT OPTION;
```

[`GRANT CURRENT GRANTS`](https://clickhouse.com/docs/reference/statements/grant#grant-current-grants-syntax) grants everything the current user is able to grant. It cannot be combined with `ON CLUSTER`:

```text theme={null}
Code: 36. DB::Exception: GRANT CURRENT GRANTS can't be executed on cluster.. (BAD_ARGUMENTS)
```

Compare the result with the privileges of `default`, then log in as `admin` on every server before you continue:

```sql theme={null}
SHOW GRANTS FOR default;
SHOW GRANTS FOR admin_role;
```

## Step 2: replace the user and password in `remote_servers` with a secret

On every server, add a `<secret>` to the definition of the cluster and remove the `<user>` and `<password>` elements of the replicas, if there are any:

```xml theme={null}
<clickhouse>
    <remote_servers>
        <my_cluster>
            <secret>REPLACE_WITH_A_LONG_RANDOM_STRING</secret>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>node1.example.com</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>node2.example.com</host>
                    <port>9000</port>
                </replica>
            </shard>
        </my_cluster>
    </remote_servers>
</clickhouse>
```

What changes with the secret:

* The servers authenticate each other with the secret instead of a user name and a password. Every server must define the cluster under the same name and with the same secret.
* A server that receives a `Distributed` query runs it as the user who started the query, not as the user from `remote_servers`. Every user who queries `Distributed` tables must therefore exist on all servers and have the privileges on the underlying local tables there. See the [`Distributed` engine](https://clickhouse.com/docs/reference/engines/table-engines/special/distributed#distributed-clusters) documentation.

The server picks up changes of the cluster configuration without a restart. Change all servers together. While a server that has the secret talks to a server that has no secret or a different one, its `Distributed` queries fail with `ALL_CONNECTION_TRIES_FAILED`, and the log of the receiving server contains `Interserver authentication failed` with the code `AUTHENTICATION_FAILED`.

Check the result with a query that reaches every replica and reads its own entries in `system.processes`:

```sql theme={null}
SELECT
    hostName() AS host,
    user,
    initial_user
FROM clusterAllReplicas('my_cluster', system.processes)
WHERE initial_query_id = initialQueryID()
ORDER BY host;
```

The `user` column shows the account under which each replica runs its part of the query. Without the secret, the remote replicas report the user from `remote_servers`. This is the output on a cluster of two servers, for a query started by `admin` on `node1`:

```text theme={null}
   ┌─host──┬─user────┬─initial_user─┐
1. │ node1 │ admin   │ admin        │
2. │ node2 │ default │ admin        │
   └───────┴─────────┴──────────────┘
```

With the secret in effect, every replica reports the user you are logged in as:

```text theme={null}
   ┌─host──┬─user──┬─initial_user─┐
1. │ node1 │ admin │ admin        │
2. │ node2 │ admin │ admin        │
   └───────┴───────┴──────────────┘
```

Do not use the [`currentUser`](https://clickhouse.com/docs/reference/functions/regular-functions/other-functions#currentUser) function for this check: in a distributed query it returns the name of the user who started the query on every replica, with or without the secret.

### Reload the `Distributed` tables

The check above covers queries that read. The background sends of a [`Distributed`](https://clickhouse.com/docs/reference/engines/table-engines/special/distributed) table are different: a table that existed before the change keeps the connections it was created with, so its queued inserts still go to the other servers as `default`, with or without the secret in the configuration. This is tracked in [issue #136247](https://github.com/ClickHouse/ClickHouse/issues/136247). If you remove `default` in this state, the inserts of every such table stay in its queue and fail with `AUTHENTICATION_FAILED` for `default`. No rows are lost, but none arrive either.

After the secret is in place on all servers, make every server load its `Distributed` tables again. Either restart the servers one by one, or run this on each server for each `Distributed` table:

```sql theme={null}
DETACH TABLE db.events_distributed;
ATTACH TABLE db.events_distributed;
```

Then check that the queues are empty and report no error:

```sql theme={null}
SELECT database, table, data_files, last_exception
FROM system.distribution_queue
WHERE data_files > 0 OR last_exception != '';
```

If you removed `default` before this step, the same two statements make the queued rows go out.

## Step 3: remove the `default` user

The `default` user is defined in `users.xml`, not in SQL, so `DROP USER default` fails with `ACCESS_STORAGE_READONLY`. Remove it with an override file on every server, for example `/etc/clickhouse-server/users.d/remove_default_user.xml`:

```xml theme={null}
<clickhouse>
    <users>
        <default remove="remove"/>
    </users>
</clickhouse>
```

The `remove` attribute deletes the element from the [merged configuration](https://clickhouse.com/docs/concepts/features/configuration/server-config/configuration-files.md#merging), and the server reloads the users configuration without a restart. A connection as `default` is then refused:

```text theme={null}
Code: 516. DB::Exception: default: Authentication failed: password is incorrect, or there is no user with such name.
```

## Why the order matters

If you remove `default` while `remote_servers` has neither a secret nor another `<user>`, the servers still try to connect to each other as `default`, and every `Distributed` query that needs another server fails to authenticate. Configure the secret first, verify it, and remove the user last.

Before you remove the user, also look for everything else that connects as `default` without saying so:

* Clients, scripts and monitoring agents that do not pass a user name.
* Dictionaries with a `CLICKHOUSE` source and no `USER`: such a source logs in as `default`, and the dictionary fails to load with `AUTHENTICATION_FAILED` when that user does not exist. Set `USER` and `PASSWORD` in the source first.

`ON CLUSTER` statements and replication do not depend on the `default` user. `ON CLUSTER` statements are distributed through a task queue in ClickHouse Keeper, and replicas authenticate to each other with [`interserver_http_credentials`](https://clickhouse.com/docs/reference/settings/server-settings/settings/interserver-http#interserver_http_credentials), if you configured them.

> [!NOTE]
>
> Instead of a secret you can keep `<user>` and `<password>` in `remote_servers` and point them to a dedicated account. In that case the remote servers run all `Distributed` queries as that one account, whoever started the query.

## Related documentation

* [`Distributed` table engine: clusters](https://clickhouse.com/docs/reference/engines/table-engines/special/distributed#distributed-clusters)
* [Configuration files](https://clickhouse.com/docs/concepts/features/configuration/server-config/configuration-files.md)
* [Access control and account management](https://clickhouse.com/docs/concepts/features/security/access-rights.md)
