1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)
1.1 主机名
修改主机名 和centos7一样 修改后永久生效
root@ubuntu:~# hostnamectl set-hostname ubuntu2004.waluna.top
root@ubuntu:~# cat /etc/hostname
ubuntu2004.waluna.top
root@ubuntu:~# hostname
ubuntu2004.waluna.top
root@ubuntu:~# echo $HOSTNAME
ubuntu
root@ubuntu:~# exit
logout
ubuntu@ubuntu:~$ sudo -i
root@ubuntu2004:~# echo $HOSTNAME
ubuntu2004.waluna.top
1.2 网卡名称
默认ubuntu的网卡名称和centos7类似,如:ens33,ens38等
修改网卡名称为传统命名方式:
# 修改配置文件为下面形式
root@ubuntu2004:~# vim /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
# 或者sed修改
root@ubuntu2004:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
# 生效新的grub.cfg文件
root@ubuntu2004:~# grub-mkconfig -o /boot/grub/grub.cfg
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-112-generic
Found initrd image: /boot/initrd.img-4.15.0-112-generic
done
# 或者
root@ubuntu2004:~# update-grub
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-112-generic
Found initrd image: /boot/initrd.img-4.15.0-112-generic
done
root@ubuntu2004:~# grep net.ifnames /boot/grub/grub.cfg
linux /boot/vmlinuz-4.15.0-112-generic root=UUID=eb982e5d-8588-4714-92ac-bb09908d16c8 ro net.ifnames=0
linux /boot/vmlinuz-4.15.0-112-generic root=UUID=eb982e5d-8588-4714-92ac-bb09908d16c8 ro net.ifnames=0
linux /boot/vmlinuz-4.15.0-112-generic root=UUID=eb982e5d-8588-4714-92ac-bb09908d16c8 ro recovery nomodeset net.ifnames=0
# 重启生效
root@ubuntu2004:~# reboot
1.3 网卡配置
1.3.1 配置自动获取IP
网卡配置文件才用YAML格式,必须以/etc/netplan/xxx.yaml文件命名方式存放
可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里
root@ubuntu2004:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
修改网卡配置文件后需执行命令生效:
root@ubuntu2004:~# netplan apply
1.3.2 配置静态IP
root@ubuntu2004:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresss: [192.168.8.10/24,10.0.0.10/8] # 或者用下面两行,两种格式不能混用
- 10.0.0.129/24
- 192.168.8.10/24
gateway4: 10.0.0.2
nameservers:
search: [waluna.top,waluna.xyz]
addresses: [114.114.114.114,8.8.8.8]
查看IP和gateway
root@ubuntu2004:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:93:a6:2e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.129/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe93:a62e/64 scope link
valid_lft forever preferred_lft forever
root@ubuntu2004:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
查看DNS
root@ubuntu2004:~# ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Mar 17 02:37 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
root@ubuntu2004:~# systemd-resolve --status
Global
DNSSEC NTA: 10.in-addr.arpa
16.172.in-addr.arpa
168.192.in-addr.arpa
17.172.in-addr.arpa
18.172.in-addr.arpa
19.172.in-addr.arpa
20.172.in-addr.arpa
21.172.in-addr.arpa
22.172.in-addr.arpa
23.172.in-addr.arpa
24.172.in-addr.arpa
25.172.in-addr.arpa
26.172.in-addr.arpa
27.172.in-addr.arpa
28.172.in-addr.arpa
29.172.in-addr.arpa
30.172.in-addr.arpa
31.172.in-addr.arpa
corp
d.f.ip6.arpa
home
internal
intranet
lan
local
private
test
Link 2 (eth0)
Current Scopes: DNS
LLMNR setting: yes
MulticastDNS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNS Servers: 114.114.114.114
8.8.8.8
2、编写脚本实现登陆远程主机(使用expect和shell脚本两种形式)
# 最小化安装需要安装expect工具
[root@centos8 ~]# dnf install expect -y
# 利用ssh远程登录到10.0.0.7
[root@centos8 ~]# rm -rf .ssh/
[root@centos8 ~]# cat expect1
#!/usr/bin/expect # 注意shebang要用#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "waluna\n"}
}
expect eof
[root@centos8 ~]# chmod +x expect1 # 不是bash脚本,只能加执行权限
[root@centos8 ~]# ./expect1
spawn ssh 10.0.0.7
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:BvEOFVidIWSNe478SZ34jegCVOHesBaPh7bWvtccBkU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
Last login: Sat Jun 26 21:32:12 2021 from 10.0.0.8
[root@centos7 ~]#
# expect变量
[root@centos8 ~]# rm -rf .ssh/
[root@centos8 ~]# cat expect2
#!/usr/bin/expect
set ip 10.0.0.7
set user root
set password waluna
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n"}
}
interact
[root@centos8 ~]# chmod +x expect2
[root@centos8 ~]# ./expect2
spawn ssh root@10.0.0.7
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:BvEOFVidIWSNe478SZ34jegCVOHesBaPh7bWvtccBkU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
Last login: Sat Jun 26 21:39:13 2021 from 10.0.0.8
[root@centos7 ~]# exit
logout
Connection to 10.0.0.7 closed.
# expect位置变量
[root@centos8 ~]# rm -rf .ssh/
[root@centos8 ~]# cat expect3
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n"}
}
interact
[root@centos8 ~]# chmod +x expect3
[root@centos8 ~]# ./expect3 10.0.0.7 root waluna
spawn ssh root@10.0.0.7
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:BvEOFVidIWSNe478SZ34jegCVOHesBaPh7bWvtccBkU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
Last login: Sat Jun 26 21:40:25 2021 from 10.0.0.8
[root@centos7 ~]# exit
logout
Connection to 10.0.0.7 closed.
# shell脚本调用expect
[root@centos8 ~]# rm -rf .ssh/
[root@centos8 ~]# cat expect.sh
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n"}
}
expect eof
EOF
[root@centos8 ~]# bash expect.sh 10.0.0.7 root waluna
spawn ssh root@10.0.0.7
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:BvEOFVidIWSNe478SZ34jegCVOHesBaPh7bWvtccBkU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
Last login: Sat Jun 26 21:59:33 2021 from 10.0.0.8
[root@centos7 ~]#
3、生成10个随机数保存于数组中,并找出其最大值和最小值
[root@centos8 ~]# cat array.sh
#!/bin/bash
declare -i min max
declare -a nums
for((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo MAX is $max
echo MIN is $min
[root@centos8 ~]# bash array.sh
All numbers are 13012 7448 7543 3514 15786 6363 2398 5646 12774 6680
MAX is 15786
MIN is 2398
4、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@centos8 ~]# cat bubbile.sh
#!/bin/bash
read -p "请输入数值的个数:" n
declare -i max min
declare -a nums nums1
for((i=0;i<$n;i++));do
nums[$i]=$RANDOM
done
echo "排序前:${nums[*]}"
for((j=0;j<n;j++));do
for((k=0;k<`echo ${#nums[*]}`;k++));do
[ $k -eq 0 ] && min=${nums[0]} && continue
[ ${nums[$k]} -lt $min ] && min=${nums[$k]}
done
nums1[$j]=$min
nums=(${nums[*]/$min})
done
echo "排序后:${nums1[*]}"
[root@centos8 ~]# bash bubbile.sh
请输入数值的个数:7
排序前:22651 6839 6230 14934 27000 29716 27821
排序后:6230 6839 14934 22651 27000 27821 29716
5、显示统计占用系统内存最多的进程,并排序。
[root@centos8 ~]# ps axo %mem,cmd k -%mem|head
%MEM CMD
3.5 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
2.9 /usr/lib/polkit-1/polkitd --no-debug
2.5 /usr/sbin/NetworkManager --no-daemon
2.4 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
1.7 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=files
1.7 /usr/sbin/sssd -i --logger=files
1.4 /usr/lib/systemd/systemd-udevd
1.3 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
1.2 /usr/bin/VGAuthService -s
[root@centos8 ~]# ps axo %mem,cmd --sort -%mem|head
%MEM CMD
3.5 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
2.9 /usr/lib/polkit-1/polkitd --no-debug
2.5 /usr/sbin/NetworkManager --no-daemon
2.4 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
1.7 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=files
1.7 /usr/sbin/sssd -i --logger=files
1.4 /usr/lib/systemd/systemd-udevd
1.3 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
1.2 /usr/bin/VGAuthService -s
[root@centos8 ~]# ps axo %mem,cmd --sort=-%mem|head
%MEM CMD
3.5 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
2.9 /usr/lib/polkit-1/polkitd --no-debug
2.5 /usr/sbin/NetworkManager --no-daemon
2.4 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
1.7 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=files
1.7 /usr/sbin/sssd -i --logger=files
1.4 /usr/lib/systemd/systemd-udevd
1.3 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
1.2 /usr/bin/VGAuthService -s
6、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
# for循环
[root@centos8 ~]# cat for_ip.sh
#!/bin/bash
host=192.168.0.
for i in {0..255};do
{
ping -c1 -w1 ${host}$i &> /dev/null && echo "success!" || echo "fail!"
}&
done
wait
[root@centos8 ~]# bash for_ip.sh
fail!
success!
success!
success!
fail!
...省略...
# while循环
[root@centos8 ~]# cat while_ip.sh
#!/bin/bash
host=192.168.0.
i=0
while ((i<=255));do
{
ping -c1 -w1 ${host}$i &> /dev/null && echo "${host}$i success!" || echo "fail!"
}&
let i++
done
wait
[root@centos8 ~]# bash while_ip.sh
[root@centos8 ~]# bash while_ip.sh
success!
success!
success!
fail!
fail!
...省略...







Comments | 2 条评论
命令用的很6啊!脚本写的也不错,继续加油!
@14660463
还好这次没翻车