计算机基础8

网络配置

为网卡配置ip信息

[root@bogon ~]# ifconfig eno33 192.168.16.33/24

  • 添加删除网关
    [root@bogon ~]# route add default gw 192.168.16.1 netmask 255.255.255.0
    [root@bogon ~]# route del default gw 192.168.16.254 netmask 255.255.255.0

  • 查看网关信息
    [root@bogon ~]# route -n
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    0.0.0.0 192.168.16.254 0.0.0.0 UG 100 0 0 ens33
    192.168.16.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

以上均为临时修改,systemctl restart network之后就不会生效

/etc/systemconfig/network
[root@bogon network-scripts]# vim ifcfg-ens33

TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=b83e5525-967c-4721-a12b-e875fd4d30c5
DEVICE=ens33
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes

  • 本地解析规则
    /etc/hosts

[root@bogon etc]# vim hosts
[root@bogon etc]# ping localhost2
PING localhost2 (211.98.71.195) 56(84) bytes of data.
64 bytes from 211.98.71.195 (211.98.71.195): icmp_seq=1 ttl=249 time=13.0 ms
64 bytes from 211.98.71.195 (211.98.71.195): icmp_seq=2 ttl=249 time=8.11 ms
64 bytes from 211.98.71.195 (211.98.71.195): icmp_seq=3 ttl=249 time=2.65 ms
^C
--- localhost2 ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 5983ms
rtt min/avg/max/mdev = 2.658/7.940/13.048/4.244 ms

外部dns
vim /etc/resolv.conf

基于网络(ssh)的远程拷贝
[root@bogon etc]# scp /etc/host 192.168.16.32:/tmp
[root@bogon etc]# scp 192.168.16.32:/tmp/host .

客户端访问服务端无需密码
ssh-keygen #生成钥匙
[root@bogon etc]# ls /root/.ssh/
id_rsa id_rsa.pub
[root@bogon etc]# ssh-copy-id -i 192.168.16.151 #发送给客户端

ssh配置文件
[root@bogon etc]# vim /etc/ssh/sshd_config

shell

流程控制

  • if
  • while
  • for

函数

function test(){
#   read -p 'please input your hostname';
    hostnamectl set-hostname www;
    hostname;
     }

元字符

>
$ 取值;字符串展开
'<''>'' 输入输出重定向
% 取余运算
^ 取反
& 后台执行
&& ‘与’运算
|| ‘或’运算
| 管道

  • 乘法运算
    ! 取非运算;匹配历史命令;匹配最近的一次历史命令
    >& 合并2和1输出
    , 枚举分隔符
    . source ; 当前目录
    / 目录分隔符
    ? 单个字符
    回车 命令执行
  • 通配符:任意字符
    ? 通配符:任一字符
    [abc] 列表项之一
    [^abc] 对列表取非 也可以使用范围 [a-z] 代表aAbBcC…,[0-9]代表012345。。。

{} 循环列表

[root@MiWiFi-R3-srv test]# touch {1..3}{a..d}.txt
[root@MiWiFi-R3-srv test]# ls
1a.txt 1b.txt 1c.txt 1d.txt 2a.txt 2b.txt 2c.txt 2d.txt 3a.txt 3b.txt 3c.txt 3d.txt

作业

作业一:完成作业未做完的集群架构

作业二:临时配置网络(ip,网关,dns)+永久配置

[root@scott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.16.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@scott ~]# route add default gw 192.168.16.1 netmask 255.255.255.0
[root@scott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.16.1    255.255.255.0   UG    0      0        0 ens33
0.0.0.0         192.168.16.254  0.0.0.0         UG    100    0        0 ens33
192.168.16.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.16.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@scott ~]# route del default gw 192.168.16.1 netmask 255.255.255.0
[root@scott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.16.254  0.0.0.0         UG    100    0        0 ens33
192.168.16.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.16.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@scott network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.16.33
NETMASK=255.255.255.0
GATEWAY=192.168.16.254
DNS1=192.168.16.254
DNS2=202.106.0.20
DNS3=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=no
IPV6_DEFROUTE=no
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=b83e5525-967c-4721-a12b-e875fd4d30c5
DEVICE=ens33
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

作业三:为集群内的机器设定主机名,利用/etc/hosts文件来解析自己的集群中所有的主机名,相应的,集群的配置应该改成使用主机名的方式

作业四:ssh登录,scp上传、下载,ssh秘钥登录,修改ssh server端的端口为8888然后进行登录和scp测试

[root@scott test]# scp /etc/hosts 192.168.16.174:/etc/hosts
The authenticity of host '192.168.16.174 (192.168.16.174)' can't be established.
ECDSA key fingerprint is 2b:03:aa:2d:47:b9:f7:20:6d:6a:58:51:49:a5:0e:8f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.16.174' (ECDSA) to the list of known hosts.
root@192.168.16.174's password:
hosts                                       100%  201     0.2KB/s   00:00  
[root@scott ~]# ping host1
PING host1 (192.168.16.173) 56(84) bytes of data.
64 bytes from host1 (192.168.16.173): icmp_seq=1 ttl=64 time=0.504 ms
64 bytes from host1 (192.168.16.173): icmp_seq=2 ttl=64 time=0.247 ms
64 bytes from host1 (192.168.16.173): icmp_seq=3 ttl=64 time=0.240 ms
64 bytes from host1 (192.168.16.173): icmp_seq=4 ttl=64 time=0.475 ms
^C
--- host1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.240/0.366/0.504/0.124 ms
[root@scott ~]# ssh 192.168.16.173 -p 8080
The authenticity of host '[192.168.16.173]:8080 ([192.168.16.173]:8080)' can't be established.

vim /etc/ssh/ssh_config

#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
Port 8080
#   Protocol 2,1
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
#
# Uncomment this if you want to use .local domain
# Host *.local

作业五:整理bash命令类型,验证寻找一个命令的优先级

优先级:

1.alias--别名

用法
alias xx='command' #定义xx 为command
unalias xx #取消xx的定义

[root@scott test]# alias ls
alias ls='ls --color=auto'
[root@scott test]# alias cd
-bash: alias: cd: 未找到

2. Compound Commands--复合命令

  • 老师所说的compound commands就是if while for
    所以验证:
[root@scott test]# alias if='ls -l'
[root@scott test]# if
总用量 8
drwxr-xr-x. 5 root root  33 3月  14 11:26 a
drwxr-xr-x. 4 root root  24 3月  14 11:26 b
drwxr-xr-x. 3 root root  15 3月  14 11:28 c
drwxr-xr-x. 3 root root  67 3月  21 18:57 dir
dr--------. 2 root root  20 3月  14 15:54 file1
drwxr-xr-x. 3 root root  47 3月  20 17:16 mysite
drwxr-xr-x. 2 root root   6 3月  14 14:10 t
-rw-r--r--. 1 root root 327 3月  21 19:33 testfun_1.sh
-rw-r--r--. 1 root root  93 3月  21 19:37 testfun_2.sh
drwxr-xr-x. 2 root root   6 3月  14 14:10 u
drwxr-xr-x. 2 root root   6 3月  14 14:10 y

if语句被定义为'ls -l',所以优先级低于alias

查看复合命令

[root@scott test]# hostname;date;who
scott
2017年 03月 21日 星期二 18:56:07 CST
(unknown) :0           2017-03-21 16:24 (:0)
root     tty2         2017-03-21 16:34
root     pts/0        2017-03-21 16:49 (192.168.16.17)
root     pts/1        2017-03-21 17:59 (web1)
root     pts/2        2017-03-21 18:33 (web1)

将复合命令结果输入到文本中保存
hostname;date;who > /test/dir/multicom.txt

[root@scott test]# cat /test/dir/multicom.txt
(unknown) :0           2017-03-21 16:24 (:0)
root     tty2         2017-03-21 16:34
root     pts/0        2017-03-21 16:49 (192.168.16.17)
root     pts/1        2017-03-21 17:59 (web1)
root     pts/2        2017-03-21 18:33 (web1)
# 这里我们看到只有最后一条命令的结果被保存了

(hostname;date;who) > /test/dir/multicom.txt

[root@scott test]# cat /test/dir/multicom.txt
scott
2017年 03月 21日 星期二 18:59:08 CST
(unknown) :0           2017-03-21 16:24 (:0)
root     tty2         2017-03-21 16:34
root     pts/0        2017-03-21 16:49 (192.168.16.17)
root     pts/1        2017-03-21 17:59 (web1)
root     pts/2        2017-03-21 18:33 (web1)

这次所有命令的内容都被保存了

  • 为什么使用{}语法(syntax)
    更像是命令组一样地使用
    让你能把括号内的所有命令运行在当前的shell环境中。
[root@scott test]# [ $# -eq 0 ] && { echo "usage: $0 filename";exit 1; }
usage: -bash filename
登出

Connection closed by foreign host.

Disconnected from remote host(192.168.16.173:22) at 19:04:24.

3.function--函数

语法

function functname {
    shell commands
}
functname ( ){
    shell commands
}

function由set命令来找到

[root@scott test]# function fsum( ){ echo 'my func fsum'; }
[root@scott test]# set |grep fsum
fsum ()
    echo 'my func fsum'

4.built-ins--内部命令

不用多说
man builtin

5.hash

6.$PATH

在命令搜索路径PATH下的脚本和可执行文件

作业六:通配符实验

  • 疑问
[root@scott test]# ls
a  b  c  dir  file1  mysite  t  testfun_1.sh  testfun_2.sh  u  y
[root@scott test]# res=`ls`
[root@scott test]# echo $res
a b c dir file1 mysite t testfun_1.sh testfun_2.sh u y

[root@scott test]# res=`ll`
[root@scott test]# echo $res
总用量 8 drwxr-xr-x. 5 root root 33 3月 14 11:26 a drwxr-xr-x. 4 root root 24 3月 14 11:26 b drwxr-xr-x. 3 root root 15 3月 14 11:28 c drwxr-xr-x. 3 root root 67 3月 21 18:57 dir dr--------. 2 root root 20 3月 14 15:54 file1 drwxr-xr-x. 3 root root 47 3月 20 17:16 mysite drwxr-xr-x. 2 root root 6 3月 14 14:10 t -rw-r--r--. 1 root root 327 3月 21 19:33 testfun_1.sh -rw-r--r--. 1 root root 93 3月 21 19:37 testfun_2.sh drwxr-xr-x. 2 root root 6 3月 14 14:10 u drwxr-xr-x. 2 root root 6 3月 14 14:10 y

[root@scott test]# res=`ls |grep *txt`
[root@scott test]# echo $res
[root@scott test]# res=$(echo $(ls))
[root@scott test]# res=$(echo $(ls |grep *.txt))
[root@scott test]# echo $res

[root@scott test]# res=$(echo $(ls))
[root@scott test]# echo $res
a b c dir file1 mysite t testfun_1.sh testfun_2.sh u y

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

posted @ 2017-09-06 22:23  sc0T7_ly  阅读(192)  评论(0编辑  收藏  举报