Linux指令详解之:ctl相关命令大礼包

6.4 服务管理命令(ctl大礼包)

CTL大礼包:

hostnamectl :修改和查看主机名

systemctl :服务管理

timedatectl :查看和修改时间(时区)

localctl :查看和修改语言,字符集

6.4.1 systemctl
  • 一般来说rpm、deb、yum、apt安装的服务,这些服务默认通过systemctl管理
  • 二进制安装,编译安装默认无法通过systemctl管理服务
  • 服务关闭重启,查看状态
  • 服务开机自启设置

案例258. 查看crond服务状态(是否运行,是否开机自启动)

[root@kylin-ren-class ren]# systemctl start crond
[root@kylin-ren-class ren]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2025-01-14 21:30:32 CST; 3s ago

案例259. 查看是否开机自启动

[root@kylin-ren-class ren]# systemctl is-enabled sshd
enabled

案例260. 开启crond服务,设置crond服务开机自启动

[root@kylin-ren-class ren]# systemctl restart crond
[root@kylin-ren-class ren]# systemctl enable crond

案例261. 关闭防火墙firewalld(麒麟,Centos系统)

#麒麟系统
1. systemctl stop firewalld.service

2. systemctl disable firewalld.service

3. systemctl status firewalld.service

4. systemctl is-active firewalld.service

5. systemctl is-enabled firewalld.service

案例262. 关闭防火墙ufw(Ubuntu系统)

#ubuntu
1. systemctl stop ufw

2. systemctl disable ufw
6.5.2 systemctl小结
# 立即开启
systemctl enable --now xxx

# 立即关闭
systemctl disable --now xxx

# 普通开启 + 开机自启动
systemctl start xxx
systemctl enable xxx

# 普通关闭 + 关闭开机自启动
systemctl stop xxx
systemctl disable xxx

# 查看状态
systemctl status xxx

# 重启服务
systemctl restart xxx

# 精简查看是否开启
systemctl is-active xxx

# 精简查看是否开机自启
systemctl is-enabled xxx

# 查看系统所有服务
systemctl list-unit-files
systemctl list-units
6.5.3 timedatectl

查看时间(时区),修改时间(时区)

案例263. 查看时间与时区

[root@kylin-ren-class service_scripts]# timedatectl
               Local time: Wed 2025-01-15 16:16:52 CST #当地时间
           Universal time: Wed 2025-01-15 08:16:52 UTC #世界时间
                 RTC time: Wed 2025-01-15 08:16:52 #硬件时间
                Time zone: Asia/Shanghai (CST, +0800) #时区
System clock synchronized: yes #系统时钟同步
              NTP service: active #NTP服务
          RTC in local TZ: no #表示硬件时钟设置为协调世界时

案例264. 修改时区为上海

timedatectl set-timezone Asia/Shanghai

案例265. 系统时钟同步

ntpdate ntp1.aliyun.com

# ntp2.aliyun.com、ntp3.aliyun.com、ntp4.aliyun.com、ntp5.aliyun.com

案例266. 查看当前时间

[root@kylin-ren-class service_scripts]# date
Wed Jan 15 16:27:54 CST 2025
6.5.4 localectl

查看系统的语言和字符集

案例267. 查看系统语言字符集

[root@kylin-ren-class service_scripts]# localectl
   System Locale: LANG=zh_CN.UTF-8
       VC Keymap: cn  # 键盘映射
      X11 Layout: cn  # X11布局

案例268. 修改字符集

localectl set-locale LANG-zh_CN.UTF-8

案例269. 脚本中临时修改语言为英文

# 书写脚本的时候,同意改为英文,方便进行过滤与操作
export LANG=en_US.UTF-8
6.5.5 networkctl

Ubuntu修改网卡配置

案例270. 列出所有连接及其状态

ubuntu01-class@ubuntu-01:~$ networkctl
IDX LINK  TYPE     OPERATIONAL SETUP     
  1 lo    loopback carrier     unmanaged
  2 ens33 ether    routable    configured
  3 ens37 ether    routable    configured

3 links listed.

案例271. 显示连接的状态信息

ubuntu01-class@ubuntu-01:~$ networkctl status
●        State: routable                        
  Online state: online                          
       Address: 10.0.0.201 on ens33
                10.0.0.220 on ens37
                fe80::20c:29ff:febf:cd0 on ens33
                fe80::20c:29ff:febf:cda on ens37
       Gateway: 10.0.0.2 on ens33
           DNS: 223.5.5.5
                223.6.6.6

案例272. 显示网络接口 eth0 的各种信息

ubuntu01-class@ubuntu-01:~$ networkctl status ens33
● 2: ens33                                                                                 >                     Link File: /usr/lib/systemd/network/99-default.link
                  Network File: /run/systemd/network/10-netplan-ens33.network
                          Type: ether
                         State: routable (configured)
                  Online state: online                                                     >             Alternative Names: enp2s1
                          Path: pci-0000:02:01.0
                        Driver: e1000
                        Vendor: Intel Corporation
                         Model: 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT S>                    HW Address: 00:0c:29:bf:0c:d0 (VMware, Inc.)
                           MTU: 1500 (min: 46, max: 16110)
                         QDisc: fq_codel
  IPv6 Address Generation Mode: eui64
          Queue Length (Tx/Rx): 1/1
              Auto negotiation: yes
                         Speed: 1Gbps
                        Duplex: full
                          Port: tp
                       Address: 10.0.0.201
                                fe80::20c:29ff:febf:cd0
                       Gateway: 10.0.0.2
                           DNS: 223.5.5.5
                                223.6.6.6
             Activation Policy: up
           Required For Online: yes
             DHCP6 Client DUID: DUID-EN/Vendor:0000ab11b34b3e4488a01c6f0000
                  Connected To: n/a on port 00:50:56:c0:00:08
posted @ 2025-03-02 17:39  小时候老白啦  阅读(90)  评论(0)    收藏  举报