ludwig125のブログ

頑張りすぎずに頑張る父

Ubuntu14.0.4でホスト名とユーザ名を変更する

概要

VMのUbuntu14.0.4でホスト名とユーザ名を変更したのが意外と大変だったのでメモ

以下では、ホスト名も、ユーザ名も、gitアカウントと同じ「ludwig125」にすることを目指した

参考

Ubuntu14.04でホスト名を変更する - Qiita

ホスト名

ホスト名:hostnameコマンドの結果出力するUbuntuのホスト名を指す

ホスト名の変更は簡単だった

  • 以下のホスト名を変更するだけ
 sudo vim /etc/hostname
 sudo vim /etc/hosts

反映させるにはUbuntuの再起動が必要

確認

$ hostname
ludwig125-virtual-machine

※「-virtual-machine」の部分をつけたほうがホスト名として分かり易いのでつけた

ユーザ名の変更

ユーザ名:whoコマンドで出力されるユーザの名前

ユーザ名の変更はusermodで行う

Man page of USERMOD

usermodをする上での問題

しかし、自分のユーザ名を変更しようとしても、自分自身のプロセスがあるので変えられないという問題がある

$ sudo usermod -l ludwig125 <今のユーザ名>
usermod: user <今のユーザ名> is currently used by process 1495
$ 

別のユーザを作る

自分自身のプロセスが動いている状態で自分のユーザ名を変えられないので、別にユーザを作った (もっといい方法があるかもしれない・・・)

  • adduserで「temp_user」ユーザを追加
$ sudo adduser temp_user
ユーザー `temp_user' を追加しています...
新しいグループ `temp_user' (1001) を追加しています...
新しいユーザー `temp_user' (1001) をグループ `temp_user' に追加しています...
ホームディレクトリ `/home/temp_user' を作成しています...
`/etc/skel' からファイルをコピーしています...
新しい UNIX パスワードを入力してください: 
新しい UNIX パスワードを再入力してください: 
passwd: パスワードは正しく更新されました
temp_user のユーザ情報を変更中
新しい値を入力してください。標準設定値を使うならリターンを押してください
    フルネーム []: temp
    部屋番号 []: 
    職場電話番号 []: 
    自宅電話番号 []: 
    その他 []: 
以上で正しいですか? [Y/n] y

temp_userにsudo権限を追加

作ったばかりのtemp_userにはsudo権限がないので temp_userでusermodをすると以下のエラーが出た

$ usermod
コマンド 'usermod' は '/usr/sbin/usermod' で利用できます
'/usr/sbin'がPATH環境変数に含まれていないためコマンドを特定できませんでした。
大抵の場合、お使いのユーザーアカウントに管理者特権がないことが原因です。
usermod: コマンドが見つかりません
temp_user@ludwig125-virtual-machine:/home$ useradd
コマンド 'useradd' は '/usr/sbin/useradd' で利用できます
'/usr/sbin'がPATH環境変数に含まれていないためコマンドを特定できませんでした。
大抵の場合、お使いのユーザーアカウントに管理者特権がないことが原因です。
useradd: コマンドが見つかりません

temp_userでusermodをできるように、予めtemp_userにsudo権限を付けておく

sudoユーザーを追加する方法 - Linux入門 - Webkaru

以下はtemp_userではなく、もとのユーザ権限で実行

 usermod -G sudo temp_user

temp_userでログインし直す

ubuntuのユーザをみて、temp_userに切り替える

temp_userでusermod実行

temp_user内で改めてusermodを実行

$ sudo usermod -l ludwig125 <今のユーザ名>
usermod: user <今のユーザ名> is currently used by process 1495
$ 

もとのユーザのプロセスが動いているので、全部killする

sudo kill -9 プロセスID

再度usermodを実行したら、今度はうまくいった

ユーザディレクトリの名前を変更

$ ls /home
$ cd /home
$ sudo mv もとのユーザ ludwig125
$ ls /home
ludwig125  temp_user

whoで確認

[~] $ who
ludwig125 :0           2017-12-16 22:42 (:0)
ludwig125 pts/5        2017-12-16 22:46 (:0:S.0)
ludwig125 pts/13       2017-12-16 22:46 (:0:S.1)
[~] $ 

更に/etc/passwdのユーザディレクトリを変える

$ sudo vim /etc/passwd
で
ludwig125:x:1000:1000:ludwig125:/home/ludwig125:/bin/bash
↑この/home以下のディレクトリ名をもとのユーザ名から変える

上記までの対応によって生じた問題

トラブル1.VMとホストOSとの間でコピペができない

名前変更後、VMのデータをコピーしてホストOSのWindowsにペーストできないという問題が生じた

ホスト名を変えてしまったのでvmwaretoolsが機能していない可能性がある

いったんVMもホストOSも再起動したらコピペができるようになった

VMware Tools の概要
VMware Tools は、仮想マシンのゲスト OS のパフォーマンスを強化し、仮想マシンの管理を改善するユーティリティのスイートです。ゲスト OS に VMware Tools をインストールしておかないと、ゲストで重要な機能を実行できません。VMware Tools をインストールすると、次のような問題が排除または改善されます。
ビデオの解像度が低い
カラー深度が不十分
ネットワーク速度の表示が正しくない
マウスの動きが制限される
コピー アンド ペーストやファイルのドラッグ アンド ドロップができない
音声が出ない
ゲスト OS の静止スナップショットを作成する機能が用意される
ゲスト OS とホストの時刻が同期される

トラブル2. linuxのグループが変わっていない

グループ名が昔のまま変わっていなかったので変える

usermod -g グループ名 ユーザ名
[/home] $ sudo groupadd ludwig125
[/home] $ sudo  usermod -g  ludwig125 ludwig125
[/home] $ 

トラブル3. git に接続できない

名前変更後、gitに接続できない

git pullできない 「Permission denied (publickey).」が出た

[~/git/work] $ git pull
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
[~/git/work] $ 

以下を参考に調べて見る

githubで"Permission denied (publickey)."が出た時はssh -Tで繋ぎ直す - Qiita Error: Permission denied (publickey) - User Documentation Git - SSH 公開鍵の作成

[~/git/work] $ ssh -vT git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.255.112] port 22.
debug1: Connection established.
debug1: identity file /home/ludwig125/.ssh/id_rsa type 1
debug1: identity file /home/ludwig125/.ssh/id_rsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_dsa type -1
debug1: identity file /home/ludwig125/.ssh/id_dsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_ecdsa type -1
debug1: identity file /home/ludwig125/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_ed25519 type -1
debug1: identity file /home/ludwig125/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: Remote protocol version 2.0, remote software version libssh_0.7.0
debug1: no match: libssh_0.7.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/ludwig125/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/ludwig125/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/ludwig125/.ssh/id_dsa
debug1: Trying private key: /home/ludwig125/.ssh/id_ecdsa
debug1: Trying private key: /home/ludwig125/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denie
[~/git/work] $

ホスト名を変えたからなのか、なぜなのかわからないけれど、git にssh鍵が登録されていないみたい

以前書いた自分のブログを参考にssh鍵を作って登録し直す

ludwig125.hatenablog.com

登録後

[~/git/work] $ ssh -vT git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.255.113] port 22.
debug1: Connection established.
debug1: identity file /home/ludwig125/.ssh/id_rsa type 1
debug1: identity file /home/ludwig125/.ssh/id_rsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_dsa type -1
debug1: identity file /home/ludwig125/.ssh/id_dsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_ecdsa type -1
debug1: identity file /home/ludwig125/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/ludwig125/.ssh/id_ed25519 type -1
debug1: identity file /home/ludwig125/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: Remote protocol version 2.0, remote software version libssh_0.7.0
debug1: no match: libssh_0.7.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/ludwig125/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.255.113]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = ja_JP.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi ludwig125! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3312, received 1872 bytes, in 0.2 seconds
Bytes per second: sent 14182.2, received 8016.0
debug1: Exit status 1
[~/git/work] $

成功 git pullもgit pushもできるようになった! ブログ書いててよかったよ

トラブル4. pip3が使えなくなってた

誤ったインタプリタです: そのようなファイルやディレクトリはありません

pip3を使おうとしたらこんなエラーが

[~/git/work/src/python/memo/pandas] $ pip3 install pandas_ply
/home/ludwig125/.pyenv/pyenv.d/exec/pip-rehash/pip: /home/ludwig125/.pyenv/versions/3.5.4/bin/pip3: /home/前のユーザ名/.pyenv/versions/3.5.4/bin/python3.5: 誤ったインタプリタです: そのようなファイルやディレクトリはありません

前のユーザ名のパスを見ようとしている?

解決できないのでpyenvごと削除して作り直す

[~] $ pyenv versions
* system (set by /home/ludwig125/.python-version)
  3.5.4
[~] $ pyenv uninstall 3.5.4
pyenv: remove /home/ludwig125/.pyenv/versions/3.5.4? yes
[~] $ 


[~] $  pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
-> https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
Installing Python-3.5.4...
Installed Python-3.5.4 to /home/ludwig125/.pyenv/versions/3.5.4

$ pyenv local 3.5.4で移動

これでpip3を使えるようにになった

[~] $ 
[~] $ pip3 --version
pip 9.0.1 from /home/ludwig125/.pyenv/versions/3.5.4/lib/python3.5/site-packages (python 3.5)
[~] $