欧博娱乐Linux Command Guide

The `su` (substitute user or switch user) command is a fundamental utility in Unix and Linux systems that allows a user to temporarily become another user during a login session. Most commonly, it's used to gain root (administrative) privileges without logging out and logging back in as the root user. When invoked without any arguments, `su` defaults to attempting to become the superuser (root). When a username is provided as an argument, `su` attempts to become that specific user. In either case, authentication is required by entering the target user's password, not the password of the current user (unlike `sudo`, which uses the current user's password). There are two primary modes of operation for `su`: 1. **Non-login Shell Mode**: By default, `su` provides a non-login shell. This means it switches to the target user but keeps most of the current environment variables. The working directory remains the same, and only the USER, LOGNAME, and HOME variables are changed. This mode is useful for quickly running commands as another user without changing the entire environment. 2. **Login Shell Mode**: When used with the `-` or `-l` option, `su` simulates a full login for the target user. It changes to the target user's home directory, sets up their environment variables by reading the standard login scripts (like ~/.profile, ~/.bash_profile, etc.), and provides an experience close to what the user would get if they had logged in directly. This mode is ideal when you want to work extensively as the target user. The `su` command is commonly used for: - Performing system administration tasks that require elevated privileges - Running applications or services as a different user for security purposes - Testing the environment or configuration of another user account - Diagnosing permission-related issues by temporarily becoming the affected user It's important to note some security considerations when using `su`: - Regular users can only switch to another user if they know that user's password - The root user can become any user without providing a password - All `su` attempts (successful or failed) are typically logged in system logs - Modern security practices often favor using `sudo` over `su` for more granular control - Some systems may restrict `su` access to users in specific groups (e.g., the 'wheel' group) While powerful, `su` should be used judiciously, especially when switching to the root user, as mistakes made with root privileges can have serious consequences for system stability and security.

2025-07-16 03:54 点击量:0