远程拷贝、查看端口、vim常见快捷键、查找替换命令、grep命令、查看存储空间的命令、chkconfig命令、系统自动启动级别、主机名配置、IP地址配置、域名映射、防火墙设置...

    xiaoxiao2023-08-02  156

    2.1.远程拷贝

    (将/export/servers/hadoop上的文件拷贝到bigdate@192.168.1.1:/export/servers/

    scp –r /export/servers/hadoop bigdate@192.168.1.1:/export/servers/

     上面的意思是说将 /export/servers/hadoop 拷贝到192.168.1.1这台服务器下的bigdate用户下的/export/servers/这个文件夹下

    2.2.查看端口

    netstat –nltp查看端口号

     

    2.3.vim常见快捷操作

    一些有用的快捷键(在一般模式下使用):

    a  在光标后一位开始插入

    A   在该行的最后插入

    I   在该行的最前面插入

    gg   直接跳到文件的首行

    G    直接跳到文件的末行

    dd    删除一行

    3dd  删除3行

    yy    复制一行

    3yy  复制3行

    p     粘贴

    u     undo

    ctrl + r   redo

     

    v  进入字符选择模式,选择完成后,按y复制,按p粘贴

    ctrl+v 进入块选择模式,选择完成后,按y复制,按p粘贴

    shift+v 进入行选择模式,选择完成后,按y复制,按p粘贴

     

    2.4.查找并替换

    (在底行命令模式中输入)

    显示行号

    :set nu

    隐藏行号

    :set nonu

    查找关键字

    (在底行命令模式中输入)

    A:显示行号

    :set nu

    B:隐藏行号

    :set nonu

    C:查找关键字

    :/you       ## 效果:查找文件中出现的you,并定位到第一个找到的地方,按n可以定位到下一个匹配位置(按N定位到上一个)

     

    2.5替换操作

    :s/sad/bbb  查找光标所在行的第一个sad,替换为bbb

    :%s/sad/bbb 查找文件中所有sad,替换为bbb

     

    2.6.查找命令

    查找可执行的命令所在的路径:

    which ls

    查找可执行的命令和帮助的位置:

    whereis ls

    从某个文件夹开始查找文件

    find / -name"hadooop*"

    find / -name "hadooop*" -ls

    查找并删除文件

    find / -name "hadooop*" -ok rm {}\;

    find / -name "hadooop*" -exec rm{} \;

    查找用户为hadoop的文件

    find /usr  -user  hadoop -ls

    查找用户为hadoop的文件夹

    find /home -user hadoop -type d -ls

    查找权限为777的文件

    find / -perm -777 -type d -ls

     

    **、显示历史命令

    history

     

    2.7.grep命令

    1/ 基本使用

    查询包含hadoop的行

    grep hadoop /etc/password

    查找所有txt文件中含有aaa的文件

    grep aaa ./*.txt

     

    2/ cut截取以:分割保留第七段

    grep hadoop /etc/passwd | cut -d: -f7

     

    3/ 查询不包含hadoop的行

    grep -v hadoop  /etc/passwd

     

    4/ 正则表达包含hadoop

    grep 'hadoop' /etc/passwd

     

    5/ 正则表达(点代表任意一个字符)

    grep 'h.*p' /etc/passwd

     

    6/ 正则表达以hadoop开头

    grep '^hadoop' /etc/passwd

     

    7/ 正则表达以hadoop结尾

    grep 'hadoop$' /etc/passwd

    8、查找指定目录下的所有文件中含有特殊字符串的命令

    grep -rl "正在登录" /usr/cms5/UCMSServer/

     

    正则表达式的简单规则:

    .  : 任意一个字符

    a* : 任意多个a(零个或多个a)

    a? : 零个或一个a

    a+ : 一个或多个a

    .* : 任意多个任意字符

    \. : 转义.

    o\{2\} : o重复两次

     

    查找不是以#开头的行

    grep -v '^#' a.txt | grep -v '^$'

     

    以h或r开头的

    grep '^[hr]' /etc/passwd

     

    不是以h和r开头的

    grep '^[^hr]' /etc/passwd

     

    不是以h到r开头的

    grep '^[^h-r]' /etc/passwd

     

    注意:grep搜索关键词的时候会把自己也搜索出来,对比以下两种写法

    [root@localhost ~]# ps -ef | grep sixunhuan

    root       2857   2465 30 02:41 pts/0    00:00:07 sh sixunhuan.sh

    root       2874   2858  0 02:42 pts/1    00:00:00 grep sixunhuan

    [root@localhost ~]# ps -ef | grep sixunhuan | grep -v grep

    root       2857   2465 34 02:41 pts/0    00:00:25 sh sixunhuan.sh

    [root@localhost ~]# kill -9 2857

     

    2.8.存储空间查看,统计文件或文件夹的大小

    df –h 存储空间查看

    du -sh /mnt/cdrom/packages    #统计指定路径下的所有子目录和文件的大小

    df –h 查看磁盘的剩余空间

    du -h --max-depth=1 xxx     文件夹大小查看

     

    系统服务管理

    service --status-all   # 查看系统所有的后台服务进程

    service sshd status   # 查看指定的后台服务进程的状态

    service sshd stop    # 停止sshd服务

    service sshd start    # 开启sshd服务

    service sshd restart  # 重启sshd服务

     

    2.9.配置后台服务进程的开机自启(chkconfig)

    chkconfig httpd on  ## httpd服务开机自启

    chkconfig httpd off  ## httpd服务开机不要自启

    [root@localhost ~]# chkconfig httpd off

    [root@localhost ~]# chkconfig --list | grep httpd

    httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

    [root@localhost ~]# chkconfig --level 35 httpd on

    [root@localhost ~]# chkconfig --list | grep httpd

    httpd           0:off   1:off   2:off   3:on    4:off   5:on    6:off

     

    2.10.系统启动级别管理

    vi /etc/inittab

     

    # inittab is only used by upstart for the default runlevel.

    #

    # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

    #

    # System initialization is started by /etc/init/rcS.conf

    #

    # Individual runlevels are started by /etc/init/rc.conf

    #

    # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf

    #

    # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,

    # with configuration in /etc/sysconfig/init.

    #

    # For information on how to write upstart event handlers, or how

    # upstart works, see init(5), init(8), and initctl(8).

    #

    # Default runlevel. The runlevels used are:

    #   0 - halt (Do NOT set initdefault to this)

    #   1 - Single user mode

    #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

    #   3 - Full multiuser mode   ##没有图形界面的全功能的多用户的启动级别

    #   4 - unused

    #   5 - X11                ##有图形界面的启动级别

    #   6 - reboot (Do NOT set initdefault to this)

    #

    id:3:initdefault:             ##配置默认启动级别

     

    ## 通常将默认启动级别设置为:3

    2.10.主机名配置

    查看主机名

    hostname

    修改主机名(重启后无效)

    hostname hadoop

    修改主机名(重启后永久生效)

    vi /ect/sysconfig/network

     

    2.11.IP地址配置

    修改ip地址

    方式一:setup

    用root输入setup命令,进入交互式修改界面

     

    方式二:修改配置文件

    (重启后永久生效)

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

     

    方式三:ifconfig命令

    (重启后无效)

    ifconfig eth0 192.168.12.22

     

    2.12.域名映射

    /etc/hosts文件 用于在通过主机名进行访问时做ip地址解析之用

    所以,你想访问一个什么样的主机名,就需要把这个主机名和它对应的ip地址配置在/etc/hosts文件中

     

    2.13.系统中网络进程的端口监听情况

    netstat -nltp

     

    2.14.防火墙配置

    防火墙根据配置文件/etc/sysconfig/iptables来控制本机的“出、入”网络访问行为

    其对行为的配置策略有四个策略表

     

    查看防火墙状态

    service iptables status

    关闭防火墙

    service iptables stop

    启动防火墙

    service iptables start

    禁止防火墙自启

    chkconfig iptables off

     

    1、列出iptables规则

    iptables -L -n

    列出iptables规则并显示规则编号

    iptables -L -n --line-numbers

     

    2、列出iptables nat表规则(默认是filter表)

    iptables -L -n -t nat

     

    3、清除默认规则(注意默认是filter表,如果对nat表操作要加-t nat)

    #清除所有规则

    iptables -F

     

    #重启iptables发现规则依然存在,因为没有保存

    service iptables restart

     

    #保存配置

    service iptables save

     

    4、禁止ssh登陆(若果服务器在机房,一定要小心)

    iptables -A INPUT -p tcp--dport 22 -j DROP

     

    #删除规则

    iptables -D INPUT -p tcp --dport 22 -j DROP

     

    加入一条INPUT规则开放80端口

    iptables -I INPUT -p tcp  --dport 80 -j ACCEPT

     

    加入一条NAT规则

    iptables -t nat -A PREROUTING -d119.254.228.20 -p tcp -m tcp --dport 8022 -j DNAT --to-destination172.16.0.111:22

     

    删除一条NAT规则

    iptables -t nat -D PREROUTING 6

     

    其他iptables的相关命令:

    #查看帮助

    iptables -h

    man iptables

     

    列出iptables规则

    iptables -L -n

    列出iptables规则并显示规则编号

    iptables -L -n --line-numbers

     

    列出iptables nat表规则(默认是filter表)

    iptables -L -n -t nat

     

    清除默认规则(注意默认是filter表,如果对nat表操作要加-t nat)

    #清楚所有规则

    iptables -F

     

    #重启iptables发现规则依然存在,因为没有保存

    service iptables restart

     

    #保存配置

    service iptables save

     

    #禁止ssh登陆(若果服务器在机房,一定要小心)

    iptables -A INPUT -p tcp --dport 22 -j DROP

    #删除规则

    iptables -D INPUT -p tcp --dport 22 -j DROP

     

    iptables -t nat -D PREROUTING 6

     

     

    -A, --append chain   追加到规则的最后一条

    -D, --delete chain [rulenum]    Delete rule rulenum (1 = first) from chain

    -I, --insert chain [rulenum]       Insert in chain as rulenum (default 1=first) 添加到规则的第一条

    -p, --proto  proto   protocol: by number or name, eg. 'tcp',常用协议有tcp、udp、icmp、all

    -j, --jump target 常见的行为有ACCEPT、DROP和REJECT三种,但一般不用REJECT,会带来安全隐患

     

    注意:INPUT和DROP这样的关键字需要大写

     

    #禁止192.168.33.0网段从eth0网卡接入

    iptables -A INPUT -p tcp -i eth0 -s 192.168.33.0 -j DROP

    iptables -A INPUT -p tcp --dport 22 -i eth0 -s 192.168.33.61  -j ACCEPT

     

    #禁止ip地址非192.168.10.10的所有类型数据接入

    iptables -A INPUT ! -s 192.168.10.10 -j DROP

     

    #禁止ip地址非192.168.10.10的ping请求

    iptables -I INPUT -p icmp --icmp-type 8 -s 192.168.50.100 -j DROP

     

    #扩展匹配:1.隐式扩展 2.显示扩展

             #隐式扩展

             -p tcp

                       --sport PORT 源端口

                       --dport PORT 目标端口

     

             #显示扩展:使用额外的匹配规则

             -m EXTENSTION --SUB-OPT

             -p tcp --dport 22 与 -p tcp -m tcp --dport 22功能相同

     

             state:状态扩展,接口ip_contrack追踪会话状态

                       NEW:新的连接请求

                       ESTABLISHED:已建立的连接请求

                       INVALID:非法连接

                       RELATED:相关联的连接

            

     

    #匹配端口范围

    iptables -I INPUT -p tcp --dport 22:80 -j DROP

     

    #匹配多个端口

    iptables -I INPUT -p tcp -m multiport --dport 22,80,3306 -j ACCEPT

     

    #不允许源端口为80的数据流出

    iptables -I OUTPUT -p tcp --sport 80 -j DROP

    最新回复(0)