第一周作业之:Linux系统常用命令使用格式介绍

基于ContOS7下演示ifconfg、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date这些命令的常见使用方式

1、Linux系统命令通用使用格式 COMMAND OPTIONS ARGUMENTS

  • COMMAND——命令
    • 命令有两种类型:
      • 内部命令
      • 外部命令
    • 查看命令类型的方法为:type COMMAND
  • OPTIONS——选项
    选项有两种类型

    • 短选项
      短选项可以合并
    • 长选项
      长选项不能合并
  • ARGUMENTS——参数
    有些命令可带多个参数,参数中间以空格分割

2、常用命令详解

ifconfig命令:

NAME
ifconfig – configure a network interface

SYNOPSIS
ifconfig [-v] [-a] [-s] [interface]
ifconfig [-v] interface [aftype] options | address …

命令格式:

ifconfig[-a] [interface]  :查看接口信息

:-a display all interfaces which are currently available, even if down

: interface

The name of the interface

ifconfig  interface [aftype] options | address …

[aftype]:

Address Families

options:

[-]arp:arp
[-]promisc :混杂模式
[-]allmulti :多播模式

示例:

# 配置网卡ens33接口地址

[root@localhost ~]# ifconfig ens33 172.16.47.77 netmask 255.255.255.0 up

[root@localhost ~]# ifconfig ens33 172.16.47.77/24

[root@localhost ~]# ifconfig ens33 down

 

#启用混杂模式

[root@localhost ~]# ifconfig ens33 promisc

#关闭混杂模式

[root@localhost ~]# ifconfig ens33 -promisc

echo命令:

NAME
echo – display a line of text

SYNOPSIS
echo [SHORT-OPTION]… [STRING]…
echo LONG-OPTION

-n :不在最后自动换行

-e : 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:

转义字符如下:

\a 发出警告声;

\b 删除前一个字符;

\c 最后不加上换行符号;

\n 换行且光标移至行首;

\r 光标移至行首,但不换行;

\v 与\f相同;

\\ 插入\字符;

\0nn 插入0nn(八进制)代表的ASCII;

–help 显示帮助
–version 显示版本信息

示例:

#文本直接输出

[root@localhost ~]# echo ‘Hello word’
Hello word!

#输出变量的值:假设定义一个变量,i=”Hello word”

[root@localhost ~]# i=”Hello word”
[root@localhost ~]# echo $i
Hello word

#注意:echo后的单引号表示强引用,单引号里面是什么就输出什么,而双引号是弱引用,变量的值会代替变量名输出。

[root@localhost ~]# echo “$i”
Hello word
[root@localhost ~]# echo ‘$i’
$i

#转义字符演示

[root@localhost ~]# echo “Hello Linux\tHello World”
Hello Linux\tHello World
[root@localhost ~]# echo -e “Hello Linux\tHello World”
Hello Linux Hello World
[root@localhost ~]# echo -e “Hello Linux\nHello World”
Hello Linux
Hello World
tty命令

NAME
tty – print the file name of the terminal connected to standard input

SYNOPSIS
tty [OPTION]…

tty命令用于显示终端机连接标准输入设备的文件名称。
在Linux操作系统中,所有外围设备都有其名称与代号,这些名称代号以特殊文件的类型存放于/dev目录下。你可以执行tty(teletypewriter)指令查询目前使用的终端机的文件名称。

-s, –silent, –quiet
print nothing, only return an exit status

–help display this help and exit

–version
output version information and exit

示例:

#显示当前终端

[root@localhost ~]# tty
/dev/pts/0

此处解释下tty、pty、pts的概念区别:

tty(终端设备的统称):
tty一词源于Teletypes,或teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西被键盘和显示器取代,所以现在叫终端比较合适。
终端是一种字符型设备,他有多种类型,通常使用tty来简称各种类型的终端设备。
pty(虚拟终端):
但是假如我们远程telnet到主机或使用xterm时不也需要一个终端交互么?是的,这就是虚拟终端pty(pseudo-tty)
pts/ptmx(pts/ptmx结合使用,进而实现pty):
pts(pseudo-terminal slave)是pty的实现方法,和ptmx(pseudo-terminal master)配合使用实现pty。

startx命令:

NAME
startx – initialize an X session

SYNOPSIS
startx [ [ client ] options … ] [ — [ server ] [ display ] options … ]

注意:startx为启动X Window的script文件,实际上启动X Window的程序为xinit。

startx options

示例
要在工作站上或 X 终端上启动 X 会话,请输入:

startx
要在工作站上强制启动 X 会话,请输入:

startx -w
要为 X 终端启动 X 会话,并注销用户的 Telnet 会话,请输入:

startx; kill -9 $$
要使用 .xinitrc 脚本启动 X 会话,请输入:

startx -x .xinitrc
要使用 mwm 窗口管理器启动 X 会话,请输入:

startx -m mwm

export命令:

功能说明:

设置或显示环境变量。(比如我们要用一个命令,但这个命令的执行文件不在当前目录,这样我们每次用的时候必须指定执行文件的目录,麻烦,在代码中先执行export,这个相当于告诉程序,执行某某东西时,需要的文件或什么东东在这些目录里)

export [-fn] [name[=value] …]

export -p

-f  代表[变量名称]中为函数名称。

-n  删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。

-p  列出所有的shell赋予程序的环境变量。

一个变量创建时,它不会自动地为在它之后创建的shell进程所知。而命令export可以向后面的shell传递变量的值。当一个shell脚本调用并执行时,它不会自动得到原为脚本(调用者)里定义的变量的访问权,除非这些变量已经被显式地设置为可用。export命令可以用于传递一个或多个变量的值到任何后继脚本。

一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量。例如我的mips-linux-gcc编译器在“/opt/au1200_rm /build_tools/bin”目录下,build_tools就是我的编译工具,则有如下三种方法来设置环境变量:

(1)、直接用export命令:
#export PATH=$PATH:/opt/au1200_rm/build_tools/bin
查看是否已经设好,可用命令export查看:

[root@localhost bin]#export

(2)、修改profile文件:
#vi /etc/profile
在里面加入:
export PATH=”$PATH:/opt/au1200_rm/build_tools/bin”

(3)、修改.bashrc文件:
# vi /root/.bashrc
在里面加入:
export PATH=”$PATH:/opt/au1200_rm/build_tools/bin”

后两种方法一般需要重新注销系统才能生效,最后可以通过echo命令测试一下:
# echo $PATH
看看输出里面是不是已经有了 /my_new_path这个路径了

pwd命令:

NAME
pwd – print name of current/working directory

SYNOPSIS
pwd [OPTION]…

–help:显示帮助信息;

–version:显示版本信息;

示例:

#查看当前路径

[root@localhost ~]# pwd
/root

history命令:

history :shell进程会在其会话中保存此前用户提交执行过的命令:

history [-c] [-d offset] [n]
history -anrw [filename]
history -ps arg [arg…]

      -c clear the history list by deleting all of the entries
      -d          offset delete the history entry at offset OFFSET.
      -a append history lines from this session to the history file
      -n read all history lines not already read from the history file
      -r read the history file and append the contents to the history
    list
      -w write the current history to the history file
    and append them to the history list
      -p perform history expansion on each ARG and display the result
    without storing it in the history list
      -s append the ARGs to the history list as a single entry
示例:

[root@localhost ~]# history -c 清除历史
[root@localhost ~]# history -d 100 10 删除第100条后10个
[root@localhost ~]# history -a 将新增的命令行追加到历史文件中
[root@localhost ~]# history -w 将当前内存历史列表追加到历史文件中
[root@localhost ~]# history -r 从历史文件中读取到当前内存历史列表
[root@localhost ~]# history 10 显示最近10条

shutdown、halt、poweroff、reboot命令

NAME
shutdown – Halt, power-off or reboot the machine

SYNOPSIS
shutdown [OPTIONS…] [TIME] [WALL…]

shutdown 命令:

shutdown以一种安全的方式关闭系统。所有登陆用户都可以看到关机信息提示shutdown本质是通知Init进程,要求它改变runlevel来实现。

–help
Print a short help text and exit.

-H, –halt
Halt the machine.

-P, –poweroff
Power-off the machine (the default).

-r, –reboot
Reboot the machine.

-h
Equivalent to –poweroff, unless –halt is specified.

-k
Do not halt, power-off, reboot, just write wall message.

–no-wall
Do not send wall message before halt, power-off, reboot.

-c
Cancel a pending shutdown. This may be used cancel the effect of an invocation of shutdown
with a time argument that is not “+0” or “now”.

示例:

#立即重启

[root@localhost ~]# shutdown -r now

#关机命令

#shutdown -h +30 	    延迟30分钟后关机         
#shutdown -H now	    立刻执行关机  now等于+0 
#shutdown -P 15:20	    下午15:20分执行关机     
#取消后台关机命令
[root@localhost ~]# shutdown -c

TIME说明

必须指定TIME,即执行shutdown命令的时间[只有当取消shutdown命令时候不指定时间]

TIME格式大致有2种

<1>绝对时间:即hh:mm格式

<2>相对时间:即+m格式,从当前延时m分钟关机,请注意shutdown -h now中的now等于+0

halt命令

江湖人称最简单的关机命令,但是没有shutdown那么安全。本事是调用shutdown -h。halt执行时﹐杀死应用进程﹐执行sync系统调用,文件系统写操作完成后就会停止内核

 

halt [OPTIONS…]

无参数                           调用shutdown -h执行关机

-n, –no-sync                不执行sync系统调用就关机

-f, –force                      用自己的关机方式,不去调用shutdown -h

#halt -f 	            强制关机,不去调用shutdown,未保存的数据会丢失
#halt -force	            同上

-p                                   关机后关电源
#halt -p 	            关机后顺便关闭电源,请注意缺省会带-p
-w                                “假关机”
#halt -w 	            并不会真的关机,只是把记录写到 /var/log/wtmp 档案里

Poweroff命令:
Poweroff基本和halt一致,不过使用poweroff会关机后并且切断电源。poweroff在关闭计算机操作系统之后,最后还会发送ACPI指令,通知电源,最后切断电源供应,当然路由器等嵌入系统不支持ACPI的,所以这个无效。
#power -i                   关闭所有网络接口,然后关机,关电源
#power -h                   关闭操作系统之前将系统中所有的硬件设置为备用模式

Reboot命令

Reboot基本和halt一致,不过他是重启动作,功能是重新启动.

#reboot -i                  关闭所有网络接口,然后重启
#reboot -w                  假重启,并不会真的重启,只是把重开机数据写到 /var/log/wtmp 档案里

hwclock命令

NAME
hwclock – query or set the hardware clock (RTC)

SYNOPSIS
hwclock [function] [option…]

option:


  –adjust  hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用–adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟。
–debug  显示hwclock执行时详细的信息。
–directisa  hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟。
–hctosys  将系统时钟调整为与目前的硬件时钟一致。
–set –date=<日期与时间>  设定硬件时钟。
–show  显示硬件时钟的时间与日期。
–systohc  将硬件时钟调整为与目前的系统时钟一致。
–test  仅测试程序,而不会实际更改硬件时钟。
–utc  若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作。
–version  显示版本信息。

示例:

# 显示硬件时钟
[root@localhost ~]# hwclock
Sun 03 Dec 2017 02:36:39 PM CST -0.664565 seconds
[root@localhost ~]# hwclock -r
Sun 03 Dec 2017 02:36:51 PM CST -0.038219 seconds
[root@localhost ~]# hwclock –show
Sun 03 Dec 2017 02:37:29 PM CST -0.085739 seconds

#修改日期并同步到硬件时钟
[root@localhost ~]# date
Sun Dec 3 14:37:54 CST 2017
[root@localhost ~]# hwclock -w

date 命令

NAME
date – print or set the system date and time

SYNOPSIS
date [OPTION]… [+FORMAT]

date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

FORMAT

%n : 下一行
%t : 跳格
%H : 小时(00..23)
%I : 小时(01..12)
%k : 小时(0..23)
%l : 小时(1..12)
%M : 分钟(00..59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00..61)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区 %a : 星期几 (Sun..Sat)
%A : 星期几 (Sunday..Saturday)
%b : 月份 (Jan..Dec)
%B : 月份 (January..December)
%c : 直接显示日期与时间
%d : 日 (01..31)
%D : 直接显示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第几天 (001..366)
%m : 月份 (01..12)
%U : 一年中的第几周 (00..53) (以 Sunday 为一周的第一天的情形)
%w : 一周中的第几天 (0..6)
%W : 一年中的第几周 (00..53) (以 Monday 为一周的第一天的情形)
%x : 直接显示日期 (mm/dd/yy)
%y : 年份的最后两位数字 (00.99)
%Y : 完整年份 (0000..9999)

date -s //设置当前时间,只有root权限才能设置,其他只能查看。
date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
date -s 01:01:01 //设置具体时间,不会对日期做更改
date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
date -s “01:01:01 20080523″ //这样可以设置全部时间
date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
date -s “20080523 01:01:01″ //这样可以设置全部时间

date +%Y%m%d         //显示现在天年月日
date +%Y%m%d –date=”+1 day”  //显示后一天的日期
date +%Y%m%d –date=”-1 day”  //显示前一天的日期
date +%Y%m%d –date=”-1 month”  //显示上一月的日期
date +%Y%m%d –date=”+1 month”  //显示下一月的日期
date +%Y%m%d –date=”-1 year”  //显示前一年的日期
date +%Y%m%d –date=”+1 year”  //显示下一年的日期

示例:

#显示现在的时间

[root@localhost ~]# date “+It’s %Y-%m-%d %H:%M:%S now”
It’s 2017-12-03 14:48:20 now

#显示的时间不是当前时间,而是经过运算的时间,则可以用-d选项。例如显示三年前的时间:

[root@localhost ~]# date “+Three yours later is %Y-%m-%d %H:%M:%S ” -d “-3 year”
Three yours later is 2014-12-03 14:51:32

#获取当前时间距离1970年0时0分0秒所经历的秒数

[root@localhost ~]# date “+%s”
1512283980

#用 -s选项可以设置系统时间:

[root@localhost ~]# date -s “20100809 10:10:10”

[root@localhost ~]# date -s “2010-08-09 10:10:10”

[root@localhost ~]# date -s “8/9/2010 10:10:10″

[root@localhost ~]# date -s ” 10:10:10 20100809″

 

 

 

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89398

(0)
上一篇 2017-12-03 14:59
下一篇 2017-12-03 16:20

相关推荐