Consolidate sudo checks.

Closes #20318.