1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)
修改主机名
root@ubuntu1804:~# hostnamectl set-hostname ubuntu18
root@ubuntu1804:~# cat /etc/hostname
ubuntu18
root@ubuntu1804:~# hostname
ubuntu18
root@ubuntu1804:~# echo $HOSTNAME
ubuntu1804
root@ubuntu1804:~# exit
logout
wang@ubuntu1804:~$ sudo -i
root@ubuntu1804:~# echo $HOSTNAME
ubuntu18
修改网卡名称
#修改配置文件为下面形式
root@ubuntu18:~#vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
#或者sed修改
root@ubuntu18:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#'
/etc/default/grub
#生效新的grub.cfg文件
root@ubuntu18:~# grub-mkconfig -o /boot/grub/grub.cfg
#或者
root@ubuntu18:~# update-grub
root@ubuntu18:~# grep net.ifname /boot/grub/grub.cfg
linux /boot/vmlinuz-4.15.0-55-generic root=/dev/mapper/ubuntu18--vg-root ro net.ifnames=0
linux /boot/vmlinuz-4.15.0-55-generic root=/dev/mapper/ubuntu18--vg-root ro net.ifnames=0
linux /boot/vmlinuz-4.15.0-55-generic root=/dev/mapper/ubuntu18--vg-root ro recovery nomodeset net.ifnames=0
#重启生效
root@ubuntu18:~# reboot
修改网卡配置
root@ubuntu18:~$ 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:
addresses: [ 10.0.0.108/24 ]
gateway4: 10.0.0.1
nameservers:
addresses:
- "10.0.0.1"
#执行命令使配置生效
root@ubuntu18:~# netplan apply
2、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
[root@node1 ~]# cat ssh.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 "]#" { send "useradd hehe\n" }
expect "]#" { send "echo magedu |passwd --stdin hehe\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
[root@node1 ~]# ./ssh.sh 10.0.0.108 wx562635 wx562635
spawn ssh wx562635@10.0.0.108
The authenticity of host '10.0.0.108 (10.0.0.108)' can't be established.
ECDSA key fingerprint is SHA256:HAjPzqc98ofpb4KoMLCCex2IsIOb7iGFpLfs58tpFtk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.108' (ECDSA) to the list of known hosts.
wx562635@10.0.0.108's password:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-55-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun May 23 21:12:45 CST 2021
System load: 0.0 Processes: 172
Usage of /: 8.1% of 18.61GB Users logged in: 1
Memory usage: 6% IP address for eth0: 10.0.0.108
Swap usage: 0%
0 packages can be updated.
0 updates are security updates.
New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sun May 23 20:56:13 2021 from 10.0.0.2
wx562635@ubuntu18:~$
3、生成10个随机数保存于数组中,并找出其最大值和最小值
root@ubuntu18:~# 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@ubuntu18:~# ./array.sh
All numbers are: [ 21593 18038 4357 2197 28404 4108 1843 18185 17934 23687 ]
Max is 28404
Min is 1843
4、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
root@ubuntu18:~# cat maopao.sh
root@ubuntu18:~# cat maopao.sh
#!/bin/bash
i=0
flag=0
#--------------------------------------------保存数组值---------------------------------------------
while true;do
read -p "请输入数字(输入q退出):" num
if [[ "$num" == ^"" ]];then
echo "你的输入有误,请输入数字"
fi
if [[ $num == q ]];then
break
elif [[ $num =~ ^[-]{0,1}[[:digit:]]{0,}[.]{0,1}[[:digit:]]{0,}[[:digit:]]$ ]];then #筛选出 整数 小数 负数
echo $num
array[$i]=$num
let i++
else
echo "你的输入有误,请输入数字"
fi
done
#---------------------------------------------比较两个数大小------------------------------------------
compare () {
echo $1 > tmp
echo $2 >> tmp
max=$(cat tmp|sort -nr|head -1)
if [[ "$max" == "$1" ]];then
flag=1
else
flag=0
fi
}
#-------------------------------------------冒泡排序---------------------------------------------------
length=${#array[*]}
for i in $(seq 0 $length);do
for j in $(seq 0 $[$length-$i]);do
t=$[$j+1]
compare ${array[$j]} ${array[$t]}
if [ $flag -eq 1 ];then
temp=${array[$j]}
array[$j]=${array[$t]}
array[$t]=$temp
fi
done
done
echo "从小到大排序为:${array[*]}"