If $g$ is the generator of a group $G$, order $n$, when is $g^k$ a generator? [duplicate]

Say a cyclic group of order $9$ is denoted by $G = \langle g \rangle$. I have a claim that $\langle g^k \rangle = G $ whenever $k$ is a unit in $\mathbb{Z}_9$. I'm struggling to understand why this is true. Is this specific to order 9? In general, for a group of order $n$, do we require that $k$'s multiplicative inverse in $\mathbb{Z}_n$ be $n$ itself? Otherwise the generating group will be of order less than $n$, and thus cannot be equal to the group.


Hint: $\langle g^k \rangle = \langle g^d \rangle$ for $d=\gcd(k,n)$.

One inclusion is obvious. The other follows from Bézout's identity.


Let $k = \gcd(k,n)d$ for some $d$. Then $g^{k} = (g^{\gcd(k,n)})^d$. If $n=\gcd(k,n)d'$ then if $\gcd(k,n)>1$ $d'<n$ and therefore $g^k$ cannot be a generator (as its order is at most $d'$ which is less than $n$). But then also if $\gcd(k,n)=1$ then there are integers, $x,y$ so that $kx+ny=1$ and therefore $g^{kx}=g^{kx+ny}=g$ so $g^k$ generates $g$ and therefore all of $G=\langle g\rangle$ so $\gcd(k,n)=1$ is also sufficient for $g^k$ to generate $G$.