見出し画像

conoha vpsで最初にやること

プログラマーの嫌いそうな奴だねw

webのコンソールよりrootログインする

一般ユーザーを作る

ここではadminを作りつつパスワードを設定無効にしている

# adduser --disabled-password admin

権限をスイッチしてsshの準備をする

# su - admin

公開鍵

公開鍵がない?頑張って

wget https://github.com/<ユーザー>.keys

みたいに最近だとgithubでもgitlabでも、この手のアカウントを伝えればそれは公開鍵を伝えたのと同じようになったりもするでしょう。

なお、コンソールが化けるというのはとりあえず気にしなくてok

このような化けは無視する。このコンソールは日本語を表示できないのである。

~/.sshを700で作成する

mkdir ~/.ssh
chmod 700 ~/.ssh

このプロセスはssh localhostしてキャンセルして作成してもいい。

downloadした鍵を~/.ssh/authorized_keysにつっこむ

cat ○○.keys >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

外部ホストから当該ホストにログインする

ssh -l ユーザー  <IP>

ログインできれば終わり。

sudoer

sudoerは手編集せず、visudoを経由すること

# visudo
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
<ユーザー名> ALL=(ALL) NOPASSWD: ALL

とかやってNOPASSWDで通している。

そして

$ sudo apt update
sudo: ホスト xxx-xxx-xxx-xxx の名前解決ができません: 名前またはサービスが不明です
取得:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
取得:2 http://security.debian.org/deb
...

とできればok。sudoで名前解決のエラーになっているのは後で解消しよう。

パッケージの更新

$ sudo apt update; sudo apt upgrade
sudo: ホスト xxx-xxx-xxx-xxx の名前解決ができません: 名前またはサービスが不明です
ヒット:1 http://deb.debian.org/debian bookworm InRelease
ヒット:2 http://deb.debian.org/debian bookworm-updates InRelease
ヒット:3 http://security.debian.org/debian-security bookworm-security InRelease
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
状態情報を読み取っています... 完了
アップグレードできるパッケージが 39 個あります。表示するには 'apt list --upgradable' を実行してください。
N: Repository 'Debian bookworm' changed its 'firmware component' value from 'non-free' to 'non-free-firmware'
N: More information about this can be found online in the Release notes at: https://www.debian.org/releases/bookworm/amd64/release-notes/ch-information.html#non-free-split
sudo: ホスト 160-251-42-13 の名前解決ができません: 名前またはサービスが不明です
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
状態情報を読み取っています... 完了
アップグレードパッケージを検出しています... 完了
以下のパッケージが新たにインストールされます:
  linux-image-6.1.0-12-amd64
以下のパッケージはアップグレードされます:
  base-files bind9-dnsutils bind9-host bind9-libs dbus dbus-bin dbus-daemon
  dbus-session-bus-common dbus-system-bus-common dbus-user-session kpartx
  libc-bin libc-l10n libc6 libcurl3-gnutls libdbus-1-3 libnftables1
  libnss-systemd libpam-systemd libsystemd-shared libsystemd0 libudev1 libxml2
  linux-image-amd64 locales nftables ntp ntpdate ntpsec ntpsec-ntpdate
  ntpsec-ntpdig python3-ntp qemu-block-extra qemu-guest-agent qemu-utils sudo
  systemd systemd-sysv udev
アップグレード: 39 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
92.4 MB のアーカイブを取得する必要があります。
この操作後に追加で 403 MB のディスク容量が消費されます。
続行しますか? [Y/n]

続行するとパッケージが最新になる

DNSの設定

ssh ipアドレスだと辛いので何かの名前を割り当てる、っつってもこれは基本このサーバーでやる事では無いので外のネームサーバーでAレコードを設定する事になるだろう(このホストでやってもいいけど…どっちにしたってNSをこれに向けないといけないね)。

ここでは仮にarchive.example.comとしたとする

hostnameの設定

/etc/hostname

archive

とする

さらに /etc/hosts

127.0.0.1       localhost
127.0.1.1       archive archive.example.com
# [IPアドレス] [ホスト名] [エイリアス1] [エイリアス2] ...

とする。127.0.1.1はループバック2って感じである。まあこの段階ならシステムを再起動した方が早いとおもう。これでsudoの時に名前がどうのこうのが修正されるだろう。

etckeeper

これから/etcを沢山さわっていくに当ってetckeeperを導入してみようってか、host系さわる前の方が本当はよかったかもね。以下はchatgptが説明してくれました。

aptで導入する

root@archive:~# apt install etckeeper
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  git git-man liberror-perl patch
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk gitweb
  git-cvs git-mediawiki git-svn ed diffutils-doc
The following NEW packages will be installed:
  etckeeper git git-man liberror-perl patch
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 9420 kB of archives.
After this operation, 48.2 MB of additional disk space will be used.
Do you want to continue? [Y/n]

このようにデフォルトではgitで行う

この段階で何がどうのこうのってことはないので、更にシステムを設定していってみよう

sshd_config

ssh自体の設定

とまあこれに従ってちょいちょい書き換える。X11転送は、んー、、非常にマレにつかうかもしれん(まあ使わねえか)

変更すると、daily autocommitされてない限り、git diffでこのように検知できる

root@archive:/etc/ssh# git diff
diff --git a/ssh/sshd_config b/ssh/sshd_config
index 0800f1f..ce1db72 100644
--- a/ssh/sshd_config
+++ b/ssh/sshd_config
@@ -30,12 +30,12 @@ Include /etc/ssh/sshd_config.d/*.conf
 # Authentication:

 #LoginGraceTime 2m
-PermitRootLogin yes
+PermitRootLogin prohibit-password
 #StrictModes yes
 #MaxAuthTries 6
 #MaxSessions 10

-#PubkeyAuthentication yes
+PubkeyAuthentication yes

 # Expect .ssh/authorized_keys2 to be disregarded by default in future.
 #AuthorizedKeysFile    .ssh/authorized_keys .ssh/authorized_keys2
@@ -54,7 +54,7 @@ PermitRootLogin yes
 #IgnoreRhosts yes

 # To disable tunneled clear text passwords, change to no here!
-PasswordAuthentication yes
+PasswordAuthentication no
 #PermitEmptyPasswords no

 # Change to yes to enable challenge-response passwords (beware issues with

sshd -tでチェックして

root@archive:~# sshd -t

よければ(出力が無ければ)

root@archive:/etc/ssh# systemctl restart sshd

このように反映する。このsshセッションはキープされるので、この段階で新しいsshを発行して確認しておくこと。

git commit -m "Updated sshd_config"

このように変更をコミットしてもいいし、放置してたら勝手にcronがcommitしていく。この段階でシステムの再起動チェックを行った方が傷口は浅いかもしれん。

fail2ban

ん〜

bookwormのfail2banはちょっと新しいシステムの対応についていけてないフシがあるなーwww 設定をやると動くんだけどディープになってるので、ちょっとスルー。これは別セクションでやるかもだし、やらないかもしれない。

時刻の設定

これは最初から何とかしてくれてるので何とかなると思う(真理)。ntp系も最初から設定されている

パッケージの自動アップデート

list-changesも入れている

root@archive:~# apt install unattended-upgrades apt-listchanges
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
apt-listchanges is already the newest version (3.24).
The following additional packages will be installed:
  gir1.2-glib-2.0 libgirepository-1.0-1 python3-dbus python3-distro-info python3-gi
Suggested packages:
  python-dbus-doc bsd-mailx default-mta | mail-transport-agent needrestart powermgmt-base
The following NEW packages will be installed:
  gir1.2-glib-2.0 libgirepository-1.0-1 python3-dbus python3-distro-info python3-gi unattended-upgrades
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 645 kB of archives.
After this operation, 2544 kB of additional disk space will be used.
Do you want to continue? [Y/n]
dpkg-reconfigure -plow unattended-upgrades

する

-plowは詳細的な設定ウィザードを出すみたいなイメージでいいと思う


これはもちろんYesとするわけだ

システム通知

emailで管理するよりslackで通知した方がよさそうだけどまあ、長くなってきたし、またにしますか。







この記事が気に入ったらサポートをしてみませんか?