如何从 ssh 配置文件中删除主机条目? [英] How can I remove a Host entry from an ssh config file?
问题描述
文件的标准格式为:
主机示例主机名 example.com端口 2222主机示例2主机名 two.example.com主机三.example.com端口 4444
知道主持人的简称,我想删除整个条目,以便用新的详细信息重新添加.
我得到的最接近的是这个,除了我需要保留以下 Host 声明并且第二个搜索词将捕获两个多个词(例如 HostName
):
sed '/Host example/,/\nHost/d'
使用 GNU sed:
host=示例"sed 's/^Host/\n&/' 文件 |sed '/^Host ''$host''$/,/^$/d;/^$/d'
输出:
<预>主机示例2主机名 two.example.com主机三.example.com端口 4444<块引用>
s/^Host/\n&/
:在以Host"开头的每一行之前插入一个换行符
/^Host '"$host"'$/,/^$/d
: 删除所有匹配Host $host"的行;到下一个空行
/^$/d
:清理:删除每一个空行
The standard format of the file is:
Host example
HostName example.com
Port 2222
Host example2
Hostname two.example.com
Host three.example.com
Port 4444
Knowing the host's short name, I want to remove an entire entry in order to re-add it with new details.
The closest I have got is this, except I need to keep the following Host declaration and the second search term will capture two many terms (like HostName
):
sed '/Host example/,/\nHost/d'
With GNU sed:
host="example"
sed 's/^Host/\n&/' file | sed '/^Host '"$host"'$/,/^$/d;/^$/d'
Output:
Host example2 Hostname two.example.com Host three.example.com Port 4444
s/^Host/\n&/
: Insert a newline before every line that begins with "Host"
/^Host '"$host"'$/,/^$/d
: delete all lines matching "Host $host" to next empty line
/^$/d
: clean up: delete every empty line
这篇关于如何从 ssh 配置文件中删除主机条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!