您的位置:首页 > 理论基础 > 计算机网络

RHEL6基础之十基本网络设置和ssh服务开启

2013-09-26 14:40 836 查看
主机名
[b]查看[/b]
[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]#
[b]临时修改[/b]
[root@localhost ~]# hostname justin
[root@localhost ~]# hostname
justin
[root@localhost ~]#
[b]永久生效[/b]
[root@localhost ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=justin
host文件
[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4     #建议保留此行
10.15.72.194 justin
ssh服务开启
默认情况下RHEL会安装好ssh服务并且已经配置好,如果无法远程查看配置文件/etc/ssh/sshd_config,找到PermitRootLogin、PermitEmptyPasswords、PasswordAuthentication将其修改为yes,注意前面的#为注释,需要删除掉,以下为正常ssh后的配置。供参考:
[root@localhost ~]# vim /etc/ssh/sshd_config
41 #LoginGraceTime 2m
42 #PermitRootLogin yes
43 #StrictModes yes
64 #PasswordAuthentication yes
65 #PermitEmptyPasswords no
66 PasswordAuthentication yes
67
68 # Change to no to disable s/key passwords
不允许root直接登陆,将#PermitRootLogin yes”前面的“#”去掉,“Yes”改为“No”;如果调整为:PermitRootLogin without-password则可以用root用户访问ssh服务端,但前提是使用公/私钥的方式访问,而不能是password的认证方式.

能够ssh远程登陆,本地无法登陆
[root@localhost ~]# vi /etc/pam.d/login
#session    required     /lib/security/pam_limits.so   #注释该行
[root@localhost ~]#
ssh最大尝试连接次数
如果多次连接服务器失败会提Too many authentication failures for root (code 2)
此时可以换台机器尝试,然后修改/etc/ssh/sshd_config中MaxAuthTries=1值

ssh连接设置超时:
[root@localhost ~]# vim /etc/profile
TMOUT=300             #300秒无任何操作客户端自动断开
HISTIZE=30            #HISTSIZE是系统自动保存的历史命令30条数
[root@localhost ~]# source /etc/profile
[root@localhost ~]#
ssh连接超时问题解决方案:
[root@localhost ~]# vim /etc/ssh/sshd_config
119 #ClientAliveInterval 0     #定义了每隔多少秒给SSH客户端发送一次信号,如果没有就自己加一行;去掉下前面的#,修改为60,数值是秒,
120 #ClientAliveCountMax 3     #指如果发现客户端没有相应,则判断一次超时,这个参数设置允许超时的次数,去掉下面两行前面的#,修改为如3 、5等自定义,0 不允许超时次数
Mac地址修改
[root@localhost network-scripts]# ifconfig eth0|grep HWadd
eth0      Link encap:Ethernet  HWaddr 00:0C:29:81:38:08
[root@localhost network-scripts]# ifconfig eth0 down
[root@localhost network-scripts]#ifconfig eth0 hw ether 00:0C:29:28:28:28
[root@localhost network-scripts]# ifconfig eth0 | grep HWaddr
eth0      Link encap:Ethernet  HWaddr 00:0C:29:28:28:28
[root@localhost network-scripts]# ifconfig eth0 up
IP配置
[b]临时设置[/b]
[root@localhost ~]# ifconfig eth0 10.15.72.194 netmask 255.255.255.0
[b]永久生效[/b]
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eth0   ifdown-isdn    ifup-aliases  ifup-plusb     init.ipv6-global
ifcfg-lo     ifdown-post    ifup-bnep     ifup-post      net.hotplug
ifdown       ifdown-ppp     ifup-eth      ifup-ppp       network-functions
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-routes    network-functions-ipv6
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-sit
ifdown-ippp  ifdown-tunnel  ifup-isdn     ifup-tunnel
ifdown-ipv6  ifup           ifup-plip     ifup-wireless
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0bak   #备份
[root@localhost network-scripts]# vim ifcfg-eth0
DEVICE=eth0             #指出设备名称
NM_CONTROLLED=yes       #network mamager的参数,实时生效,不需要重启
ONBOOT=yes              #设置为yes,开机自动启用网络连接
IPADDR=10.15.72.194   #IP地址
BOOTPROTO=static        #设置为none禁止DHCP,设置为static启用静态IP地址,设置为dhcp开启DHCP服务
NETMASK=255.255.255.0   #子网掩码
DNS1=8.8.8.8            #第一个dns服务器
TYPE=Ethernet           #网络类型为:Ethernet
GATEWAY=10.15.72.254    #设置网关
DNS2=8.8.4.4            #第二个dns服务器
IPV6INIT=no             #禁止IPV6
USERCTL=no              #是否允许非root用户控制该设备,设置为no,只能用root用户更改
HWADDR=00:0C:29:2C:E1:0F   #网卡的Mac地址
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
NAME="System eth0"    #定义设备名称
DEVICE、ONBOO、TBOOTPROTO、IPADDR、NETMASK、GATEWAY必须配置设置网卡后需要重启网卡
[root@localhost ~]# service network restart
正在关闭接口 eth0: 错误:断开设备 'eth0'(/org/freedesktop/NetworkManager/Devices/0)失败:This device is not active
[失败]
正在关闭接口 wlan0: 错误:断开设备 'wlan0'(/org/freedesktop/NetworkManager/Devices/1)失败:This device is not active
[失败]
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0: 错误:未知连接:aa67128b-2d66-4fd8-a346-4e69622bff67
[失败]
[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:81:38:08
inet6 addr: fe80::20c:29ff:fe81:3808/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:9126 errors:1 dropped:0 overruns:0 frame:0
TX packets:810 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:999666 (976.2 KiB)  TX bytes:115751 (113.0 KiB)
Interrupt:19 Base address:0x2000
[root@localhost ~]#
重启网卡提示错误:This device is not active,用ifconfig eth0查看IP地址未生效,出现该故障的原因是启动网络的两个服务有冲突:/etc/init.d/network 和 /etc/init.d/NetworkManager这两个服务。只需把NetworkManager重启一下就好或者关闭,报错RTNETLINK answers: File exists错误也是因为NetworkManager导致的
[root@localhost ~]# service NetworkManager restart
停止 NetworkManager 守护进程:                             [确定]
设置网络参数...                                            [确定]
正在启动 NetworkManager 守护进程:                         [确定]
[root@localhost ~]# /etc/init.d/network stop
[root@localhost ~]# chkconfig --level 345 NetworkManager off
[root@localhost ~]# /etc/init.d/network restart
正在关闭接口 eth0: 设备状态:3 (断开连接)            [确定]
正在关闭接口 wlan0: 设备状态:3 (断开连接)        [确定]
关闭环回接口:         [确定]
弹出环回接口:          [确定]
弹出界面 eth0: 错误:未知连接:aa67128b-2d66-4fd8-a346-4e69622bff67         [失败]
[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:81:38:08
inet addr:10.15.72.194  Bcast:10.15.72.255  Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe81:3808/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:11620 errors:2 dropped:0 overruns:0 frame:0
TX packets:964 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1271085 (1.2 MiB)  TX bytes:138181 (134.9 KiB)
Interrupt:19 Base address:0x2000
[root@localhost ~]#
关闭NetworkManager后重启网络,出现新的错误提示: eth0: 错误:未知连接:aa67128b-2d66-4fd8-a346-4e69622bff67 [失败] 这个问题出现在虚拟化的操作系统里,只需把/etc/sysconfig/network-scripts/ifcfg-eth0配置文件中的UUID="aa67128b-2d66-4fd8-a346-4e69622bff67"注释掉,删掉也可以,因为当前网卡设备UUID和配置文件里的这个UUID不匹配。例如之前删除过网卡后重新添加了网卡倒着配置文件里的UUID还是之前的网卡UUIDeth1: Error, some other host already uses address 10.0.0.233.
[root@localhost libexec]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:50:56:96:37:60
inet6 addr: fe80::250:56ff:fe96:3760/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:57363 errors:0 dropped:0 overruns:0 frame:0
TX packets:29 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5584220 (5.3 MiB)  TX bytes:2150 (2.0 KiB)
[root@localhost libexec]# service network restart
Shutting down interface eth1:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth1:  Error, some other host already uses address 10.0.0.233.
[FAILED]
[root@localhost libexec]# vim /etc/sysconfig/network-scripts/ifcfg-eth
#if ! arping -q -c 2 -w 3 -D -I ${REALDEVICE} ${IPADDR} ; then
# net_log $"Error, some other host already uses address ${IPADDR}."
# exit 1
#fi
[root@localhost libexec]#
注释掉/etc/sysconfig/network-scripts/ifcfg-eth文件里以上4行
[b]单网卡设置双IP[/b]
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
DEVICE=eth0:0
NM_CONTROLLED=yes
ONBOOT=yes
IPADDR=10.15.72.195
BOOTPROTO=static
NETMASK=255.255.255.0
DNS1=8.8.8.8
TYPE=Ethernet
GATEWAY=10.15.72.254
DNS2=8.8.4.4
IPV6INIT=no
USERCTL=no
HWADDR=00:0C:29:2C:E1:0F
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
NAME="System eth0:0"
[b]双网卡双IP
[/b]
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
HWADDR=00:50:56:8b:7d:67
IPADDR=10.15.24.222
DNS1=8.8.8.8
NETMASK=255.255.255.0
DNS2=202.96.209.133
GATEWAY=10.15.24.254
[root@localhost network-scripts]# cat ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=10.10.2.59
NETMASK=255.255.255.0
[root@localhost network-scripts]#
这里第二块网卡不设置网关(网关地址是10.10.2.254),然后添加静态路由,添加静态路由后可能只有一个IP是通的,需要做如下设置

Centos7 以前
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/eth1/arp_ignore
[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/eth1/arp_announce

拥挤有效修改/etc/sysctl.conf
arp_ignore:定义对目标地址为本地IP的ARP询问不同的应答模式0
0 - (默认值): 回应任何网络接口上对任何本地IP地址的arp查询请求
1 - 只回答目标IP地址是来访网络接口本地地址的ARP查询请求
2 -只回答目标IP地址是来访网络接口本地地址的ARP查询请求,且来访IP必须在该网络接口的子网段内
3 - 不回应该网络界面的arp请求,而只对设置的唯一和连接地址做出回应
4-7 - 保留未使用
8 -不回应所有(本地地址)的arp查询

arp_announce:对网络接口上,本地IP地址的发出的,ARP回应,作出相应级别的限制: 确定不同程度的限制,宣布对来自本地源IP地址发出Arp请求的接口
0 - (默认) 在任意网络接口(eth0,eth1,lo)上的任何本地地址
1 -尽量避免不在该网络接口子网段的本地地址做出arp回应. 当发起ARP请求的源IP地址是被设置应该经由路由达到此网络接口的时候很有用.此时会检查来访IP是否为所有接口上的子网段内ip之一.如果改来访IP不属于各个网络接口上的子网段内,那么将采用级别2的方式来进行处理.
2 - 对查询目标使用最适当的本地地址.在此模式下将忽略这个IP数据包的源地址并尝试选择与能与该地址通信的本地地址.首要是选择所有的网络接口的子网中外出访问子网中包含该目标IP地址的本地地址. 如果没有合适的地址被发现,将选择当前的发送网络接口或其他的有可能接受到该ARP回应的网络接口来进行发送.

Centos 7
[root@localhost ~]# cat /etc/sysctl.conf
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.eno33554960.rp_filter = 0
net.ipv4.conf.eno50332184.rp_filter = 0
net.ipv4.conf.eno16777728.rp_filter = 0
net.ipv4.conf.ens36.rp_filter = 0
[root@localhost ~]#


[b]添加、删除临时路由(重启机器失效)[/b]
[root@localhost ~]# ip route add 192.168.0.0/16 via 10.10.2.254
[root@localhost ~]# ip route add 10.17.0.0.0/16 dev eth1
[root@localhost ~]# ip route add 10.15.0.0 gw 255.255.255.0 gw 10.10.2.254
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     10.10.2.254    255.255.255.0   UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         10.15.24.254    0.0.0.0         UG    0      0        0 eth0
....
[root@localhost ~]# ip route del 192.168.0.0/24 via 10.10.2.254

方法一:                                              方法二:
添加路由                                              添加路由
route add -net 192.168.0.0/24 gw 192.168.0.1          ip route add 192.168.0.0/24 via 192.168.0.1
route add -host 192.168.1.1 dev eth1                  ip route add 192.168.1.1 dev eth1
删除路由                                              删除路由
route del -net 192.168.0.0/24 gw 192.168.0.1          ip route del 192.168.0.0/24 via 192.168.0.1

add 增加路由                                         add 增加路由
del 删除路由                                         del 删除路由
-net 设置到某个网段的路由                            via 网关出口 IP地址
-host 设置到某台主机的路由                           dev 网关出口 物理设备名
gw 出口网关 IP地址
dev 出口网关 物理设备名

增加默认路由                                     增加默认路由
route add default gw 192.168.0.1                  ip route add default via 192.168.0.1 dev eth0
默认路由一条就够了

route -n 查看路由表

如加入:
route add -net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1

则static-routes的格式为
any net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1

# route  [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
其中:add : 添加一条路由规则

del : 删除一条路由规则

-net : 目的地址是一个网络

-host : 目的地址是一个主机

target : 目的网络或主机

netmask : 目的地址的网络掩码

gw : 路由数据包通过的网关

dev : 为路由指定的网络接口

[b]添加永久路由(重启有效)[/b]

[root@localhost src]# cat /etc/rc.d/init.d/network
133         # Add non interface-specific static-routes.
134         if [ -f /etc/sysconfig/static-routes ]; then
135            grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
136               /sbin/route add -$args
137            done
138         fi
也就是说,将静态路由加到/etc/sysconfig/static-routes 文件中就行了(没有static-routes的话就手动建立一个这样的文件)如加入:
route add -net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1
则static-routes的格式为:
any net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1
[root@localhost ~]# vim /etc/sysconfig/static-routes
#any net 192.168.0.0 netmask 255.255.255.0 gw 19.15.24.254
any net 192.168.0.0/16 gw 10.10.2.254
any net 10.15.0.0/16 gw 10.10.2.254
any net 10.17.0.0/16 gw 10.10.2.254
any net 10.10.0.0/16 dev eth2
[root@localhost ~]# service network restart
Shutting down interface eth2:                              [  OK  ]
Shutting down interface eth3:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth2:  Determining if ip address 10.15.24.222 is already in use for device eth0...
[  OK  ]
Bringing up interface eth3:  Determining if ip address 10.10.2.59 is already in use for device eth1...
[  OK  ]
SIOCADDRT: No such process
SIOCADDRT: No such process
SIOCADDRT: No such process
SIOCADDRT: No such process
[root@localhost ~]#
重启网卡时,出现错误“SIOCADDRT: No such process”,原因是要添加的网关不在你主机所在的网段,Linux默认网关如果设置在/etc/sysconfig/network文件中,对所有网卡有效。而此时多个网卡的环境下,如果某个网卡配置的IP与网关不在一个网段就会出现这个问题。所以在配置多IP时候不要在/etc/sysconfig/network中添加默认网关直接在网卡配置文件ifcfg-eth中配置。至此双网卡双IP配置完成。
[b]查看路由[/b]
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.2.0       0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.17.0.0       10.10.2.254     255.255.0.0     UG    0      0        0 eth0
10.10.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth0
192.168.0.0     10.10.2.254     255.255.0.0     UG    0      0        0 eth0
10.15.0.0       10.10.2.254     255.255.0.0     UG    0      0        0 eth0
[root@localhost ~]#
Destination目标网段或者主机
Gateway网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask网络掩码
Flags标记。一些可能的标记如下:
U — 路由是活动的
H — 目标是一个主机
G — 路由指向网关
R — 恢复动态路由产生的表项
D — 由路由的后台程序动态地安装
M — 由路由的后台程序修改
! — 拒绝路由
Metric路由距离,到达指定网络所需的中转数(linux 内核中没有使用)
Ref路由项引用次数(linux 内核中没有使用)
Use此路由项被路由软件查找的次数
Iface该路由表项对应的输出接口

路由类型

主机路由

主机路由是路由选择表中指向单个IP地址或主机名的路由记录。主机路由的Flags字段为H。例如,在下面的示例中,本地主机通过IP地址192.168.1.1的路由器到达IP地址为10.0.0.10的主机。
Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     -------            -----     ------    ---    ---    -----
10.0.0.10     192.168.1.1    255.255.255.255   UH       0    0      0    eth0

网络路由

网络路由是代表主机可以到达的网络。网络路由的Flags字段为N。例如,在下面的示例中,本地主机将发送到网络192.19.12的数据包转发到IP地址为192.168.1.1的路由器。
Destination    Gateway       Genmask Flags    Metric    Ref     Use    Iface
-----------    -------     -------         -----    -----   ---    ---    -----
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0

默认路由

当主机不能在路由表中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关)上。默认路由的Flags字段为G。例如,在下面的示例中,默认路由是IP地址为192.168.1.1的路由器。
Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     ------- -----      ------    ---    ---    -----
default       192.168.1.1     0.0.0.0    UG       0        0     0    eth0

DNS设置
[root@justin ~]# vim /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.100.189
nameserver 202.96.209.5
nameserver 202.96.209.133
# NOTE: the libc resolver may not support more than 3 nameservers.
# The nameservers listed below may not be recognized.
nameserver 210.22.70.3
网络节点测试
[root@localhost network-scripts]# traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1  10.15.72.254 (10.15.72.254)  7.568 ms  7.880 ms  8.071 ms
网络连通性测试
[root@localhost network-scripts]# ping -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=46 time=32.3 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=46 time=31.8 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=46 time=33.4 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 3 received, 40% packet loss, time 5007ms
rtt min/avg/max/mdev = 31.891/32.554/33.458/0.662 ms
[root@localhost network-scripts]#
ARP信息
[root@localhost network-scripts]# arp -n   #查看当前系统的ARP表
Address                  HWtype  HWaddress           Flags Mask            Iface
10.15.72.254             ether   08:d0:9f:e2:4d:c3   C                     eth0
10.15.72.73              ether   00:25:64:a6:08:af   C                     eth0
[root@localhost network-scripts]# arp -s 10.15.72.254 08:d0:9f:e2:4d:c3   #网关绑定ip和网关的mac
[root@localhost network-scripts]# arp -d 10.15.72.73   #删除arp记录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息