Linux基础之网络

Linux基础之网络

1、网络配置

一、ifconfig命令

ifconfig显示或设置网络设备,相似Windows下的ipconfig。算法

[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet  HWaddr 00:0C:29:5E:8B:43  
          inet addr:192.168.93.128  Bcast:192.168.93.255 
          Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe5e:8b43/64 Scope:Link

二、修改网卡名称

eth(Ethernet的缩写)新安装的虚拟机为eth0,为何原来的eth0会变成eth1?apache

不少Linux distribution使用udev动态管理设备文件,并根据设备的信息对其进行持久化命名。vim

udev会在系统引导的过程当中识别网卡,将mac地址和网卡名称对应起来记录在udev的规则脚本中。而对于新的虚拟机,会自动为虚拟机的网卡生成MAC地址,当你克隆或者重装虚拟机软件时,因为你使用的是之前系统虚拟硬盘的信息,而该系统中已经有eth0的信息,对于这个新的网卡,udev会自动将其命名为eth1 (累加的原则),因此在你的系统启动后,你使用ifconfig看到的网卡名为eth1。bash

[root@localhost ~]# vim /etc/udev/rules.d/70-persistent-net.rules
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:0f:fe:1a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:5e:8b:43", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

如何把名字改回eth0?服务器

udev记录网络规则的脚本为:/etc/udev/rules.d/70-persistent-net.rules。网络

打开该文件,这时你会发现,里面有eth0,eth1 这两个网卡的信息,但实际上你ifconfig时只能发现eth1这一个网卡的信息,这时由于eth0根本就不存在。将其中eth0的信息删掉,并将eth1这行信息中的设备名改成eth0,重启系统,你看到的网卡就是eth0了,或者删掉其中全部的信息重启系统udev会帮你发现新的设备的。dom

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost ~]# service network restart

也等于:service network stop && service network start。
网卡配置文件:ssh

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

三、虚拟机的几种链接方式

1.NAT

NAT即为网络地址转换,一般它的名称为VMnet8,经过nat的链接方式能够使得虚拟机和真实机的网卡在不一样的网段中,从而实现联网。ide

2.bridge

bridge即为桥接,一般它的名称为VMnet0,使用桥接的方式使得本身的虚拟机和本身的真实机网卡在同一个网段,从而实现联网。工具

3.Host-only

host-only即为仅主机,一般它的名称是VMnet1,使用host-only的方式是不能和外界通讯的,只可以和本机的物理网卡通讯。

修改网络连接方式后,记得执行如下命令:

[root@localhost ~]# service network restart/status/start/stop

2、网络操做

一、经过主机名查看ip

经过主机名查看IP使用命令为:host。

命令格式:host 主机名

示例:

[root@localhost ~]#host www.baidu.com

二、远程管理

1.远程拷贝

远程拷贝使用的命令为:scp。

命令格式:scp –r /home/demo root@服务器IP:/root

将本地的demo目录拷贝到服务器IP的/root目录下。

2.远程登陆

远程登陆使用的命令为:ssh。

命令格式:ssh  用户名@服务器IP

从本机中登陆远程服务器。
示例:
将本地的/root目录下的erlang-solutions-1.0-1.noarch.rpm拷贝到192.168.93.129的/root目录下。

[root@localhost ~]# scp /root/erlang-solutions-1.0-1.noarch.rpm root@192.168.93.129:/root

从当前虚拟机(61)中ssh到192.168.80.62中,建立一个文件,查看;退出后再次查看。

[root@localhost ~]#ssh  root@192.168.80.62
[root@localhost ~]#touch  61dcyy
[root@localhost ~]#ls
[root@localhost ~]#logout
[root@localhost ~]#ls

3.SSH远程免密登陆

在服务器上每次ssh别的服务器都须要输入密码,若是解决这种麻烦的操做?

SSH最经常使用的Linux远程登陆工具。

免密码登陆设置(在集群管理等场景会常常使用到):

RSA:非对称加密算法,秘钥对分为公钥、私钥,对公钥加密的内容,只有拥有私钥者才可解密。

免密码操做

具体实现方法,客户端执行生成公钥/私钥对(70)  #70是虚拟机的IP地址。

ssh-keygen

将公钥拷贝到服务器端(71)

ssh-copy-id  <username>@<server>
[root@localhost ~]#ssh  root@192.168.80.71

示例:

#生成秘钥
[root@hadoopFD ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
1e:d3:25:9d:83:56:ba:c2:20:35:d4:b6:42:4e:92:fe root@hadoopFD
The key's randomart image is:
+--[ RSA 2048]----+
|     o+.    .    |
|    o.o.o  = .   |
|   ..=.. .= =    |
|    ..oo.o + .   |
|     . .S o      |
|      E. +       |
|        .        |
|                 |
|                 |
+-----------------+
#拷贝秘钥
[root@hadoopFD ~]# ssh-copy-id root@192.168.75.150
root@192.168.75.150's password: 
Now try logging into the machine, with "ssh 'root@192.168.75.150'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
#远程登陆
[root@hadoopFD ~]# ssh 192.168.75.150
Last login: Fri May 25 04:09:14 2018 from 192.168.75.10
#免密登陆成功
[root@hadoop ~]#

图片详解:

下面重点说一下,这段话:

这是拷贝以后的正常提示内容,不是什么错误信息,我把汉语意思写上了。曾经有人问我这段话是什么错误,我很无语啊!

三、网络下载

网络下载使用的命令为:wget。

命令格式:wget [参数] [URL地址]

用于从网络上下载资源,没有指定目录,下载资源默认存储到当前目录。

–支持断点下载功能

–同时支持FTP和HTTP下载方式

–支持代理服务器

示例:

#使用wget下载单个文件
wget http://www.tedu.cn
#使用wget -O下载并以不一样的文件名保存
wget -O newname.new  http://www.tedu.cn
#使用wget --limit-rate限速下载(单位,byte/秒)
wget --limit-rate=300k  http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.34.tar.bz2
#使用wget -c断点续传
wget -c http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.25.tar.bz2
#使用wget -b后台下载
wget -b http://mirrors.hust.edu.cn/apache//httpd/mod_fcgid/mod_fcgid-2.3.9.tar.gz
wget  http://mirrors.hust.edu.cn/apache//httpd/mod_fcgid/mod_fcgid-2.3.9.tar.gz
#使用wget -i下载多个文件
wget -i urlfile.txt

上一篇:Linux基础之文件系统

下一篇:Linux基础之进程

相关文章
相关标签/搜索