# Programmatically add canister controller from within canister

**URL:** <https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009>\
**Category:** Developers\
**Created:** [August 21, 2022, 5:23pm UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009 "2022-08-21T17:23:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [August 21, 2022, 5:23pm UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009/1 "2022-08-21T17:23:07Z")

</div>

Is there any way to programmatically set the canister controller or custodian from within canister itself? I’d like to write a function that transfers ownership of the canister to a principal that’s passed in as an argument.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 21, 2022, 6:32pm UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009/2 "2022-08-21T18:32:01Z")

</div>

[update\_settings](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-update_settings) maybe?

When I create canisters it’s the one I use to define the controllers ([source](https://github.com/papyrs/ic/blob/713a0ea7f9c280138d4decb54f3a72a569a81bd6/canisters/src/utils/canister.utils.mo#L34)).

```auto
public func updateSettings(canisterId: Principal, manager: Principal): async () {
            let controllers: ?[Principal] = ?[canisterId, manager];

            await ic.update_settings(({canister_id = canisterId; settings = {
                controllers = controllers;
                freezing_threshold = null;
                memory_allocation = null;
                compute_allocation = null;
            }}));
        };

```

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [August 22, 2022, 8:11am UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009/3 "2022-08-22T08:11:10Z")

</div>

This is exactly what i was needing. Thank you!

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 22, 2022, 8:27am UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009/4 "2022-08-22T08:27:31Z")

</div>

Cool, my pleasure! Let me know if it works out

---

<div class="post-metadata">

**Author:** ![Safik](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/safik/32/953_2.png) [@Safik](https://forum.dfinity.org/u/Safik)\
**Post date:** [May 12, 2023, 2:22pm UTC](https://forum.dfinity.org/t/programmatically-add-canister-controller-from-within-canister/15009/5 "2023-05-12T14:22:19Z")

</div>

Is it working now? I had problems, but once they did not arise.  
I can still add another controller via the command:  
`dfx canister update-settings authorization-rbac --add-controller`  
But an error appears from the code in motoko  
`The Replica returned an error: code 4, message: "Only the controllers of the canister wckdt-raaaa-aaaaa-aaatq-cai can control it.`
