Linux常用命令汇总

1. 查看当前ip - linux

# ifconfig

查看当前ip - windows

cmd -> ipconfig


2.CRT/XSHELL 链接 Linux

3.文件创建
  vi 和 touch xxx.log 创建一个空的文件

4.文件夹创建
  mkdir xxx  创建1层
  mkdir -p 1/2/3 连续创建3层
  mkdir 4 5 6   同层1下子创建3个目录

5.mv 移动文件或文件夹   原路径是不存在

 

mv aaa.log /shanghai

6.cp 复制文件或者文件夹 原路径是存在的

文件: cp aaa.log sh2.log sh3.log sh4.log /test

7.查看文件的内容
cat 20180427.log 一下子将内容刷新出来
more 20180427.log 一页页的按空格键翻
less 20180427.log

8. tail 实时查看文件内容
tail -f 20180427.log

tail -F 20180427.log  -F = -f -retry

tail -200f install.log.syslog  倒着查看最新200行,且实时

所以一般使用 tail -F

如果要让 tail -F 查看文件测试的话,要用 cat xxx.log  >> xxx.log 不能使用 echo 来输出显示,可能会有BUG无法显示

log4j  10份

xxx.log 系统记录日志10份 
100M就切1次:
mv xxx.log xxx.log1
touch xxx.log

8.

echo "456" > 20180502.log  覆盖

echo "123" >> 20180502.log 追加

9.

输出打印

echo "1234"

10.mv 和 cp谁快?

在同文件系统中mv的速度要比cp快

11.别名
alias
临时: alias
hi='cd /test/shanghai/'

永久: 取决于设置全局还是个人

12.环境变量文件
全局:
/etc/profile  
source /etc/profile  生效

个人:  .bash_profile 、.bashrc
~/.bash_profile
source ~/.bash_profile  生效
. ~/.bash_profile     生效

13.删除
rm xxx.log 删除一个文件,询问

rm -f xxx.log 删除一个文件,不询问,强制删除

rm -r 递归删除

rm -rf xxx  删除文件夹
rm -rf / 不能做
在shell脚本:一定要校验path变量是否等于空
path=""   
rm -rf $path/* ==> rm -rf /*

14.设置变量
path=6 
key=value

15.history !70 查看历史命令和执行第70行

16.用户,用户组的常用命令
[root@hadoop000 ruoze]# 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@hadoop000 ruoze]# 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@hadoop000 ruoze]# 
 

[root@hadoop000 ruoze]# useradd ruoze   自动创建一个用户和用户组,名称一样
[root@hadoop000 ruoze]# id ruoze
uid=501(ruoze) gid=501(ruoze) groups=501(ruoze)
[root@hadoop000 ruoze]# 
gid: 主组
groups:所有组
 

删除
[root@hadoop000 ruoze]# userdel -r ruoze
[root@hadoop000 ruoze]# id ruoze
id: ruoze: No such user
[root@hadoop000 ruoze]# 

再次创建
[root@hadoop000 ruoze]# useradd ruoze
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@hadoop000 ruoze]# 
查看/home/xxx用户名称的文件夹
[root@hadoop000 ruoze]# ll /home/
total 8
drwx------. 4 jepson jepson 4096 May  2 22:14 jepson
drwx------. 4 ruoze  ruoze  4096 May  2 22:29 ruoze
[root@hadoop000 ruoze]# 
用户和用户组的文件
[root@hadoop000 ruoze]# cat /etc/passwd | grep ruoze
ruoze:x:501:501::/home/ruoze:/bin/bash
[root@hadoop000 ruoze]# 
[root@hadoop000 ruoze]# cat /etc/group | grep ruoze
ruoze:x:501:
[root@hadoop000 ruoze]# 
[root@hadoop000 ruoze]# groupadd bigdata
[root@hadoop000 ruoze]# id ruoze
uid=501(ruoze) gid=501(ruoze) groups=501(ruoze)
[root@hadoop000 ruoze]# usermod -a -G bigdata ruoze
[root@hadoop000 ruoze]# id ruoze
uid=501(ruoze) gid=501(ruoze) groups=501(ruoze),502(bigdata)
[root@hadoop000 ruoze]# usermod -g bigdata ruoze
[root@hadoop000 ruoze]# id ruoze
uid=501(ruoze) gid=502(bigdata) groups=502(bigdata)
17.管道符 |
cat /etc/passwd | grep ruoze
18.查看命令帮助
usermod --help
man usermod
19.设置密码
passwd ruoze
20.切换用户
[root@hadoop000 ruoze]# su ruoze
[ruoze@hadoop000 ruoze]$ pwd
/root/ruoze
[ruoze@hadoop000 ruoze]$ 
[root@hadoop000 ruoze]# 
[root@hadoop000 ruoze]# su - ruoze
[ruoze@hadoop000 ~]$ 
- 1.切换用户之后,执行环境变量文件.bash_profile 
  2.且进入该用户的家目录
  hadoop
  su hadoop  su - hadoop
  xxxx       xxxxxx
记住2件事:1.  history 
           2.  查看环境变量文件


21.exit 退出当前用户,返回上一次用户


22.临时获取root的权限 sudo


[jepson@hadoop000 ~]$ rz
-bash: cd: /root/ruoze/6/: Permission denied
[jepson@hadoop000 ~]$ 
[jepson@hadoop000 ~]$ 
[jepson@hadoop000 ~]$ sudo rz
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:


    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.


[sudo] password for jepson: 
jepson is not in the sudoers file.  This incident will be reported.
[jepson@hadoop000 ~]$ 


将一个用户添加到/etc/sudoers文件然后无密码
[root@hadoop000 1]# vi /etc/sudoers
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

 

[jepson@hadoop000 ~]$ ls -l /root
ls: cannot open directory /root: Permission denied
[jepson@hadoop000 ~]$ 
[jepson@hadoop000 ~]$ sudo ls -l /root
total 108
-rw-r--r--. 1 root root     0 May  2 21:12 20180502.log
-rw-------. 1 root root  1382 Apr 28 05:21 anaconda-ks.cfg
drwxr-xr-x. 3 root root  4096 Apr 27 22:37 Desktop
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Documents
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Downloads
-rw-r--r--. 1 root root 49565 Apr 28 05:20 install.log
-rw-r--r--. 1 root root 10033 Apr 28 05:16 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Music
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Pictures
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Public
drwxr-xr-x. 3 root root  4096 May  2 22:20 ruoze
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Templates
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Videos
[jepson@hadoop000 ~]$ 


23.进程


查看
[jepson@hadoop000 ~]$ ps -ef | grep tail
root     24215  2254  0 21:29 pts/1    00:00:00 tail -f 20180502.log
root     26027  2254  0 21:32 pts/1    00:00:00 tail -F 20180502.log
root     26034  2254  0 21:35 pts/1    00:00:00 tail -F 20180502.log
root     26049  2254  0 21:37 pts/1    00:00:00 tail -F 20180502.log


第二列是pid
kill -9 24215
杀死关于tail命令的所有进程之前,给我ps -ef|grep tail查看确认清楚
kill -9 26027 26034  26049
kill -9 $(pgrep -f tail) 杀死关于tail命令所的所有进程


24.端口号


[root@hadoop000 ~]# ps -ef|grep ssh
root      1432     1  0 20:45 ?        00:00:00 /usr/sbin/sshd
root      2248  1432  0 21:09 ?        00:00:01 sshd: root@pts/1,pts/2,pts/3,pts/4
root     26570  2332  0 23:14 pts/2    00:00:00 grep ssh
[root@hadoop000 ~]# netstat -nlp|grep 1432
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1432/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1432/sshd           
[root@hadoop000 ~]# 
 

打开某个xxx服务的web界面: http://ip:端口/
ifconfig
ps -ef|grep xxx -->pid
netstat -nlp|grep pid -->port
netstat -nlp|grep xxx -->port

 

1.权限
[root@hadoop000 ~]# ll
total 108
-rw-r--r--. 1 root root     0 May  2 21:12 20180502.log
-rw-------. 1 root root  1382 Apr 28 05:21 anaconda-ks.cfg
drwxr-xr-x. 3 root root  4096 Apr 27 22:37 Desktop
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Documents
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Downloads
-rw-r--r--. 1 root root 49565 Apr 28 05:20 install.log
-rw-r--r--. 1 root root 10033 Apr 28 05:16 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Music
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Pictures
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Public
drwxr-xr-x. 3 root root  4096 May  2 22:20 ruoze
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Templates
drwxr-xr-x. 2 root root  4096 Apr 27 21:40 Videos
[root@hadoop000 ~]# 
读:     r 4
写:     w 2
执行:   x 1  shell脚本
无权限:-
第一列 drwxr-xr-x
第一个: d文件夹 -文件  l连接 
rwx r-x r-x: 三组
           第一组: rwx 7  代表root用户对这个文件或文件夹的权限 
  第二组: r-x 5  代表root用户组的所有用户对这个文件或文件夹的权限 
           第三组: r-x 5  代表其他组的所有用户对这个文件或文件夹的权限 
第三列: 所属的用户root
第四列:所属的组root
Permission denied  hadoop
修改文件和文件夹的权限
chmod 777 xxx.log  
chmod -R 755 xxxdir
chmod -R 755 xxxdir/*
修改文件和文件夹的所属的用户和用户组
chown jepson:jepson xxx.log
chown -R jepson:jepson xxxdir
chown -R jepson:jepson xxxdir/*
xxx服务 安装目录的 xxxuser:xxxuser
可执行: 针对于shell脚本 
chmod 764 date.sh 只对所属的用户
chmod +x  date.sh 所有的用户


2.yum 


yum --help 命令帮助格式
yum -y install httpd
[root@hadoop000 ~]# netstat -nlp|grep 2525
tcp        0      0 :::80                :::*                        LISTEN      2525/httpd   
:::80  
127.0.0.1:80
0.0.0.0:80
192.168.137.251:80
timeout 去查看服务的端口号对应的ip
ping 192.168.137.251
[root@hadoop000 ~]# yum install telnet
https://jingyan.baidu.com/article/95c9d20d96ba4aec4f756154.html
[root@hadoop000 ~]# which telnet
/usr/bin/telnet
[root@hadoop000 ~]# telnet 192.168.137.251 80
Trying 192.168.137.251...
Connected to 192.168.137.251.
Escape character is '^]'.
^Z^X^C^Z
Connection closed by foreign host.
[root@hadoop000 ~]# 
[root@hadoop000 ~]# 
[root@hadoop000 ~]# 
[root@hadoop000 ~]# telnet 192.168.137.251 808
Trying 192.168.137.251...
telnet: connect to address 192.168.137.251: Connection refused
[root@hadoop000 ~]# 
卸载:
[root@hadoop000 ~]# rpm -qa |grep http
httpd-tools-2.2.15-60.el6.centos.6.x86_64
httpd-2.2.15-60.el6.centos.6.x86_64
[root@hadoop000 ~]# rpm --nodeps -e httpd-2.2.15-60.el6.centos.6.x86_64
[root@hadoop000 ~]# 
拓展: CentOS6.x使用163和epel yum源的选择


http://blog.itpub.net/30089851/viewspace-2130239/

 


3.找命令或者shell脚本


[root@hadoop000 ~]# which java
/usr/bin/java
[root@hadoop000 ~]# locate java
[root@hadoop000 ~]# 


4.搜索


find / -name '*abc*'      全文
find /tmp -name '*abc*'   指定目录
find ./ -name '*hadoop*'  当前目录


5.vi


命令模式: i键进入编辑 或者 shift+: 进入尾行模式
gg 第一行的第一个字符
G  最后一行的第一个字符
shift +$ 行尾
dd 删除当前行
dG 删除光标以下的所有行
ndd 删除光标以下的n行
场景: 清空一个文件
      echo '' > xxx.log
      cat /dev/null > xxx.log
      vi:  1.gg  
           2.dG
编辑模式: esc退出到命令模式
尾行模式: 
     :q  退出
     :q! 强制退出
     :wq 保存退出
     :wq! 强制保持退出
     :/内容  n向下 N向上

 


    行号
    :set nu
    :set nonu
    跳转第N行
    :n
    
6.查看硬盘 内存 系统情况
df -h
free -m
top


7.压缩 tar
Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.
  [root@hadoop000 ~]#    tar -czf ruoze.tar.gz  ruoze/*  压缩
  [root@hadoop000 test]# tar -xzvf ruoze.tar.gz       解压


8.压缩 zip


zip -r ruoze.zip ruoze/*

unzip ruoze.zip

 

1.rz sz
yum install lszrz
rz: window-->linux
sz: linux-->window
rz
sz jepson.log
2.作业调度
crontab 
        -e      (edit user's crontab) 编辑
        -l      (list user's crontab) 查看
[root@hadoop000 ~]# crontab -e
* * * * * /root/date.sh >> /tmp/date.log
格式: * * * * * 命令 
      每1分钟
5月6号 14:37
37 14 06 05 *
第1个: 分    * 或者 */1 表示每分钟    
第2个: 小时  
第3个: 日  
第4个: 月  
第5个: 周  
每10秒:
休眠sleep 10s
[root@hadoop000 ~]# vi date.sh 
#!/bin/bash

 

 

for((i=1;i<=6;i++)); 
do
        date
        sleep 10s
done  
[root@hadoop000 ~]# crontab -e
* * * * * /root/date.sh >> /tmp/date.log
3.绝对路径和相对路径
cd /root/ruoze
cd ruoze
   ./ruoze
4.后台执行命令 不交互
./date.sh &
nohup ./date.sh


生产:
==> nohup ./date.sh &
    nohup ./date.sh > /tmp/date.log 2>&1 &

 


screen  多人合作 后台会话  
[root@hadoop000 ~]# yum install screen
-S sockname   创建一个会话
-r            进入一个detached会话  
-list         查看会话列表
会话状态:
1.detached 无人
2.Attached 有人
退出会话,会话后台执行: crtl+a+d


[root@hadoop000 ~]# screen -S ruozedata
[root@hadoop000 ~]# screen -list
There is a screen on:
        2380.ruozedata  (Attached)
1 Socket in /var/run/screen/S-root.
[root@hadoop000 ~]# screen -r 2380

 

[root@hadoop000 ~]# screen -r ruozedata

 

补充:

hostname修改

修改主机名:1./etc/sysconfig/network内容:networking=yeshostname=lh //主机名(没有这行?那就添加这一行吧)gateway=192.168.1.12.运行命令:#hostname lh//lh为主机名3./etc/hosts

修改服务器的IP地址:

修改IP可用 netconfig 或 ifconfig eth0 192.168.1.1 255.255.255.0 #不保存 ip add dev eth0 192.168.1.1/24 #不保存或 vi /etc/sysconfig/network-scripts/ifcfg-eth0

service network restart #重启服务
 

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值