Some Setting of Linux

Linux is one of the greatest open source projects so far. It is stable, safe and efficient.

Posted by Dusign on 2019-09-29
Words 4.5k and Reading Time 19 Minutes
Viewed Times

Linux is a set of free-to-use and free-to-spread Unix-like operationg system. It is a multi-user, multi-task, multi-threaded and multi-CPU operationg system based on POSIX and Unix. It can run major Unix tools, applications and network protocols. It supports 32-bit and 64-bit hardware. Linux inherits Unix’s network-centric design idea and is a stable multi-user network operationg system. Next, I’ll introduce some knowledge about Linux configuration.

基础操作

  1. 窗口模式与命令行模式切换:ctrl+alt+[F1-F6] 或者 init [0-6] 或者 startx

    1
    2
    3
    4
    5
    6
    7
    8
    各个模式:
    0 -> 关机
    1 -> 字符界面单用户模式
    2 -> 字符界面多用户模式,没有网络
    3 -> 字符界面多用户模式
    4 -> 没有在使用,用户可以自定义
    5 -> 图形界面
    6 -> 重新启动
  2. 运行级别,设置目录 /etc/inittab
    runlevel —> 查看当前运行级别
    systemctl get-default —> 查看开机默认运行级别
    systemctl set-default multi-user.target —> 修改开机默认运行级别为 3
    systemctl set-default graphical.target —> 修改开机默认运行级别 5

  3. 系统语言设置
    修改系统语言 /etc/locale.conf,英文 en_US.UTF-8,中文 zh_CN.UTF-8

  4. 网络设置
    无线网络连接 iw
    iw dev —> 查看可用网卡
    ip link set 网卡名 up —> 启用网卡
    iw 网卡名 scan | grep SSID —> 查看所有可用的无线网络
    wpa_supplicant -B -i 网卡名 -c <(wpa_passphrase "wifi名" "密码") —> 连接wifi
    无线网络连接 ip
    ip addr —> 查看可用无线网卡
    ip link set wlp3s0 up —> 打开无线网卡 wlp3s0
    ip link show wlp3s0 —> 查看网络 wlp3s0 网络接口的状态
    wpa_supplicant -B -t wlp3s0 -c <(wpa_passphrase "wifi名称" "wifi密码") —> 网络连接
    dhclient wlp3s0 —> dhcp 自动分配 ip
    ip addr show wlp3s0 —> 查看分配的 ip 地址
    无线网络连接 nmcli
    yum install NetworkManager* —> install nmcli
    nmcli r wifi on —> start wifi
    nmcli dev wifi —> scan available wifi
    nmcli --ask dev wifi connect SSID —> connect network
    nmtui —> graphical network connect

  5. 防火墙
    firewall-cmd --query-port=8020/tcp —> 查询端口是否开启
    firewall-cmd --add-port=8020/tcp --permanent —> 开启端口
    systemctl start firewalld —> 启动 firewalld
    systemctl status firewalld —> 查看 firewalld状态
    systemctl disable firewalld —> 停止 firewalld
    systemctl stop firewalld —> 禁用 firewalld,关闭防火墙

  6. systemctl
    systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体
    systemctl start firewalld.service —> 启动一个服务
    systemctl stop firewalld.service —> 关闭一个服务
    systemctl restart firewalld.service —> 重启一个服务
    systemctl status firewalld.service —> 显示一个服务的状态
    systemctl enable firewalld.service —> 在开机时启用一个服务
    systemctl disable firewalld.service —> 在开机时禁用一个服务
    systemctl is-enabled firewalld.service —> 查看服务是否开机启动
    systemctl list-unit-files|grep enabled —> 查看已启动的服务列表
    systemctl --failed —> 查看启动失败的服务列表

  7. 配置firewalld-cmd
    firewall-cmd --version —> 查看版本
    firewall-cmd --help —> 查看帮助
    firewall-cmd --state —> 显示状态
    firewall-cmd --zone=public --list-ports —> 查看所有打开的端口
    firewall-cmd --reload —> ,重启防火墙,更新防火墙规则
    firewall-cmd --get-active-zones —> 查看区域信息
    firewall-cmd --get-zone-of-interface=eth0 —> 查看指定接口所属区域
    firewall-cmd --panic-on —> 拒绝所有包
    firewall-cmd --panic-off —> 取消拒绝状态
    firewall-cmd --query-panic —> 查看是否拒绝
    firewall-cmd --zone=public --add-port=80/tcp --permanent —> 在public中永久开放80端口,--permanent 永久生效,没有此参数重启后失效)添加端口
    firewall-cmd --reload —> 重新载入端口
    firewall-cmd --zone=public --query-port=80/tcp —> 查看端口是否开启,--zone 为作用域,默认为 public
    firewall-cmd --zone=public --remove-port=80/tcp --permanent —> 删除端口
    firewall-cmd --list-all-zones —> 查看所有的zone信息
    firewall-cmd --get-default-zone —> 查看默认zone是哪一个
    firewall-cmd --zone=internal --change-zone=p3p1 —> 临时修改接口p3p1所属的zone为internal
    firewall-cmd --add-service=http —> 暂时开放http
    firewall-cmd --permanent --add-service=http —> 永久开放http
    firewall-cmd --permanent --zone=public --remove-service=ssh —> 从public zone中移除服务
    firewall-cmd --reload —> 重新加载配置

  8. SElinux
    Security Enhanced Linux (SELinux)是一个访问控制服务,基于安全策略加强对系统资源的访问控制。在系统安全的基础上,解决“May <subject> do <action> to <object>”的访问控制问题。
    SELinux是一个Linux内核的扩展模块,通过LSM(Linux Security Module)框架在链接时被加入到Linux内核中,SElinux虽然是一款用来提高系统安全性的软件,但也会和好多服务都冲突。
    查看当前状态
    getenforce
    临时关闭
    setenforce 0
    永久关闭
    修改 /etc/selinux/config 中的 SELINUX=enforcingSELINUX=disabled

  9. CentOS端口命令
    netstat -lntp —> 查看监听(Listen)的端口
    netstat -antp —> 查看所有建立的TCP连接
    查看Linux系统主机名:

    1
    2
    # hostname
    localhost.localdomain

    查看服务器IP地址:

    1
    2
    3
    # ifconfig|grep 'inet addr:'|grep -v '127.0.0.1'|cut -d: -f2|awk '{ print $1}'
    192.168.17.238
    192.168.1.9

    查看linux网关:

    1
    2
    # route |grep default
    default 192.168.1.1 0.0.0.0 UG 0 0 0 em1

    查看linux打开服务:

    1
    2
    3
    # chkconfig --list|grep 启用 #查看开启的服务
    sshd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
    httpd 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:关闭 6:关闭

    查看服务器DNS配置:

    1
    2
    3
    # cat /etc/resolv.conf
    nameserver 192.168.0.66
    nameserver 202.106.0.20

    其他网络信息:

    1
    2
    3
    # iptables -L #查看防火墙规则
    # route -n #查看路由表
    # netstat -s #查看网络统计信息
  10. .bashrc
    PS1="[\u@\h \d \A \W]\$"
    \[\e[F;Bm\] cotent \[\e[0m\] —> 颜色设置
    下面是我的设置
    PS1="[\[\e[37;41m\]\u@\h\[\e[0m\]\[\e[37;42m\]\d \A\[\e[0m\]\[\e[37;43m\]\W\[\e[0m\]\[\e[37;0m\]]\$\[\e[0m\]"

  11. U盘操作
    linux挂载U盘过程

  • sudo fdisk -l 查看U盘状态以及U盘分区名字
  • sudo mount /dev/sdb /mnt 挂载U盘到 /mnt
  • 卸载U盘 umount /mnt ,卸载的时候要退到挂载目录之外

linux格式化U盘过程

  • sudo fdisk -l 查看U盘状态以及U盘分区名字
  • mkfs.vfat /dev/sdb 格式化U盘

配置软件源

安装epel源 rpm -ivh http://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/e/epel-release-8-5.el8.noarch.rpm

工具及用法

Linux中有好多非常有用的命令行工具,也有好多好玩的命令行工具,下面笔者介绍一些工具,可能一次性写的不全,后面会慢慢的补上去。

cmatrix

  • MAC 系统安装方法:brew install cmatrix
  • RedHat 系Linux安装方法:sudo yum install cmatrix
  • Ubuntu 系Linux安装方法:sudo apt-get install cmatrix
  • Arch 系Linux安装方法:sudo pacman -S cmatrix

如果没有配置相应的软件园,没法用包管理工具直接安装的可以选择编译安装,安装方法如下:

  1. 访问 GitHub 项目地址 https://github.com/abishekvashok/cmatrix 在 releases 中下载 cmatrix-1.2.tar,也可以使用 wget 命令下载
  2. 解压 cmatrix-1.2.tar ,解压的时候会用到 tar,如果没有安装的话请使用命令安装,以 CentOS 为例 sudo yum install tar,解压命令为 tar -xvf cmatrix-1.2.tar
  3. ‘cd cmatrix’
  4. ‘./configure’
  5. ‘make && make install’,如果编译失败的话,使用 yum install ncurses-devel 安装 ncurses-devel 后再编译安装

使用方法:直接用 cmatrix 命令就可以运行,还有一些可选项如下

1
2
3
4
5
6
7
8
9
10
11
12
13
-a:异步滚动
-b:启用粗体字符
-b:所有粗体字符(覆盖-b)
-f:强制启用linux$term类型
-l:Linux模式(使用矩阵控制台字体)
-o:使用旧式滚动
-h:打印使用和退出
-n:无粗体字符(覆盖-b和-b,默认)
-s:”屏幕保护程序“模式,在第一次按键时退出
-x:窗口模式,如果您的 `xterm` 使用 `mtx.pcf`,则使用
-v:打印版本信息并退出
-u [0-10](默认4):屏幕更新延迟
-C [颜色]:将此颜色用于矩阵(默认为绿色)

运行效果如下
CMATRIX

Python

  1. python3.7 install
    problem:ModuleNotFoundError:No module named '_ctypes'
    solution:yum install libffi-devel -y

Git

Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。下面简单介绍一下Git。

Git的工作流程是:Working Directory —> Stage(Index) —> Repository(HEAD)
git config --global user.name "name" ;set user name
git config --global user.email "email" ;set user email
git config --list ;list all config

git init ;The current directory is Git’s repository directory.Initialized empty Git repository

git add file README.md ;add file to Stage
git commit -m "add a readme file" ;commit file form Stage to repository
git status ;Check git’s current working status
git reset README.md ;Undo the stage changes to working directory
git checkout -- README.md ;Overwrite the contents of working directory with the contents of the stage
git log ;View historical submission history

Working Directory —(add)—> Stage(Index) —(commit)—> Repository(HEAD)
Working Directory <—(checkout)— Stage(Index) <—(reset)— Repository(HEAD)

git reset --soft/--mixed/--hard HEAD~
git reset [id]

git diff ;Compare Stage and Working Directory
git diff id1 id2 ;Compare snapshots(repository) with id 1 and snapshots(repository) with id 2
git diff id ;Compare repository with id and the contents of the working directory
git diff HEAD ;Compare the latest snapshots(repository) and the contents of the working directory
git diff --cached id ;Compare repository with id and the contents of the stage

git commit --amend -m "new instructions" ;Edit the latest submission instructions
git rm name ;Delete files from git
git mv [oldname] [new name] ;Modify file name

git branch [branch name] ;Create a branch which name is [branch name]
git checkout [branch name] ;Switch branch which name is [branch name]
git log --decorate [--oneline] ;Show all references to the submission
git log --decorate --oneline --graph --all
git merge [branch name] ;Merge branch to the current branch
git branch -d [branch name] ;Delete branch
git branch --delete [branch name]
git show ;Detailed display of the last change record
git log -p [file name] ;View detailed edit history of a file

用户管理

Linux 有很严格的用户权限,我们可以通过用户管理命令创建或删除用户,设置用户组以及为每个用户设置权限。下面我介绍以下Linux中常用的用户管理命令。

  • useradd —> 建立用户
  • userdel —> 删除用户
  • usermod —> 修改用户信息
  • passwd —> 设置密码
  • chage —> 密码老化
  • groupadd —> 添加组
  • groupdel —> 删除组账户
  • groupmod —> 修改组
  • newgrp —> 转换组
  • groups —> 显示组
  • gpasswd —> 添加用户到组
  • whoami —> 显示当前用户名称
  • who —> 显示登陆用户
  • id —> 显示用户信息
  • pwck —> 检测账户
  • grpck —> 检测用户组账户
  • chsh —> 设置 shell
  • chfn —> 修改用户信息

进程管理

进程是正在运行的程序实体,并且包括这个运行的程序中占据的所有系统资源,比如说CPU(寄存器),IO,内存,网络资源等。很多人在回答进程的概念的时候,往往只会说它是一个运行的实体,而会忽略掉进程所占据的资源。比如说,同样一个程序,同一时刻被两次运行了,那么他们就是两个独立的进程。接下来我介绍一些常用的进程管理命令。

  • at —> 定时运行命令,指定时刻执行指定命令
  • atq —> 查询已经设定的命令序列
  • atrm —> 删除已经设定的命令序列
  • batch —> 系统负载低于一个设定值(0,8),执行设定的命令序列

  • bg —> 后台运行命令,使一个被挂起的进程在后台执行 (快捷键 Ctrl + z

  • fg —> 挂起程序,使一个被挂起的命令在前台执行
  • jobs —> 显示后台程序
  • kill —> 终止进程

  • crontab —> 安装、删除或列出任务表,该配置由 cron 守护进程在后台执行

  • ps —> 查看权限、查看系统中进程的状态
  • pstree —> 列出当前进程的树状结构
  • top —> 显示系统当前的进程状态
  • nice —> 改变程序执行的优先权等级
  • renice —> 修改一个正在运行的进程的优先权
  • sleep —> 暂停进程
  • nohup —> 退出系统之后继续工作
  • pgrep —> 查找当前运行进程并列出匹配给定条件的进程 pid
  • chkconfig —> 检查、设置系统的各种服务

进程一般分为:交互进程、批处理进程、守护进程
进程组成:代码段、数据段、堆栈段
PCB 在进程核心堆栈底部,不需要额外分配空间
poppen()system 类似,它以管道方式处理输出

磁盘管理

  • df —> 查看磁盘信息,显示磁盘文件系统与使用情况
  • du —> 某目录下所有文件和子目录的占用字盘的大小
  • dd —> 从标准输入或文件读取数据,依指定格式转换数据,再输出
  • fdisk —> 磁盘分区
  • mount —> 加载文件系统
  • umount —> 卸载文件系统
  • mkfs —> 建立各种文件系统
  • mkfs.ext2 —> 建立 ext2/ext3 文件系统
  • mkbootdisk —> 建立系统启动盘
  • fsck —> 检查文件系统
  • fsck.ext2 —> 检查 ext2/ext3 文件系统
  • hdparm —> 检测、显示与设定磁盘参数
  • mkswap —> 建立交换分区
  • dump —> 配分文件系统
  • restore —> 还原文件
  • sync —> 将内存缓冲区数据写入磁盘
  • e2label —> 设定或显示 ext2/ext3 分区卷标
  • badblock —> 检查磁盘
  • quota —> 显示磁盘已使用空间与限制
  • quotacheck —> 检查磁盘的使用空间与限制
  • quotaoff —> 关闭磁盘空间限制
  • quotaon —> 开启磁盘空间限制
  • quotastats —> 显示磁盘空间限制
  • repquota —> 检查磁盘空间限制状态
  • parted —> 磁盘分区工具

文件系统处理

文件系统是操作系统用于明确存储设备(常见的是磁盘及基于NAND Flash的固态硬盘)或分区上的文件的方法和数据结构;即在存储设备上组织文件的方法。操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统。文件系统由三部分组成:文件系统的接口,对对象操纵和管理的软件集合,对象及属性。从系统角度来看,文件系统是对文件存储设备的空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统。具体地说,它负责为用户建立文件,存入、读出、修改、转储文件,控制文件的存取,当用户不再使用时撤销文件等。下面介绍一些常用的文件操作命令。

  • cat —> 显示文本文件内容
  • less —> 分屏显示文件
  • grep —> 查找字符串
  • head —> 显示文件内容的前多少行
  • tail —> 显示文件内容的后多少行
  • sort —> 按照顺序显示文件内容
  • uniq —> 忽略文件中的重复行
  • diff —> 比较两个文件
  • diffstat —> 读取 diff 的结果,统计各文件的插入、删除、修改等差异计量
  • file —> 通过探测文件内容来判断文件类型
  • date —> 显示日期和时间
  • script —> 记录执行过的所有命令以及命令的输出
  • apropos —> 在 whatis 资料库中搜寻特定字串
  • locate —> 查找符合条件的文件
  • basename —> 显示文件或目录的基本名称
  • chattr —> 改变文件属性,防止关键文件被修改
  • cksum —> 打印每个文件的 CRC 校验以及字节统计
  • cmp —> 比较文件差异
  • split —> 切割文件
  • dirname —> 显示文件除名字外的路径
  • findfs —> 改变列表或用户 ID 查找文件系统
  • ln —> 链接文件或目录
  • lndir —> 链接目录内容
  • lsattr —> 改变文件属性
  • od —> 以八进制显示文件内容
  • paste —> 把每个文件以列对列的方式一列一列的合并
  • stat —> 以文字的格式显示 inode 内容
  • tee —> 读取标准输入的数据,并将其内容输出成文件
  • tmpwatch —> 删除临时文件
  • tree —> 以树状图显示目录内容
  • umask —> 指定建立文件时预设的权限掩码
  • chmod —> 改变文件或目录权限
  • chgrp —> 改变文件或目录所属群组
  • chown —> 改变文件拥有者或群组

网络管理

Linux 继承了 Unix 的特性,具有非常强大的网络功能,其支持所有的英特网协议,包括 TCP/IPv4、TCP/IPv6 和链路层拓扑程序等,且可以利用 Unix 的网络特性开发出新的协议栈。下面介绍一些常用的网络管理命令。

  • arp —> 管理系统中的 ARP 高速缓存
  • arpwatch —> 监听 ARP 记录
  • adsl —> 调制解调器命令
  • ifconfig —> 设置网络接口
  • iwconfig —> 设置无线网卡
  • hostname —> 显示主机名 (主机名配置文件: /etc/hosts
  • ifup —> 激活设备
  • ifdown —> 禁用网络设备
  • mii-tool —> 调整网卡模式
  • route —> 设置路由表
  • netstat —> 查看网络连接
  • ping —> 检测网卡接口
  • minicom —> 设置调制解调器
  • pppd —> 建立 PPP 连接
  • pppstats —> 显示 PPP 连接状态
  • chat —> 拨号命令
  • traceroute —> 显示数据包到主机间的路径
  • rcp —> 远程复制
  • finger —> 查找并显示用户信息
  • tcpdump —> 网络数据分析器
  • ip —> 网络集成命令工具

远程连接提示

Linux 远程用 SSH 登录会有提示信息,这个提示能够用来发布信息或者告诉登录者一些使用注意事项,下面我介绍以下登录提示信息的设置方法。

登录提示信息设置的文件有三个,分别为:/etc/issue/etc/issue.net/etc/motd, 其中

1
2
3
4
5
6
7
8
9
10
11
12
13
/etc/issue         # 本地登录显示信息,本地登陆前
/etc/issue.net # 网络登录显示信息,登陆后显示
/etc/motd # 登陆后提示信息

注:\m:机器类型
\r:内核版本
\d:日期
\t:时间
\l:登录到的终端
\n:主机名
\s:操作系统类型
\u:已经登录的用户数
\v:操作系统版本

下面是笔者自己的服务器的提示信息
MOYAN
下面分享一个好看的字符 logo 以及笔者自己的提示信息形式
1
2
3
4
5
6
7
8
9
10
11
12
13
-----------------------------------------------------------------------------------
* .=""=. *
* / _ _ \ *
* | d b | *
* \ /\ / Welcome to RICS computer server ! *
* ,/'-=\/=-'\, This is the server developed by *
* / / \ \ XXXXXXXX XXXXX XXXXXXXXXX XXXXXXXXXX CO.LTD for *
* | / RICS \ | XXXXXX XXXXXX XXXXXX CO.LTD ! *
* \/ \ / \/ Don't touch anyone except the administrator ! *
* '. .' *
* _|`~~`|_ *
* /|\ /|\ *
-----------------------------------------------------------------------------------


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !

...

...

00:00
00:00