Linux常用命令

1 虚拟机安装及x-shell远程连接

2 如何做快照和克隆

2.1 可以基于某个快照做克隆

1 如果直接克隆虚拟机也要对虚拟机进行关机
2 快照可以是关机状态,也可以是挂机状态,也可以是开机状态
3 但是克隆只能克隆关机状态的快照

3 如何连接xshell连接虚拟机

1 去大保健的路要是通的,也就是说网络要是正常
1 查看网卡是否配置好
****************
命名 vi /etc/sysconfig/network-scripts/ifcfg-ens33
里面的 ONBOOT = yes BOOTPROTO = none 保存退出
****************************
重启网卡,命令为:   systemctl restart network
(如果你执行上面的命令报错了,说明你配置写错了,检测)
*************
检查你本地的网卡是否开启了
在更改网络适配器里面看你的映射到虚拟机的网卡有没有开启。
2 去大保健你要看看是否有警察(防火墙)
systemctl stop firewalld  #临时关闭防火墙,一旦关机重启防火墙就起来了
systemctl disable firewalld #永久关闭防火墙
systemctl status firewalld #查看防火墙
3 大保健上班没有?就是查看sshd服务是否开启
systemctl status sshd #如果是active,就表示开门了。
4 去大保健你不能进错了门。(链接ip和端口用xshell要填对吧)
所要去的地方的地址别弄错了。为了下次更好的知道,服务的对象。连接名,最好有识别性

1 修改主机名

[root@localhost ~]# hostnamectl set-hostname 名字  #修改名称
[root@localhost ~]# reboot -f   #重启虚拟机

2 linux系统目录结构详解

2.1 /bin

存放经常使用的指令,比如ls,cp,rm

2.2 /sbin

系统管理员使用的系统管理命令

2.3 /home

存放普通用户的主目录,在linux中每个用户都有一个自己的目录,一般该目录是以该用户的账号名为目录名

2.4 /root

系统管理员的用户主目录

2.5 /boot

存放的是启动linux是的核心文件

2.6 /lib

存放库文件的目录

2.7 /etc

存放所有系统管理所需要的配置文件,如果我们的网卡

2.8 /usr

用户的很多应用程序和文件都放在这个目录,有点像windows中的program files目录

2.9 /proc 别动

这是系统内存的映射

2.10 /srv ,别动

service 的缩写,存放的是一些服务启动之后所需要的数据

2.11 /sys 别动

系统相关的文件

2.12 /tmp

用来存放临时文件

2.13 /dev

类型与windows的设备管理器,把所有的硬件用文件的形式存储

2.14 /media

linux 会识别一些设备,比如u盘,光驱,linux会把这个设备挂载到这个目录底下

2.15 /mnt

用于让用户临时挂载别的文件系统,我们可以将外部的存储挂载在/mnt上,我们进入目录进行查看

2.16 /opt

这个一般用来放安装包的

2,17 /var

存放经常需要被修改的文件,比如日志文件

2,18 /usr/local

程序安装后存放的地方

3文件系统常用命令

1 pwd 显示当前所在的路径

pwd -->print working directory 显示打印当前所在的目录
[root@test1 sysconfig]# pwd
/etc/sysconfig
[root@test1 sysconfig]# 

2 cd 切换目录结构

解释:cd --> change directory 改变目录信息
用法:cd 你想要去的路径

绝对路径:
[root@test1 network-scripts]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# cd /tmp
[root@test1 tmp]# pwd
/tmp

相对路径的用法:
[root@test1 tmp]# cd /etc/sysconfig/
[root@test1 sysconfig]# pwd
/etc/sysconfig
[root@test1 sysconfig]# cd network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# 

快速回到自己进过的目录:
[root@test1 sysconfig]# cd /etc/sysconfig/
[root@test1 sysconfig]# pwd
/etc/sysconfig
[root@test1 sysconfig]# cd /tmp
[root@test1 tmp]# pwd
/tmp
[root@test1 tmp]# cd -
/etc/sysconfig
[root@test1 sysconfig]# pwd
/etc/sysconfig


返回当前路径的上一级目录中:
[root@test1 sysconfig]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# cd ../
[root@test1 sysconfig]# pwd
/etc/sysconfig
[root@test1 sysconfig]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# cd ../..
[root@test1 etc]# pwd
/etc
[root@test1 etc]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# cd ../../
[root@test1 etc]# pwd
/etc
[root@test1 etc]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# cd ../../../../../../../../../
[root@test1 /]# pwd
/

如果返回到当前登入用户的家目录
[root@test1 /]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# cd ~
[root@test1 ~]# pwd
/root
[root@test1 ~]# cd /etc/sysconfig/network-scripts/
[root@test1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test1 network-scripts]# cd 
[root@test1 ~]# pwd
/root

3 mkdir 创建目录信息

mkdir ---> make directory 

[root@test1 /]# cd /test2
-bash: cd: /test2: No such file or directory
[root@test1 /]# mkdir /test2
[root@test1 /]# cd /test2
[root@test1 test2]# pwd
/test2


用-p参数创建多级目录
[root@test1 test2]# mkdir /test2/test3/json
mkdir: cannot create directory ‘/test2/test3/json’: No such file or directory
[root@test1 test2]# mkdir /test2/test3
[root@test1 test2]# mkdir /test2/test3/json
[root@test1 test2]# cd /test2/test3/json/
[root@test1 json]# pwd
/test2/test3/json
[root@test1 json]# cd /test2/
[root@test1 test2]# pwd
/test2
[root@test1 test2]# mkdir -p  /test2/staff_list/zhang
[root@test1 test2]# cd /test2/staff_list/zhang
[root@test1 zhang]# pwd
/test2/staff_list/zhang

我们在创建目录的时候做好是绝对路径创建

4 创建文件touch(摸)

[root@test1 zhang]# cd /test2/
[root@test1 test2]# pwd
/test2
[root@test1 test2]# touch test2.txt
[root@test1 test2]# ls
test2.txt  test3  staff_list
[root@test1 test2]# touch /test2/test2.txt 

5 ls 检查文件或者目录是否存在,并列出目录底的文件

ls --->list
[root@test1 /]# ls
bin   dev  home  lib64  mnt     opt   root  sbin  sys  usr
boot  etc  lib   media  test2  proc  run   srv   tmp  var
[root@test1 /]# cd test2/
[root@test1 test2]# ls
test2.txt  test3  staff_list
[root@test1 test2]# ls old.txt
ls: cannot access old.txt: No such file or directory
[root@test1 test2]# ls -l 
total 4
-rw-r--r--. 1 root root  4 Mar  6 10:50 test2.txt
drwxr-xr-x. 3 root root 18 Mar  6 10:35 test3
drwxr-xr-x. 3 root root 18 Mar  6 10:36 staff_list
[root@test1 test2]# ls -ltr
total 4
drwxr-xr-x. 3 root root 18 Mar  6 10:35 test3
drwxr-xr-x. 3 root root 18 Mar  6 10:36 staff_list
-rw-r--r--. 1 root root  4 Mar  6 10:50 test2.txt

#ls -l 默认是创建时间最新到最老排序
# 如何按照时间的创建顺序反排
ls -ltr 

6cat 查看文件信息的命令

# cat查看文件内容
[root@test1 test2]# cat test2.txt 
123
# 查看多个文件的内容同时输出
[root@test1 test2]# cat test2.txt staff_list.txt 
123
456

#将多个文件的内容读取出来以后,放入到一个文件当中

[root@test1 test2]# cat test2.txt staff_list.txt > lisi.txt
[root@test1 test2]# cat lisi.txt
123
456
[root@test1 test2]# ls
lisi.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt

7 echo命令可以将后面跟的字符进行输出

功能:显示字符,echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开,并在最后加上换行号。
语法:
echo [-neE][字符串]
选项:
-E (默认)不支持\解释功能
-n 不自动换行
-e 启用\字符的解释功能
显示变量

echo  "$VAR_NAME"  # 用变量值替换,弱引用
echo  "$VAR_NAME"  # 变量不会替换,强引用

启用命令选项-e,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出
\a 发出警告声
\b 退格键
\c 最后不加上换行符号
\e escape,相当于\033
\n 换行且光标移至行首
\r 回车,即光标移至行首,但不换行
\t 插入\字符
\ 插入\字符
\0nnn插入nnn(八进制)所代表的ASCII字符
\xHH插入HH(十六进制)所代表的ASCII数字(man 7 ascii)

echo -e 'a\x0Ab'
a
b

echo -e '\033[43;31;1;5mmddge\e[0m'
mmddge

echo -e '\x57\x41\x4E\x47'
WANG

echo \$PATH
$PATH

echo -e '\E[1;31m你好\E[0m'  # 你好 被加成红色字体

echo \ # 没有任何东西

echo \\ # 返回一个斜杠
\

echo \\\ # 返回一个大于号
>

echo \\\\ # 返回双斜杠
\\

echo高级用法
在终端中,ANSI定义了用于屏幕显示的Escape屏幕控制码
具有颜色的字符,其格式如下:
"\033[字符背景颜色;字体颜色m字符串\033[0m"

\033[30m -- \033[37m 设置前景色
\033[40m -- \033[47m 设置背景色

字符背景颜色范围:40--47

40:黑
41:红
42:绿
43:黄
44:蓝
45:紫
46:深绿
47:白色

字体颜色: 30--47

30:黑
31:红
32:绿
33:黄
34:蓝
35:紫
36:深绿
37:白色

加颜色只是以下控制码中的一种,下面是常见的一些ANSI控制码:

  \033[0m 关闭所有属性
\033[1m 设置高亮度
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n列
\033[nD 光标左移n列
\033[x;yH 设置光标位置x行y列
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标
\033[2J\033[0;0H 清屏且将光标置顶

# 直接输出信息
[root@test1 test2]# echo "hello world"
hello world
[root@test1 test2]# echo hello world
hello world

#将echo的内容写入文件,> 是覆盖的意思,>> 是追加
# 格式:echo 内容 > 文件
# 格式:echo 内容 >> 文件

[root@test1 test2]# echo hello world > text1
[root@test1 test2]# ls
text1  lisi.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt
[root@test1 test2]# cat text1
hello world
[root@test1 test2]# echo hello world > text1
[root@test1 test2]# cat text1
hello world
[root@test1 test2]# echo hello world1 > text1
[root@test1 test2]# cat text1
hello world1
[root@test1 test2]# echo hello world >> text1
[root@test1 test2]# cat text1
hello world1
hello world
[root@test1 test2]# 




8 cp复制

cp ---> copy
语法格式: cp 参数(可选) 要进行复制的信息  复制到什么位置

#复制文件

在复制文文件时,不要在文件名称后面加/,一般只能在目录后面加/
[root@test1 test2]# cp /etc/hosts /test2
[root@test1 test2]# ls
text1  hosts  lisi.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt
[root@test1 test2]# cat hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

#如果存在会请求你是否覆盖
[root@test1 test2]# cp /etc/hosts /test2
cp: overwrite ‘/test2/hosts’? y

#复制文件夹

[root@test1 test2]# ls /test2/staff_list/
zhang
[root@test1 test2]# cp /etc/sysconfig/ /test2/staff_list/
cp: omitting directory ‘/etc/sysconfig/’
[root@test1 test2]# cp -r  /etc/sysconfig/ /test2/staff_list/
[root@test1 test2]# ls /test2/staff_list
sysconfig  zhang
cp  参数:
-d 和链接相关的文件
-r 进行递归复制
-p 保持属性不变
-a == -drp

#利用cp 做备份
[root@test1 test2]# cp test2.txt  test2.txt.bak
[root@test1 test2]# ls
cwx  text1  hosts  lisi.txt  test2.txt  test2.txt.bak  test3  staff_list  staff_list.txt  zhang.txt
[root@test1 test2]# rm -rf test2.txt
[root@test1 test2]# ls
cwx  text1  hosts  lisi.txt  test2.txt.bak  test3  staff_list  staff_list.txt  zhang.txt
[root@test1 test2]# cp   test2.txt.bak test2.txt
[root@test1 test2]# ls
cwx  text1  hosts  lisi.txt  test2.txt  test2.txt.bak  test3  staff_list  staff_list.txt  zhang.txt

# 多文件信息重复,会多次确认提示,如何避免
[root@test1 test2]# cp -r  /etc/sysconfig/ /test2/staff_list/
cp: overwrite ‘/test2/staff_list/sysconfig/ip6tables-config’? y
cp: overwrite ‘/test2/staff_list/sysconfig/iptables-config’? y
cp: overwrite ‘/test2/staff_list/sysconfig/cbq/avpkt’? y
cp: overwrite ‘/test2/staff_list/sysconfig/cbq/cbq-0000.example’? ^C

#解决方法
[root@test1 test2]# \cp -r  /etc/sysconfig/ /test2/staff_list/
[root@test1 test2]# 

9 mv 剪切命令

mv -->move
对文件或者文件夹进行剪切(移动)
语法格式 : mv 参数(可选) 要移动的文件或者文件夹  移动什么位置
#我们要先创建这个文件
[root@test1 test2]# mkdir /test
[root@test1 test2]# cd /test
[root@test1 test]# mkdir test1
[root@test1 test]# ls
test1
[root@test1 test]# touch heihei.txt
[root@test1 test]# ls
heihei.txt  test1

# 移动 /test/heihei.txt 到/test2/shanghai
[root@test1 test2]# ls /test2/shanghai
ls: cannot access /test2/shanghai: No such file or directory
[root@test1 test2]# mkdir /test2/shanghai
[root@test1 test2]# mv /test/heihei.txt  /test2/shanghai/
[root@test1 test2]# ls /test2/shanghai
heihei.txt
[root@test1 test2]# mv /test/heihei.txt  /test2/shanghai/
mv: cannot stat ‘/test/heihei.txt’: No such file or directory
[root@test1 test2]# mv  /test2/shanghai/heihei.txt  /test/
[root@test1 test2]# ls /test2/shanghai/
[root@test1 test2]# 


# 利用mv 命令给文件重命名
[root@test1 test2]# mv lisi.txt  jasongdx.txt
[root@test1 test2]# ls
cwx   hosts         test2.txt      test3   staff_list.txt  zhang.txt
text1  jasongdx.txt  test2.txt.bak  staff_list  shanghai

10 rm 命令(删除)

rm --->remove
语法 :rm 参数 要删除的数据信息

#删除文件
[root@test1 test2]# rm test2.txt.bak 
rm: remove regular file ‘test2.txt.bak’? y
[root@test1 test2]# ls
cwx  text1  hosts  jasongdx.txt  test2.txt  test3  staff_list  staff_list.txt  shanghai  zhang.txt

#删除文件夹
[root@test1 test2]# rm shanghai
rm: cannot remove ‘shanghai’: Is a directory
[root@test1 test2]# rm -r  shanghai
rm: remove directory ‘shanghai’? y
[root@test1 test2]# ls
cwx  text1  hosts  jasongdx.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt

# 强制删除
[root@test1 test2]# ls
text1  hosts  jasongdx.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt
[root@test1 test2]# rm -f   zhang.txt 
[root@test1 test2]# ls
text1  hosts  jasongdx.txt  test2.txt  test3  staff_list  staff_list.txt

[root@test1 test2]# rm -rf   cwx
[root@test1 test2]# ls
text1  hosts  jasongdx.txt  test2.txt  test3  staff_list  staff_list.txt  zhang.txt

说明centos6.7以后对删除根目录做了保护
rm -rf /

vim 编辑器 功能比vi

到 https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b11q17Xub 切换我们yum源
1.首先安装wget:wget就相当于我们的request请求一样,后面跟一个连接我们就可以下载这个链接中的库资源;   yum install wget

2.之后,执行切换镜像源命令:wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3.安装软件命令:yum install 软件 -y 不需要征求是否同意

安装vim命令:yum -y install vim 
安装查看网络配置的命令: yum -y install net-tools  # 不安装net-tools,不能在Linux虚拟机上使用ifconfig(类似于Windows的ipconfig命令)命令

vim 有种状态
1 正常模式(我们用vim打开文件就是进入正常模式)
	比如复制,粘贴等
	
2 插入模式
	在插入模式下,我们们输入内容,编辑内容
	如何从正常模式切换插入模式:i,o,a,I,O,A,R任意一个键都能重正常模式进入插入模式
	正常习惯按i,因为好记,i-->insert
	
3 命令模式
	在这个模式下,我们可以输入相关的命令,比如退出,,保存,等操作
终结:vim三种模式可以随意切换


正常模式下的命令:
    拷贝:yy  粘贴:p

    拷贝当前行向下2行,并粘贴
    拷贝2行:2yy
    粘贴:p

    删除当前行
    删除:dd
    向下删除2行
    删除:2dd
    
    光标移到最后一行:	G
    光标移动了首行: gg
    光标移动2行: 2gg
    
    撤销: u
    
编辑模式下:进入以后就可以编辑



命令模式:
    1 查找内容
    :/关键字
    2 取消高亮
    :nohl
    3 显示行数
    :set nu
    4 取消行号
    :set nonu
    5 没有修改的情况下退出
    :q
    6如果我们改了,但是我们不想他保存
    :q!
    7 如果我们改了,并想保存退出,
    :wq

4 关机、重启命令

shutdown 命令:
	shutdown -h now   #立即关机
	shutdown -h 1     #1分钟后关机
	shutdown -r now   # 立即重启
	shutdown -r 1     #1分钟后重启
	
halt 关机
reboot 重启
sync  把内存中的数据同步到硬盘中

注意:当关机或者重启的时候,一定要先执行sync命令,防止数据丢失。

5 用户 与用户主

7.1 为什么要用户

正常公司的服务器,不会给你root用户,就算给你root用户,你也不要要,权限越大风险就越大。正常情况下使用的是普通用户登入。当然可以切换用户

7.2 如何查看当前登入的用户

命令:

[root@localhost ~]# whoami
root

7.3 如何添加用户

useradd 选项 用户名
例如:
useradd lxx
说明:成功创建后,就会自动创建于用户名同名的家目录

给用户自定家目录
useradd -d 目录路径 用户名

指定用户组
useradd -g 用户主名 用户名

7.4 给指定用户添加或修改密码

passwd 用户名
密码最少要8个字符

7.5 如何删除用户

userdel 用户名                 删除用户,保留家目录
userdel -r 用户名              删除用户,不保留家目录

#一般不会删除用户的家目录

7.6如何查询用户是否存在

id 用户名

如何切换用户:su - 用户名
回到原来的用户: exit

7.7 编辑用户组

因为我们要对权限做统一的管理,运维,开发,方便权限的管理

1 如何添加一个组
groupadd  组名 

2 删除组
groupdel 组名

groupdel如果该组里面有用户的话,是不能删除的,除非删除这个用户

3 如何给用户切换组
usermod -g 组名 用户名

6 文件权限

查看文件权限ls -lhi /etc 最多用的是ll
结果如下:
16829808 -rw-r--r--.  1 root root    0 Oct 31  2018 environment
17220842 -rw-r--r--.  1 root root 1.3K Apr 11  2018 ethertypes
16829809 -rw-r--r--.  1 root root    0 Jun  7  2013 exports
17109460 lrwxrwxrwx.  1 root root   56 Mar  5 10:26 favicon.png ->/usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
16829810 -rw-r--r--.  1 root root   70 Oct 31  2018 filesystems
651307 drwxr-x---.  7 root root  133 Mar  5 10:27 firewalld
16777282 -rw-r--r--.  1 root root  465 Mar  5 10:24 fstab
01        02          03 04   05    06      07       08

01 文件索引节点信息 inode
02 文件的类型以及文件的权限信息
03 硬链接数
04 文件所属的用户
05 文件所属的用户组
06 文件大小
07 最后一次被修改的时间
08 文件名

*******解释02***********
d    rwx    r-x    ---
1     2      3      4

上述*******解释02***********的1,表示文件的类型
d  directory 目录类型文件
-  file   普通文件
l  softlint 链接类型

上述*******解释02***********的2,表示当前用户对当前文件权限
上述*******解释02***********的3,表示当前用户组对当前文件权限
上述*******解释02***********的4,表示其他用户对当前文件的权限
总结:
文件(你的玩具) :上述*******解释02***********的2,你自己对你的玩具有什么权限
文件(你的玩具):上述*******解释02***********的3,你的家人对你的玩具有什么权限
文件(你的玩具): 上述*******解释02***********的4,隔壁老王,陌生人对你的玩具有什么权限

一个文件的权限有3位组成:
rwx --->分别代表了读,写,执行这个三个权限
r -->read-->读权限  数值 4
w --->write--->写权限  数值 2
x ---> exwcute-->执行  数值 1
- --->没有权限          数值 0

rwx  r-x  r-x  请问属主什么权限  属主组有什么权限   其他用户有什么权限
7    5    5    读 写 执行           读 执行       读 执行

############
04 文件所属的用户 root -->该文件的属主是root
05 文件所属的用户组root  ---》该文件的属主组是root组


6.1 如何修改文件的权限

语法 : chomd 参数  权限值 文件路径
drwxr-xr-x.   2 root root   19 Mar  6 15:33 test2
执行 :chmod 777 /test2
drwxrwxrwx.   2 root root   19 Mar  6 15:33 test2

让文件夹以及子文件递归变成我们指定的权限
执行 :chmod  -R 777 /test2

6.2 如何修改文件的属主及属主组

chown 参数 用户名.组名  文件/文件夹

让文件夹以及子文件递归变成我们指定的用户.组
执行 :chown  -R jj.sb a.txt

linux命令补充:点这里

posted on 2020-03-06 21:11  jueyuanfengsheng  阅读(318)  评论(0编辑  收藏  举报