The command your in Linux: change user in terminal

Introduction

In the Linux world, the terminal is one of the most powerful tools to manage the system. We often need to run actions with the privileges of another user, either to perform maintenance tasks, test settings or access restricted files. The commandsu(switch user) allows you to change the user directly from the command line without closing the current session. In this article we will explore its operation, syntax, more common options and some safety recommendations for responsible use.

What does your command do?

The commandsustart a new shell under the identity of another user. By default, if no user name is specified,suchange the root user. Unlikesudowhich runs a single command with high privileges,suopens a full interactive session, allowing you to run several commands successively under the same identity.

Basic syntax

The simplest way is:

your [user _ name]

If omittednombre_de_usuario, the system assumesroot. After running the command, the target user password will be requested. Once authenticated, a new shell will open and the prompt will change to reflect the new user.

Most commonly used options

  • -lor--login: starts a login shell, which means that the user profile files are read (.profile, .bashrc, etc.) and the environment is established as if it had normally login.
  • -c commandor--command comando: ejecuta únicamente el comando especificado y luego vuelve a la shell original, sin mantener una sesión interactiva.
  • -s shellor--shell shell: indica qué shell se debe usar (por ejemplo, /bin/zsh) instead of the default of the user.
  • -m, -por--preserve-environment: retains the current user's environment variables instead of loading the target user's environment.
  • --helpand--version: show the help and the command version.

Practical examples

    • Change to root and get a login shell:
its -l
    • Change the userjuanand run a single command:
su -c "ls - / home / juan" juan
    • Start a shell likejuanconserving the current environment:
its -p juan
    • Specify a different shell:
su -s / bin / zsh juan

Differences between your and sudo

Although both allow actions with other user privileges, their behavior is different:

  • itscompletely change the user and open a new shell, requiring the target user's password.
  • sudorun a single command with the privileges of another user (usually root) using the current user password, provided it is authorized in the file/etc/sudoers.
  • In environments where it is intended to minimize the exposure of root passwords,sudoIt's usually preferable because you don't need to know the root key.
  • When you need a prolonged session under another user (for example, to test several scripts),suIt's more comfortable.

Good security practices

  • Usesu -lprovided you need a clean and complete environment; this prevents unexpected environment variables from influencing the session.
  • Avoid leaving sessions ofsuopen in unsupervised terminals; finalise withexitorCtrl+DWhen you're done.
  • Preferencesudofor specific tasks and reservesufor cases where you really need an interactive shell.
  • Check the file/etc/pam.d/suto limit who can usesuto root (e.g. by the groupwheel).
  • It monitors attempts tosufailed with tools likeauth.logorjournalctlto detect possible brute force attacks.
  • If your distribution allows, disable direct access to root bysuand forces to usesudoOnly.

Conclusion

The commandsuis an essential tool for the management of Linux systems, allowing a quick and flexible change of user. Know your options and understand when it is more appropriate thansudohelps work more efficiently and safely. By applying the good practices described above, you will be able to take advantage of its full potential without compromising the integrity of your system. Practice in a test environment and incorporatesuin your daily workflow!

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish