Linux 是一种开源操作系统,可在从恒温器和智能手机到大型数据服务器场的各种设备上运行。Linux 有许多集成库可用,可提供非常稳定、快速、安全、可扩展和可定制的环境。
Linux 发行版是围绕 Linux 内核构建的操作系统。不同发行版之间的主要区别在于目录结构、可用命令以及软件包管理器。软件包管理器用于安装发行版存储库中的新软件。Raspberry Pi 电路板通常运行一个名为 Raspbian 的 Linux 发行版作为操作系统。Raspbian 是基于非常受欢迎的 Debian Linux 发行版。
与任何其他操作系统一样,Raspbian 允许通过外壳命令行界面 (CLI) 使用标准 Linux 命令控制电路板。虽然这听起来很可怕,尤其是现在的图形用户界面 (GUI) 使得所有操作都很直观且非常容易,但是命令行仍然是适用于高级操作的非常强大的工具。本实验将允许您尝试在大多数 Linux 发行版上可用的一些基本 CLI 命令。
pwd
命令并按 Enter 键,从而显示当前目录。cd /home/pi
以更改为 pi 目录。pwd
命令并按 Enter 键,从而显示当前目录。cd ~
。输入 pwd
命令并按 Enter 键,从而显示当前目录。
root@chestnut:/home/pi/notebooks# pwd
/home/pi/notebooks
root@chestnut:/home/pi/notebooks# cd /home/pi
root@chestnut:/home/pi# pwd
/home/pi
root@chestnut:/home/pi# cd ~
root@chestnut:~# pwd
/root
root@chestnut:~#
注:~ 代表到用户主目录的路径。每个 Linux 用户都拥有自己的主目录。
键入 ls
命令以列出当前目录的内容。
ls -l
命令。键入 ls -la
命令。
root@chestnut:~# ls
root@chestnut:~# ls -l
total 0
root@chestnut:~# ls -la
total 4684
drwx------ 13 root root 4096 Dec 1 09:20 .
drwxr-xr-x 21 root root 4096 Jul 20 00:57 ..
-rw------- 1 root root 2868 Jul 28 21:38 .bash_history
-rw-r--r-- 1 root root 565 Jul 20 02:00 .bashrc
drwxr-xr-x 4 root root 4096 Nov 30 14:40 .cache
drwxr-xr-x 4 root root 4096 Mar 4 2016 .config
drwxr-xr-x 5 root root 4096 Mar 4 2016 .ipython
drwx------ 3 root root 4096 Dec 4 22:17 .jupyter
drwx------ 3 root root 4096 Mar 3 2016 .local
-rw-r--r-- 1 root root 140 Nov 19 2007 .profile
drwx------ 2 root root 4096 Jul 20 21:40 .ssh
root@chestnut:~#
注:您可以键入 ls -l -a
,也可以将选项加入到 ls –la
中。选项解释如下:
-l
- 显示输出的详细版本
-a
- 显示所有文件和目录,包括隐藏文件。在 Linux 中,隐藏文件以一个句点开头。
使用 nano 创建一个新文件,用于即将执行的任务。
root@chestnut:~# nano myPythonScript.py
在编辑器中输入窗口中显示的行。在最后一行后面,使用组合键 Control-X。编辑器将提示您确认保存文件,请按 y 键回答,然后按 ENTER 键接受建议的文件名“myPythonScript.py
”。
使用 ls
命令验证是否已创建该文件。
root@chestnut:~# ls
myPythonScript.py
使用 cat
命令显示新建文件的内容。
root@chestnut:~# cat myPythonScript.py
#!/usr/bin/env python3
a = 1
b = 2
if (a < b):
print ("a")
else:
print ("b")
print ("bye bye")
使用 ls -l iot.txt
命令检查名为 iot.txt
的文件。
root@chestnut:~# ls -l iot.txt
ls: cannot access ioe.txt: No such file or directory
使用 touch
命令通过键入 touch iot.txt
来创建 iot.txt
文件。
root@chestnut:~# touch iot.txt
使用 ls –l iot.txt
命令显示文件列表。
root@chestnut:~# ls -l iot.txt
-rw-r--r-- 1 root root 0 May 13 14:15 iot.txt
使用 head
命令显示文件的前几行。
tail
命令显示文件的后几行。root@chestnut:~# head -3 myPythonScript.py
#!/usr/bin/python
a = 1
root@chestnut:~# tail -3 myPythonScript.py
print "b"
print "bye bye"
注:head 和 tail 的数字选项定义要显示的文件的行数。
使用 mkdir. 命令创建一个名为
myFolder` 的新目录。
root@chestnut:~# mkdir myFolder
列出目录的内容,以验证是否已使用 ls
命令创建新目录。
root@chestnut:~# ls
iot.txt myFolder myPythonScript.py
使用 cp
命令创建名为 backupPythonScript.py
的 myPythonScript.py
备份。
使用 ls
命令验证是否已创建该文件。
root@chestnut:~# cp myPythonScript.py backupPythonScript.py
root@chestnut:~# ls
backupPythonScript.py iot.txt myFolder myPythonScript.py
使用 mv
命令将 backupPythonScript.py
移动到 myFolder
目录。
ls
命令验证是否已创建该文件。root@chestnut:~# mv backupPythonScript.py myFolder/
root@chestnut:~# ls myFolder
backupPythonScript.py
man ls
以显示 Linux 命令 ls
的帮助手册。您可以通过按 Enter 键逐行浏览输出,按空格键并使用 PageUp 和 PageDown 键逐页浏览。要退出,请按 q
键。使用 more
和 less
命令来控制输出滚动。注:less 命令的作用方式与 more 命令一样,但用户可以使用 PageUp 和 PageDown 键来浏览文件。man 命令的输出使用 less 来显示信息,使得浏览手册页面更加容易。
发出命令 less /etc/passwd
以显示密码文件。结束时按“q
”。
root@chestnut:~# less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
pi:x:1000:1000:,,,:/home/pi:/bin/bash
使用 ps
命令显示当前用户的基本进程信息。
root@chestnut:~# ps
PID TTY TIME CMD
7195 pts/0 00:00:00 bash
7223 pts/0 00:00:00 ps
使用 ps aux
命令显示所有运行进程的信息。
root@chestnut:~# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 2148 1360 ? Ss 13:57 0:02 init [
root 2 0.0 0.0 0 0 ? S 13:57 0:00 [kthre
root 3 0.0 0.0 0 0 ? S 13:57 0:00 [ksoft
root 5 0.0 0.0 0 0 ? S< 13:57 0:00 [kwork
root 6 0.1 0.0 0 0 ? S 13:57 0:44 [kwork
root 7 0.0 0.0 0 0 ? S 13:57 0:10 [rcu_p
root 8 0.0 0.0 0 0 ? S 13:57 0:00 [rcu_s
root 9 0.0 0.0 0 0 ? S 13:57 0:00 [rcu_b
root 10 0.0 0.0 0 0 ? S< 13:57 0:00 [khelp
root 11 0.0 0.0 0 0 ? S 13:57 0:00 [kdevt
root 12 0.0 0.0 0 0 ? S< 13:57 0:00 [netns
root 13 0.0 0.0 0 0 ? S< 13:57 0:00 [perf]
root 14 0.0 0.0 0 0 ? S 13:57 0:00 [khung
root 15 0.0 0.0 0 0 ? S< 13:57 0:00 [write
使用 top
命令显示占用系统资源的进程。
top - 20:34:35 up 6:36, 0 users, load average: 0.29, 0.25, 0.32
Tasks: 72 total, 2 running, 70 sleeping, 0 stopped, 0 zombie
%Cpu(s): 19.9 us, 6.8 sy, 0.0 ni, 73.0 id, 0.0 wa, 0.0 hi, 0.3 si,
KiB Mem: 445804 total, 149900 used, 295904 free, 53924 buffer
KiB Swap: 102396 total, 0 used, 102396 free, 41940 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAN
2551 pi 20 0 98.4m 14m 6820 R 25.0 3.3 124:50.45 wyliodri
7342 pi 20 0 3140 1988 1672 R 1.0 0.4 0:00.48 top
6 root 20 0 0 0 0 S 0.3 0.0 0:45.06 kworker/
2715 root 20 0 0 0 0 S 0.3 0.0 0:44.91 kworker/
1 root 20 0 2148 1360 1256 S 0.0 0.3 0:02.59 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:01.00 ksoftirq
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/
7 root 20 0 0 0 0 S 0.0 0.0 0:10.59 rcu_pree
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_sche
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
11 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kdevtmpf
注:要退出 top 命令,请按键盘上的 q 键。
以下序列将创建一个在后台运行并睡眠 500 秒的进程。一旦进程启动,则可以确定并终止进程 ID。
sleep 500 &
以在后台启动一个进程。ps
命令以确定进程 ID,在该示例中,睡眠进程为 8178
。kill 8178
以终止与之前的 sleep 命令相关联的进程。root@chestnut:~# sleep 500 &
[1] 8178
root@chestnut:~# ps
PID TTY TIME CMD
7195 pts/0 00:00:01 bash
8178 pts/0 00:00:00 sleep
8182 pts/0 00:00:00 ps
root@chestnut:~# kill 8178
[1]+ Terminated sleep 500
发出 ifconfig eth0
命令以显示基本网络信息。
root@chestnut:~# ifconfig eth0
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether b8:27:eb:ae:ce:5e txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
注:如果您通过无线连接,则 eth0 接口当前没有 IP 地址。您可以输入 ifconfig wlan0
命令来显示有关无线接口的网络信息。
输入 ip route
以显示额外的网络信息。
root@chestnut:~# ip route
default via 10.100.80.1 dev wlan0
10.100.80.0/24 dev wlan0 proto kernel scope link src 10.100.80.132
本节重点介绍文件权限及其对执行文件中指令的能力的影响。
发出命令 ls –l myPythonScript.py
,以显示之前创建的 myPythonScript.py 的当前文件属性。
root@chestnut:~# ls -l myPythonScript.py
-rw-r--r-- 1 root root 88 May 13 14:08 myPythonScript.py
注:文件权限分为四个不同的部分。第一部分是该行开头的“-”,表示这是一个正常文件、目录或链接文件。“-”表示这是一个正常文件。接下来的三部分由三个表示权限的特定字符组成。这三个部分是指所有者、组和其他用户。此文件的所有者具有读取和写入权限 (rw-),组具有读取权限 (r--),其他用户具有读取权限 (r--)。
尝试通过键入文件名来执行文件。有必要在文件的名称之前加上“./
”,这样系统就会知道文件的位置。发出命令 ./myPythonScript.py
。
root@chestnut:~# ./myPythonScript.py
bash: ./myPythonScript.py: Permission denied
注:为了使文件运行,必须设置执行位。至少权限的所有者部分应指示读取/写入/执行权限 (rwx),而不是 (rw)。另请确保使用确切的文件名。在 Linux 环境中,文件名分区大小写。
使用命令 chmod
为 myPythonScript.py
文件添加可执行标志。输入命令 chmod +x myPythonScript.py
。
root@chestnut:~# chmod +x myPythonScript.py
root@chestnut:~# ls -l myPythonScript.py
-rwxr-xr-x 1 root root 88 May 13 14:08 myPythonScript.py
注:现在所有者的权限为 (rwx),组的权限为 (r-x),其他用户的权限为 (r-x)。该文件现在具有不同的颜色,也表示它现在可执行。
发出命令 ./myPythonScript.py
,立即执行该文件。
root@chestnut:~# ./myPythonScript.py
a
bye bye
使用 free
命令检查内存使用情况。
root@chestnut:~# free
total used free shared buffers cached
Mem: 445804 234824 210980 0 97948 71208
Swap: 102396 0 102396
使用 -h
选项以及 free 以“人类可读”格式显示内存,用兆字节而不是字节列出相应值。输入命令 free –h
。
root@chestnut:~# free -h
total used free shared buffers cached
Mem: 435M 229M 205M 0B 95M 69M
Swap: 99M 0B 99M
使用 df 和 df -h
命令检查文件系统上的可用磁盘空间量。
root@chestnut:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 7065848 2748116 3982176 41% /
devtmpfs 443788 0 443788 0% /dev
tmpfs 448396 0 448396 0% /dev/shm
tmpfs 448396 7412 440984 2% /run
tmpfs 5120 4 5116 1% /run/lock
tmpfs 448396 0 448396 0% /sys/fs/cgroup
/dev/mmcblk0p1 510984 139580 371404 28% /boot
posix-overlay(/boot/notebooks) 510984 139580 371404 28% /home/pi/notebooks
root@chestnut:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 6.8G 2.7G 3.8G 41% /
devtmpfs 434M 0 434M 0% /dev
tmpfs 438M 0 438M 0% /dev/shm
tmpfs 438M 7.3M 431M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 438M 0 438M 0% /sys/fs/cgroup
/dev/mmcblk0p1 500M 137M 363M 28% /boot
posix-overlay(/boot/notebooks) 500M 137M 363M 28% /home/pi/notebooks
shutdown
命令用于关闭 Pi。必须拥有更高的权限才能运行该命令。输入 sudo shutdown –h
命令,立即关闭系统。
root@chestnut:~# sudo shutdown –h now
如果之前的命令已执行,则需要通过拔下电源并重新插入来重新启动 Pi。当 Pi 重新联机时,请重新连接到 PL-App。
apt-get
命令检索、安装和管理 Raspberry Pi 上的软件包。root@chestnut:~# sudo apt-get install mc
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
zip links w3m lynx arj dbview odt2txt gv catdvi
djvulibre-bin imagemagick python-boto python-tz
The following NEW packages will be installed:
mc
0 upgraded, 1 newly installed, 0 to remove and 100 not upgraded.
Need to get 405 kB of archives.
After this operation, 1160 kB of additional disk space will be
used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main
mc armhf 3:4.8.3-10 [405 kB]
Fetched 405 kB in 1s (239 kB/s)
Selecting previously unselected package mc.
(Reading database ... 95174 files and directories currently ins
talled.)
Unpacking mc (from .../mc_3%3a4.8.3-10_armhf.deb) ...
Processing triggers for menu ...
Processing triggers for desktop-file-utils ...
Setting up mc (3:4.8.3-10) ...
Processing triggers for menu ...
注:Raspberry Pi 上使用的 Raspbian Linux 发行版基于 Debian。您可以使用 apt-get 安装官方存储库中的任何软件包。在上述示例中,已从存储库下载 Midnight Commander(CLI 图形文件管理器),并使用 apt-get 进行安装。