Linux - 常用网络操作

时间:2022-07-31 16:39:58

01 - Linux虚拟机出现多个默认路由,导致部分网络连接异常

# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.16.1 0.0.0.0 UG 100 0 0 enp0s3
0.0.0.0 10.0.3.2 0.0.0.0 UG 101 0 0 enp0s8
......

处理方法:更改网关192.168.16.1的路由设置。

  • CentOS7:应用程序---》系统工具---》设置---》网络---》以太网(enp0s3),点击右下角的设置按钮---》IPv4---》选择“仅对此网络上的资源使用此连接”---》应用。
  • Ubuntu1604:系统设置---》网络---》有线,选择对应网卡---》选项---》IPv4设置---》路由---》选择“仅将此连接用于相对应的网络上的资源”---》确定。
# route -n
......
192.168.16.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
......

02 - Linux查看端口状态

检测本机8080端口状态:netstat –apn | grep 8080

检测192.168.0.1主机的8080端口状态:telnet 192.168.0.1 8080nmap -sS -P0 -n -p 22 192.168.0.1

检测192.168.0.1主机的1到1024端口状态:nc -z 192.168.0.1 1-1024

# nmap -sS -P0 -n -p 22 10.140.0.135
Starting Nmap 6.40 ( http://nmap.org ) at 2016-11-09 10:42 CST
Nmap scan report for 10.140.0.135
Host is up (0.00040s latency).
PORT STATE SERVICE
22/tcp open ssh
MAC Address: 08:00:27:3F:19:79 (Cadmus Computer Systems)
Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds # nmap -sS -P0 -n -p 80 10.140.0.135
Starting Nmap 6.40 ( http://nmap.org ) at 2016-11-09 10:50 CST
Nmap scan report for 10.140.0.135
Host is up (0.00046s latency).
PORT STATE SERVICE
80/tcp closed http
MAC Address: 08:00:27:3F:19:79 (Cadmus Computer Systems)
Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

03 - Linux命令行设置CentOS计算机名称、hosts及网卡地址

$ echo "anliven" > /etc/hostname
$ echo -e "192.168.16.166 master\n192.168.16.167 slave01\n192.168.16.168 slave02" >> /etc/hosts
$ sed -i '/IPADDR/s/192.168.16.200/192.168.16.166/' /etc/sysconfig/network-scripts/ifcfg-enp0s8
$ reboot

04 - Linux CentOS7关闭SELinux

  • 永久方法:修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启。
# getenforce
Enforcing
# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
# reboot
  • 临时方法:执行setenforce 0命令设置SELinux成为permissive模式
sudo systemctl status firewalld.service
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

05 - Ubuntu安装ssh并允许root登录

  1. Install openssh-server
apt-get update
apt-get install openssh-server
  1. Change ssh configuration
root@anliven:~# vim /etc/ssh/sshd_config
root@anliven:~#
root@anliven:~# cat /etc/ssh/sshd_config |grep RootLogin
#PermitRootLogin without-password
PermitRootLogin yes
# the setting of "PermitRootLogin without-password".
root@anliven:~#
root@anliven:~# /etc/init.d/ssh restart
ssh stop/waiting
ssh start/running, process 3782
root@anliven:~#
  1. Check ssh status
root@anliven:~# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 3630 root 3u IPv4 19611 0t0 TCP *:ssh (LISTEN)
sshd 3630 root 4u IPv6 19613 0t0 TCP *:ssh (LISTEN)
root@anliven:~#
root@anliven:~# ps -ef |grep ssh
root 3630 1 0 13:43 ? 00:00:00 /usr/sbin/sshd -D
root 3750 3732 0 13:46 pts/1 00:00:00 grep --color=auto ssh
root@anliven:~#

06 - Linux常用网络软件包

  • inetuils-ping --- ping
  • net-tools --- ifconfig/route/netstat/......
  • iproute --- ip
  • traceroute
  • bind-utils --- nslookup/dig/host/......
  • bridge-utils --- brctl
  • nmap
  • tcpdump
  • wireshark
  • curl
  • wget

    ......

07 - Linux设置ssh免密码认证

示例:slave01和slave02两个主机ssh免密码相互登录

### 设置slave01 node
[hadoop@slave01 ~]$ ssh-keygen
[hadoop@slave01 ~]$
[hadoop@slave01 ~]$ cd .ssh
[hadoop@slave01 .ssh]$ ssh-copy-id -i hadoop@slave01
[hadoop@slave01 .ssh]$ ssh-copy-id -i hadoop@slave02
[hadoop@slave01 .ssh]$
[hadoop@slave01 .ssh]$ ll -a
total 20
drwx------ 2 hadoop hadoop 76 Mar 7 23:34 .
drwx------. 6 hadoop hadoop 4096 Mar 7 23:30 ..
-rw------- 1 hadoop hadoop 1187 Mar 7 23:34 authorized_keys
-rw------- 1 hadoop hadoop 1675 Mar 7 23:33 id_rsa
-rw-r--r-- 1 hadoop hadoop 396 Mar 7 23:33 id_rsa.pub
-rw-r--r-- 1 hadoop hadoop 551 Mar 7 23:34 known_hosts
[hadoop@slave01 .ssh]$ ### 设置slave02 node
[hadoop@slave02 ~]$ ssh-keygen
[hadoop@slave02 ~]$
[hadoop@slave02 ~]$ cd .ssh
[hadoop@slave02 .ssh]$ ssh-copy-id -i hadoop@slave01
[hadoop@slave02 .ssh]$ ssh-copy-id -i hadoop@slave02
[hadoop@slave02 .ssh]$
[hadoop@slave02 .ssh]$ ll -a
total 20
drwx------ 2 hadoop hadoop 76 Mar 7 23:35 .
drwx------. 6 hadoop hadoop 4096 Mar 7 23:30 ..
-rw------- 1 hadoop hadoop 1187 Mar 7 23:35 authorized_keys
-rw------- 1 hadoop hadoop 1675 Mar 7 23:33 id_rsa
-rw-r--r-- 1 hadoop hadoop 396 Mar 7 23:33 id_rsa.pub
-rw-r--r-- 1 hadoop hadoop 551 Mar 7 23:35 known_hosts
[hadoop@slave01 .ssh]$

08 - Linux设置路由并指定metric值

  • 默认路由:route add default gw 10.0.2.2 metric 1
  • 指定路由:route add 192.168.0.0 mask 255.255.0.0 192.168.16.1 metric 10

09 -xxx