Ubuntu22.04 resolve.conf 設定スクリプト

Ubuntu

Ubuntu22.04(Ubuntu server)でのresolve.conf 設定スクリプトです。
# 環境に合わせた修正箇所ここから を修正して使用してください。

スクリプト実行前の/etc/resolve.conf

cat /etc/resolv.conf

$ cat /etc/resolv.conf 
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search .

スクリプト実行後

cat /etc/resolv.conf

$ cat /etc/resolv.conf 
nameserver 192.168.0.11
nameserver 192.168.0.1
options edns0 trust-ad
search .

スクリプトを新規作成します。

nano ubuntu_resolv_conf.sh

$ nano ubuntu_resolv_conf.sh

以下のスクリプトをコピーペーストします。# 設定内容ここから から # 環境に合わせた修正箇所ここまで を環境に合わせた値に修正して保存してください。

#!/bin/bash

### /etc/resolv.conf 設定スクリプトVer1
### 参考にさせていただいたURL 
# https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/replacing-etc-resolv-conf-with-a-symbolic-link-to-manually-configure-dns-settings_manually-configuring-the-etc-resolv-conf-file

### 使い方
# nano ubuntu_resolv_conf.sh
# sudo chmod +x ubuntu_resolv_conf.sh
# ./ubuntu_resolv_conf.sh

# 設定内容ここから
# 1.既存のresolv.conf.manually-configured バックアップ
# 2.新規ファイル作成 /etc/resolv.conf.manually-configured
# 3./etc/resolv.conf 削除
# 4./etc/resolv.conf という名前のシンボリックリンクを作成
# 設定内容ここまで

# 動作確認
# lsb_release -a
#No LSB modules are available.
#Distributor ID:	Ubuntu
#Description:	Ubuntu 22.04 LTS
#Release:	22.04
#Codename:	jammy

# 環境に合わせた修正箇所ここから
# nameserver
DNS_IP="192.168.0.11"
DNS_IP2="192.168.0.1"
# options
OPT_WD="edns0 trust-ad"
# search
SEA_WD="." 
# 環境に合わせた修正箇所ここまで

# ----------- 処理ここから -----------
cd

make_resolv_conf_manually-configured() {
        # 新規resolv.conf.manually-configured 作成
        echo nameserver $DNS_IP > resolv.conf.manually-configured
        echo nameserver $DNS_IP2 >> resolv.conf.manually-configured
        echo options $OPT_WD >> resolv.conf.manually-configured  
        echo search $SEA_WD >> resolv.conf.manually-configured 
}

# /etc/resolv.conf.manually-configured 存在する場合
# 1.既存のresolv.conf.manually-configured バックアップ
if [ -e /etc/resolv.conf.manually-configured ]; then
        sudo mv /etc/resolv.conf.manually-configured /etc/resolv.conf.manually-configured.bak
fi

# 2.新規ファイル作成 /etc/resolv.conf.manually-configured
make_resolv_conf_manually-configured

sudo mv resolv.conf.manually-configured /etc/
sudo chown root:root /etc/resolv.conf.manually-configured

# 3./etc/resolv.conf 削除
sudo rm /etc/resolv.conf

# /etc/resolv.conf.manually-configured を参照する
# 4./etc/resolv.conf という名前のシンボリックリンクを作成
sudo ln -s /etc/resolv.conf.manually-configured /etc/resolv.conf
# ----------- 処理ここまで -----------

新規作成したスクリプトを実行可能にします。

sudo chmod +x ubuntu_resolv_conf.sh

$ sudo chmod +x ubuntu_resolv_conf.sh

新規作成したスクリプトを実行します。

./ubuntu_resolv_conf.sh

$ ./ubuntu_resolv_conf.sh


タイトルとURLをコピーしました