Ubuntu常规操作记录
1.更换阿里云源
换源原因?
还用说吗,官方服务器在海外,速度太慢了。
更换源步骤:
1.更改源文件的权限
如果是以普通用户状态进入系统的,推荐先更改源文件的独写权限,这样子方便后续维护操作。
sudo chmod 777 /etc/apt/sources.list
2.更改源文件内容
可以利用vim等编辑器进入sources.list
之后禁用默认的源,直接在默认源之前加入#即可。
vi /etc/apt/sources.list
然后在sources.list
里面添加以下内容(阿里云)
<p style="color:red">Ubuntu 16安装国内阿里云源</p>
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
<p style="color:red">Ubuntu 18安装国内阿里云源</p>
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
[scode type="blue"]<p style="color:red">ubuntu20.04 LTS 更换国内163源、阿里源、清华源、中科大源</p>
阿里云源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
清华源
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
网易163源
deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
[/scode]
[scode type="blue"]<p style="color:red">Ubuntu 20.10安装国内阿里云源</p>
deb http://mirrors.aliyun.com/ubuntu/ groovy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ groovy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ groovy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ groovy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ groovy-backports main restricted universe multiverse
# 预发布软件源,不建议启用
#deb http://mirrors.aliyun.com/ubuntu/ groovy-proposed main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ groovy-proposed main restricted universe multiverse
<p style="color:red">Ubuntu 20.10安装国内清华源</p>
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
<p style="color:red">Ubuntu 20.10安装国内中科大源</p>
deb https://mirrors.ustc.edu.cn/ubuntu/ groovy main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ groovy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
## Not recommended
# deb https://mirrors.ustc.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
[/scode]
3.更新与升级
sudo apt-get update
#检查更新
sudo apt-get upgrade
#更新软件包
sudo apt-get dist-upgrade
#更新内核
上面这两个命令的顺序尽量不要错,调换顺序的影响暂时不清楚,也不想尝试,麻烦。。
另外,如果我们使用dpkg
强制安装了某些deb
包,或者在build-dep
的是否手动更改了某些Packages
的文件和版本时, 那么在再次使用apt-get install
或者build-dep
来安装library
和packages
的时就很可能出现问题,出现unmet dependencies
。
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
解决方法
使用系统提示的apt-get -f install
命令去修复
2.更改root密码
终端输入
sudo passwd root
/*更改用户root密码*/
[sudo] password for user:
/*然后输入当前登陆用户密码*/
Enter new UNIX password:
/*然后输入新的root用户密码*/
Retype new UNIX password:
/*再次确认新的root用户密码*/
passwd: password updated successfully
//修改成功
3.更改ssh连接端口
服务器默认为22端口,这样会造成有被暴力破解密码
的风险,下面是更换ssh端口过程
1.添加ssh端口
vim /etc/ssh/sshd_config
打开配置文件,添加我们需要更改的端口号,此时不要删除默认22端口,让两个端口同时存在,如果我们直接修改了端口,然后启动防火墙之后,就会出现我们没有使用防火墙开放端口,导致我们连接不上服务器,我们暂且保留默认22,如果更改过后,使用新端口号没问题,再删除默认22端口不迟
我们想把端口改为9981,就如图添加上去,保存退出,然后重启ssh服务
service sshd restart
# 乌班图
systemctl restart sshd.service
# Centos7
###4.配置防火墙规则(两种方案任选其一)
A.配置防火墙规则
#启动防火墙
systemctl start firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
# 重启防火墙
systemctl restart firewalld.service
# 查看防火墙状态
systemctl status firewalld.service
# 设置开机启动防火墙
systemctl enable firewalld.service
# 设置开机不启动防火墙
systemctl disable firewalld.service
首先启动防火墙,然后添加防火墙规则
firewall-cmd --zone=public --add-port=9981/tcp --permanent
#说明:开放9981端口
# –zone 作用域
# –add-port=9981/tcp 添加端口,格式为:端口/通讯协议
# –permanent 永久生效,没有此参数重启后失效
# 添加完毕过后重新读取防火墙规则或者重启防火墙,规则才生效
# 重新读取防火墙规则
firewall-cmd --reload
# 或者重启防火墙:
systemctl restart firewalld.service
#多个端口
firewall-cmd --zone=public --add-port=80-90/tcp --permanent
#centos7查看防火墙所有信息
firewall-cmd --list-all
#centos7查看防火墙开放的端口信息
firewall-cmd --list-ports
#删除80端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
#关闭39000-40000端口
firewall-cmd --zone=public --remove-port=39000-40000/tcp --permanent
关闭或开启端口后都需要重启防火墙:
systemctl restart firewalld.service
#重启防火墙:
firewall-cmd --list-ports
#centos7查看防火墙开放的端口信息
[scode type="yellow"]断开当前ssh连接,然后把ssh连接端口改为我们示例所修改的9981尝试连接,如果连接正常,我们继续如下步骤。[/scode]
正常连接过后,我们此时就可以删除(或者注释)默认的22端口了
vim /etc/ssh/sshd_config
# 删除(或者注释)Port 22 这一行,只留下我们的Port 9981,然后重启ssh服务
systemctl restart sshd.service
至此,默认ssh端口修改完成。
B.配置防火墙规则
#查看是否安装过ufw
sudo dpkg --get-selections | grep ufw
#安装ufw
sudo apt-get install ufw
#默认规则为拒绝
ufw default deny
#防火墙9981端口的tcp访问
sudo ufw allow 9981
#开放vnc服务端口(如不需要可以不操作)
sudo ufw allow 5901
sudo ufw allow 5902
#将22端口改为9981
sudo vi /etc/ssh/sshd_config
#重启ssh服务
service sshd restart
#查看更改是否生效
netstat -tlnp
#防火墙生效并开机启动
ufw enable
#检查设置规则
sudo ufw status
[scode type="blue"]最后可以重启系统后验证shutdown -r now
[/scode]
5.启用root ssh登陆
sudo passwd root # 更改root 密码
sudo vi /etc/ssh/sshd_config #编辑配置文件
PermitRootLogin prohibit-password
#需要 在 PermitRootLogin prohibit-password 加# 注释
PermitRootLogin yes
#启用root ssh登陆
[collapse status="false" title="完整代码如下:"]
# Authentication:
LoginGraceTime 120
# PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes
[/collapse]
输入命令sudo update-rc.d ssh defaults
开启ssh服务开机自启动,然后
sudo /etc/init.d/ssh restart
重启ssh连接
6.安装桌面环境
在命令环境下运行如下命令:
apt-get update -y //更新软件库
apt-get upgrade -y //升级软件
apt-get install ubuntu-desktop -y //安装Ubuntu桌面系统
安装时间有点长,需要耐心等待。reboot重启后发现桌面环境不能使用root账户登录.
重启然后使用Ctrl + Alt + F1 进入命令行使用root登陆
运行命令
sudo vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
# 打开50-ubuntu.conf
在末尾添加
greeter-show-manual-login=true
# 允许切换用户登陆
allow-guest=false
# 禁用Guest
完整代码
[Seat:*]
user-session=ubuntu
greeter-show-manual-login=true
allow-guest=false
保存重启(在VI模式下编辑完成后使用Esc键切换到末行模式,然后输入:wq
退出编辑)发现root用户登陆后还是有警告
修改/root/.profile
文件 (图形界面下修改请勾选显示隐藏文件)
将mesg n
替换为 tty -s && mesg n
完整代码
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
tty -s && mesg n || true
保存后重启即可完美在桌面环境登陆root