tools:How do I create a permanent Bash alias?

博客主要探讨如何创建永久Bash别名。介绍了两种常用方法,一是直接在特定文件中添加别名,二是创建单独的别名文件。还提及不同方法的操作步骤及一些用户在使用过程中遇到的问题,如部分标志无效等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

How do I create a permanent Bash alias?

There are a lot of ways to create an alias. The most used ways are:

  1. Add aliases directly in your ~/.bashrc file

    For example: append these line to ~/.bashrc file

    alias ll='ls -l'
    alias rm='rm -i'

    Next time (after you have logged out/in, or done . ~/.bashrc) when you type rm the rm -icommand will be executed.

  2. The second method lets you make a separate aliases file, so you won't have to put them in .bashrc, but to a file of your choice. First, edit your ~/.bashrc file and add the following lines if they don't already exist, or uncomment them if they do:

    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
    fi

    Save it and close the file. After that, all you have to do is create a ~/.bash_aliases file and add your aliases there, with the same format specified in the first method.

    Contents of my ~/.bash_aliases file:

    alias cs='cd;ls'

shareimprove this answer

edited Feb 14 at 16:44

Dan

7,38055 gold badges4747 silver badges7474 bronze badges

answered Dec 15 '10 at 8:21

aneeshep

23.1k1212 gold badges5656 silver badges7474 bronze badges

  • 71

    +1 for using ~/.bash_aliases. – ændrük Jun 10 '11 at 5:48

  • Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias -> alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases", so the alias became avail at saving, and if you make some mistake it will advert you. – erm3nda Apr 27 '17 at 12:08

  • somehow after I added the alias such as alias ls='ls -althr', some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why? – Sajuuk Mar 22 '18 at 8:43 

  • 2

    By default ~/.bashrc contains inclusion for ~/.bash_aliases, no need to edit it. – Jaakko May 14 '18 at 9:49

  • 1

    Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy. – Jose May 30 '18 at 18:10

add a comment

<think>我们正在处理一个关于NFS(NetworkFileSystem)的错误:`clnt_create:RPC:Programnotregistered`。根据提供的引用,这个错误通常发生在配置NFS环境后,使用`showmount-e`命令检查共享目录时。错误信息表明RPC(RemoteProcedureCall)程序没有注册成功。###错误解释这个错误的原因通常是NFS相关的服务没有正确启动,特别是`rpcbind`服务(或portmap服务,取决于系统)和NFS服务。RPC服务用于管理RPC程序的端口映射,NFS依赖于RPC服务来注册其服务。如果RPC服务未运行,或者NFS服务没有成功注册到RPC服务,就会出现这个错误。###解决步骤以下步骤可以帮助解决此问题:1.**检查并启动rpcbind服务**:首先确保`rpcbind`服务已经启动并运行。在大多数Linux发行版中,`rpcbind`是NFS依赖的服务。```bashsystemctlstatusrpcbind```如果服务未运行,启动它:```bashsystemctlstartrpcbindsystemctlenablerpcbind#设置开机自启```2.**检查并启动NFS服务**:确保NFS服务(包括`nfs-server`或`nfs`)已经启动。不同的发行版服务名称可能略有不同(例如,在CentOS/RHEL中通常是`nfs`,而在较新版本中可能是`nfs-server`)。```bashsystemctlstatusnfs-server#或者nfs```如果服务未运行,启动它:```bashsystemctlstartnfs-serversystemctlenablenfs-server```3.**重启服务**:有时候,重启相关服务可以解决问题(注意顺序:先启动rpcbind,再启动NFS):```bashsystemctlrestartrpcbindsystemctlrestartnfs-server```4.**检查RPC注册状态**:使用`rpcinfo`命令检查NFS服务是否已经正确注册到RPC:```bashrpcinfo-p```正常情况下,你应该看到`nfs`、`mountd`、`nlockmgr`、`status`等程序已经注册。如果没有,说明NFS服务没有成功注册。5.**检查防火墙设置**:防火墙可能会阻止RPC通信。确保所需的端口(特别是RPC端口和NFS端口)是开放的。NFS通常使用TCP和UDP的2049端口,而RPC服务使用动态端口。你可以考虑暂时关闭防火墙进行测试:```bashsystemctlstopfirewalld#对于使用firewalld的系统#或者iptables-F#清除iptables规则(注意:这可能会影响其他服务)```如果问题解决,则需要配置防火墙规则以允许NFS和RPC通信。6.**检查网络连通性**:确保客户端和服务器之间的网络是连通的。使用`ping`命令测试网络连通性。7.**重新导出共享目录**:在NFS服务器上,重新导出共享目录:```bashexportfs-ra```8.**查看日志**:如果问题仍然存在,查看系统日志(如`/var/log/messages`或`journalctl-unfs-server`)以获取更多错误信息。###注意事项-在较新的系统中,`rpcbind`可能被`rpcbind`服务替代,但通常服务名称还是`rpcbind`。-如果使用的是NFSv4,它不需要`portmap`或`rpcbind`,但是`showmount`命令仍然依赖于`rpcbind`。因此,即使使用NFSv4,也需要`rpcbind`服务运行。通过以上步骤,大多数情况下可以解决`clnt_create:RPC:Programnotregistered`错误。§§相关问题§§1.如何检查NFS共享是否已经正确导出?2.如何配置防火墙以允许NFS通信?3.为什么在NFSv4中仍然需要rpcbind服务?</think>### 错误解释:`clnt_create: RPC: Program not registered` 该错误发生在NFS(网络文件系统)客户端尝试访问NFS服务器时,表明**RPC服务未正确注册或NFS相关服务未完全启动**。核心原因是: 1. **RPC程序未注册**:NFS依赖的RPC服务(如`rpc.mountd`)未在RPC端口映射器(`rpcbind`)中注册。 2. **服务未启动**:NFS服务器端的`rpcbind`或`nfs-server`服务未运行。 3. **配置未生效**:修改NFS配置后未重新加载服务或重新导出共享目录[^1][^3]。 --- ### 解决方案 #### 步骤1:检查并重启关键服务 ```bash # 在NFS服务器端执行: sudo systemctl restart rpcbind # 重启RPC端口映射服务 sudo systemctl restart nfs-server # 重启NFS主服务 sudo systemctl restart rpc-statd # 重启NFS状态监控服务(可选) ``` #### 步骤2:强制重新注册RPC程序 ```bash # 在NFS服务器端执行: sudo exportfs -ra # 重新导出所有NFS共享目录 sudo rpc.mountd # 手动启动mountd守护进程(部分系统需安装nfs-kernel-server) ``` #### 步骤3:验证RPC注册状态 ```bash # 在NFS服务器端执行: rpcinfo -p # 检查注册的RPC程序 ``` **正常输出应包含**: ``` 100005 1 udp 20048 mountd # 关键程序 100005 1 tcp 20048 mountd 100003 3 tcp 2049 nfs ``` #### 步骤4:排查防火墙(常见阻断原因) ```bash # 在NFS服务器端执行: sudo firewall-cmd --add-service={nfs,rpc-bind,mountd} --permanent sudo firewall-cmd --reload ``` 或临时关闭防火墙测试: ```bash sudo systemctl stop firewalld # CentOS/RHEL sudo ufw disable # Ubuntu/Debian ``` #### 步骤5:检查客户端命令语法 确保客户端使用正确的IP地址: ```bash showmount -e <NFS服务器IP> # 非localhost时需指定服务器IP[^2] ``` --- ### 关键要点总结 | 原因 | 解决手段 | |---------------------|----------------------------| | `rpcbind`未运行 | `systemctl restart rpcbind` | | `rpc.mountd`未注册 | `exportfs -ra` + 重启服务 | | 防火墙阻断RPC通信 | 开放端口:111(TCP/UDP), 2049(TCP), 20048(TCP/UDP) | | 客户端使用localhost | 指定NFS服务器真实IP | > ⚠️ **注意**:若仍报错`Port mapper failure`,需检查**网络连通性**(如`ping <服务器IP>`)和**路由配置**[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值