Post

linux(CentOS7) 网络管理

系统启动控制、进程管理

系统启动控制

  • 0—-停机
  • 1—单用户模式
  • 2—多用户模式(没有网络文件系统)
  • 3—完全多用户模式
  • 4—忽略
  • 5—X11 一般图形界面
  • 6— 重新启动(不推荐这操作,请谨慎操作)

如果你远程黑进了系统 可以试试这个 6 操作 应该会一直重启

设置操作

  • runleve 查看当前系统启动状态,N 代表代表上次运行级别
  • init 设置启动状态
1
2
3
[root@Cc RedHad]# runlevel
N 3
[root@Cc RedHad]# init 5

Linux 初始化脚本 /etc/inittab 的解析

Linux 启动服务控制

  • start 开启
  • ` stop` 停止
1
2
3
[root@Cc RedHad]# /etc/init.d/network start
Starting network (via systemctl):                          [  确定  ]
[root@Cc RedHad]# /etc/init.d/network stop
使用chkconfig 查看各个服务运行级别情况
  • 查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@Cc RedHad]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

aegis           0:关    1:关    2:开    3:开    4:开    5:开    6:关
cloudmonitor    0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@Cc RedHad]#
  • 设置
1
2
3
4
5
6
[root@Cc ~]# chkconfig --list network
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@Cc ~]# chkconfig --level 6 network on
[root@Cc ~]# chkconfig --list network
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:on
[root@Cc ~]#
  • 添加
  • chkconfig ---add [服务名]
  • service [服务名] start 开始
  • service [服务名] stop 停止
  • service [服务名] restart 重启

进程管理

概述

  • 分类

    • 交互进程
    • 批处理进程
    • 守护进程
  • 父进程和子进程

    两者管理关系和被管理关系,父进程终止时子进程也随之终止。但是子进程终止父进程不会因子进程终止而终止。

进程管理工具、常用命令

进程监视ps
1
2
3
4
5
6
7
    ps -u		按用户名启动时间顺序监视
    ps -a		显示所用用户进程
    ps -x		显示无控制终端进程
    ps -l		长格式输出
    ps -j		任务格式显示
    ps -r		显示运行中进程
    ps -f		列出全部进程相关信息
系统状态监视top
1
2
3
    top -n 1			显示一次退出
    top -n 1 -p  1		显示某一进程状态且显示一次退出
    top -u testUser		显示指定用户进程信息
终止进程
下载web程序
  • windows 体验
  • Linux 运行:
示例
  • kill [进程 ID]

进程号终止进程

1
2
3
4
5
6
7
[root@Cc ~]# ps -ef  |grep HelloWorld
root     18233 10980  0 21:38 pts/1    00:00:00 ./HelloWorld
root     21817 17944  0 21:40 pts/4    00:00:00 grep --color=auto HelloWorld
[root@Cc ~]# kill -9 18233
[root@Cc ~]# ps -ef  |grep HelloWorld
root     23420 17944  0 21:41 pts/4    00:00:00 grep --color=auto HelloWorld
[root@Cc ~]#
  • killall [进程 ID]

终止一批进程

1
[root@Cc ~]# killall -9 HelloWorld

系统管理员常见操作

更改sshd 默认端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@Cc src]# vim /etc/ssh/sshd_config
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
Port 23456
#ListenAddress 0.0.0.0
#ListenAddress ::

# 建议保留 22
# 防火墙需要放行 设置端口
[root@Cc RedHad]# semanage port -l|grep ssh
ssh_port_t                     tcp      22
[root@Cc RedHad]# semanage port -a -t ssh_port_t -p tcp 23456
[root@Cc RedHad]# semanage port -l|grep ssh
ssh_port_t                     tcp      23456, 22
[root@Cc RedHad]# netstat -ntlp |grep sshd
tcp        0      0 0.0.0.0:23456           0.0.0.0:*    LISTEN      18631/sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*    LISTEN      18631/sshd
[root@Cc RedHad]#

通过另个服务器远程修改过端口的服务器

1
2
3
4
5
[root@Lgr init.d]# ssh -p 23456 root@lichaocheng.top
root@lichaocheng.top's password:
Last login: Sat Mar 28 21:38:24 2020 from 113.118.200.16
Welcome to Alibaba Cloud Elastic Compute Service !
[root@Cc ~]#

查看某个用户所有进程

1
2
3
[root@Cc RedHad]# ps -ef |grep userTest
root     32651 10980  0 23:07 pts/1    00:00:00 grep --color=auto userTest
[root@Cc RedHad]#

终止某个用户所有进程

1
2
3
testUser:x:1004:10002::/home/testUser:/bin/bash
[root@Cc ~]# killall -u testUser
[root@Cc ~]#

根据端口查对应的进程

1
2
3
4
5
6
7
[root@Cc ~]# yum install -y lsof
# 在联网的情况下可以直接使用以上命令
# 如果没有联网使用 rpm 或者源码安装
[root@Cc ~]# lsof -i:8180
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
server  2155 root    6u  IPv6  40398      0t0  TCP *:8180 (LISTEN)
[root@Cc ~]#

通过以上知识可以做进程监控(shell)

可能会用到 rsync 命令 安装即可

1
2
#!/bin/bash
## 进程监控
This post is licensed under CC BY 4.0 by the author.