歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> RHEL7基本命令詳解

RHEL7基本命令詳解

日期:2017/2/28 13:49:35   编辑:Linux教程

Terminal

TTY

TTY是TeleTYpe的一個老縮寫。
Teletypes,或者teletypewriters,原來指的是電傳打字機,是通過串行線用打印機鍵盤通過閱讀和發送信息的東西,和古老的電報機區別並不是很大。
之後,當計算機只能以批處理方式運行時(當時穿孔卡片閱讀器是唯一一種使程序載入運行的方式),電傳打字機成為唯一能夠被使用的“實時”輸入/輸出設備。
最終,電傳打字機被鍵盤和顯示器終端所取代,但在終端或TTY接插的地方,操作系統仍然需要一個程序來監視串行端口。
一個getty“Get TTY”的處理過程是:一個程序監視物理的TTY/終端接口。

PTY

但是假如我們遠程telnet到主機或使用xterm時不也需要一個終端交互麼?是的,這就是虛擬終端pty(pseudo-tty)。

PTS/PTMX

pts(pseudo-terminal slave)是pty的實現方法,和ptmx(pseudo-terminal master)配合使用實現pty(PTS/PTMX結合使用實現PTY)。

實驗:驗證SSH的終端

在圖形界面下打開一個終端,查看當前終端對應的pts號

[root@ linuxidc.com ~]# who am i
root     pts/0        2016-08-07 12:06 (:0)

使用SSH連接本機:

[root@ linuxidc.com ~]# ssh [email protected]
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is 3b:42:8d:2e:84:6f:1e:b9:b6:eb:6d:34:23:b5:f2:57.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Sun Aug  7 12:02:57 2016 from localhost

再次查看pts號:

[root@ linuxidc.com ~]# who am i
root     pts/1        2016-08-07 12:08 (localhost)

可以看出已經不是之前的終端號了,說明啟用了一個新的偽終端
查看進程:

[root@ linuxidc.com ~]# ps -aux | grep pts
root       7767  0.0  0.1 116676  3340 pts/0    Ss   12:06   0:00 /bin/bash
root       7820  0.0  0.2  80420  4208 pts/0    S+   12:08   0:00 ssh [email protected]
root       7821  0.0  0.2 143344  5440 ?        Ss   12:08   0:00 sshd: root@pt/1
root       7824  0.0  0.1 116564  3264 pts/1    Ss   12:08   0:00 -bash
root       7888  0.0  0.0 141576  1672 pts/1    R+   12:11   0:00 ps -aux
root       7889  0.0  0.0 112644   952 pts/1    R+   12:11   0:00 grep --color=auto pts

可以看出通過ssh連接後 其實使用的是pts終端

Shell提示符

對比普通用戶和root用戶的Shell提示符
普通用戶

[courier@ linuxidc.com ~]$

root用戶

[root@ linuxidc.com ~]#

[用戶名@主機名 當前所在目錄]root/普通用戶
$為普通用戶 #為root用戶

Bash Shell 基本語法

使用下面的兩個命令查看所有的解釋器:

[root@ linuxidc.com root]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@ linuxidc.com root]# chsh -l
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

命令的格式:命令字 [選項][參數]
命令字:具體執行的命令
選項:匹配的條件
參數:命令處理的對象
舉例說明:

[root@ linuxidc.com root]# touch example.txt
[root@ linuxidc.com root]# ls
anaconda-ks.cfg  Documents  example.txt           Music     Public     Videos
Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates
[root@ linuxidc.com root]# rm -rf example.txt
[root@ linuxidc.com root]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos

常用基本命令

ls

作用:查看當前目錄下有哪些文件
語法:ls 目錄 如果不加目錄則查看當前目錄

[root@ linuxidc.com root]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
[root@ linuxidc.com ~]# ls ~
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music  

-l 顯示詳細信息(ll命令等同於ls -l)

[root@ linuxidc.com ~]# ls -l
total 8
-rw-------. 1 root root 1545 Aug  7 09:04 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Desktop
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Documents
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Downloads
-rw-------. 1 root root 1638 Aug  7 00:23 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Music
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Pictures
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Public
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Templates
drwxr-xr-x. 2 root root    6 Aug  7 00:23 Videos

-a 顯示所有 包括隱藏文件

[root@ linuxidc.com ~]# ls -a
.                .bashrc    Downloads             Pictures   .viminfo
..               .cache     .esd_auth             Public     .xauthO64wyA
anaconda-ks.cfg  .config    .ICEauthority         .ssh
.bash_history    .cshrc     initial-setup-ks.cfg  .tcshrc
.bash_logout     Desktop    .local                Templates
.bash_profile    Documents  Music                 Videos

-d 查看目錄(不查看裡面的內容)

[root@ linuxidc.com ~]# ls -d
.

pwd命令

作用:查看當前所在目錄的絕對路徑

[root@ linuxidc.com ~]# cd /etc/sysconfig/network-scripts/
[root@ linuxidc.com network-scripts]# pwd
/etc/sysconfig/network-scripts

cd命令

作用:切換目錄
語法:cd 目錄名
不跟目錄名則切換到當前用戶目錄
.表示當前目錄 ..當前目錄的上級目錄

[root@ linuxidc.com network-scripts]# cd
[root@ linuxidc.com ~]# pwd
/root
[root@ linuxidc.com ~]# cd /home
[root@ linuxidc.com home]# pwd
/home
[root@ linuxidc.com home]# cd /etc/sysconfig/network-scripts/
[root@ linuxidc.com network-scripts]# cd .
[root@ linuxidc.com network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@ linuxidc.com network-scripts]# cd ..
[root@ linuxidc.com sysconfig]# pwd
/etc/sysconfig
[root@ linuxidc.com sysconfig]# cd ./../
[root@ linuxidc.com etc]# pwd
/etc

系統時間管理

查看BIOS時間

[root@ linuxidc.com ~]# hwclock
Sun 07 Aug 2016 01:18:25 PM EDT  -0.147639 seconds

查看系統時間

[root@ linuxidc.com ~]# date
Sun Aug  7 13:19:29 EDT 2016

修改時間

[root@ linuxidc.com ~]# date -s 2016-10-1
Sat Oct  1 00:00:00 EDT 2016

格式化時間

[root@ linuxidc.com ~]# date '+%Y-%m-%d %H:%M'
2016-10-01 00:02

命令太多如何獲取幫助

命令字 -h/--help

[root@ linuxidc.com ~]# systemctl --help
[root@ linuxidc.com ~]# systemctl -h

man 命令字

[root@ linuxidc.com ~]# man systemctl

關機命令

shutdown

作用:關機/重啟/定時關機
語法:shutdown [選項]
-r 重啟計算機

[root@ linuxidc.com ~]# shutdown -r
Shutdown scheduled for Mon 2016-08-08 01:38:34 EDT, use 'shutdown -c' to cancel.
[root@ linuxidc.com ~]#
Broadcast message from root@ linuxidc.com (Mon 2016-08-08 01:37:34 EDT):

The system is going down for reboot at Mon 2016-08-08 01:38:34 EDT!
[root@ linuxidc.com ~]# shutdown -now
[root@ linuxidc.com ~]# shutdown -r +10
Shutdown scheduled for Sun 2016-08-07 13:50:56 EDT, use 'shutdown -c' to cancel.
[root@ linuxidc.com ~]#
Broadcast message from root@ linuxidc.com (Sun 2016-08-07 13:40:56 EDT):

The system is going down for reboot at Sun 2016-08-07 13:50:56 EDT!
[root@ linuxidc.com ~]# shutdown -r 01:50
Shutdown scheduled for Mon 2016-08-08 01:50:00 EDT, use 'shutdown -c' to cancel.

-h 關機

[root@ linuxidc.com ~]# shutdown -h +10
Shutdown scheduled for Sun 2016-08-07 13:54:14 EDT, use 'shutdown -c' to cancel.

Broadcast message from root@ linuxidc.com (Sun 2016-08-07 13:44:14 EDT):

The system is going down for power-off at Sun 2016-08-07 13:54:14 EDT!
[root@ linuxidc.com ~]# shutdown -h 01:55
Shutdown scheduled for Mon 2016-08-08 01:55:00 EDT, use 'shutdown -c' to cancel.
[root@ linuxidc.com ~]# shutdown -h now

init 0 關機

[root@ linuxidc.com ~]# init 0

reboot 重啟

[root@ linuxidc.com ~]# reboot

poweroff 關機

[root@ linuxidc.com ~]# poweroff

啟動級別

命令:init
作用:切換系統運行級別
語法:init 0-6

7個啟動級別

0 系統停機模式 系統默認運行級別不能設置為0,否則不能正常啟動機器關閉
1 單用戶模式 root權限,用於系統維護,禁止遠程登陸,類似windows安全模式
2 多用戶模式 沒有NFS網絡支持
3 完整的多用戶文本模式 有NFS網絡支持,登陸後進入控制台命令行模式
4 系統未使用 保留一般不用,在一些特殊情況下可以用它來做一些事情,例如:筆記本電腦的電池用盡時可以切換到該模式做一些設置
5 圖形化模式 登陸後進入圖形GUI模式
6 重啟模式 默認運行級別不能設為6,否則不能正常啟動,執行init 6機器就會重啟

啟動級別設置

RHEL7不再使用/etc/inittab文件進行默認的級別配置
查看inittab文件

[root@ linuxidc.com ~]# vim /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#                 

該文件的注釋中已經說明不再使用inittab進行默認級別的配置
根據該文件注釋,下面使用紅帽7提供的級別進行配置
切換到第3運行級

[root@ linuxidc.com ~]# systemctl isolate multi-user.target
[root@ linuxidc.com ~]# systemctl isolate runlevel3.target

切換到第5運行級

[root@ linuxidc.com ~]# systemctl isolate graphical.target
[root@ linuxidc.com ~]# systemctl isolate runlevel5.target

設置默認的運行級別為第三運行級別

[root@ linuxidc.com ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

設置默認的運行級別為第五運行級別

[root@ linuxidc.com ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

查看當前運行級別

[root@ linuxidc.com ~]# runlevel
3 5

3為上次的運行級別 5為本次 如果系統剛啟動 上次則會顯示為N

查看默認運行級別

[root@ linuxidc.com ~]# systemctl get-default
graphical.target

設置服務器在來電後自動開機

該功能需要BIOS支持

  1. 進入BIOS(一般是在開機後出現主板畫面時Del鍵,部分品牌機可能按F2/F1)
  2. 找到Power Management Setup進入電源設置
  3. 找到Wake Up Event Setup
  4. 將Disabledd更改為Enabled
  5. 再繼續設置時間和日期
  6. 保存並退出

附錄

Linux下不同的顏色代表了不同的文件類型

顏色類型舉例 藍色 目錄 /etc 黑色 文件 /etc/passwd 淺藍色 鏈接 /etc/grub2.cfg 紅色 壓縮包 boot.tar.gz 綠色 可執行文件 /etc/init.d/network 黑底黃字 設備文件 /dev/sda

who、whoami命令 和 who am i 命令的區別 http://www.linuxidc.com/Linux/2008-04/12001.htm
Linux中tty、pty、pts的概念區別 http://www.linuxidc.com/Linux/2006-10/397.htm

Copyright © Linux教程網 All Rights Reserved