A simple CLI utility for promoting an existing Moodle user to a Site Administrator.
- Shell access to the Moodle server
- PHP CLI
- A working Moodle installation
- Permission to execute PHP scripts from the command line
Copy 'makeadmin.php' into your Moodle installation's 'admin/cli' directory.
moodle/
├── admin/
│ └── cli/
│ └── makeadmin.php
├── config.php
└── ...
Ensure the script includes the correct path to Moodle's 'config.php': php require(DIR . '/../../config.php');
Run the script from the Moodle root:
bash
php admin/cli/makeadmin.php <username>
Or from within the 'admin/cli' directory:
bash
php makeadmin.php <username>
bash
php admin/cli/makeadmin.php jsmith
Output:
bash
User 'jsmith' (ID 42) is now a site administrator.
If the user is already an administrator:
bash
User 'jsmith' is already a site administrator.
If the user cannot be found:
bash
Error: User 'jsmith' not found.
The script:
- Loads the Moodle environment.
- Looks up the specified username.
- Reads the current 'siteadmins' configuration.
- Adds the user's ID if it is not already present.
- Saves the updated administrator list.
No existing administrators are removed.
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Incorrect usage (missing username) |
| 2 | User not found |
- The user must already exist in Moodle.
- The script only grants the Site Administrator role.
- Running the script multiple times for the same user is safe; duplicate administrator entries are not created.
This script modifies Moodle's global administrator configuration. Use it only if you have appropriate administrative access to the Moodle server.