Unix 简明教程

Getting Started with Linux

让我们从头开始,Linux 的第一步是启动系统以使其生效,使用户能够与其进行交互。因此,让我们从系统启动开始。

Let’s start from very begining and the first step of Linux is to boot the system to make it live which allows users to interact with it. So let’s start with System Bootup.

System Bootup

如果你有一台安装了 Linux 操作系统的计算机,则只需打开该系统的电源即可使其生效。

If you have a computer which has the Linux operating system installed in it, then you simply need to turn on the power of the system to make it live.

只要打开 Linux 系统(你也可以称其为 Linux 机器),它就开始启动并最终提示你登录系统,这是一项登录系统并在你的日常活动中使用它的活动。

As soon as you turn on the Linux system (You can say also Linux Machine), it starts booting up and finally it prompts you to log into the system, which is an activity to log into the system and use it for your day-to-day activities.

Ubuntu Linux 系统的典型登录屏幕如下所示:

A typical login screen for Ubuntu Linux System looks like as follows:

ubuntu login screen

Login Linux

当你首次连接到 Linux/Unix 系统时,你通常会看到一个登录提示,如下所示:

When you first connect to a Linux/Unix system, you usually see a Login prompt such as the following −

login:

To log in

  1. Have your userid (user identification) and password ready. Contact your system administrator if you don’t have these yet.

  2. Type your userid at the login prompt, then press ENTER. Your userid is case-sensitive, so be sure you type it exactly as your system administrator has instructed.

  3. Type your password at the password prompt, then press ENTER. Your password is also case-sensitive.

  4. If you provide the correct userid and password, then you will be allowed to enter into the system. Read the information and messages that comes up on the screen, which is as follows.

login : amrood
amrood's password:
Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
$

一旦您成功登录到 Linux 系统,您将获得一个命令提示符(有时称为 $ 提示符),您可以在其中键入所有 Linux 命令。例如,要检查日历,您需要键入 cal 命令,如下所示:

Once you are successfully logged into the Linux system, you will be provided with a command prompt (sometime called the $ prompt ) where you type all Linux commands. For example, to check calendar, you need to type the cal command as follows −

$ cal
     June 2009
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

$

以下部分将讨论几个重要的 Linux 函数及其关联的命令。

Following section will discuss few important Linux functions alongwith associated commands.

Change Password

所有 Linux 系统都需要密码,以帮助确保您的文件和数据属于您自己,并且系统本身不受黑客和攻击者的侵害。建议您经常更改密码,以防止黑客或任何其他滥用行为。以下是更改密码的步骤:

All Linux systems require passwords to help ensure that your files and data remain your own and that the system itself is secure from hackers and crackers. It is advised to change your password frequently to save it from hackers or any other misuse. Following are the steps to change your password −

Step 1 - 要开始,请在命令提示符处键入 passwd 命令,如下所示。

Step 1 − To start, type passwd command at the command prompt as shown below.

Step 2 - 输入您的旧密码,即您当前正在使用的密码。

Step 2 − Enter your old password, the one you’re currently using.

Step 3 - 输入您的新密码。始终保持密码足够复杂,以便没有人能够猜到它。但确保你能记住它。

Step 3 − Type in your new password. Always keep your password complex enough so that nobody can guess it. But make sure, you remember it.

Step 4 - 您必须通过再次输入密码来验证密码。

Step 4 − You must verify the password by typing it again.

$ passwd
Changing password for amrood
(current) Linux password:******
New Linux password:*******
Retype new Linux password:*******
passwd: all authentication tokens updated  successfully

$

我们在这里添加星号 (*) 只是为了显示您需要输入当前和新密码的位置,否则在您的系统中看不到任何字符。当您输入时,它不会向您显示任何字符。

We have added asterisk (*) here just to show the location where you need to enter the current and new passwords otherwise at your system. It does not show you any character when you type.

Listing Directories and Files

Linux 中的所有数据都组织成文件,所有文件都组织成不同的目录。这些目录组织成一个称为文件系统的树形结构。

All the data in Liux is organized into files and all the files are organized into different directories. These directories are organized into a tree-like structure called the filesystem.

您可以使用 Linux ls 命令列出目录中可用的所有文件或目录。以下是 ls-l 选项一起使用的示例。

You can use the Linux ls command to list out all the files or directories available in a directory. Following is the example of using ls command with -l option.

$ ls -l
total 19621
drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml
-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpg
drwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univ
drwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia
-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tar
drwxr-xr-x  8 root   root        4096 Nov 25  2007 usr
-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php
-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar
-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid
-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swf

$

此处以 d…​.. 开头的条目表示目录。例如,uml、univ 和 urlspedia 是目录,其余条目是文件。

Here entries starting with d…​.. represent directories. For example, uml, univ and urlspedia are directories and rest of the entries are files.

Changing Directories

在处理不同的文件和目录时,您需要进入不同的目录。您可以使用 cd 命令进入目录,如下所示:

While working with different files and directories you will need to go in different directories. You can go inside a directory using the cd command as follows:

$ cd uml

$

上述命令将带您进入 uml 目录,您可以在其中列出可用的目录和文件。一旦您在目录中完成了工作,您可以使用 cd .. 返回到父目录,如下所示:

Above command will take you inside uml directory, where you can list available directories and files. Once you are done with your work in a directory, you can go back to parent directory using cd .. as follows:

$ cd ..

$

Who Are You?

当您登录到系统时,您可能想知道: Who am I ?或者您想知道您当前使用的是谁的登录名?

While you’re logged into the system, you might be willing to know : Who am I? or you would like to know whose login you are using at present?

找到“你是什么”的最简单的方法是输入 whoami 命令如下所示:

The easiest way to find out "who you are" is to enter the whoami command as follows −

$ whoami
 amrood

$

在你的系统上尝试一下。此命令列出了与当前登录关联的账户名称。你也可以尝试 who am i 命令来获取自己的信息。

Try it on your system. This command lists the account name associated with the current login. You can try who am i command as well to get information about yourself.

$ who am i
root     pts/2        2024-04-24 19:22 (49.205.240.120)

$

Who is Logged in?

有时你可能想知道同时登录到电脑的是谁。

Sometime you might be interested to know who is logged in to the computer at the same time.

有三个命令可以让你获取此信息,根据你了解其他用户的程度: userswhow

There are three commands available to get you this information, based on how much you wish to know about the other users: users, who, and w.

$ users
 amrood bablu qadir

$ who
amrood pts1 Oct 8 14:10 (limbo)
bablu  pts2 Oct 4 09:08 (calliope)
qadir  pts3 Oct 8 12:09 (dent)

$

在你的系统上尝试 w 命令以查看输出。这列出了登录到系统用户的关联信息。

Try the w command on your system to check the output. This lists down information associated with the users logged in the system.

$ w
 19:37:51 up 91 days, 13 min,  2 users,  load average: 1.43, 1.77, 1.86
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
bablu    pts/1    49.205.240.120   19:03    7.00s  6.82s  6.79s vi unix-getting-started.htm
amrood   pts/2    49.205.240.120   19:22    0.00s  0.04s  0.01s w

$

Checking Disk Usage

很多时候,你希望了解你的电脑的磁盘使用情况,以确保你在系统上有足够的存储空间。有各种命令可以检查可用磁盘,但使用最频繁的是 df -h ,它提供此类信息:

Many a times you will be interested in knowing the disk usage of your computer to make sure you have enough storage available on the system. There are various commands to check the available disk, but the most frequently used is df -h and it provides information like this:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            13G   16M   13G   1% /run
/dev/nvme0n1p3  1.8T  260G  1.4T  16% /
tmpfs            63G     0   63G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/nvme0n1p2  975M  255M  669M  28% /boot
tmpfs            13G     0   13G   0% /run/user/0

$

Logging Out

当你结束会话时,你需要退出系统。这样做是为了确保其他人无法访问你的文件。

When you finish your session, you need to log out of the system. This is to ensure that nobody else accesses your files.

To log out

To log out

  1. Just type the logout command at the command prompt, and the system will clean up everything and break the connection.

System Shutdown

通过命令行正确关闭 Linux 系统的最一致的方法是使用以下命令之一:

The most consistent way to shut down a Linux system properly via the command line is to use one of the following commands −

Sr.No.

Command & Description

1

halt Brings the system down immediately

2

init 0 Powers off the system using predefined scripts to synchronize and clean up the system prior to shutting down

3

init 6 Reboots the system by shutting it down completely and then restarting it

4

poweroff Shuts down the system by powering off

5

reboot Reboots the system

6

shutdown Shuts down the system

你通常需要成为超级用户或 root(Linux 系统上最具特权的账户)才能关闭系统。然而,在一些独立或个人拥有的 Linux 设备上,管理员用户有时普通用户也可以这样做。

You typically need to be the super user or root (the most privileged account on a Linux system) to shut down the system. However, on some standalone or personally-owned Linux boxes, an administrative user and sometimes regular users can do so.