Shell笔记系列六

ifconfig

用于配置及显示网络接口,子网掩码等细节信息,它通常位于/sbin/ifconfig中。

[land@localhost ~]$ ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.192.191  netmask 255.255.255.0  broadcast 192.168.192.255
        inet6 fe80::c165:577c:9253:847d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:37:5d:63  txqueuelen 1000  (Ethernet)
        RX packets 88  bytes 14165 (13.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 89  bytes 10542 (10.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 68  bytes 5920 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5920 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens32为网卡的名字,inet为IPV4地址192.168.192.191,网关255,255,255,255,广播地址为192.168.192.255.

设置网络接口的IP地址,注意执行此操作需要使用root权限。也可以使用netmask设置子网掩码

[land@localhost ~]$ ifconfig virbr0 192.168.122.2
SIOCSIFADDR: Operation not permitted
SIOCSIFFLAGS: Operation not permitted

域名服务

将IP地址映射为符号名称叫做域名服务,名字服务器是在文件/etc/resolv.conf中定义:

[land@localhost ~]$ cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.192.2

添加名字服务器使用以下命令

sudo echo nameserver IP_ADDRESS >> /etc/resolv.conf

或者直接修改resolv.conf文件即可。

host

用于列出某个域名对应的所有IP地址,(nslookup)命令也可以完成名字与IP地址之间的相互映射。

[land@localhost ~]$ host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 61.135.169.125
www.a.shifen.com has address 61.135.169.121
[land@localhost ~]$ 

 也可以在/etc/hosts中加入条目来实现名字解析,格式为:

IP_ADDRESS name1 name2

route

显示路由表的信息,路由表记录着信息从哪去哪

[land@localhost ~]$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens32
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.192.0   0.0.0.0         255.255.255.0   U     100    0        0 ens32

ping

用于检查网络上主机之间的连通性,ping命令使用Internet控制消息协议(ICMP)中的echo分组检查网络上两台主机之间的连通性。

检查某台主机是否可达:ping  ADDRESS,默认情况下,ping会连续发送分组,回应信息将被打印在终端上,可用Ctrl+C来停止ping命令。

[land@localhost ~]$ ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=18.2 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=2.68 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=7.97 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=2.48 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=128 time=3.66 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4016ms
rtt min/avg/max/mdev = 2.480/7.020/18.294/5.978 ms

ping命令信息解释:

64 bytes即返回的数据,61.135.169.121即目标主机,icmp_seq即序列号,ping发出的每个分组都有一个序列号,从1开始,指导ping结束。ttl(Time To Live)生存时间,ping命令发送的每个分组都有一个可以在被丢弃前完成的条数,这个值是预先定义好的,分组途径的每个路由器会将该值减1,表明发出ping命令的主机和目的主机之间相隔了多少个路由器。

-c 选线可以指定发送的分组数量

traceroute

用于显示分组途径的所有网关的地址,

[land@localhost ~]$ traceroute google.com
traceroute to google.com (216.58.197.110), 30 hops max, 60 byte packets
 1  gateway (192.168.192.2)  0.121 ms  0.158 ms  0.075 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *

ssh

SSH代表的是Secure Shell,使用加密隧道连接两台计算机,ssh能够让你访问远程计算机的shell,从而在其上执行交互命令并接受结果,或启动交互会话。GNU/Linux发布版中默认不包含SSH,需要自己安装,SSH服务运行在端口22上。

[land@localhost ~]$ ssh land@192.168.192.134 
The authenticity of host '192.168.192.134 (192.168.192.134)' can't be established.
ECDSA key fingerprint is SHA256:/yJdpunmxQXFzsHp4Yxh38BnVTIxl5g*************.
ECDSA key fingerprint is MD5:f2:11:b7:fc:d4:ea:c9:d8:30**********************.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.192.134' (ECDSA) to the list of known hosts.
land@192.168.192.134's password: 
Last login: Wed Dec 12 20:06:57 2018

也可以不登录,直接执行相应的命令,并将结果返回给终端

[land@localhost ~]$ ssh land@192.168.192.134 'ls'
land@192.168.192.134's password: 
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
test
Videos

ftp/sftp

ftp(file transfer Protocol)文件传输协议,用于文件共享,FTP服务器通常运行在21端口,远程主机上必须安装并运行ftp服务器才能使用ftp.sftp是一个运行在SSH连接之上并模拟FTP接口的文件传输胸,它不需要远端运行ftp服务器来进行文件传输,但必须有SSH服务器。


[land@localhost ~]$ sftp land@192.168.192.134
land@192.168.192.134's password: 
Connected to 192.168.192.134.
sftp> dir
Desktop    Documents  Downloads  Music      Pictures   Public     Templates  Videos     test       
sftp> 

SCP

SCP是Secure Copy Program 安全复制程序,scp是一个安全的文件复制命令,文件通过SSH加密通道进行传输。

[land@localhost test]$ scp hello.txt land@192.168.192.134:/home/land/test/
land@192.168.192.134's password: 
hello.txt                                                                                                 100%   16    15.2KB/s   00:00   

如果想要复制目录,则可以使用-r选项,在两台网络主机间以递归形式复制目录

scp -r /home/usernameuser@remotehost:/home/land/test

实现SSH的免密码自动登录

ssh的使用过程中,通常使用用户名和密码进行认证,在终端进行交互式访问可以,但是如果在脚本中使用ssh,则输入用户名和密码就比较麻烦,所以可以进行配置,实现免密码登录。

ssh使用了非对称加密技术,即使用公钥和私钥,公钥分配给网络中的指定范围的用户,私钥则自身保存,例如:当B想和A通信,B使用A的公钥,对信息进行加密,并将信息发送给A,如果信息被C截获,但是因为C没有A的私钥,所以不能对信息进行解密,只有A收到信息后,才可以对加密的信息使用私钥进行解读。所以,如果想实现免密码登录,则公钥应该放在服务器中(即加入文件~/.ssh/authorized_keys),私钥则应该放入用户所在的客户机器的~/.ssh目录中。其他的配置可在/etc/ssh/sshd_config中进行配置。

使用ssh-keygen命令可以创建一对认证密钥。

实现步骤:

⑴在本地主机上创建SSH密钥

⑵将生成的公钥传给主机并将其加入到文件~/.ssh/autorized_keys中,

[land@localhost test]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/land/.ssh/id_rsa): yes
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in yes.
Your public key has been saved in yes.pub.
The key fingerprint is:
SHA256:CyCe21SVt9vTXRbQSDuU+GsRIUkAuZWEE9rABN34mSw land@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|   .=oooB++o+=*  |
|     ++*.o.oo+.o |
|  . ..+.*. ..o. .|
| . o E *  .  o. o|
|  o . o S  o .oo.|
|   +   . .. oo. .|
|  . .   .   ..   |
|                 |
|                 |
+----[SHA256]-----+

ssh-keygen程序会生成两个文件:~/.ssh/xx.pub 是公钥  ~/.ssh/xxx 是私钥 

-rw-------. 1 land land     1675 Dec 12 21:34 yes
-rw-r--r--. 1 land land      408 Dec 12 21:34 yes.pub

使用命令添加公钥到服务器的~/.ssh/authorized_keys

[land@localhost test]$ ssh user@remote_host "cat >> ~/.ssh/authorized_keys" < xxx.pub

如果私钥没有在~/.ssh目录中,则需要将私钥放入到该目录中。   

lsof

可以列出已打开的文件,选项-i将范围限制在已经打开的网络连接。由于直接输出lsof会出现很多的信息,所以使用管道和head只列出前几个信息。

[root@localhost ~]# lsof | head -5
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND    PID  TID           USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd      1                root  cwd       DIR              253,0       224         64 /
systemd      1                root  rtd       DIR              253,0       224         64 /
systemd      1                root  txt       REG              253,0   1612152   34349141 /usr/lib/systemd/systemd
systemd      1                root  mem       REG              253,0     20112     165272 /usr/lib64/libuuid.so.1.3.0

netstat

也可以用来查看开放端口以及网络流量相关信息。

[root@localhost ~]# netstat -tnp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  

netcat/nc

可以创建用于在TCP/IP网络上传输数据的套接字,我们需要两个套接字,一个负责侦听连接,一个负责发起连接:

设置侦听套接字,端口自定义,但是尽量不要使用特殊的端口

[land@localhost ~]$ nc -l 6666

连接到该套接字,指定需要连接的IP地址,如果是本地机器,则可以换成localhost

[land@localhost ~]$ nc 192.168.192.135 6666

然后就可以在两个终端输入信息并按回车键,消息就会发送给对方。

iptables

如果系统没有安装,则可以手动安装。

如果不想访问特定站点,可以阻止发送到该IP地址的流量

[root@localhost ~]# iptables -A OUTPUT -d 8.8.8.8 -j DROP
[root@localhost ~]# 
[land@localhost ~]$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

阻止发送到特定端口的流量

iptables -A OUTPUT -p tcp -dport 21 -j DROP

参数解读:

-A表明向链中添加一条新的规则,链就是若干规则的集合,OUTPUT链可以控制所有的出站流量,而INPUT链能够控制所有的入站流量,-d指定了所要匹配的分组目的地址,-s指定了分组的源地址,-j指示iptables执行到特定的处理。-p指定适应的协议,-dport指定了对应的端口。

 

 

参考《Linux Shell脚本攻略》

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值