【笔记】systemctl&rc.local&fstab&profile&hosts&inittab&motd&issue issue.net

systemctl

是在Linux系统中控制服务的命令
语法格式:
systemctl 动作 服务

案例:重启网络服务

[root@ahui ~]# systemctl restart network

动作:

 start   启动
 stop    停止
 restart 重启
 status  状态
 reload  重新加载
 enable  开机自动运行
 disable 开机禁止运行

网络服务: 在系统中有两个网络服务
第一个网络服务: network
第二个网络服务:NetworkManager 这种不使用 (默认使用)

/etc/rc.local

文件作用: 开机自动执行命令(可以执行的命令) 实际文件就是shell脚本
shell脚本: 命令的堆积 将可执行命令写入文本中 从上往下按照顺序依次执行 称为shell脚本
注: 开机系统会自定加载脚本文件 其中包括rc.local ./rc.local方式来运行

文件中->软链接 类似windows的快捷方式

[root@ahui ~]# 
[root@ahui ~]# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 Apr 27 11:16 /etc/rc.local -> rc.d/rc.local

源文件

[root@ahui ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Apr  1  2020 /etc/rc.d/rc.local

案例: 开机自动创建/data目录

第一步: 将可执行命令写入/etc/rc.local

[root@ahui ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
mkdir /data

第二步: 给源文件一个执行权限 可读 可写 可执行(针对文件中是命令的文件)
可读 r read
可写 w write
可执行: x excute

[root@ahui ~]# ll test.txt 
-rw-r--r--. 1 root root 5 May  6 09:42 test.txt
[root@ahui ~]# chmod +x test.txt 
[root@ahui ~]# ll test.txt 
-rwxr-xr-x. 1 root root 5 May  6 09:42 test.txt
[root@ahui ~]# cat test.txt
touch /tmp/test.rc
[root@ahui ~]# ./test.txt 
[root@ahui ~]# ll /tmp/test.rc 
-rw-r--r--. 1 root root 0 May  6 09:46 /tmp/test.rc
#授权rc.local 可执行
[root@ahui ~]# ll /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 485 May  6 09:41 /etc/rc.d/rc.local
[root@ahui ~]# chmod +x /etc/rc.d/rc.local
[root@ahui ~]# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 485 May  6 09:41 /etc/rc.d/rc.local

第三步: 重启测试是否执行

[root@ahui ~]# reboot
[root@ahui ~]# ll /data/
total 0

案例2: 开机自动运行服务

ssh服务对应22端口: 使用xshell连接系统服务器
第一个步骤: 关闭ssh服务自动运行

[root@ahui ~]# systemctl disable sshd	#关闭ssh服务自动运行
[root@ahui ~]# systemctl status sshd	#查看ssh
● sshd.service - OpenSSH server daemon					表示开机禁止运行
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)

第二个步骤: 重启测试是否能连接系统
无法连接

第三个步骤: 把启动命令写入到rc.local

[root@ahui ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
systemctl start sshd

第四个步骤: 重启测试

[root@ahui ~]# reboot

[root@ahui ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-05-06 10:12:16 CST; 15s ago

最后恢复环境:

使用systemctl开机自动运行sshd
[root@ahui ~]# systemctl enable sshd

删除rc.local中的命令

/etc/fstab

文件作用: 开机自动挂载硬件到目录上
所有的硬件都需要一个入口来访问
/boot 200m sda1 第一块磁盘的第一个分区
swap 2000m sda2 第二块磁盘的第二个分区
/ 17G sda3 第三块磁盘的第三个分区
/etc —> sda3

[root@ahui ~]# ll /dev/cdrom 			# cdrom是 sr0的软链接 类似windows快捷方式
lrwxrwxrwx. 1 root root 3 May  6 10:34 /dev/cdrom -> sr0
[root@ahui ~]# ll /dev/sr0 
brw-rw----. 1 root cdrom 11, 0 May  6 10:34 /dev/sr0

如果没有门(没有挂载点) 设备相当于是一个没有窗户没有门的监狱 进不去出不来

cat /etc/fstab
UUID=177579fb-d8e6-42a2-8428-04c22a1b2266    /        xfs     defaults       0  0
   ----------------------------------  	   ---	 	 -----    --------     ---  ---
					01						02		  03		 04			05  06

01: 设备唯一标识 可以使用blkid查看硬件的唯一标识
第一列可以设置为UUID也可以配置成设备名称(/dev/sda3)

[root@ahui ~]# blkid
		/dev/sda1: UUID="98739bdb-87d9-4a1a-9460-a87105f807dd" TYPE="xfs" 
		/dev/sda2: UUID="dbd2c8ad-d1c4-4c06-8e27-ae5c8fd9ac2f" TYPE="swap" 
		/dev/sda3: UUID="177579fb-d8e6-42a2-8428-04c22a1b2266" TYPE="xfs" 
		/dev/sr0: UUID="2020-04-22-00-54-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 

02: 挂载点(入口 大门) / swap /boot /data /mnt
通过目录访问对应的硬件

03: 挂载系统文件类型 centos7.x 默认是xfs

04: 挂载的参数(参数决定了是否能对硬件有读写的权限)

05: 开机是否备份磁盘 0不备份 1为备份

06: 开机是否自检 0 不检查 1检查 fsck命令检查

案例: 开机自动挂载cdrom

第一步: 在命令行测试挂载光驱

[root@ahui ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@ahui ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.5M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda3        18G  1.9G   16G  11% /
/dev/sda1       197M  110M   88M  56% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sr0        4.5G  4.5G     0 100% /mnt

第二步: 按照固定格式写入fstab

[root@ahui ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Apr 27 11:13:59 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=5932f071-1639-4751-81ef-0438f7a2085d /                       xfs     defaults        0 0
UUID=78b419fb-beee-40be-b8ce-4a90316b8fad /boot                   xfs     defaults        0 0
UUID=31870095-4001-49a9-bb06-386f25df8c99 swap                    swap    defaults        0 0
/dev/cdrom              /mnt                    iso9660   defaults        0 0
#(与上一行起相同作用)UUID=2020-04-22-00-54-00-00               /mnt                    iso9660   defaults        0 0

第三步: 重启系统使用df -h 查看磁盘挂载情况

[root@ahui ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.5M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda3        18G  1.9G   16G  11% /
/dev/sr0        4.5G  4.5G     0 100% /mnt

/etc/profile

文件作用: 存放系统环境变量的文件 在开机的时候 在使用xshell重新连接系统的时候 都会自动加载/etc/profile

什么是变量:

1.x=1 y=x+1
x为变量 y为变量名称
用一个固定的值表示不固定的值
2.目录 类似变量
目录–>对应的内容

变量分类:

全局变量 针对所有的生效 类似国家的法律
局部变量 针对一部分生效 类似家规

系统默认的变量让系统更好的运行

变量的定义:

第一步: name是变量的名称(下划线 数字 字母组合 不能用数字开头 等号两端不能有空格)

[root@ahui ~]# name=ahui
第二步: 获取变量的值使用 $
[root@ahui ~]# echo $name
ahui

案例: 变量的重复调用

[root@ahui ~]# eth0=/etc/sysconfig/network-scripts/ifcfg-eth0
[root@ahui ~]# echo $eth0
/etc/sysconfig/network-scripts/ifcfg-eth0
[root@ahui ~]# vim $eth0
[root@ahui ~]# echo $eth0
/etc/sysconfig/network-scripts/ifcfg-eth0
[root@ahui ~]# ll $eth0
-rw-r--r--. 1 root root 107 May  5 11:41 /etc/sysconfig/network-scripts/ifcfg-eth0
[root@ahui ~]# cat $eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.200
PREFIX=24
GATEWAY=10.0.0.2
[root@ahui ~]# vim $eth0

让变量文件重新加载使用source或者 . 点后面要有空格

[root@ahui ~]# source /etc/profile
[root@ahui ~]# echo $age
100
[root@ahui ~]# . /etc/profile

系统中的变量:

[root@ahui ~]# echo $PS1
[root@ahui ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

命令执行的过程:

第一步: 输入命令
第二步: 系统会查找PATH变量中的路径 在路径下找你输入的命令是否存在
第三步: 存在则执行 不存在则提示 command not found

[root@ahui ~]# cat test.txt 
touch /tmp/test.rc
[root@ahui ~]# catttt test.txt 
-bash: catttt: command not found

查看命令所在的路径使用: which

[root@ahui ~]# which cat
/usr/bin/cat

案例: 将cat移走 无法使用cat命令

[root@ahui ~]# which cat
/usr/bin/cat
[root@ahui ~]# mv /usr/bin/cat /data/
[root@ahui ~]# ll /data/
total 56
-rwxr-xr-x. 1 root root 54080 Aug 20  2019 cat
[root@ahui ~]# cat test.txt
-bash: /usr/bin/cat: No such file or directory
[root@ahui ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

解决方法:
1)将cat移动到/usr/bin
2)将/data目录加到PATH路径中

[root@ahui ~]# PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data"
[root@ahui ~]# cat test.txt
touch /tmp/test.rc

注意: 安装软件下载软件包 部分软件下载后命令不在默认的路径下 使用PATH解决
/usr/local/mvn/config
/usr/local/mvn/package
/usr/local/mvn/bin/

永久生效: 将变量写入/etc/profile

[root@ahui ~]#vim /etc/profile

在文件末尾加上
PATH=“/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data”

/etc/hosts

文件作用: 本地的域名解析 二阶段使用
访问网站流程: windows
浏览器输入www.ahui.com
首先会查找hosts文件中是否有域名对应关系
如果有对应关系 直接将IP地址返回给浏览器 建立连接
如果没有对应关系 去查找网卡配置的DNS进行解析拿到IP地址返回给浏览器

windows hosts文件的位置:
C:\Windows\System32\drivers\etc

/etc/inittab

文件作用: 开机自动运行的级别 centos6.x 有效 centos7.x不使用次配置文件
运行级别表示不同的功能 类似 windows 正常模式 安全模式
七个运行级别:
0: 表示关机
a.不能将运行级别设置为0 否则无法开机
1: 表示单用户模式
a.类似安全模式 可以修改密码
b.进入安全模式不需要root密码
在开机界面 进入安全模式
2: 多用户模式
a.不支持NFS 网络文件系统
b.不使用这个级别
3: 完全多用户模式
a.默认使用的级别
4: 待开发 未使用
5: 桌面模式
a.默认没有安装桌面图形 需要安装desktop
b.在企业中不使用桌面模式
6: 表示重启

查看当前模式的级别:

[root@ahui ~]# runlevel 
N 3

配置运行级别: 企业用不到
centos6.x /etc/fstab 文件中
id:3:initdefault 修改默认运行级别
centos7.x

[root@ahui ~]# systemctl set-default runlevel3.target 	#直接使用命令更改运行级别为3

扩展: 安装自动补全命令包 然后执行上面的set-default 按tab键 可以看到所有的运行级别

[root@ahui ~]# yum -y install bash-completion.noarch

/etc/motd

文件作用: 开机自动显示motd中的内容

[root@ahui ~]# cat /etc/motd
Hehe
[root@ahui ~]# reboot
Last login: Thu May  5 20:29:48 2022 from 10.0.0.1
hehe

/etc/issue issue.net

文件作用:本地登录显示的标语和远程登录显示的标语
需要在/etc/ssh/sshd_config文件中修改Banner引导文件才可显示

[root@oldboyedu ~]# cat /etc/issue
FF!!!!!!
U FUCKING PUSSY!!!!!!
#重启Xshell重新连接
Xshell 5 (Build 0991)
Copyright (c) 2002-2016 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

Connecting to 10.0.0.201:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

FF!!!!!!
U FUCKING PUSSY!!!!!!

Last login: Thu May  5 20:29:48 2022 from 10.0.0.1
hehe
[root@ahui ~]#

优化系统,为在被攻击时不显示内核版本等信息

[root@ahui ~]# > /etc/issue 
[root@ahui ~]# > /etc/issue.net 	#> 表示清空文件

小结:

1./etc/rc.local
开机自动运行命令
给源文件加x执行权限
2./etc/fstab 开机自动挂载
/dev/sda3 / xfs defaults 0 0
3./etc/hosts 本地域名解析
做host劫持 #www.baidu.com --> 10.0.0.200
windows常用
改完后用ping测试
4./etc/profile 环境变量文件
PATH 命令的执行过程
如果命令没有在PATH如何定义
临时定义: 重启失效 重新连接xshell失效
命令行: name=ahui PATH=xxxxx
永久定义: 写入/etc/profile
vim /etc/profile
name=ahui
PATH=xxxxx

让profile直接生效使用 source 或者.空格 source /etc/profile
重新使用xshell连接系统 加载/etc/profile

5./etc/inittab
七个运行级别:

  1. (0)关机
  2. (1)单用户
  3. (2)多用户 不支持NFS
  4. (3)完全多用户 默认
  5. (4)待开发 未使用
  6. (5)桌面 图形化
  7. (6)重启

6./etc/motd登陆后显示的标语
7.issue 和issue .net作为优化项清空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值