1、Ubuntu系統網絡配置總結(包括主機名稱、網卡名稱、網卡配置)
(1) 修改主機名
1) 臨時修改,重啓主機後生效
hostname 主機名
2) 永久生效
hostnamectl set-hostname 主機名
或者
直接修改/etc/hostname文件
(2) 修改網卡名稱
1) 修改配置文件爲下面形式[root@Ubuntu16 ~]#vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
或者sed修改[root@Ubuntu16 ~]#sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
2) 生效新的grub.cfg文件[root@Ubuntu16 ~]#grub-mkconfig -o /boot/grub/grub.cfg
或者[root@Ubuntu16 ~]#update-grub
[root@Ubuntu16 ~]#grep net.ifnames /boot/grub/grub.cfg
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro net.ifnames=0
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro net.ifnames=0
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro recovery nomodeset dis_ucode_ldr net.ifnames=0
3) 重啓生效[root@Ubuntu16 ~]# reboot
(3) 網卡配置
Ubuntu從17.10開始,已放棄在/etc/network/interfaces裏配置IP地址,即使配置也不會生效,而是改成netplan方式,配置寫在/etc/netplan/01-netcfg.yaml或者類似名稱的yaml文件裏
1) 配置自動獲取IP[root@Ubuntu18 ~]# cat /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
修改網卡配置文件後需執行命令生效:[root@Ubuntu18 ~]#netplan apply
2) 配置靜態IP[root@Ubuntu18 ~]#vim /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [192.168.8.10/24,10.0.0.10/8]
gateway4: 192.168.8.1
nameservers:
search: [magedu.com, magedu.org]
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
配置好後,查看方式如下:
a) 查看ip和gateway[root@Ubuntu18 ~]#ip addr
[root@Ubuntu18 ~]#route -n
b) 查看DNS[root@Ubuntu18 ~]#systemd-resolve --status
2、編寫腳本實現登錄遠程主機(實現expect和shell腳本兩種形式)
(1) expect腳本形式[root@CentOS8 script]#cat expect4
#!/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" }
}
interac[root@CentOS8 script]#./expect4 10.0.0.71 root root123
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Jan 26 17:15:54 2021 from 10.0.0.1[root@CentOS7-1 ~]#
(2) shell腳本形式[root@CentOS8 script]#vim expect5.sh
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "echo abc | mail -s just_test root\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
[root@CentOS8 script]#./expect5.sh 10.0.0.71 root root123
3、生成10個隨機數保存於數組中,並找出其最大值和最小值
[root@CentOS8 script]#cat sort.sh
#!/bin/bash
declare -i min max
declare -a number
for ((i=0;i<10;i++));do
number[$i]=$RANDOM
[ $i -eq 0 ] && min=${number[$i]} && max=${number[$i]} && continue
[ ${number[$i]} -gt $max ] && max=${number[$i]}
[ ${number[$i]} -lt $min ] && min=${number[$i]}
done
echo ${number[]}
echo max=$max min=$min*
4、生成若干個數值存入數組中,採用冒泡法進行升序或降序排序
(1) 升序排列
[root@CentOS8 script]#vim bubble_sort.sh
#!/bin/bash
##############################################
#File Name: bubble_sort.sh
#Version: V1.0
#Author: LiRui
#Created Time: 2021-02-01 09:23:53
#Description: The test script
##############################################
n=$1
declare -a number
for ((i=0;i<$n;i++));do
number[$i]=$RANDOM
done
echo ${number[@]}
echo " ************************"
if [ $n -gt 1 ];then
for ((j=$[$n-1];j>0;j--));do
for ((k=0;k<$j;k++));do
if [ ${number[$k]} -lt ${number[$k+1]} ];then
temp=${number[$k]}
number[$k]=${number[$k+1]}
number[$k+1]=$temp
fi
done
echo ${number[@]}
done
elif [ $n -eq 1 ];then
echo ${number[@]}
fi
[root@CentOS8 script]#bash bubble_sort.sh 4
31629 30118 5066 27319
31629 30118 27319 5066
31629 30118 27319 5066
31629 30118 27319 5066[root@CentOS8 script]#bash bubble_sort.sh 6
623 445 11470 11797 1367 15913
623 11470 11797 1367 15913 445
11470 11797 1367 15913 623 445
11797 11470 15913 1367 623 445
11797 15913 11470 1367 623 445
15913 11797 11470 1367 623 445
(2) 降序排列
[root@CentOS8 script]#vim bubble3_sort.sh
#!/bin/bash
n=$1
declare -a number
for ((i=0;i<$n;i++));do
number[$i]=$RANDOM
done
echo ${number[@]}
echo " ************************"
if [ $n -gt 1 ];then
for ((j=$[$n-1];j>0;j--));do
for ((k=0;k<$j;k++));do
if [ ${number[$k]} -gt ${number[$k+1]} ];then
temp=${number[$k]}
number[$k]=${number[$k+1]}
number[$k+1]=$temp
fi
done
echo ${number[@]}
done
elif [ $n -eq 1 ];then
echo ${number[@]}
fi
[root@CentOS8 script]#bash bubble3_sort.sh 4
15524 21396 8625 29902
15524 8625 21396 29902
8625 15524 21396 29902
8625 15524 21396 29902[root@CentOS8 script]#bash bubble3_sort.sh 7
6985 26599 10050 26870 5789 8515 14775
6985 10050 26599 5789 8515 14775 26870
6985 10050 5789 8515 14775 26599 26870
6985 5789 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870