Linux常用命令
rename
批量重命名
rename .txt .txt.bak *.txt
使用rename命令,批量重命名文件夹,多用于备份/etc/yum.repo.d/目录下的repo文件。
vim
查看文档编码
:set fileencoding
返回编码格式
查看文档tab空格和换行
:set list 进入List Mode
:set nolist 退出List Mode
TAB 键显示为 ^I, **$**显示在每行的结尾,表示换行;空格仍然显示为空格
搜索不区分大小写
:set ic
替换
# 将全文每一行的一第个a1替换成a3
:%s/a1/a3
# 全文将a1替换为a3
:%s/a1/a3/g
# 将1-5行的一第个b1替换成b3
:1,5s/b1/b3
# 将1-5行的所有b1替换为b3
:1,5s/b1/b3/g
多行注释
注释过程:
- 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;
- 在行首使用上下键选择需要注释的多行;
- 按下键盘(大写)“I”键,进入插入模式;
- 然后输入注释符(“//”、“#”等);
- 最后按下“Esc”键。
取消注释:
- 首先按esc进入命令行模式下,按下Ctrl + v, 进入列模式;
- 选定要取消注释的多行;
- 按下“x”或者“d”. 注意:如果是“//”注释,那需要执行两次该操作,如果是“#”注释,一次即可
多行删除
1.首先在命令模式下,输入“:set nu”显示行号;
2.通过行号确定你要删除的行;
3.命令输入“:32,65d”,回车键,32-65行就被删除了,很快捷吧如果无意中删除错了,可以使用‘u’键恢复(命令模式下)
date
将CST时间改为GMT
cp /usr/share/zoneinfo/GMT /etc/localtime
上述改完有可能会出现一个问题,如果时间一切正常则无事,如果时间比当前真实时间慢8个小时的话,需要进一步修改
[root@slave log]# cat /etc/localtime
TZif2GMTTZif2GMT
GMT0
在/etc/profile
中增加下列一行,source
使生效即可
export TZ='CST-8'
nmap
查找一个网段可用的IP地址
[root@manager ~]# nmap -v -sn 10.170.249.0/24 | grep "host down"
查看在线的主机
[root@manager ~]# nmap -sP 10.170.249.0/24
网络
删除网桥
# 使某个网桥down
[root@master neutron]# ifconfig brq2a23260e-a6 down
# 然后才能删除
[root@master neutron]# brctl delbr brq2a23260e-a6
系统语言
查看本地环境,需要将LANG=zh_CN.UTF-8
改成en_US.UTF-8
[root@master yum.repos.d]# date
2020年 08月 01日 星期六 15:49:53 GMT
[root@master yum.repos.d]# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
修改 /etc/locale.conf
文件(很多文章说/etc/sysconfig/i18n
这个文件,我是不存在)。
LANG="en_US.UTF-8"
# 修改后执行source让生效
source /etc/locale.conf
source
前后的变化
[root@master sysconfig]# echo $LANG
zh_CN.UTF-8
[root@master sysconfig]# source /etc/locale.conf
[root@master sysconfig]# echo $LANG
en_US.UTF-8
混乱的时间
安装完系统后,先安装ntp服务,找个标准时间对一下
[root@ftpserver centos7]# yum install ntp
[root@ftpserver centos7]# ntpdate -u time1.aliyun.com
10 Aug 09:59:45 ntpdate[15620]: step time server 203.107.6.88 offset -28747.137680 sec
[root@ftpserver centos7]# date
Mon Aug 10 09:59:50 GMT 2020
[root@ftpserver centos7]# cat /etc/localtime
TZif2GMTTZif2GMT
GMT0
[root@ftpserver centos7]# cat /etc/timezone
cat: /etc/timezone: No such file or directory
[root@ftpserver centos7]# timedatectl
Local time: Mon 2020-08-10 10:03:41 GMT
Universal time: Mon 2020-08-10 10:03:41 UTC
RTC time: Mon 2020-08-10 10:02:48
Time zone: Asia/Shanghai (GMT, +0000)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
可以看到,本地的时间是GMT +0000
执行完
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
或者其它指令,要么没有修改成功,要么应该是需要重启的
可以采用这种办法,添加下边配置到/etc/profile
文件
export TZ='CST-8'
修改后,时间变正常
[root@ftpserver centos7]# date
Mon Aug 10 18:09:51 CST 2020
每天定时时间同步
编辑/etc/crontab
文件
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# 如果有脚本,也可以
12 1 * * * root /opt/inotify/cron_databak.sh
# 每天0点30分执行时间同步
30 0 * * * root ntpdate ntp.aliyun.com
查看crontab的执行情况
tail -f /var/log/cron
运维-系统用户及登录
查看哪些用户登录了Linux
/var/log/wtmp 是一个二进制文件, 记录了每个用户的登录次数和登录时间
[root@localhost ~]# last -f /var/log/wtmp
wong pts/5 192.168.3.180 Wed Aug 4 09:35 still logged in
root pts/4 117.159.15.236 Wed Aug 4 09:31 still logged in
root pts/4 117.159.15.236 Wed Aug 4 09:26 - 09:29 (00:03)
wong pts/3 192.168.3.180 Wed Aug 4 09:19 - 09:35 (00:15)
wong pts/2 192.168.3.180 Wed Aug 4 09:09 still logged in
wong pts/1 192.168.3.180 Wed Aug 4 09:09 still logged in
rong pts/0 192.168.3.144 Wed Aug 4 09:09 still logged in
rong pts/1 192.168.3.144 Tue Aug 3 16:39 - 17:08 (00:28)
root pts/5 192.168.3.157 Tue Aug 3 16:30 - 16:30 (00:00)
root pts/5 192.168.3.157 Tue Aug 3 16:27 - 16:29 (00:02)
或者
[root@localhost ~]who /var/log/wtmp
root pts/5 Aug 3 16:27 (192.168.3.157)
root pts/5 Aug 3 16:30 (192.168.3.157)
rong pts/1 Aug 3 16:39 (192.168.3.144)
rong pts/0 Aug 4 09:09 (192.168.3.144)
wong pts/1 Aug 4 09:09 (192.168.3.180)
wong pts/2 Aug 4 09:09 (192.168.3.180)
wong pts/3 Aug 4 09:19 (192.168.3.180)
root pts/4 Aug 4 09:26 (117.159.15.236)
root pts/4 Aug 4 09:31 (117.159.15.236)
wong pts/5 Aug 4 09:35 (192.168.3.180)
查看当前登录的用户,在做什么事情
[root@localhost ~]# w
10:21:22 up 9 days, 17:28, 6 users, load average: 0.00, 0.02, 0.78
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
rong pts/0 192.168.3.144 09:09 5:06 0.34s 0.17s vim namelist.input
wong pts/1 192.168.3.180 09:09 31:46 0.24s 0.24s -bash
wong pts/2 192.168.3.180 09:09 51:46 0.31s 0.31s -bash
root pts/3 117.159.15.236 10:19 2.00s 0.20s 0.06s w
root pts/4 117.159.15.236 09:31 8:42 0.29s 0.01s tail -1000f /var/log/secure
wong pts/5 192.168.3.180 09:35 45:54 0.15s 0.15s -bash
查看登录成功的日志信息
[root@localhost ~]# less /var/log/secure | grep 'Accepted'
Aug 4 09:19:41 localhost sshd[22251]: Accepted password for wong from 192.168.3.180 port 61717 ssh2
Aug 4 09:26:02 localhost sshd[22749]: Accepted password for root from 117.159.15.236 port 58160 ssh2
Aug 4 09:31:02 localhost sshd[23168]: Accepted password for root from 117.159.15.236 port 59058 ssh2
Aug 4 09:34:37 localhost sshd[23465]: Accepted password for rong from 192.168.3.144 port 57075 ssh2
Aug 4 09:35:01 localhost sshd[23611]: Accepted password for wong from 192.168.3.180 port 61862 ssh2
Aug 4 09:47:44 localhost sshd[23914]: Accepted password for rong from 192.168.3.144 port 57645 ssh2
Aug 4 10:03:57 localhost sshd[24345]: Accepted password for rong from 192.168.3.144 port 57960 ssh2
Aug 4 10:19:21 localhost sshd[26610]: Accepted password for root from 117.159.15.236 port 58577 ssh2
查看su过程的日志信息
[root@localhost ~]# less /var/log/secure | grep 'su'
Aug 2 19:21:36 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/0 ruser=airviro rhost= user=root
Aug 2 19:21:36 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 2 19:22:04 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/0 ruser=airviro rhost= user=root
Aug 2 19:22:04 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 2 19:22:57 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/0 ruser=airviro rhost= user=root
Aug 2 19:22:57 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 3 16:13:56 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/4 ruser=airviro rhost= user=root
Aug 3 16:13:56 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 3 17:52:04 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/4 ruser=airviro rhost= user=root
Aug 3 17:52:04 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 3 23:05:33 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/0 ruser=airviro rhost= user=root
Aug 3 23:05:33 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 3 23:06:35 localhost su: pam_unix(su-l:auth): authentication failure; logname=airviro uid=1001 euid=0 tty=pts/0 ruser=airviro rhost= user=root
Aug 3 23:06:35 localhost su: pam_succeed_if(su-l:auth): requirement "uid >= 1000" not met by user "root"
Aug 4 09:34:41 localhost su: pam_unix(su:session): session opened for user root by wong(uid=1005)
Aug 4 09:35:15 localhost su: pam_unix(su:session): session closed for user root
requirement “uid >= 1000” not met by user “root”
修改配置文件/etc/pam.d/system-auth
,将需要UID>=1000
的一行注释掉
[root@localhost pam.d]# cd /etc/pam.d/
[root@localhost pam.d]# vim system-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth required pam_env.so
auth required pam_faildelay.so delay=2000000
auth sufficient pam_fprintd.so
auth sufficient pam_unix.so nullok try_first_pass
# 注释下面一行,注意备份
# auth requisite pam_succeed_if.so uid >= 1000 quiet_success
auth required pam_deny.so
cat
查看某个日志文件,只想看前500行
[root@zzjly-data-1 log]# cat messages | grep systemd | head -n 1000
查看目录下存储的使用情况
du -sh * | sort -nr
配置阿里yum
如果有系统iso文件,可以把文件mount到某个目录,使用这个目录做为下载源
但有的时候,没有这个文件,可以完全使用阿里的下载源
(如果没有网络,则没法下载)
将原有下载源配置文件备份
[root@localhost yum.repos.d]# rename .repo .repo.bak *
[root@localhost yum.repos.d]# ll
total 40
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 Oct 23 2020 CentOS-CR.repo.bak
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo.bak
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo.bak
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo.bak
-rw-r--r--. 1 root root 1331 Oct 23 2020 CentOS-Sources.repo.bak
-rw-r--r--. 1 root root 8515 Oct 23 2020 CentOS-Vault.repo.bak
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo.bak
基础操作系统源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
epel源 (Extra Packages for Enterprise Linux)
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
或者使用curl,把请求输出内容写到文件当中
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/epel-7-aa.repo http://mirrors.aliyun.com/repo/epel-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 3551 0 --:--:-- --:--:-- --:--:-- 3569
[root@localhost yum.repos.d]# ll
total 48
-rw-r--r--. 1 root root 2523 Dec 26 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 Oct 23 2020 CentOS-CR.repo.bak
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo.bak
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo.bak
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo.bak
-rw-r--r--. 1 root root 1331 Oct 23 2020 CentOS-Sources.repo.bak
-rw-r--r--. 1 root root 8515 Oct 23 2020 CentOS-Vault.repo.bak
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo.bak
-rw-r--r--. 1 root root 664 Jul 15 14:29 epel-7-aa.repo
[root@localhost yum.repos.d]#
执行yum源的更新
yum clean all
yum makecache