aquaballoon

Network Tweak

May 23rd, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.02 KB | None | 0 0
  1. Debugging
  2.  
  3. # Show the ethernet status
  4. Code:ethtool eth0
  5.  
  6. # Force 100Mbit Full duplex
  7. Code:ethtool -s eth0 speed 100 duplex full
  8.  
  9. # Disable auto negotiation
  10. Code:ethtool -s eth0 autoneg off
  11.  
  12. # Blink the ethernet led
  13. Code:ethtool -p eth0
  14.  
  15. # Display all interfaces (similar to ifconfig)
  16. Code:ip link show
  17.  
  18. # Bring device up (or down). Same as "ifconfig eth0 up"
  19. Code:ip link set eth0 up
  20.  
  21. # Display all IP addresses (similar to ifconfig)
  22. Code:ip addr show
  23.  
  24. # Similar to arp -a
  25. Code:ip neigh show
  26.  
  27. # Ping on ethernet layer
  28. Code:arping 192.168.16.254
  29.  
  30. # uses tcp instead of icmp to trace throught firewalls (install via sudo apt-get install tcptraceroute)
  31. Code:tcptraceroute -f 5 cb.vu
  32.  
  33.  
  34. Routing
  35.  
  36. Print routing table
  37.  
  38. # use "ip route"
  39. Code:route -n
  40.  
  41. Code:netstat -rn
  42.  
  43.  
  44. Add and delete a route
  45.  
  46. Code:route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.16.254
  47.  
  48. # same as above with ip route
  49. Code:ip route add 192.168.20.0/24 via 192.168.16.254
  50.  
  51. Code:route add -net 192.168.20.0 netmask 255.255.255.0 dev eth0
  52.  
  53. Code:route add default gw 192.168.51.254
  54.  
  55. # same as above with ip route
  56. Code:ip route add default via 192.168.51.254 dev eth0
  57.  
  58. Code:route delete -net 192.168.20.0 netmask 255.255.255.0
  59.  
  60.  
  61. Configure additional IP addresses
  62.  
  63. # First IP
  64. Code:ifconfig eth0 192.168.50.254 netmask 255.255.255.0
  65.  
  66. # Second IP
  67. Code:ifconfig eth0:0 192.168.51.254 netmask 255.255.255.0
  68.  
  69. # Equivalent Commands:
  70. Code:ip addr add 192.168.50.254/24 dev eth0
  71.  
  72. Code:ip addr add 192.168.51.254/24 dev eth0 label eth0:1
  73.  
  74.  
  75. Change MAC address
  76.  
  77. #Normally you have to bring the interface down before the change.
  78.  
  79. Code:ifconfig eth0 down
  80.  
  81. Code:ifconfig eth0 hw ether 00:01:02:03:04:05
  82.  
  83.  
  84. Ports in use
  85.  
  86. #Listening on open ports:
  87. Code:netstat -an | grep LISTEN
  88.  
  89. # lists all Internet connections
  90. Code:lsof -i
  91.  
  92. # displays list of open sockets (use apt-get install procinfo)
  93. Code:socklist
  94.  
  95. Code:netstat -anp --udp --tcp | grep LISTEN
  96.  
  97. # List active connections to/from system
  98. Code:netstat -tup
  99.  
  100. # List listening ports from system
  101. Code:netstat -tupl
  102.  
  103.  
  104. Firewall (iptables)
  105.  
  106. # For status
  107. Code:iptables -L -n -v
  108.  
  109. # Open everything
  110. Code:iptables -P INPUT       ACCEPT
  111.  
  112. Code:iptables -P FORWARD     ACCEPT
  113.  
  114. Code:iptables -P OUTPUT      ACCEPT
  115.  
  116. # Zero the packet and byte counters in all chains
  117. Code:iptables -Z
  118.  
  119. # Flush all chains
  120. Code:iptables -F
  121.  
  122. # Delete all chains
  123. Code:iptables -X
  124.  
  125. IP Forward for routing
  126.  
  127. # Check and then enable IP forward with:
  128.  
  129. # Check IP forward 0=off, 1=on
  130. Code:nano -w /proc/sys/net/ipv4/ip_forward
  131.  
  132. Code:echo 1 > /proc/sys/net/ipv4/ip_forward
  133.  
  134. # or edit /etc/sysctl.conf with:
  135.  
  136. Code:net.ipv4.ip_forward = 1
  137.  
  138.  
  139. NAT Network Address Translation
  140.  
  141. # to activate NAT
  142. Code:iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  143.  
  144. # Port forward 20022 to internal IP port ssh
  145. Code:iptables -t nat -A PREROUTING -p tcp -d 78.31.70.238 --dport 20022 -j DNAT --to 192.168.16.44:22
  146.  
  147. # Port forward of range 993-995
  148. Code:iptables -t nat -A PREROUTING -p tcp -d 78.31.70.238 --dport 993:995 -j DNAT --to 192.168.16.254:993-995
  149.  
  150. Code:ip route flush cache
  151.  
  152. # Check NAT status
  153. Code:iptables -L -t nat
  154.  
  155.  
  156. NOTE: You can delete a port forward with -D instead of -A.
  157.  
  158.  
  159. DNS
  160.  
  161. On *nix the DNS entries are valid for all interfaces and are stored in /etc/resolv.conf.
  162. The domain to which the host belongs is also stored in this file. A minimal configuration is:
  163.  
  164. Code:nameserver 78.31.70.238
  165. search mydomain.net intern.lab
  166. domain mydomain.net
  167.  
  168.  
  169. Check the system domain name with:
  170.  
  171. # Same as dnsdomainname
  172. Code:hostname -d
  173.  
  174.  
  175. Forward queries
  176.  
  177. Dig is used to test the DNS settings.
  178. See from which server the client receives the answer (simplified answer).
  179. in this example, we use google.com
  180.  
  181. dig google.com
  182. google.com.267INA64.233.187.99
  183. ;; SERVER: 192.168.1.254#53(192.168.1.254)
  184.  
  185.  
  186. The router 192.168.1.254 answered and the response is the A entry.
  187. Any entry can be queried and the DNS server can be selected with @:
  188.  
  189. # To test the local server
  190. Code:dig @127.0.0.1 NS sun.com
  191.  
  192. # Query an external server
  193. Code:dig @204.97.212.10 NS MX heise.de
  194.  
  195. # Get the full zone (zone transfer)
  196. Code:dig AXFR @ns1.xname.org cb.vu
  197.  
  198.  
  199. The program host is also quite powerful.
  200.  
  201. # Get the mail MX entry
  202. Code:host -t MX google.com
  203.  
  204. # Get the NS record over a TCP connection
  205. Code:host -t NS -T google.com
  206.  
  207. # Get everything
  208. Code:host -a google.com
  209.  
  210.  
  211.  
  212. Reverse queries
  213.  
  214. Find the name belonging to an IP address (in-addr.arpa.). This can be done with dig, host and nslookup:
  215.  
  216. Code:dig -x 78.31.70.238
  217.  
  218. Code:host 78.31.70.238
  219.  
  220. Code:nslookup 78.31.70.238
  221.  
  222.  
  223. Single hosts can be configured in the file /etc/hosts instead of running named locally
  224. to resolve the hostname queries. The format is simple, for example:
  225.  
  226. 64.233.187.99 google.com google
  227.  
  228.  
  229. DHCP
  230.  
  231. The default ubuntu dhcp client is dhclient, however, i like dhcpcd a lot better,
  232. and that is what i will use in my examples
  233.  
  234. apt-get install dhcpcd to install it.
  235.  
  236. # Trigger a renew (does not always work)
  237. Code:dhcpcd -n eth0
  238.  
  239. # release and shutdown
  240. Code:dhcpcd -k eth0
  241.  
  242.  
  243. The lease with the full information is stored in:
  244.  
  245. /var/lib/dhcpcd/dhcpcd-eth0.info
  246.  
  247.  
  248. For dhclient:
  249.  
  250. Code:dhclient eth0
  251.  
  252.  
  253. The lease with the full information is stored in:
  254.  
  255. /var/db/dhclient.leases.eth0
  256.  
  257.  
  258. Use
  259.  
  260. /etc/dhclient.conf
  261.  
  262. to prepend options or force different options:
  263.  
  264. Code:nano -w /etc/dhclient.conf
  265.  
  266. interface "eth0" {
  267. prepend domain-name-servers 127.0.0.1;
  268. default domain-name "google.com";
  269. supersede domain-name "google.com";
  270. }
  271.  
  272.  
  273. Traffic analysis
  274.  
  275. Bmon http://people.suug.ch/~tgr/bmon/ is a small console bandwidth monitor and can display the
  276. flow on different interfaces. You can install it on ubuntu with apt-get install bmon
  277.  
  278. Sniff with tcpdump (tcpdump comes with ubuntu)
  279.  
  280. Code:tcpdump -nl -i eth0 not port ssh and src \(192.168.16.121 or 192.168.16.54\)
  281.  
  282. # select to/from a single IP
  283. Code:tcpdump -n -i eth0 net 192.168.16.121
  284.  
  285. # select traffic to/from a network
  286. Code:tcpdump -n -i eth0 net 192.168.16.0/24
  287.  
  288. # Buffered output
  289. Code:tcpdump -l > dump && tail -f dump
  290.  
  291. # Write traffic headers in binary file
  292. Code:tcpdump -i eth0 -w traffic.eth0
  293.  
  294. # Write traffic + payload in binary file
  295. Code:tcpdump -i eth0 -s 0 -w traffic.eth0
  296.  
  297. # Read from file (also for ethereal
  298. Code:tcpdump -r traffic.eth0
  299.  
  300. # The two classic commands
  301. Code:tcpdump port 80
  302.  
  303. # Check if pop or imap is secure
  304. Code:tcpdump host google.com
  305.  
  306. Code:tcpdump -i eth0 -X port \(110 or 143\)
  307.  
  308. # Only catch pings
  309. Code:tcpdump -n -i eth0 icmp
  310.  
  311. # -s 0 for full packet -A for ASCII
  312. Code:tcpdump -i eth0 -s 0 -A port 80 | grep GET
  313.  
  314.  
  315. Additional important options:
  316.  
  317.  
  318. * -A Print each packets in clear text (without header)
  319.  
  320. * -X Print packets in hex and ASCII
  321.  
  322. * -l Make stdout line buffered
  323.  
  324. * -D Print all interfaces available
  325.  
  326. Scan with nmap
  327.  
  328. Nmap http://insecure.org/nmap/ is a port scanner with OS detection,
  329. it is usually installed on most distributions. Install it in ubuntu
  330. with apt-get install nmap
  331.  
  332. # scans all reserved TCP ports on the host
  333. Code:nmap google.com
  334.  
  335. # Find out which IP are used and by which host on 0/24
  336. Code:nmap -sP 192.168.16.0/24
  337.  
  338. # Do a stealth SYN scan with version and OS detection
  339. Code:nmap -sS -sV -O google.com
  340.  
  341.  
  342. Other non standard but useful tools are hping (www.hping.org) an IP packet assembler/analyzer
  343. and fping (fping.sourceforge.net). fping can check multiple hosts in a round-robin fashion.
  344.  
  345. Traffic control (QoS)
  346.  
  347. Traffic control manages the queuing, policing, scheduling, and other traffic parameters for a network.
  348. The following examples are simple practical uses of the Linux capabilities to better use the available
  349. bandwidth.
  350.  
  351. Limit upload
  352.  
  353. DSL or cable modems have a long queue to improve the upload throughput.
  354. However filling the queue with a fast device (e.g. ethernet) will dramatically
  355. decrease the interactivity. It is therefore useful to limit the device
  356. upload rate to match the physical capacity of the modem, this should
  357. greatly improve the interactivity. Set to about 90% of the modem maximal (cable) speed.
  358.  
  359. For a 512 Kbit upload modem:
  360.  
  361. Code:tc qdisc add dev eth0 root tbf rate 480kbit latency 50ms burst 1540
  362.  
  363. # Status
  364. Code:tc -s qdisc ls dev eth0
  365.  
  366. # Delete the queue
  367. Code:tc qdisc del dev eth0 root
  368.  
  369. *code:tc qdisc change dev eth0 root tbf rate 220kbit latency 50ms burst 1540
  370.  
  371.  
  372. Quality of service
  373.  
  374. Priority queuing with tc to optimize VoIP. See the full example on voip-info.org or
  375. www.howtoforge.com. Suppose VoIP uses udp on ports 10000:11024 and device eth0
  376. (could also be ppp0 or so). The following commands define the QoS to three queues
  377. and force the VoIP traffic to queue 1 with QoS 0x1e (all bits set).
  378. The default traffic flows into queue 3 and QoS Minimize-Delay flows into queue 2.
  379.  
  380. Code:tc qdisc add dev eth0 root handle 1: prio priomap 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 0
  381.  
  382. Code:tc qdisc add dev eth0 parent 1:1 handle 10: sfq
  383.  
  384. Code:tc qdisc add dev eth0 parent 1:2 handle 20: sfq
  385.  
  386. Code:tc qdisc add dev eth0 parent 1:3 handle 30: sfq
  387.  
  388. Code:tc filter add dev eth0 protocol ip parent 1: prio 1 u32
  389.  
  390. # use server port range (added after u32 above)
  391. Code:match ip dport 10000 0x3C00 flowid 1:1
  392.  
  393. # or/and use server IP (added after u32 above)
  394. Code:match ip dst 123.23.0.1 flowid 1:1
  395.  
  396.  
  397. Status and remove with
  398.  
  399. # queue status
  400. Code:tc -s qdisc ls dev eth0
  401.  
  402. # delete all QoS
  403. Code:tc qdisc del dev eth0 root
  404.  
  405.  
  406. Calculate port range and mask
  407.  
  408. The tc filter defines the port range with port and mask which you have to calculate.
  409. Find the 2^# ending of the port range, deduce the range and convert to HEX.
  410. This is your mask. Example for 10000 -> 11024, the range is 1024.
  411.  
  412. # ending is 2^14 = 16384
  413. Code:2^13 (8192) < 10000 < 2^14 (16384)
  414.  
  415. # mask is 0x3C00
  416. Code:echo "obase=16;(2^14)-1024" | bc
  417.  
  418.  
  419.  
  420. NIS Debugging
  421.  
  422. Some commands which should work on a well configured NIS client:
  423.  
  424. # get the connected NIS server name (apt-get install nis to use)
  425. Code:ypwhich
  426.  
  427. # The NIS domain name as configured
  428. Code:domainname
  429.  
  430. # should display the group from the NIS server
  431. Code:ypcat group
  432.  
  433. # Rebuild the yp database
  434. Code:cd /var/yp && make
  435.  
  436.  
  437. Is ypbind running?
  438.  
  439. Code:ps auxww | grep ypbind
  440.  
  441. Code:/usr/sbin/ypbind
  442.  
  443. Code:yppoll passwd.byname
  444.  
  445. Map passwd.byname has order number 1190635041. Mon Sep 24 13:57:21 2007
  446. The master server is servername.domain.net.
  447.  
  448. Code:nano -w /etc/yp.conf
  449.  
  450. Code:ypserver servername
  451.  
  452. Code:domain domain.net broadcast
  453.  
  454.  
  455.  
  456. Netcat
  457.  
  458. Netcat http://netcat.sourceforge.net (nc) is better known as the "network Swiss Army Knife",
  459. it can manipulate, create or read/write TCP/IP connections. Here some useful examples,
  460. there are many more on the net, for example g-loaded.eu[...]
  461. http://www.g-loaded.eu/2006/11/06/ne...seful-examples and here
  462. http://www.terminally-incoherent.com...-netcat-tricks.
  463.  
  464.  
  465. File transfer
  466.  
  467. Copy a large folder over a raw tcp connection. The transfer is very quick (no protocol overhead) and you don't need to mess up with NFS or SMB or FTP or so, simply make the file available on the server, and get it from the client. Here 192.168.1.1 is the server IP address.
  468.  
  469. # Serve tar folder on port 4444
  470. Code:server: tar -cf - -C VIDEO_TS . | nc -l -p 4444
  471.  
  472. # Pull the file on port 4444
  473. Code:client: nc 192.168.1.1 4444 | tar xpf - -C VIDEO_TS
  474.  
  475. # Server a single file
  476. Code:server: cat largefile | nc -l 5678
  477.  
  478. # Pull the single file
  479. Code:client: nc 192.168.1.1 5678 > largefile
  480.  
  481. # Server partition image
  482. Code:server: dd if=/dev/da0 | nc -l 4444
  483.  
  484. # Pull partition to clone
  485. Code:client: nc 192.168.1.1 4444 | dd of=/dev/da0
  486.  
  487. # Pull partition to file
  488. Code:client: nc 192.168.1.1 4444 | dd of=da0.img
  489.  
  490.  
  491. Other hacks
  492.  
  493.  
  494. Remote shell
  495.  
  496. # Provide a remote shell on port 4444 (aserver backdoor)
  497. *code:nc -lp 4444 -e /bin/bash
  498.  
  499. Emergency web server
  500.  
  501. Serve a single file on port 80 in a loop.
  502.  
  503. Code:while true; do nc -l -p 80 < unixtoolbox.xhtml; done
  504.  
  505.  
  506. Chat
  507.  
  508. Joe and Carter can chat over a simple TCP socket. The text is transferred with the enter key.
  509.  
  510. Code:Joe: nc -lp 4444
  511.  
  512. Code:Carter: nc 192.168.1.1 4444
Advertisement
Add Comment
Please, Sign In to add comment