English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Commande Linux su

Linux 命令大全

La commande Linux su (en anglais complet : switch user) est utilisée pour changer l'identité de l'utilisateur, à l'exception de root, il est nécessaire de saisir le mot de passe de cet utilisateur.

Autorisation d'utilisation : tous les utilisateurs.

Grammaire

su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]]

Explication des paramètres:

  • -f ou --fast ne lit pas le fichier de démarrage (comme csh.cshrc, etc.), utilisé uniquement pour csh ou tcsh
  • -m -p ou --preserve-environment ne change pas les variables d'environnement lors de l'exécution de su
  • -c command ou --command = command change l'utilisateur en USER et exécute l'instruction (command) avant de revenir à l'utilisateur d'origine
  • -s shell ou --shell = shell spécifie le shell à exécuter (bash, csh, tcsh, etc.), la valeur par défaut étant /etc/passwd 内的该使用者(USER) shell
  • --help 显示说明文件
  • --version 显示版本信息
  • - -l 或 --添加 login 参数后,就如同重新 login 为该使用者一样,大部分环境变量(HOME SHELL USER等等)都是以该使用者(USER)为主,并且工作目录也会改变,如果没有指定 USER ,默认是 root
  • USER 想要变更的使用者账户
  • ARG 传入新的 shell 参数

在线示例

将账户变更为 root 并在执行 ls 指令后退出变回原使用者

su -c ls root

将账户变更为 root 并传递 -f 参数传递给新执行的 shell

su root -f

将账户变更为 clsung 并将工作目录变更为 clsung 的家目录(home dir)

su - clsung

切换用户

[email protected]:~$ whoami //显示当前用户
hnlinux
[email protected]:~$ pwd //显示当前目录
/home/hnlinux
[email protected]:~$ su root //切换到root用户
密码: 
[email protected]:/home/hnlinux# whoami 
root
[email protected]:/home/hnlinux# pwd
/home/hnlinux

切换用户,改变环境变量

[email protected]:~$ whoami //显示当前用户
hnlinux
[email protected]:~$ pwd //显示当前目录
/home/hnlinux
[email protected]:~$ su - root //切换到root用户
密码: 
[email protected]:/home/hnlinux# whoami 
root
[email protected]:/home/hnlinux# pwd //显示当前目录
/root

Linux 命令大全