Linux(一)Linux简介、目录结构、网络配置与系统服务

1 Linux简介

Linux基于Unix,是多用户分时系统

Ctrl + Alt + F2、F3。。。F6打开多个Linux Shell终端控制器;F1为图形化界面,终端为仿真器

2 Linux文件与目录结构:一切皆文件

2.1 Linux目录结构

1 bin目录

binary的缩写,二进制目录,包含可以直接执行的机器码(常用命令 cd ls)

/下的bin并不是真正的目录而是一个链接,链接地址为/usr/bin

/usr下面还有一个/sbin,存放系统管理员可以执行的命令

2 lib目录

存放系统和应用程序的库文件,该目录同上链接到/usr/lib

lib64存放64位系统和应用程序的库文件

3 usr目录

存放用户级别的数据和文件,相当于Windows的Program Files

4 dev目录

device的缩写,用于存放设备的管理信息

5 etc目录

存放系统管理需要的配置文件和对应的一些子目录,如安装的数据库的配置文件

6 home目录

存放普通用户的文件目录

7 root目录

系统超级管理员root的用户主目录

8 opt目录

option可选目录,第三方软件的安装目录

9 media目录

识别一些可移动媒体设备,如U盘、光驱,识别后系统将其挂在到该目录,作为挂载点

10 mnt目录

mount挂载目录,作为挂载点

11 proc目录

proccess进程目录,存放当前系统的硬件和一些进程的信息

12 run目录

运行目录,存放系统运行以来的系统信息,重启后小时

13 serv目录

存放系统服务相关信息

14 sys目录

存放系统硬件信息相关文件

15 tmp目录

临时目录

16 var目录

可变目录,存放经常被修改、变化的文件,如日志文件

2.2 文件系统

文件系统

CenterOs6使用的是ext4.7(第四代扩展文件系统)使用的是xfs(64位高性能日志文件系统),擅长处理大文件。

挂载点

Linux的文件目录并不意味着下面的所有目录文件都在根分区的磁盘中。比如:在划分分区时,先划分了一个引导分区,设置好文件格式后直接挂载到/boot文件目录的挂载点。

2.3 Vi/Vim文本编辑器

vim三种模式

一般模式:删除、复制、粘贴

按u可以回退写入的修改

R可进入替换模式

编辑模式:编辑文本

进入:i、a、o

退出:esc

a是在i所在光标往后移动一格

I光标跳转到当前行头

A光标跳转到当前行尾

o光标跳转到当前行下一行

O光标跳转到当前行上一行

命令模式

进入: :或者/

退出:esc

3 网络配置

windows查看IP信息:ipconfig 192.168.99.147

   IPv4 地址 . . . . . . . . . . . . : 192.168.99.147

linux查看IP信息:ifconfig 192.168.60.

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.60.3  netmask 255.255.255.0  broadcast 192.168.60.255
        inet6 fe80::c983:e95b:df60:5e6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:21:a6:d3  txqueuelen 1000  (Ethernet)
        RX packets 11498  bytes 14263437 (13.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6365  bytes 569359 (556.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

同时在windows中可以看到当前的网络连接情况,多出了两个vmware虚拟网卡,Vmware是怎么搭建好虚拟网络的呢?Vmware提供了三种网络连接方式:

3.1 网络连接方式

① 桥接模式

我们的路由器组建局域网的时候,一般是通过DHCP服务器动态地给局域网里面的设备分配ip,可以在电脑的以太网地址中查看到电脑的局域网ip,如我的电脑的局域网ip就是192.168.99.147。而桥接模式则是虚拟机直接连接外部物理网络的模块,主机起到了网桥的作用。这种模式下,路由器会为虚拟机动态分配ip,和局域网中的其他设备是同级别的,因此对外部网络可见,安全性不好。

② NAT模式

NAT即Network Address Transaction网络地址转换,虚拟机和主机构建一个专用网络,并通过虚拟网络转换设备对IP进行转换,虚拟机通过共享主机IP可以访问外部网络,但外部网络无法访问虚拟机。

③ 仅主机模式

虚拟机仅与主机共享一个专用网络,与外部网络无法通信。

3.2 修改静态IP

直接使用上面的NAT模式,虚拟机和主机处于同一个专用网络,相当于此时的VMnet8(虚拟机提供的NAT模式的虚拟网卡)充当了一个路由器,为主机和虚拟机各分配了一个ip地址,但是现在的问题是虚拟机的地址默认是动态(DHCP)的,需要去进行手动修改。

①查看网卡信息 ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.60.3  netmask 255.255.255.0  broadcast 192.168.60.255
        inet6 fe80::c983:e95b:df60:5e6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:21:a6:d3  txqueuelen 1000  (Ethernet)
        RX packets 202  bytes 29005 (28.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 185  bytes 18724 (18.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

②在/etc/sysconfig/network-script目录下找到配置文件

[hikaru@bogon network-scripts]$ pwd
/etc/sysconfig/network-scripts
[hikaru@bogon network-scripts]$ ll
total 252
-rw-r--r--. 1 root root   409 Aug 11 21:22 ifcfg-ens33

③修改配置信息

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="7d88feef-895e-488f-85f6-ece3f5193a72"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.60.3  
NETMASK=255.255.225.0  
GATEWAY=192.168.60.2
DNS1=8.8.8.8
DNS2=61.147.37.1

主要修改:

BOOTPROTO IP分配方式为静态

IPADDR 分配的静态IP

GATEWAY 网关

DNS域名解析服务器

④重启网络服务

service network restart

3.3 配置主机名

①在/etc/hostname配置文件中进行修改

②通过命令hostnamectl查看当前主机信息,然后通过命令hostnamectl set-hostname 进行修改:

[hikaru@bogon network-scripts]$ hostnamectl
   Static hostname: localhost.localdomain
Transient hostname: bogon
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 037188e3d0f94e34995da5590a508e4d
           Boot ID: 76ae6c6960214006864569f9fcbd53e3
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64
[hikaru@bogon network-scripts]$ hostnamectl set-hostname hadoop100
[hikaru@bogon network-scripts]$ hostnamectl
   Static hostname: hadoop100
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 037188e3d0f94e34995da5590a508e4d
           Boot ID: 76ae6c6960214006864569f9fcbd53e3
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

3.4 修改hosts映射文件

配置主机名是为了实现通过主机名代替ip进行访问,可以在hosts配置文件中进行修改。linux的hosts文件在目录/etc/hosts中,windows则在C:\Windows\System32\drivers\etc下。

192.168.60.100 hadoop100
192.168.60.101 hadoop101
192.168.60.102 hadoop102
192.168.60.103 hadoop103
192.168.60.104 hadoop104
192.168.60.105 hadoop105

在两边配置完成之后,linux各个主机以及windos和虚拟机之间就可以实现通过主机名访问了。

正在 Ping hadoop100 [192.168.60.100] 具有 32 字节的数据:
来自 192.168.60.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.60.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.60.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.60.100 的回复: 字节=32 时间<1ms TTL=64

192.168.60.100 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms
请按任意键继续. . .

3.5 远程访问

Xshell

XFTP WinSCP

4 linux系统服务

系统服务:启动之后一直存在,常驻内存的进程。

daemon 守护进程:以d开头的进程即为守护进程,负责启动、守护系统服务。

4.1 CenterOS6基础语法

service 服务名 start | stop | restart | status

linux守护进程都在目录/etc/init.d中,d代表守护进程

[root@hadoop100 ~]# ls /etc/init.d
functions  netconsole  network  README

4.2 systemctl CenterOS7

systemctl restart | stop | start | starus 服务名

linux守护进程都在目录/usr/bin/systemd中,d代表守护进程,下面有大量的service服务和target,表示一组服务集合

4.3 服务开机自启动

使用setup命令进入《图像化界面》设置需要开机启动的服务,空格选定Tab移动光标。从界面可以看出分为两种服务,一种是SysV initScripts,表示五版本的启动脚本,另一种是systemd service七班本的守护进程。

4.4 系统运行级别

系统的运行级别总共有七种。(其中2即为没有网络的3)

linux启动的时候,会首先挂载/boot,然后启动init进程,进程根据运行级别运行对应的服务。在centeros 6中,系统默认的运行级别需要在配置文件/etc/initb中查看。可以看到里面是没有任何东西的,而且提示systemd使用target代替了运行级别,直接列举了常用的3和5级别。

[root@hadoop100 system]# cat /etc/inittab 
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#

CentOS7的运行级别简化为:

  • multi-user.target: analogous to runlevel 3

  • graphical.target: analogous to runlevel 5

在CentOS 7下查看当前运行级别:

[root@hadoop100 system]# systemctl get-default
graphical.target

修改当前级别:

[root@hadoop100 system]# systemctl set-default multi-user.target
[root@hadoop100 system]# systemctl get-default
multi-user.target

快捷键即为之前的ctrl + alt + F2

4.5 服务启停的配置

在center os6里面,通过

在Center Os7里,通过system enable|disable 服务名设置服务的开机自启动,通过systemctl list-unit-files可以列举管理的所有启动单元。其中static表示当前服务依赖其他服务,暂时不能判定是否自启动。

[root@hadoop100 system]# systemctl list-unit-files
UNIT FILE                                     STATE   
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static  
dev-mqueue.mount                              static  
proc-fs-nfsd.mount                            static  
proc-sys-fs-binfmt_misc.mount                 static  
run-vmblock\x2dfuse.mount                     disabled
sys-fs-fuse-connections.mount                 static  
sys-kernel-config.mount                       static  
sys-kernel-debug.mount                        static  
tmp.mount                                     disabled
var-lib-nfs-rpc_pipefs.mount                  static  
brandbot.path                                 disabled
cups.path                                     enabled 

4.6 关闭系统防火墙

systemctl firewalld status

[root@hadoop100 system]# systemctl firewalld status
Unknown operation 'firewalld'.
[root@hadoop100 system]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-08-27 17:42:00 PDT; 11h ago
     Docs: man:firewalld(1)
 Main PID: 905 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─905 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

systemctl stop firewalld

[root@hadoop100 system]# systemctl stop firewalld
[root@hadoop100 system]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2022-08-28 05:41:00 PDT; 1s ago
     Docs: man:firewalld(1)
  Process: 905 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 905 (code=exited, status=0/SUCCESS)

systemctl disable firewalld

[root@hadoop100 system]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hadoop100 system]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2022-08-28 05:41:00 PDT; 17s ago
     Docs: man:firewalld(1)
 Main PID: 905 (code=exited, status=0/SUCCESS)

4.7 关机重启指令

linux为了提高硬盘读写效率,减少磁盘IO,采用了预读延迟写的操作,即使用一个缓冲池,只有当缓冲池满的时候才将内容写入磁盘,但是这样也存在着问题,即在关机的时候会导致数据的丢失,因此默认情况下linux需要一分钟的文件写入磁盘的同步操作:

sync

而使用shutdownshutdown -c可以分别进行关机和取消关机操作。

基本语法:

  • sync:将数据由内存同步至磁盘

  • halt:停机不断电

  • poweroff:断电关机

  • reboot:重启 相当于shutdown -r now

  • shutdown

posted @ 2022-12-02 12:31  Tod4  阅读(93)  评论(0编辑  收藏  举报