Linux login 命令实战示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将探索 Linux 的 login 命令及其实际应用。实验内容包括理解 login 命令的基本语法和用法、以普通用户身份登录以及以 root 用户身份登录。本实验旨在全面介绍 login 命令,这是系统管理员和用户访问其账户并在 Linux 系统上执行各种任务的关键工具。

实验首先解释了 login 命令的基本语法及其各种选项。然后演示了如何以普通用户身份登录,只需输入 login 并在提示时输入用户名和密码。最后,实验介绍了以 root 用户身份登录的过程,这需要额外的权限,应谨慎使用。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/UserandGroupManagementGroup -.-> linux/passwd("Password Changing") linux/UserandGroupManagementGroup -.-> linux/su("User Switching") subgraph Lab Skills linux/passwd -.-> lab-422766{{"Linux login 命令实战示例"}} linux/su -.-> lab-422766{{"Linux login 命令实战示例"}} end

理解 login 命令

在这一步骤中,我们将探索 Linux 中的 login 命令,该命令用于登录系统。login 命令是系统管理员和用户访问其账户并执行各种任务的关键工具。

首先,让我们了解 login 命令的基本语法:

login [options] [username]

login 命令可以与多种选项一起使用,例如 -p 用于保留环境变量,-h 用于指定主机,以及 -f 用于强制登录而无需身份验证。

要以普通用户身份登录,只需输入 login 并在提示时输入用户名和密码:

$ login
Username: labex
Password:

示例输出:

Last login: Thu Apr 13 10:30:00 UTC 2023 on pts/0
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-69-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and services to reduce
security risks and improve performance. If you need to restore functionality
please use the 'sudo apt-get install -y ubuntu-standard' command.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

labex@ubuntu:~/project$

输出显示用户 labex 已成功登录系统。

以普通用户身份登录

在这一步骤中,我们将学习如何在 Linux 系统中以普通用户身份登录。

正如前一步骤中提到的,login 命令用于登录系统。要以普通用户身份登录,只需输入 login 并在提示时输入用户名和密码:

$ login
Username: labex
Password:

示例输出:

Last login: Thu Apr 13 10:30:00 UTC 2023 on pts/0
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-69-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and services to reduce
security risks and improve performance. If you need to restore functionality
please use the 'sudo apt-get install -y ubuntu-standard' command.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

labex@ubuntu:~/project$

输出显示用户 labex 已成功以普通用户身份登录系统。

以 root 用户身份登录

在这一步骤中,我们将学习如何在 Linux 系统中以 root 用户身份登录。root 用户是具有最高权限的超级用户,可以完全访问系统。

要以 root 用户身份登录,你可以使用 login 命令并指定 root 用户名:

$ login root
Password:

示例输出:

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-69-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and services to reduce
security risks and improve performance. If you need to restore functionality
please use the 'sudo apt-get install -y ubuntu-standard' command.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@ubuntu:~/project#

输出显示用户已成功以 root 用户身份登录。注意提示符已更改为 root@ubuntu:~/project#,表示当前为 root 用户。

作为最佳实践,建议使用 sudo 命令而不是直接以 root 用户身份登录。这有助于更好地维护系统的安全性和控制。

总结

在本实验中,我们探索了 Linux 的 login 命令,该命令用于登录系统。我们学习了 login 命令的基本语法,以及如何以普通用户身份登录,只需输入 login 并在提示时输入用户名和密码。我们还讨论了以 root 用户身份登录的过程,这需要特殊权限,应谨慎操作。实验提供了实际示例,并强调了 login 命令对系统管理员和用户访问账户并执行各种任务的重要性。

Linux 命令速查表