爱问知识人 爱问教育 医院库

ubuntu 连接 共享

首页

ubuntu 连接 共享

我有两台电脑 一台式机 一笔记本 台式机用Windows xp 和ubuntu 双系统 一台笔记本用Windows 7 只有一个猫 如何实现 同时上网?

提交回答

全部答案

    2018-05-18 04:19:20
  •   可以这样,需要一个无线网卡。台式机使用猫的网线上网,而笔记本通过无线连接台式机共享上网,这样的话要求台式机必须一直开机,不然关机的时候也只有拔出台式机的网线给笔记本直接连网线上网了。ubuntu下是很容易做到的,需要用到iptables的nat表,还有一个iwconfig命令。
      一般只有台式机插上无线网卡后对应的设备是wlan0。首先启动之(#号后面的是注释,不是命令内容):
    1。~$ sudo ifconfig wlan0 up #开启wlan0
    2。~$ sudo iwconfig wlan0 essid 名称 mode ad-hoc key s:密码 #此处的“密码”,“名称”由你自己决定,ad-hoc是设为共享模式,让别的无线网卡可以连接之;key指定连接时的密码,之所以加s是指定密码的方式为ASCII字符,如key s:123 ,意味着密码就是123,但是不加s,就是另一种编码,这里就不加讨论了。
      
    3。~$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE #这条命令把发送到wlan0的包通过eth0发出去
    4。~$ sudo sysctl net。
      ipv4。ip_forward=1 #开启内核的包转发功能,不然不会转发包
    还要设置一下wlan0的ip
    5。~$ sudo ifconfig wlan0 192。168。0。
      1
    对应的,把需要上网的笔记本的ip设为192。168。0。**,可以是192。168。0。2。网关设为192。168。0。1,dns服务器设成和台式机的一样就可以了。最好再装一个DHCPD服务,这样就不必设置客户端,直接设为dhcp方式获取就行了。
      我写了一个脚本如下可以复制到某个文件里,要的时候运行就行了,前提是配置好了DHCP。
    #!/usr/bin/env bash
    IPTABLES=/sbin/iptables
    wireless_card=wlan0
    output_card=eth0
    dhcpd=isc-dhcp-server
    ap_name=32
    passwd=hello
    ##judge the user
    if [ `id -u` != 0 ] ;then
    echo "you must run this script as root"。
      
    echo "exit。。。。"
    exit 1
    fi
    ###flush existing rules and set chain policy
    function iptables_nat_set()
    {
    $IPTABLES -F
    $IPTABLES -F -t nat
    $IPTABLES -X
    $IPTABLES -P INPUT ACCEPT
    $IPTABLES -P OUTPUT ACCEPT
    $IPTABLES -P FORWARD ACCEPT
    ###start ipv4 forword
    echo 1 > /proc/sys/net/ipv4/ip_forward
    ###end of start ipv4 forward
    ###use iptable nat to forward data
    $IPTABLES -t nat -A POSTROUTING -o $output_card -j MASQUERADE
    ###SET iptalbes finished
    }
    ####set the wireless card
    function judge_wirelesscard()
    {
    string=`iwconfig $wireless_card | grep 'Cell: Not-Associated'` >/dev/null 2>&1
    stat=`echo "$string"|wc -L`
    }
    function wireless_card_set()
    {
    judge_wirelesscard
    if [ $stat -ne 0 ];then
    set_start
    else
    ifconfig $wireless_card down
    set_start
    fi
    }
    function set_start()
    {
    iwconfig $wireless_card essid $ap_name key s:$passwd mode ad-hoc >/dev/null 2>&1

    }
    ####start dhcpd service
    function judge_start_dhcp()
    {
    if [ `service isc-dhcp-server status | grep not | wc -L` -ne 0 ];then
    service $dhcpd start
    else
    service $dhcpd restart
    fi
    }
    echo ">> setting iptables。
      。。"
    iptables_nat_set
    echo ">> setting wireless card。。。"
    wireless_card_set
    judge_wirelesscard
    while [ $stat != 0 ] ;
    do
    echo ">> judging wireless card' statu。
      。。
      "
    sleep 3
    ifconfig $wireless_card up
    judge_wirelesscard
    done
    judge_start_dhcp
    exit 0。

    是***

    2018-05-18 04:19:20

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):