Linux常用操作详解

第1章 Linux命令基础

1.1 习惯

操作前备份,操作后检查

1.2 简单目录结构

一切从根开始,与windows不同

1.3 规则

[root@znix ~]#
[用户名@主机名 你在哪]#

1.4 重定向符号

特殊符号-重定向符号:泼水

1.4.1 输出重定向

>或1>   标准输出重定向:

先把原文件的内容清空,然后把新的内容放到文件中

>>或1>> 追加输出重定向:

把前面输出的东西输入到后边的文件中,不会清除文件原有内容,只是追加到文件的最后一行

[root@znix ~]# echo clsnedu.com >>clsn.txt
[root@znix ~]# cat clsn.txt
clsnedu.com
[root@znix ~]# echo clsnedu.cn >clsn.txt
[root@znix ~]# cat clsn.txt
clsnedu.cn

2>>  错误追加输出重定向

把命令执行错误的信息追加存放在文件中

2>   错误输出重定向

先把原文件的内容清空,然后把错误信息放到文件中

[root@znix ~]# echa clsnedu.com 2>>clsn.txt

[root@znix ~]# cat clsn.txt
clsnedu.com
-bash: echa: command not found

把错误的内容和正确的信息都放在一个文件中.

[root@znix ~]# echo clsnedu.com   >>clsn.txt  2>>clsn.txt
[root@znix ~]# cat clsn.txt
clsnedu.com
-bash: echa: command not found
clsnedu.com

错误信息:

[root@znix ~]# cho clsnedu.com   >>clsn.txt  2>>clsn.txt
[root@znix ~]# cat clsn.txt
clsnedu.com
-bash: echa: command not found
clsnedu.com
-bash: cho: command not found

简写方式:

[root@znix ~]# cho clsnedu.com   >>clsn.txt  2>&1
[root@znix ~]# cho clsnedu.com  &>clsn.txt

1.4.2 输入重定向

<或0< 输入重定向

输入重定向重定向用于改变命令的输入。

      当前仅与xargs联用

实例1-1          

[root@znix ~]# cat /data/clsn.txt
1 2 3 4 5
[root@znix ~]# xargs  -n2 </data/clsn.txt
1 2
3 4
5

<<   追加输入重定向

使用到的地方不多,目前与cat 一起使用。

实例1-2          

[root@znix ~]# cat >>/data/clsn.txt<<EOF
I
am
sudent
> EOF

1.5 别名

别名  ==  小名

1、为了省事

2、防止犯错 给危险的linux命令加上别名

1.5.1 查询别名的方法

使用alias命令可以查看系统中都有哪些别名。

[root@znix ~]# alias rm cp mv
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

1.5.2 别名格式

别名时命令的别名,只能给一条命令设置别名。

1.5.3 给rm设置别名

①临时设置

      直接在命令行中添加别名,这个别名只能临时使用,断开连接后失效。

[root@znix ~]# alias mv='echo bieyong rm'
[root@znix ~]# alias rm
alias mv='echo bieyong rm'

②永久设置

a)      把别名放到/etc/profile 文件最后一行中去,(全局使用),所有用户都可以使用。

[root@znix ~]# tail -1 /etc/profile
alias rm='echo bieyong rm'

b)      修改 ~/.bachrc 文件,仅对当前用户设置 别名,对其他用户没有作用。

[root@znix ~]# vim  .bashrc

c)      让修改的配置文件生效

使用source命令,使配置文件生效。

[root@znix ~]# source /etc/profile
[root@znix ~]# alias rm
alias rm='echo bieyong rm'

1.5.4 查看

1.5.5 临时取消别名的方法

1)     \  转义符

[root@znix ~]# \cp /mnt/test.txt /tmp/

2)     使用命令的绝对路径

查询命令的绝对路径使用which命令

[root@znix ~]# which cp
alias cp='cp -i'
       /bin/cp

            #使用方法

[root@znix ~]# /bin/cp /mnt/test.txt /tmp/ 

1.6 相对路径与绝对路径

绝对路径, 只要是以根(/)开始的路径/位置 就是绝对路径.

 /data
 /tmp
 /etc/sysconfig/network-scripts/ifcfg-eth0

相对路径, 路径的最前面 没有 /, 不是以根开始的路径.

 etc/hosts
 etc/sysconfig/network-scripts/ifcfg-eth0

1.6.1 切换当前路径

[root@znix ~]# cd /data/
[root@znix data]# cd /tmp/

1.6.2 显示当前所在位置

[root@znix data]# pwd
/data


1.1 查看Linux版本

1.1.1 系统版本

[root@znix ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)

1.1.2 内核版本

[root@znix ~]# uname -r
2.6.32-696.el6.x86_64

1.1.3 系统架构

[root@znix ~]# uname -m
x86_64

1.2 添加用户、设置密码

1.2.1 添加用户

[root@znix ~]# useradd clsn

1.2.2 设置密码

[root@znix ~]# passwd clsn
Changing password for user clsn. ###修改clsn用户密码
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple  ###密码太简单
Retype new password:
passwd: all authentication tokens updated successfully(成功).

1.3 切换用户

1.3.1 使用这个用户 切换用户

[root@znix ~]# su - clsn

1.3.2 显示你是谁?

[clsn@znix ~]$ whoami
clsn

1.4 su 与su- 的区别

su只是切换了root身份,但Shell环境仍然是普通用户的Shell

su-连用户和Shell环境一起切换成root身份了。

只有切换了Shell环境才不会出现PATH环境变量错误。

su切换成root用户以后,pwd一下,发现工作目录仍然是普通用户的工作目录;而用su -命令切换以后,工作目录变成root的工作目录了。

1.5 关闭selinux

1.5.1 永久生效

修改配置文件: /etc/selinux/config的

[root@znix ~]# vim /etc/selinux/config

/etc/selinux/config 文档内容含义:

#enforcing     selinux默认状态 selinux已经开启,正在运行

#permissive    selinux临时关闭,显示警告

#disabled      selinux彻底关闭   

使用sed命令对/etc/selinux/conifg 文件进行修改

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

让配置文件的修改生效,使用source命令

[root@znix ~]# source /etc/selinux/config

永久修改的配置生效需要重启服务器。

使用的服务器不可以随意重启!

1.5.2 临时关闭

使用getenforce 命令查看selinux的

[root@znix ~]# getenforce
Enforcing(正在运行)

使用setenforce 命令修改selinux配置临时关闭selinux。

[root@znix ~]# setenforce
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

[root@znix ~]# setenforce 0
[root@znix ~]# getenforce
Permissive(临时关闭)

1.6 关闭防火墙

1.6.1 临时关闭

1)         查询防火墙是否正在运行

[root@znix ~]# /etc/init.d/iptables status

2)         关闭防火墙

a)      一般需要关两次,确保完全关闭。

[root@znix ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                            [  OK  ]

[root@znix ~]# /etc/init.d/iptables stop

3)         检查一下是否关闭

[root@znix ~]# /etc/init.d/iptables status
iptables: Firewall is not running.

1.6.2 永久关闭

确保开机防火墙不再启动

在chkconfig中查找iptables 的行,看他的状态。on是开,off是关。

[root@znix ~]# chkconfig|grep "ipta"
iptables         0:off      1:off      2:on       3:on       4:on       5:on       6:off

使用chkconfig的命令关闭iptables

[root@znix ~]# chkconfig iptables off

检查一下是否关闭了。

[root@znix ~]# chkconfig|grep "ipta"
iptables         0:off      1:off      2:off      3:off      4:off      5:off      6:off

1.7 显示乱码解决

1.7.1 查看linux系统字符集

[root@znix ~]# echo $LANG
en_US.UTF-8

1.7.2 查看远程软件的字符集

连接软件的字符集是否与系统的一致 

1.7.3 乱码解决办法

1)         linux系统字符集修改

a)      使用export 对变量进行修改

[root@znix ~]# export LANG=en_US.utf8
[root@znix ~]# echo $LANG
en_US.utf8

      b)修改配置文件,将/etc/sysconfig/i18n修改为utf-8字符集。

[root@znix ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

      c)使用source或. /etc/sysconfig/i18n  让配置生效

[root@znix ~]# source /etc/sysconfig/i18n
[root@znix ~]# . /etc/sysconfig/i18n
posted @ 2018-10-17 21:31  Roc_Atlantis  阅读(303)  评论(0编辑  收藏  举报