サーバー設定メモ Debian trixie(書きかけ)

2025.10.17 Debian trixie 

1. インストール

https://www.debian.org/のDownloadのリンクからdebian-13.1.0-amd64- netinst.isoをダウ ンロード

    usbスティクメモリに書き込む

# cat debian-13.1.0-amd64-netinst.iso > /dev/sdx
# sync

    書き込んだUSBのステックメモリで起動、
   パソコンがUEFIの場合、パーティションの先頭に256MB程のEFI領域を設定する
   残りは / と メモリの半分程度のswap領域を作成
      サーバーでつかうのでディスクトップ関係はインストールしない。gnomeとかxfceなどもインストールせず、
  とりあえずsshサーバーをインストールしておく。

2. vimのインストール、sudoの設定

まずコマンドラインでアップデートしてvimをインストール
$ su
   rootのパスワードを入力
# LANG=C  (文字化けしないおまじない)
# apt-get update; apt-get upgrade -y
# apt-get install vim

ユーザhogeがsudoを使えるようにする
/etc/sudoers.d/010_hoge-nopasswd
hoge ALL=(ALL)NOPASSWD:ALL

ファイルを作成したらパーミッションを変更しておく
# chmod 400  /etc/sudoers.d/010_hoge-nopasswd

3. avahi-daemonのインストール

  家庭内LANやパソコンのvirtualboxに立てたサーバなら、avahi-daemonをインストールすれば  サーバ名.local
でpingが通り、sshでアクセスするときも名前を使えて楽になる。 これは、仕組みはわからないが、インストールするだけで設定もいらないので便利  名 前解決

  これでtrixieという名前のサーバをLAN内に立てたら ssh trixie.localでアクセスすることができる。

4.sshの設定

(1) クライアント側で公開鍵ペアを作成する

鍵を作るときのパスワードはサーバーのパスワードとは別物である。
$  ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hogehoge/.ssh/id_rsa):   このままenter
Enter passphrase (empty for no passphrase):                         パスワードを入れる
Enter same passphrase again:                                        再度パスワードを入れる
Your identification has been saved in /home/hogehoge/.ssh/id_rsa.
Your public key has been saved in /home/hogehoge/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:DXkvga6+uGv9L1dUdRiNEJcLezFVedhNVMhKsB+vEgU hogehoge@akagera
The key's randomart image is:
+---[RSA 4096]----+
|     E.s+o# &    |
|         o co.@.*|
|   +    + +.o= +.|
|    <  . + =+oo  |
| a    S +.o..    |
|   .   A d o..   |
|     ..   .q.    |
|   .o @ . ..     |
|   .+ooo.+.      |
+----[SHA256]-----+


~.ssh/下に秘密鍵id_rsaと公開鍵id_rsa.pubができる

(2)パソコン側から公開鍵をサーバーに送信する

$ ssh-copy-id  -i .ssh/id_rsa.pub  hogehoge@ipアドレス
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host 'raspberrypi.local (192.168.3.30)' can't be established.
ECDSA key fingerprint is SHA256:6LtS3P30HdhynlZJZiPUvD11DTRS+X/oMKt4ADmr4vI.
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
hogehoge@raspberrypi.local's password:   こ こは鍵のパスワードではなくサーバーログインのパスワード

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'hogehoge@ipアドレス"
and check to make sure that only the key(s) you wanted were added.



サーバーにログインしてみる
$ ssh hogehoge@ipアドレス
Enter passphrase for key '/home/hogehoge/.ssh/id_rsa':秘密鍵のパスワードを入れる  

(3)sshのセキュリティ強化

 ・rootのログインの無効化と・パスワード認証の無効化をする。
 (これをやっておかないと、自宅サーバーは経験上数日から1ヶ月程度で陥落する)
  しかし(1)の作業の前にこれをやると、パソコン側から公開鍵をサーバーへ送信できなくなる。
  
  /etc/ssh/sshd_configの以下2箇所を変更


#PermitRootLogin prohibit-password   変更前
PermitRootLogin no          変更後


#PasswordAuthentication yes  変更前
PasswordAuthentication no   変更後

変更後、opensshを再起動して設定を反映させる
$ sudo systemctl restart ssh

5. firewall ufwの設定

(1)インストール

$sudo  apt-get install ufw

(2)デフォルトですべてのポートへの接続を禁止する

$ sudo ufw default deny
なんてことをsshで接続しながらやるとぷっつんするから、リモートでは気をつけよう。

(3)開放するポートを記述(ルールの記述)

(ssh のポート22  これはまず最初に開放しておかないとsshで接続できなくなるので大変)
$ sudo ufw  allow 22
 そのほか80(http)  443(ssl)など必要に応じて開放
  ルールはufwを停止したりサーバーをシャットダウンしても消えない

(4)有効化(ufwの起動)

$ sudo ufw  enable
サーバーを再起動しても有効のままになっている

既にufwが動作しているときはreloadを使う
$ sudo ufw  reload

(5)状態の確認

$ sudo ufw status

(6)ルールの消去  例えば80番ポートのルールを消去するには

$ sudo ufw delete allow 80

(7)ufwの停止(off)

$ sudo ufw disable

6.httpサーバ nginxの設定

   nginxの設定