Operations 12 min read

Understanding and Using su and sudo Commands in Linux

This article provides a comprehensive guide to the Linux su and sudo commands, explaining their differences, usage options such as login-shell and non-login-shell, how to switch users, set passwords, configure sudoers, and practical examples for managing user privileges securely.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding and Using su and sudo Commands in Linux

This guide explains the differences and usage of the Linux su and sudo commands for switching users and executing commands with elevated privileges.

Preparation : Create a test user with useradd -m test_user, then set its password using passwd test_user. Switch back to the original user with exit.

su command : Demonstrates non‑login shell ( su test_user) versus login shell ( su - test_user) and shows how environment variables like PWD differ. Includes examples of switching from ubuntu to root and back.

sudo command : Describes the meaning of sudo (“super user do”), how to run a command with root privileges, and shortcuts like sudo !!. Shows how /etc/sudoers controls who can use sudo, the use of visudo for safe editing, and adds test_user to the sudoers file with a line such as test_user ALL=(ALL:ALL) ALL.

Comparison : Highlights security differences between using su - (requires root password) and sudo su - (requires only the current user's password) and why sudo is preferred for controlled privilege escalation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem AdministrationUser ManagementSudosusudoers
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.