1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# 方案一:Shell 变量 + 别名(推荐)
export SSH_OPTS="-o HostName=NEW_IP_ADDRESS -o Port=2222"
ssh $SSH_OPTS thc_1
scp $SSH_OPTS file.txt thc_1:~
rsync -av $SSH_OPTS ./dir/ thc_1:~/dir/
# 方案二:临时别名(适合单次会话)
alias ssh-thc='ssh -o HostName=NEW_IP_ADDRESS thc_1'
ssh-thc
unalias ssh-thc
# 方案三:Bash 函数 + 环境变量(最接近 export 体验)
export THC_IP="NEW_IP_ADDRESS"
ssh_thc() {
if [ -n "$THC_IP" ]; then
ssh -o HostName="$THC_IP" thc_1
else
ssh thc_1
fi
}
ssh_thc
# 方案四:全局别名函数(写入 ~/.bashrc 或 ~/.zshrc)
ssh_custom() {
if [ -n "$SSH_CUSTOM_HOSTNAME" ]; then
ssh -o HostName="$SSH_CUSTOM_HOSTNAME" "$@"
else
ssh "$@"
fi
}
alias ssh=ssh_custom
# 使用:export SSH_CUSTOM_HOSTNAME="NEW_IP_ADDRESS" && ssh thc_1
# 恢复:unset SSH_CUSTOM_HOSTNAME && ssh thc_1
|