【已解决】Ubuntu修改主机名(hostname),以及删除当前用户,新建用户 (以腾讯云服务器Ubuntu为例)
最新在用腾讯云服务器的Ubuntu 18.04 server的时候,其自动设置了主机名(VMxxx一长串),还自动把用户名设置为ubuntu。我个人看不下去,想改一下,这里记录全过程。
前言
创作开始时间:2021年6月28日16:20:28
最新在用腾讯云服务器的Ubuntu 18.04 server的时候,其自动设置了主机名(VMxxx一长串),还自动把用户名设置为ubuntu。我个人看不下去,想改一下,这里记录全过程。
环境
- 腾讯云服务器Ubuntu 18.04 server版本
- 我本地的电脑是windows 10.
1、改主机名
这个比较好操作。
sudo nano /etc/hostname
# 这里把原来的主机名替换为自己想要的主机名
sudo nano /etc/hosts
# 一样的,这里也是把原来的主机名替换为自己想要的主机名
# 重启一下就可以了
sudo reboot
2、删除用户
这个有点坑。因为不能直接sudo deluser --remove-home ubuntu
,因为我xshell上就是用ubuntu 用户登录的,而:
1)我用sudo killall -u username && sudo deluser --remove-home -f username
,也不行,因为运行完&&
前面的指令后,xshell连接直接断开了。
2)我想用root进行xshell的ssh连接,然而密码不对,我用了sudo passwd root
设置了密码,然而也不对!!惊了,感觉腾讯云服务器没有root密码。
最后,我急中生智,这样进行了root连接:
1)我现在本地windows生成了公钥,私钥,然后把公钥拷贝到我服务器root用户下.ssh下面的authorized_keys文件中。
2)然后在xshell root连接,如下图,不输入密码,直接加载用户秘钥,这样就可以登录啦!
3)然后
deluser --remove-home <你想要删除的用户名字>
会发现已经成功了。
3、新建用户
# 添加用户
sudo adduser xxx
groups xxx
# 赋予sudo权限
sudo usermod -aG sudo xxx
#查看所在组
groups xxx
其中,xxx
是你想要添加的用户名。
4、遇到的问题及解决方案
4.1 ubuntu 18.04 不能直接改hostname
需要先运行:
/etc/cloud/cloud.cfg
把里面的
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
改为:
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
然后再执行上面的操作。即可。
小结
以上。
创作结束时间:2021-06-28 16:44:04
参考文献
参考我自己的博客:
- Ubuntu新建用户并给予sudo权限
- Ubuntu kill 杀掉进程 (关键词匹配) https://dalewushuang.blog.csdn.net/article/details/105530551
- 【详解】ubuntu切换到root用户 https://dalewushuang.blog.csdn.net/article/details/118272199
其他主要参考:
- How can I delete a user in linux when the system says its currently used in a process https://stackoverflow.com/questions/28103045/how-can-i-delete-a-user-in-linux-when-the-system-says-its-currently-used-in-a-pr
- Ubuntu Linux Change Hostname (computer name) https://www.cyberciti.biz/faq/ubuntu-change-hostname-command/
后面还发现可以直接改用户名:
- Ubuntu下更改用户名和主机名 https://www.cnblogs.com/zeusmyth/p/6231350.html 但是我没实践,因为安全性未知。
更多推荐
所有评论(0)