Linux中一些常见的命令(中)

Posted by JustDoDT on January 10, 2018

Linux中一些常见的命令(中)

history

[root@hadoop001 ~]# history 
    1  vi /etc/sysconfig/network
    2  hostname 
    3  hostname hadoop001
......
[root@hadoop001 ~]# !48   # 执行第48个命令
source /etc/profile

清空历史命令

echo "" > ~/.bash_history

[root@hadoop001 ~]# echo "" > .bash_history 
[root@hadoop001 ~]# history 
    1  123
    2  alias 123='cd /root/bigdata/'
    3  alias he='cat /root/bigdata/hello.txt20190311'
......

#注意:需要重新打开一个会话才生效
[root@hadoop001 ~]# history 
    1  history 
[root@hadoop001 ~]# cat .bash_history 
[root@hadoop001 ~]# 

用户的常用命令

[root@hadoop001 ~]# ll /usr/sbin/user*
-rwxr-x---. 1 root root 103096 Dec  8  2011 /usr/sbin/useradd
-rwxr-x---. 1 root root  69560 Dec  8  2011 /usr/sbin/userdel
-rws--x--x. 1 root root  42384 Aug 23  2010 /usr/sbin/userhelper
-rwxr-x---. 1 root root  98680 Dec  8  2011 /usr/sbin/usermod
-rwsr-xr-x. 1 root root   9000 Nov 23  2013 /usr/sbin/usernetctl

#用户组的常用命令
[root@hadoop001 ~]# ll /usr/sbin/group*
-rwxr-x---. 1 root root 54968 Dec  8  2011 /usr/sbin/groupadd
-rwxr-x---. 1 root root 46512 Dec  8  2011 /usr/sbin/groupdel
-rwxr-x---. 1 root root 50800 Dec  8  2011 /usr/sbin/groupmems
-rwxr-x---. 1 root root 61360 Dec  8  2011 /usr/sbin/groupmod
[root@hadoop001 ~]# 

#注意:一个人可以有多个用户组,但是必须只有一个主组
[root@hadoop001 home]# useradd haha
[root@hadoop001 home]# ll
total 8
drwx------. 4 haha     haha     4096 Mar 12 22:09 haha
drwx------. 4 justdodt justdodt 4096 Mar 12 19:02 justdodt

[root@hadoop001 home]# id justdodt
uid=500(justdodt) gid=500(justdodt) groups=500(justdodt)
[root@hadoop001 home]# id haha
uid=501(haha) gid=501(haha) groups=501(haha)
#gid 是主组,groups 是所有组


[root@hadoop001 home]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
.
.
.
justdodt:x:500:500:justdodt:/home/justdodt:/bin/bash
haha:x:501:501::/home/haha:/bin/bash
[root@hadoop001 home]# 


[root@hadoop001 home]# cat /etc/group
root:x:0:
bin:x:1:bin,daemon
.
.
.
justdodt:x:500:
haha:x:501:
[root@hadoop001 home]# 

删除用户

[root@hadoop001 ~]# userdel haha
    
[root@hadoop001 ~]# cat /etc/passwd 
root:x:0:0:root:/root:/bin/bash
.
.
.
tcpdump:x:72:72::/:/sbin/nologin
justdodt:x:500:500:justdodt:/home/justdodt:/bin/bash

[root@hadoop001 ~]# cat /etc/group
root:x:0:
bin:x:1:bin,daemon
.
.
.
slocate:x:21:
justdodt:x:500:
[root@hadoop001 ~]# 


[root@hadoop001 ~]# ll /home/
total 8
drwx------. 4      501      501 4096 Mar 12 22:09 haha
drwx------. 4 justdodt justdodt 4096 Mar 12 19:02 justdodt
[root@hadoop001 ~]# 

#注意:**删除用户后,/etc/passwd和/etc/group都没有记录,因为haha该组只有一个用户haha,用户被删除,组校验就他自己,就删除;但是家目录还在。

重新创建用户

[root@hadoop001 ~]# useradd haha
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists

[root@hadoop001 ~]# id haha
uid=501(haha) gid=501(haha) groups=501(haha)

[root@hadoop001 ~]# groupadd bigdata
[root@hadoop001 ~]# id haha
uid=501(haha) gid=501(haha) groups=501(haha)
[root@hadoop001 ~]# usermod -a -G bigdata haha

[root@hadoop001 ~]# id haha
uid=501(haha) gid=501(haha) groups=501(haha),502(bigdata)

[root@hadoop001 ~]# usermod -g bigdata haha
[root@hadoop001 ~]# id haha
uid=501(haha) gid=502(bigdata) groups=502(bigdata)

[root@hadoop001 ~]# usermod -a -G haha haha
[root@hadoop001 ~]# id haha
uid=501(haha) gid=502(bigdata) groups=502(bigdata),501(haha)
[root@hadoop001 ~]# 

设置密码

[root@hadoop001 ~]# passwd haha
Changing password for user haha.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@hadoop001 ~]# 
[root@hadoop001 ~]# 

切换用户

[root@hadoop001 ~]# su - haha
[haha@hadoop001 ~] pwd
/home/haha
[haha@hadoop001 ~] 
[haha@hadoop001 ~] su - justdodt
Password: 
[justdodt@hadoop001 ~] pwd
/home/justdodt

[haha@hadoop001 ~] su haha
Password: 
[haha@hadoop001 ~] su justdodt
Password: 
[justdodt@hadoop001 haha]$ pwd
/home/haha

#注意: -  代表切换用户后,进入到该用户的家目录且执行环境变量文件。


[haha@hadoop001 ~]$ alias 
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[haha@hadoop001 ~]$ su - haha
Password: 
[haha@hadoop001 ~]$ pwd
/home/haha
[haha@hadoop001 ~]$ alias 
alias 123='cd /root/bigdata/'
alias he='cat /root/bigdata/hello.txt20190311'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[haha@hadoop001 ~]$ 

临时获取root最大权限

[haha@hadoop001 ~]$ ll /root/
ls: cannot open directory /root/: Permission denied

[root@hadoop001 ~]# vi /etc/sudoers
 ## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
.
.
.
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
haha    ALL=(root)      NOPASSWD:ALL

[root@hadoop001 ~]# su - haha

[haha@hadoop001 ~]$ ll /root/
ls: cannot open directory /root/: Permission denied
[haha@hadoop001 ~]$ sudo ll /root/
sudo: ll: command not found
[haha@hadoop001 ~]$ sudo ls -l /root/
total 108
-rw-------. 1 root root  1383 Mar 12 20:01 anaconda-ks.cfg
drwxr-xr-x. 3 root root  4096 Mar 12 17:46 bigdata
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Desktop
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Documents
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Downloads
-rw-r--r--. 1 root root 49565 Mar 12 20:01 install.log
-rw-r--r--. 1 root root 10033 Mar 12 19:59 install.log.syslog
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Music
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Pictures
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Public
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Templates
drwxr-xr-x. 2 root root  4096 Mar 12 12:23 Videos
[haha@hadoop001 ~]$ 

管道符,过滤 | grep

[root@hadoop001 ~]# cat /etc/passwd | grep haha
haha:x:501:502::/home/haha:/bin/bash

[root@hadoop001 ~]# cat /etc/passwd | grep haha
haha:x:501:502::/home/haha:/bin/bash

[root@hadoop001 ~]# ps -ef | grep tail
root      31334  30804  0 00:54 pts/7    00:00:00 tail -f /root/bigdata/hello.txt20190311
root      31337  31298  0 00:54 pts/2    00:00:00 grep tail

[root@hadoop001 ~]# ps -ef | grep tail | grep -v grep`
root      31334  30804  0 00:54 pts/7    00:00:00 tail -f /root/bigdata/hello.txt20190311

进程号

[root@hadoop001 ~]# ps -ef | grep tail
root      31334  30804  0 00:54 pts/7    00:00:00 tail -f /root/bigdata/hello.txt20190311
root      31337  31298  0 00:54 pts/2    00:00:00 grep tail

端口号,不是所有的进程号都有端口号

[root@hadoop001 ~]# netstat -nlp | grep 31334

[root@hadoop001 ~]# ps -ef | grep ssh
root       1455      1  0 Mar12 ?        00:00:00 /usr/sbin/sshd
root      30577   1455  0 Mar12 ?        00:00:00 sshd: root@pts/2 
root      30604   1455  0 Mar12 ?        00:00:00 sshd: root@pts/3 
root      30627   1455  0 Mar12 ?        00:00:00 sshd: root@pts/4 
root      30800   1455  0 Mar12 ?        00:00:00 sshd: root@pts/7 
root      31381  31298  0 01:02 pts/2    00:00:00 grep ssh
[root@hadoop001 ~]# 

[root@hadoop001 ~]# netstat -nlp | grep 1455
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1455/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1455/sshd           

杀死进程,kill -9

[root@hadoop001 ~]# ps -ef | grep tail
root      31334  30804  0 00:54 pts/7    00:00:00 tail -f /root/bigdata/hello.txt20190311
root      31396  31298  0 01:04 pts/2    00:00:00 grep tail
[root@hadoop001 ~]# 
[root@hadoop001 ~]# 
[root@hadoop001 ~]# kill -9 $(pgrep -f txt)
[root@hadoop001 ~]# ps -ef | grep tail`
root      31399  31298  0 01:04 pts/2    00:00:00 grep tail
[root@hadoop001 ~]# 

#注意:kill -9 $(pgrep -f txt)  杀死带txt字符的所有进程

搜索 history ,find

[root@hadoop001 ~]# history | grep ps
    27  ps -ef | grep tail
    28  ps -ef | grep tail | grep -v grep
    29  ps -ef | grep 'log'
    30  ps -ef | grep tail | grep -v grep
    32  ps -ef | grep ssh
    35  ps -ef | grep tail
    37  ps -ef | grep tail
    39  ps -ef | grep tail
    40  history | grep ps

 [root@hadoop001 ~]# find / -name 'bigdata'
 /root/bigdata

模糊匹配

[root@hadoop001 ~]# find / -name 'bigd*'
/root/bigdata

安装rpm 软件包

[root@hadoop001 ~]# yum search http  # 首先搜索

[root@hadoop001 ~]# yum install -y httpd

卸载rpm 软件包 rpm -e , rpm -e –nodeps 不检查依赖卸载

[root@hadoop001 ~]# rpm -qa|grep httpd  #首先查看安装的软件包
httpd-2.2.15-69.el6.centos.x86_64
httpd-tools-2.2.15-69.el6.centos.x86_64

[root@hadoop001 ~]# rpm -e httpd-2.2.15-69.el6.centos.x86_64
error: Failed dependencies:
        httpd >= 2.2.0 is needed by (installed) gnome-user-share-2.28.2-3.el6.x86_64

[root@hadoop001 ~]# rpm -e --nodeps httpd-2.2.15-69.el6.centos.x86_64
warning: /etc/httpd/conf/httpd.conf saved as /etc/httpd/conf/httpd.conf.rpmsave

which ,去$PATH中找

[root@hadoop001 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[root@hadoop001 ~]# which is
/usr/bin/which: no is in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

--help 和 man 命令都是查看命令帮助,但是man更加详细。(man命令的戏称,有事找男人)