Linux 是一种自由和开放源代码的类 Unix 操作系统,其内核最初由 Linus Torvalds 于 1991 年开发

本文全面介绍了Linux系统,包括其发展背景、基本架构、核心概念与操作,还提及进阶应用场景及学习资源。同时讲解了使用Docker进行应用容器化的过程,如创建镜像、编写Dockerfile、构建和测试镜像等,帮助读者掌握Linux系统及容器化开发技能。

1. Orientation(引导)

  • 目的:帮助用户熟悉Linux系统的基本环境和操作方式。
  • 内容可能包括
    • 系统界面介绍:介绍Linux的桌面环境(如GNOME、KDE、XFCE等),包括任务栏、菜单、窗口管理等。
    • 文件系统结构:Linux的文件系统是树状结构,从根目录(/)开始,包括/home(用户目录)、/bin(可执行文件)、/etc(配置文件)等。
    • 终端使用:介绍如何打开终端(Terminal),以及一些基本的命令,如ls(列出文件)、cd(切换目录)、pwd(显示当前路径)等。
    • 用户权限:介绍用户(User)、组(Group)和权限(Permissions)的概念,以及如何使用sudo命令以管理员权限执行操作。
    • 帮助命令:介绍如何使用man(手册)命令来获取命令的帮助信息,例如man ls会显示ls命令的详细用法。

2. Setup(设置)

  • 目的:帮助用户完成Linux系统的初始配置,以便开始使用。
  • 内容可能包括
    • 安装软件:介绍如何使用包管理器(如apt在Debian/Ubuntu系统,yumdnf在Red Hat/CentOS系统)安装软件。例如:
      sudo apt update && sudo apt install <软件名>
      
    • 配置网络:设置网络连接,包括有线网络和无线网络。可能需要编辑/etc/network/interfaces文件或使用nmcli命令。
    • 用户账户设置:添加或删除用户,设置用户密码等。例如:
      sudo adduser <用户名>
      sudo passwd <用户名>
      
    • 环境变量配置:介绍如何设置环境变量,例如在~/.bashrc~/.profile文件中添加自定义变量。
    • 系统更新:确保系统是最新的,运行以下命令:
      sudo apt update && sudo apt upgrade
      
    • 个性化设置:根据用户的需求,设置桌面主题、屏幕分辨率、时区等。

示例操作

假设你正在使用Ubuntu系统,以下是一个简单的“orientation and setup”流程:

1. 打开终端
  • 在桌面环境中,可以通过快捷键Ctrl + Alt + T打开终端。
2. 查看文件系统
  • 输入以下命令:
    ls /
    
    你会看到根目录下的文件夹,如binetchome等。
3. 安装软件
  • 以安装vim文本编辑器为例:
    sudo apt update
    sudo apt install vim
    
4. 配置网络
  • 如果需要连接无线网络,可以使用nmcli命令:
    nmcli d wifi connect <SSID> password <密码>
    
5. 添加用户
  • 添加一个新用户:
    sudo adduser newuser
    
6. 更新系统
  • 运行以下命令更新系统:
    sudo apt update && sudo apt upgrade
    

Linux 系统解析:从入门到核心概念

一、Linux 系统概述与发展背景

Linux 是一种自由和开放源代码的类 Unix 操作系统,其内核最初由 Linus Torvalds 于 1991 年开发。它的诞生源于对 Unix 系统商业化闭源的反抗,如今已发展为全球应用最广泛的服务器操作系统之一,并在桌面、嵌入式设备、超级计算机等领域占据重要地位。

  • 核心特点

    • 开源免费:用户可自由获取、修改和分发源代码,降低技术门槛和成本。
    • 稳定性强:许多企业级服务器常年不重启仍保持稳定运行。
    • 安全性高:通过权限控制、SELinux 等机制构建多层安全体系。
    • 硬件兼容性广:从老旧设备到最新硬件均可高效支持。
  • 常见发行版

    发行版特点适用场景
    Ubuntu易用性强,社区支持完善桌面用户、开发环境
    CentOS/RHEL企业级稳定,长期维护支持服务器、生产环境
    Debian稳定性和兼容性极佳服务器、嵌入式系统
    Arch Linux滚动更新,高度可定制化技术爱好者、开发者
    Fedora前沿技术集成,开发测试首选开发者、新技术验证
二、Linux 系统的基本架构

Linux 系统采用分层架构设计,各层组件各司其职又紧密协作:

1. 硬件层(Hardware Layer)
  • 包括 CPU、内存、存储设备、网络适配器等物理设备,是系统运行的物理基础。
  • Linux 通过设备驱动程序(Device Drivers)与硬件交互,驱动程序作为硬件和内核之间的接口,负责将硬件操作抽象为标准接口。
2. 内核层(Kernel Layer)
  • Linux 内核是系统的核心,负责管理硬件资源、进程调度、内存分配、文件系统、网络通信等关键功能。
  • 主要模块包括:
    • 进程管理:通过进程调度器(Scheduler)分配 CPU 时间,支持多任务并发执行。
    • 内存管理:管理物理内存和虚拟内存的映射,实现内存分配与回收。
    • 文件系统管理:支持 EXT4、XFS、Btrfs 等多种文件系统,提供统一的文件访问接口。
    • 设备驱动:封装硬件操作,使应用程序无需关心底层硬件差异。
    • 网络协议栈:实现 TCP/IP 等网络协议,处理网络数据收发。
3. 系统调用接口(System Call Interface)
  • 作为内核与用户空间的接口,将内核功能以编程接口的形式暴露给应用程序(如 open()、read()、write() 等函数)。
  • 应用程序通过系统调用请求内核服务,避免直接操作硬件带来的安全风险。
4. 运行时库与工具(Runtime Libraries & Tools)
  • C 标准库(glibc):提供系统调用的封装、常用函数(如字符串处理、数学运算)等。
  • 系统工具:包括 shell(如 bash、zsh)、文件操作工具(ls、cp、mv)、进程管理工具(ps、top)等,是用户与系统交互的桥梁。
5. 应用层(Application Layer)
  • 包含用户开发的各类应用程序,如 Web 服务器(Apache/Nginx)、数据库(MySQL/PostgreSQL)、办公软件(LibreOffice)、开发工具(GCC/VS Code)等。
三、Linux 系统的核心概念与基础操作
1. 文件系统结构
  • Linux 采用树形文件系统,以根目录(/)为起点,所有文件和设备都挂载在这个树形结构中。
  • 关键目录解析:
    • /bin:存放基本命令(如 ls、cd),供所有用户使用。
    • /sbin:存放系统管理命令(如 fdisk、ifconfig),通常仅 root 用户可用。
    • /etc:存放系统配置文件(如网络配置、服务配置)。
    • /home:普通用户的主目录(如 /home/user1)。
    • /var:存放动态数据(如日志文件、邮件、缓存)。
    • /usr:存放用户程序和共享资源(类似 Windows 的 Program Files)。
    • /dev:存放设备文件(如硬盘 /dev/sda、串口 /dev/ttyS0)。
2. Shell 与命令行操作
  • Shell 是用户与 Linux 系统交互的命令解释器,负责解析用户输入的命令并调用系统功能。
  • 常用 Shell 类型:
    • bash(Bourne-Again SHell):大多数 Linux 发行版的默认 Shell。
    • zsh(Z Shell):增强型 Shell,支持自动补全、主题定制等高级功能。
  • 基础命令示例:
    • 文件操作
      ls  # 列出目录内容
      cd /path/to/directory  # 切换目录
      cp source target  # 复制文件
      mkdir new_directory  # 创建目录
      rm file  # 删除文件
      
    • 进程管理
      ps -ef  # 查看所有进程
      top  # 实时监控系统进程和资源使用
      kill 1234  # 终止进程(1234为进程ID)
      
    • 文本处理
      cat file  # 显示文件内容
      grep "keyword" file  # 在文件中搜索关键词
      sed 's/old/new/g' file  # 文本替换
      
3. 用户与权限管理
  • Linux 是多用户系统,通过用户账号和权限控制确保系统安全。
  • 用户类型
    • root:超级用户,拥有系统所有权限。
    • 普通用户:受限于文件和目录的访问权限。
  • 权限机制
    • 每个文件/目录有三种基本权限:读(r)、写(w)、执行(x)。
    • 权限分配给三类用户:所有者(Owner)、所属组(Group)、其他用户(Others)。
  • 常用命令:
    useradd new_user  # 添加新用户
    passwd new_user  # 设置用户密码
    chmod 755 file  # 修改文件权限(7=rwx,5=rx)
    chown user:group file  # 修改文件所有者和所属组
    
4. 软件包管理
  • 不同发行版采用不同的软件包管理工具:
    • Debian/Ubuntu 系:使用 aptapt-get 管理 .deb 包。
      apt update  # 更新软件包列表
      apt install package_name  # 安装软件
      apt remove package_name  # 卸载软件
      
    • RedHat/CentOS 系:使用 yumdnf 管理 .rpm 包。
      yum install package_name  # 安装软件
      yum update package_name  # 更新软件
      
    • 通用工具aptyum 均可通过 --help 查看详细用法,或使用跨平台工具(如 Docker)部署应用。
四、Linux 系统的进阶应用场景
1. 服务器与网络服务
  • Web 服务器:使用 Apache 或 Nginx 搭建网站,配合 PHP/Node.js/Python 处理动态请求。
  • 数据库服务:部署 MySQL/PostgreSQL 作为数据存储后端。
  • 云计算与容器
    • 通过 Docker 容器化应用,实现“一次构建,随处运行”。
    • 使用 Kubernetes 管理容器集群,实现服务自动扩缩容和高可用。
2. 开发与编程环境
  • 编程语言支持:内置 GCC(C/C++编译器)、Python、Java 等开发环境。
  • 版本控制:使用 Git 管理代码版本,配合 GitHub/GitLab 实现团队协作。
  • 自动化部署:通过 Shell 脚本、Ansible 等工具实现代码自动化部署和测试。
3. 嵌入式系统与物联网
  • Linux 因轻量级、可裁剪的特性,广泛应用于路由器、智能家居、工业控制设备等嵌入式场景。
  • 典型案例:树莓派(Raspberry Pi)基于 Linux 系统,支持物联网项目开发。
五、Linux 学习资源与实践建议
1. 入门学习资源
  • 在线教程
  • 书籍推荐
    • 《鸟哥的 Linux 私房菜》:适合零基础入门的经典教材。
    • 《Linux 命令行与shell脚本编程大全》:深入讲解 shell 编程。
2. 实践环境搭建
  • 虚拟机方案:使用 VMware 或 VirtualBox 在 Windows/macOS 上安装 Linux 发行版(如 Ubuntu)。
  • 云服务器:阿里云、腾讯云等平台提供免费或低价的 Linux 服务器(如 CentOS),适合远程实践。
  • 容器环境:通过 Docker 快速部署 Linux 环境,无需修改本地系统。
3. 学习路径建议
  1. 掌握基础命令(文件操作、权限管理、进程查看)。
  2. 学习 shell 脚本基础(变量、条件判断、循环)。
  3. 了解系统管理(用户管理、软件安装、日志分析)。
  4. 实践特定场景(如搭建 Web 服务器、部署 Docker 容器)。
  5. 深入内核原理(可选,适合进阶开发者)。
六、总结

Linux 系统以其开源、稳定、安全的特性,成为技术领域不可或缺的基础设施。从桌面办公到企业级服务器,从嵌入式设备到云计算集群,Linux 的应用场景几乎覆盖所有技术领域。对于初学者,建议从基础命令和系统架构入手,通过实践逐步掌握系统管理和开发技能,最终根据自身需求(如运维、开发、嵌入式)深入特定方向。
Now that you’ve set up your development environment, thanks to Docker Desktop, you can begin to develop containerized applications. In general, the development workflow looks like this:

Create and test individual containers for each component of your application by first creating Docker images.
Assemble your containers and supporting infrastructure into a complete application.
Test, share, and deploy your complete containerized application.

In this stage of the tutorial, let’s focus on step 1 of this workflow: creating the images that your containers will be based on. Remember, a Docker image captures the private filesystem that your containerized processes will run in; you need to create an image that contains just what your application needs to run.

Containerized development environments are easier to set up than traditional development environments, once you learn how to build images as we’ll discuss below. This is because a containerized development environment will isolate all the dependencies your app needs inside your Docker image; there’s no need to install anything other than Docker on your development machine. In this way, you can easily develop applications for different stacks without changing anything on your development machine.

Set up

Let us download an example project from the Docker Samples page.

Git
Windows (without Git)
Mac or Linux (without Git)

Git

If you are using Git, you can clone the example project from GitHub:

git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app

The node-bulletin-board project is a simple bulletin board application, written in Node.js. In this example, let’s imagine you wrote this app, and are now trying to containerize it.
Define a container with Dockerfile

Take a look at the file called Dockerfile in the bulletin board application. Dockerfiles describe how to assemble a private filesystem for a container, and can also contain some metadata describing how to run a container based on this image. The bulletin board app Dockerfile looks like this:

Use the official image as a parent image.

FROM node:current-slim

Set the working directory.

WORKDIR /usr/src/app

Copy the file from your host to your current location.

COPY package.json .

Run the command inside your image filesystem.

RUN npm install

Inform Docker that the container is listening on the specified port at runtime.

EXPOSE 8080

Run the specified command within the container.

CMD [ “npm”, “start” ]

Copy the rest of your app’s source code from your host to your image filesystem.

COPY . .

Writing a Dockerfile is the first step to containerizing an application. You can think of these Dockerfile commands as a step-by-step recipe on how to build up your image. This one takes the following steps:

Start FROM the pre-existing node:current-slim image. This is an official image, built by the node.js vendors and validated by Docker to be a high-quality image containing the Node.js Long Term Support (LTS) interpreter and basic dependencies.
Use WORKDIR to specify that all subsequent actions should be taken from the directory /usr/src/app in your image filesystem (never the host’s filesystem).
COPY the file package.json from your host to the present location (.) in your image (so in this case, to /usr/src/app/package.json)
RUN the command npm install inside your image filesystem (which will read package.json to determine your app’s node dependencies, and install them)
COPY in the rest of your app’s source code from your host to your image filesystem.

You can see that these are much the same steps you might have taken to set up and install your app on your host. However, capturing these as a Dockerfile allows you to do the same thing inside a portable, isolated Docker image.

The steps above built up the filesystem of our image, but there are other lines in your Dockerfile.

The CMD directive is the first example of specifying some metadata in your image that describes how to run a container based on this image. In this case, it’s saying that the containerized process that this image is meant to support is npm start.

The EXPOSE 8080 informs Docker that the container is listening on port 8080 at runtime.

What you see above is a good way to organize a simple Dockerfile; always start with a FROM command, follow it with the steps to build up your private filesystem, and conclude with any metadata specifications. There are many more Dockerfile directives than just the few you see above. For a complete list, see the Dockerfile reference.
Build and test your image

Now that you have some source code and a Dockerfile, it’s time to build your first image, and make sure the containers launched from it work as expected.

Windows users: this example uses Linux containers. Make sure your environment is running Linux containers by right-clicking on the Docker logo in your system tray, and clicking Switch to Linux containers if the option appears. Don’t worry - all the commands in this tutorial work the exact same way for Windows containers.

Make sure you’re in the directory node-bulletin-board/bulletin-board-app in a terminal or PowerShell using the cd command. Let’s build your bulletin board image:

docker build --tag bulletinboard:1.0 .

You’ll see Docker step through each instruction in your Dockerfile, building up your image as it goes. If successful, the build process should end with a message Successfully tagged bulletinboard:1.0.

Windows users: you may receive a message titled ‘SECURITY WARNING’ at this step, noting the read, write, and execute permissions being set for files added to your image. We aren’t handling any sensitive information in this example, so feel free to disregard the warning in this example.

Run your image as a container

Start a container based on your new image:

docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0

There are a couple of common flags here:
    --publish asks Docker to forward traffic incoming on the host’s port 8000, to the container’s port 8080. Containers have their own private set of ports, so if you want to reach one from the network, you have to forward traffic to it in this way. Otherwise, firewall rules will prevent all network traffic from reaching your container, as a default security posture.
    --detach asks Docker to run this container in the background.
    --name specifies a name with which you can refer to your container in subsequent commands, in this case bb.

Also notice, you didn’t specify what process you wanted your container to run. You didn’t have to, as you’ve used the CMD directive when building your Dockerfile; thanks to this, Docker knows to automatically run the process npm start inside the container when it starts up.

Visit your application in a browser at localhost:8000. You should see your bulletin board application up and running. At this step, you would normally do everything you could to ensure your container works the way you expected; now would be the time to run unit tests, for example.

Once you’re satisfied that your bulletin board container works correctly, you can delete it:

docker rm --force bb

The --force option removes the running container. If you stop the container running with docker stop bb you do not need to use --force.

Conclusion

At this point, you’ve successfully built an image, performed a simple containerization of an application, and confirmed that your app runs successfully in its container. The next step will be to share your images on Docker Hub, so they can be easily downloaded and run on any destination machine.

On to Part 3 >>
CLI references

Further documentation for all CLI commands used in this article are available here:

docker image
docker container
Dockerfile reference

containers, images, dockerfiles, node, code, coding, build, push, run
Build and run your image
Estimated reading time: 8 minutes

Orientation and setup
Build and run your image
Share images on Docker Hub

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值