服务控制和网络管理


服务控制方式

//语法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
    start name.service      //启动服务
    stop name.service       //停止服务
    restart name.service    //重启服务
    status name.service     //查看服务状态
    try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
    reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
    reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
    mask name.service       //禁止设定为开机自启
    unmask name.service     //取消禁止设定为开机自启
    list-dependencies name.service      //查看服务的依赖关系
    is-active name.service      //查看某服务当前激活与否的状态
    is-enabled name.service      //查看服务是否开机自动启动
    enable name.service     //设定某服务开机自动启动
    disable name.service    //禁止服务开机自动启动
    isolate name.target     //切换至某级别,如systemctl isolate graphical.target就是切换至图形界面
    list-unit-files --type service      //查看所有服务的开机自动启动状态(是否开机自启)
    list-units --type service           //查看所有已经激活的服务状态信息
    list-units --type target            //查看所有已装载的级别
    list-units --type service --all     //查看所有服务(已启动/已停止)的状态信息
    list-units --type target --all     //查看所有的级别
    get-default     //查看默认运行级别
    set-default name.target     //设置默认运行级别
    rescue      //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
    emergency   //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
    halt        //关机
    poweroff    //关机
    reboot      //重启
    suspend     //挂起系统,此时不能关机,否则无用
    hibernate   //创建并保存系统快照,下次系统重启时会自动载入快照
    hybrid-sleep    //混合睡眠,快照并挂起

启动服务

[root@YL ~]# systemctl  start firewalld

停止服务

[root@YL ~]# systemctl  stop firewalld

重启服务

[root@YL ~]# systemctl  restart firewalld

查看服务状态

[root@YL ~]# systemctl  status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; ena>
   Active: active (running) since Wed 2022-07-13 23:02:43 CST; 6s>
     Docs: man:firewalld(1)
 Main PID: 52620 (firewalld)

设定某服务开机自动启动

[root@YL ~]# systemctl enable firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.

禁止服务开机自动启动

[root@YL ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

查看某服务当前激活与否的状态

[root@YL ~]# systemctl  is-active firewalld
active

查看服务是否开机自动启动

[root@YL ~]# systemctl  is-enabled firewalld
disabled

查看默认运行级别

[root@YL ~]# systemctl  get-default 
multi-user.target

把httpd服务加入system中

[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/httpd/bin/httpd
ExecStop=/usr/local/httpd/bin/httpd -s stop 
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

让其生效查看是否添加成功

[root@YL system]# systemctl daemon-reload 
[root@YL system]# systemctl  status httpd.service 
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disable>
   Active: inactive (dead)
lines 1-3/3 (END)

将httpd服务设置为开机自启

[root@YL system]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service
[root@YL system]# 
[root@YL system]# systemctl  status httpd.service 
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enable>
   Active: active (running) since Thu 2022-07-14 09:07:18 CST; 7>
  Process: 1774 ExecStart=/usr/local/httpd/bin/httpd (code=exite>
 Main PID: 1775 (httpd)

修改网卡配置文件

[root@YL ~]# cd /etc/sysconfig/network-scripts/
[root@YL network-scripts]# mv ifcfg-ens160 ifcfg-eth0
[root@YL network-scripts]# vim ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes

编辑/etc/default/grub配置文件,在以GRUB_CMDLINE_LINUX开头的行内rhgb的前面加上net.ifnames=0 biosdevname=0

[root@YL network-scripts]# cat /etc/default/grub |tail -3
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

为grub2生成其配置文件

[root@YL network-scripts]# grub2-mkconfig -o /etc/grub2.cfg 
Generating grub configuration file ...
done

ifconfig

网卡配置信息含义如下:

UP: 网卡处于活动状态
BROADCAST: 支持广播
RUNNING: 网线已接入
MULTICAST: 支持组播
MTU: 最大传输单元(字节),即此接口一次所能传输的最大封包
inet: 显示IPv4地址行
inet6: 显示IPv6地址行
link/enther: 指设备硬件(MAC)地址
txqueuelen: 传输缓存区长度大小
RX packets: 接收的数据包
TX packets: 发送的数据包
errors: 总的收包的错误数量
dropped: 由于各种原因, 导致拷贝在内存过程中被丢弃
collisions: 网络信号冲突情况, 值不为0则可能存在网络故障

[root@YL ~]# ifconfig 
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.124.128  netmask 255.255.255.0  broadcast 192.168.124.255
        inet6 fe80::9fb7:6eb1:f172:7121  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:18:57:bd  txqueuelen 1000  (Ethernet)
        RX packets 5203  bytes 418833 (409.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2874  bytes 323698 (316.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<

查看当前处于活动状态的所有网络接口

[root@YL ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.124.128  netmask 255.255.255.0  broadcast 192.168.124.255
        inet6 fe80::20c:29ff:fe18:57bd  prefixlen 64  scopeid 0x20<link>

查看所有网卡状态信息, 包括禁用和启用

[root@YL ~]# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.124.128  netmask 255.255.255.0  broadcast 192.168.124.255
        inet6 fe80::20c:29ff:fe18:57bd  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:18:57:bd  txqueuelen 1000  (Ethernet)
        RX packets 119  bytes 11848 (11.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81  bytes 10487 (10.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<

查看网络接口所有地址

[root@YL ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:18:57:bd brd ff:ff:ff:ff:ff:ff

显示报文统计信息

[root@YL ~]# ip -s link show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        0       0       0       0       
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000

启用或禁用网络接口

[root@YL ~]# ip link set lo up
[root@YL ~]# ip link set eth0 up

添加ip地址

[root@YL ~]# ip addr add 192.168.255.200/24 dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:18:57:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.128/24 brd 192.168.124.255 scope global dynamic noprefixroute eth0
       valid_lft 1631sec preferred_lft 1631sec
    inet 192.168.255.200/24 scope global eth0

删除ip地址

[root@YL ~]# ip addr del 192.168.255.200/24 dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:18:57:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.128/24 brd 192.168.124.255 scope global dynamic noprefixroute eth0
       valid_lft 1545sec preferred_lft 1545sec
    inet6 fe80::20c:29ff:fe18:57bd/64 scope link 
       valid_lft forever preferred_lft forever

查看网络接口的地址

[root@YL ~]# ip addr show eth0 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:18:57:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.128/24 brd 192.168.124.255 scope global dynamic noprefixroute eth0
       valid_lft 1413sec preferred_lft 1413sec
    inet6 fe80::20c:29ff:fe18:57bd/64 scope link 
       valid_lft forever preferred_lft forever

查看当前路由表

[root@YL ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 eth0
192.168.124.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0

以数字方式显示各主机或端口等相关信息

[root@YL ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.124.2   0.0.0.0         UG    100    0        0 eth0
192.168.124.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0

查看主机名

[root@YL ~]# hostname
YL

查看主机信息

[root@YL ~]# hostnamectl 
   Static hostname: YL
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a5498e9a6f114267ab30dfad1e261e55
           Boot ID: e9f2d7f03c9d46daa78062eb6bee2f33
    Virtualization: vmware
  Operating System: CentOS Stream 8
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-257.el8.x86_64
      Architecture: x86-64
posted @ 2022-07-14 10:17  Tqing  阅读(128)  评论(0编辑  收藏  举报