ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Linux 系统相关的命令

2026/8/15 14:40:47 拓冰建站 浏览量
Linux 系统相关的命令

参考资料

  1. Linux之chmod使用
  2. 【linux】chmod命令详细用法

目录

  • 一. 系统用户相关
    • 1.1 查看当前访问的主机和用户
    • 1.2 切换用户
      • 1.2.1 设置root用户密码
      • 1.2.2 普通用户和root用户切换
    • 1.3 系统状态
      • 1.3.1 vmstat 查看当前系统的状态
    • 1.4 输入历史
      • 1.4.1 history 查看系统中输入过的命令
      • 1.4.2 fc命令列出最近输入的命令
  • 二. 内存
    • 2.1 查看可用内存
  • 三. 进程相关
    • 3.1 ps 查看系统中的进程
    • 3.2 lsof 查看占用文件的进程
  • 四. 命令相关
    • 4.1 `which` 查看命令所在位置
    • 4.2 `type` 判断命令的来源
    • 4.3 `whereis` 查找程序、man、源码等
  • 九. 其他
    • 9.1 时区查看与修改
    • 9.2 关闭登录提示消息

一. 系统用户相关

1.1 查看当前访问的主机和用户

# 查看当前访问的主机uname-n# 查看当前访问的用户id-un# 使用 ; 来将多个命令结合起来uname-n;id-un;

1.2 切换用户

1.2.1 设置root用户密码

  • 在ubuntu中,默认是没有开放root用户登录
  • 可以通过sudo passwd root命令来给root用户设置密码
  • 系统会提示输入当前用户的密码,然后再输入两次设置的root用户密码
sudopasswdroot

1.2.2 普通用户和root用户切换

⏹普通用户切换到root用户

su-

⏹切换到普通用户

su- 普通用户名

1.3 系统状态

1.3.1 vmstat 查看当前系统的状态

  • vmstat 表示查看系统的状态
  • 1表示每一秒显示一次
vmstat1

1.4 输入历史

1.4.1 history 查看系统中输入过的命令

⏹history用户列出所有输入的命令之后,通过grep命令缩小范围

history|grep"ls -l"|wc-l
[2023-11-1214:05:26.146]fengyehong@ubuntu:~$history|grep"ls -l"|wc-l[2023-11-1214:05:28.804]3

⏹通过!履历数字的方式,可以快读执行历史命令

apluser@FengYeHong-HP:0727$history|grepla989http POST https://jsonplaceholder.typicode.com/poststitle='foo'body='bar'userId:=11724la-l1987history|grepla apluser@FengYeHong-HP:0727$!1724la-ltotal8-rw-r--r--1apluser apluser194Jul2716:55 my_script_child_process.sh -rw-r--r--1apluser apluser203Jul2716:33 my_script_no_child_process.sh

1.4.2 fc命令列出最近输入的命令

⏹列出当前用户最近输入的100条命令,也可以通过!履历数字的方式来执行历史命令

apluser@FengYeHong-HP:0727$ fc-l-100|head1895rmbigfile.csv1896ls-l1897clear1898ls-l1899bashmy_script_child_process.sh&1900jobs-l1901ps-ef|grep-a"[m]y_script_child_process.sh"1902ps-opid,pgid,ppid,cmd--forest1903pstree-p6591904ls-lbigfile.csv apluser@FengYeHong-HP:0727$!1901ps-ef|grep-a"[m]y_script_child_process.sh"

二. 内存

2.1 查看可用内存

apluser@ubuntu22-1:~$free-h--totaltotal usedfreeshared buff/cache available Mem:7.7Gi6.6Gi 136Mi 111Mi 979Mi 816Mi Swap:2.0Gi1.1Gi 952Mi Total:9.7Gi7.7Gi1.1Gi apluser@ubuntu22-1:~$

三. 进程相关

3.1 ps 查看系统中的进程

⭐详情可查看下面这篇博客⭐
Linux ps命令

3.2 lsof 查看占用文件的进程

⭐详情可查看下面这篇博客⭐
Linux lsof 命令


四. 命令相关

4.1which查看命令所在位置

⭕当用户自定义了命令之后,也可是通过该命令进行查看。

whichgzip# /bin/gzipwhichgunzip# /bin/gunzipwhichwhich# /usr/bin/which

4.2type判断命令的来源

apluser@ubuntu22-1:~$typejavajavais /usr/bin/java apluser@ubuntu22-1:~$ apluser@ubuntu22-1:~$type-ajavajavais /usr/bin/javajavais /bin/java apluser@ubuntu22-1:~$

4.3whereis查找程序、man、源码等

apluser@ubuntu22-1:~$whereislsls: /usr/bin/ls /usr/share/man/man1/ls.1.gz apluser@ubuntu22-1:~$ apluser@ubuntu22-1:~$whereisgitgit: /usr/bin/git /usr/share/man/man1/git.1.gz apluser@ubuntu22-1:~$

九. 其他

9.1 时区查看与修改

⏹修改系统时区

# 修改当前系统的时区为东9区(日本时间)sudotimedatectl set-timezone Asia/Tokyo# 修改当前系统的时区为东8区(中国时间)sudotimedatectl set-timezone Asia/Shanghai

⏹在当前会话中,临时修改时区

# 东9区(日本时间)exportTZ='Asia/Tokyo'# 东8区(中国时间)exportTZ='Asia/Shanghai'

⏹设置系统时间与网络时间同步

sudotimedatectl set-ntptrue

⏹查看系统时间修改

  • timedatectl
  • 显示当前系统的时间、日期、时区及时间同步状态等信息。
apluser@ubuntu24-01:~$ timedatectl Local time: Mon2024-12-3014:15:10 JST Universal time: Mon2024-12-30 05:15:10 UTC RTC time: Mon2024-12-30 05:15:10 Time zone: Asia/Tokyo(JST, +0900)System clock synchronized:yesNTP service: active RTCinlocalTZ: no

9.2 关闭登录提示消息

  • /etc/update-motd.d/是 Linux 系统中用于动态生成登录消息(MOTD, Message of the Day)的目录。
  • MOTD是用户通过 SSH 或直接登录系统时显示的欢迎消息。
  • 这个机制允许管理员通过脚本动态生成消息内容,提供系统信息、公告或状态提示。
apluser@ubuntu24-01:~$ls-l/etc/update-motd.d/ total52-rwxr-xr-x1root root1220Apr22202400-header -rw-r--r--1root root1151Apr22202410-help-text lrwxrwxrwx1root root46Aug2723:2650-landscape-sysinfo ->/usr/share/landscape/landscape-sysinfo.wrapper -rw-r--r--1root root5023Apr22202450-motd-news -rwxr-xr-x1root root84Aug2723:2685-fwupd -rwxr-xr-x1root root218Aug2723:2690-updates-available -rwxr-xr-x1root root296Aug2723:2691-contract-ua-esm-status -rwxr-xr-x1root root558Jun22202491-release-upgrade -rwxr-xr-x1root root165Feb13202492-unattended-upgrades -rwxr-xr-x1root root379Aug2723:2695-hwe-eol -rwxr-xr-x1root root111Aug2723:2697-overlayroot -rwxr-xr-x1root root142Aug2723:2698-fsck-at-reboot -rwxr-xr-x1root root144Aug2723:2698-reboot-required

😓部分登录提示消息,没什么用,看着很碍眼

apluser@ubuntu24-01:~$sh/etc/update-motd.d/50-motd-news * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s just raised the barforeasy, resilient and secure K8s cluster deployment. https://ubuntu.com/engage/secure-kubernetes-at-the-edge
apluser@ubuntu24-01:~$sh/etc/update-motd.d/10-help-text * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro

🤔可以像下面这种方式,直接取消对应脚本的执行权限即可

sudochmod-x/etc/update-motd.d/50-motd-newssudochmod-x/etc/update-motd.d/10-help-text

💪再一次通过ssh命令登录之后,页面就清爽了很多