Linux复习笔记:04.软件和服务
大约 3 分钟
各种快捷键
强制停止
- 强制停止程序运行/重新输入命令
ctrl+c
退出登录
- 退出linux用户登录/退出python解释器
ctrl+d
bash历史
- 查看历史命令
history
- 命令前缀匹配执行
!xxx
- 输入内容搜素历史命令
ctrl+r
- 回车键直接执行
- 左右键则获取命令,编辑后执行。
光标移动
ctrl+a
跳转到命令开头ctrl+e
跳转到命令结尾ctrl+left
向左跳转一个单词ctrl+right
向右跳转一个单词
清屏
ctrl+l
快捷键clear
命令
软件安装
yum命令
yum 是RPM(readhat package manager)CentOS-红帽的包管理工具
- 安装包格式
*.rpm
语法
yum [-y] [install remove search] package-name
选项说明
-y
自动确认
示例
yum install wget -y
yum remove wget -y
yum search wget
apt和apt-get命令
- 许多其他的Debian-Liunx发行版本也在跟随Ubuntu的步伐,开始鼓励用户使用apt代替apt-get。
- 安装包格式
*.deb
apt命令取代了apt-get和apt-cache的一些命令
apt命令 | 被取代的命令 | 功能 |
---|---|---|
apt install | apt-get install | 安装一个软件包 |
apt remove | apt-get remove | 移除一个软件包 |
apt purge | apt-get purge | 移除包及相关配置 |
apt update | apt-get update | 刷新仓库索引 |
apt upgrade | apt-get upgrade | 升级所有可升级的软件包 |
apt autoremove | apt-get autoremove | 移除多余的软件包 |
apt full-upgrade | apt-get dist-upgrade | 升级软件包,并自动处理依赖 |
apt search | apt-cache search | 搜索某个程序 |
apt show | apt-cache show | 显示软件包详情 |
apt独有的命令
apt的新命令 | 命令对应的功能 |
---|---|
apt list | 列举各类软件包 (已安装,可升级,等等) |
apt edit-sources | 编辑软件源列表(sources list) |
语法格式
apt [-y] [install remove search] package-name
apt install wget -y
apt remove wget -y
apt search wget
systemctl系统服务管理
语法
systemctl [start|stop|status|reload|restart|enable|disable] service-name[.service]
- start 启动
- stop 停止
- status 状态
- reload 重新加载
- restart 重新启动
- enable 开启开机自启动
- disable 关闭开机自启动
系统内置服务
- networkManager 主网络服务
- network 副网络服务
- firewalld 防火墙服务
- sshd ssh服务
service文件编写
# 编辑开机启动服务
vim /etc/systemd/system/myservice.service
# 输入以下代码
[Unit]
Description=My Custom Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/main.sh
# User=exampleuser
# Restart=on-failure
# RuntimeDirectory=systemd/netif
[Install]
WantedBy=multi-user.target
# 启动和查看服务
sudo systemctl start myservice.service
案例1
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat /etc/systemd/system/v2ray.service
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray run -config /usr/local/etc/v2ray/config.json
Restart=on-failure
RestartPreventExitStatus=23
[Install]
WantedBy=multi-user.target
案例2
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat /usr/lib/systemd/system/systemd-networkd.service
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Network Service
Documentation=man:systemd-networkd.service(8)
ConditionCapability=CAP_NET_ADMIN
DefaultDependencies=no
# systemd-udevd.service can be dropped once tuntap is moved to netlink
After=systemd-udevd.service network-pre.target systemd-sysusers.service systemd-sysctl.service
Before=network.target multi-user.target shutdown.target
Conflicts=shutdown.target
Wants=network.target
[Service]
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW
DeviceAllow=char-* rw
ExecStart=!!/lib/systemd/systemd-networkd
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
ProtectControlGroups=yes
ProtectHome=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectSystem=strict
Restart=on-failure
RestartSec=0
RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6 AF_PACKET AF_ALG
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
RuntimeDirectory=systemd/netif
RuntimeDirectoryPreserve=yes
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service
Type=notify
RestartKillSignal=SIGUSR2
User=systemd-network
WatchdogSec=3min
[Install]
WantedBy=multi-user.target
Also=systemd-networkd.socket
Alias=dbus-org.freedesktop.network1.service
# We want to enable systemd-networkd-wait-online.service whenever this service
# is enabled. systemd-networkd-wait-online.service has
# WantedBy=network-online.target, so enabling it only has an effect if
# network-online.target itself is enabled or pulled in by some other unit.
Also=systemd-networkd-wait-online.service