linux shell命令

本文介绍Linux环境下Shell脚本的高级用法,包括命令行选项控制、用户免密sudo配置及数组遍历技巧。同时,详细讲解如何通过ssh-keygen生成密钥对,实现服务器间SSH免密登录,提升自动化运维效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、常用命令

1.

shell 脚本中 : set -x 是开启, set +x是关闭

set -e 表示有报错即退出, set +e 表示关闭这种设置。 set -e 等价于 set -o errexit , set +e 等价于 set +o errexit 。

2. 设置用户免密sudo

(1) visudo

visudo后在编辑内容中加入
chris ALL=(ALL:ALL) ALL
chris为需要免密执行sudo的用户名

(2) 命令追加

或者使用root执行命令
echo “${user} ALL=(ALL) NOPASSWD:ALL” >> /etc/sudoers

二、代码片段

1、遍历数组

for in默认支持两种

hosts=(192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229)
#hosts="192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229"
for host in ${hosts[@]}; do
    echo $host
done

for(( i=0;i<${#hosts[@]};i++)) do
    echo ${hosts[i]};
done;

for i只支持数组

hosts=(192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229)
for(( i=0;i<${#hosts[@]};i++)) do
    echo ${hosts[i]};
done;

2.自动交互

Shell脚本实现自动输入密码登录服务器

三、linux使用

1. ssh登录提示The authenticity of host ‘192.168.68.xx (192.168.68.xx)’ can’t be established

[root@docker136 ~]# ssh root@192.168.72.137 "echo a"
The authenticity of host '192.168.72.137 (192.168.72.137)' can't be established.
ECDSA key fingerprint is SHA256:dCHiCA3DV94HF9AP9IoA22UFXBAWfvap9JXaQQ3Ccdo.
ECDSA key fingerprint is MD5:0d:45:ba:eb:ec:a3:8c:93:db:ce:3c:82:5e:d0:be:ae.
Are you sure you want to continue connecting (yes/no)?

(1)ssh -o StrictHostKeyChecking=no

执行一下:
ssh -o StrictHostKeyChecking=no 192.168.xx.xx
重新连接,OK

(2)修改文件

The authenticity of host 192.168.0.xxx can't be established.

修改/etc/ssh/ssh_config中的StrictHostKeyChecking的ask 为no就可以

2.服务器间免密登录

ssh-keygen后一直回车生成公钥和私钥

[root@docker136 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1f0jlyujwSUS0ENpw43VyWlekC38gWdIoUqVVDWcpaM root@docker136
The key's randomart image is:
+---[RSA 2048]----+
|       .+.*+B=%o+|
|        .O.+.& X.|
|        .o+.+ X .|
|        ..o  o +.|
|        So .Eo +.|
|          o o o o|
|           o o . |
|            o o  |
|           .     |
+----[SHA256]-----+

查看ls /root/.ssh

[root@docker136 ~]# ls /root/.ssh
id_rsa  id_rsa.pub  known_hosts

ssh-copy-id 拷贝公钥到指定主机,接口实现免密执行ssh和scp等命令了

[root@docker136 ~]# ssh-copy-id root@192.168.72.137
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.72.137 (192.168.72.137)' can't be established.
ECDSA key fingerprint is SHA256:dCHiCA3DV94HF9AP9IoA22UFXBAWfvap9JXaQQ3Ccdo.
ECDSA key fingerprint is MD5:0d:45:ba:eb:ec:a3:8c:93:db:ce:3c:82:5e:d0:be:ae.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.72.137's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.72.137'"
and check to make sure that only the key(s) you wanted were added.

总结

# 1.生成公钥和私钥
ssh-keygen
# 2. ssh-copy-id 复制公钥到指定主机,根据提示输入yes(第一次需要)之后输入密码,以后就可以免密ssh scp等操作
ssh-copy-id root@192.168.72.137

设置shell脚本输出字体和颜色

bold= ( t p u t b o l d ) u n d e r l i n e = (tput bold) underline= (tputbold)underline=(tput sgr 0 1)
reset=$(tput sgr0)

red= ( t p u t s e t a f 1 ) g r e e n = (tput setaf 1) green= (tputsetaf1)green=(tput setaf 76)
white= ( t p u t s e t a f 7 ) t a n = (tput setaf 7) tan= (tputsetaf7)tan=(tput setaf 202)
blue=$(tput setaf 25)

Headers and Logging

underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}

set -e
set +o noglob
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值