whether we're using the A system or the B system:

Bash: alias inva='cd ~tal/projects/inventory/groupa ; export INVSTYLE=A' alias invb='cd ~tal/projects/inventory/groupb ; export INVSTYLE=B'

csh: alias inva 'cd ~tal/projects/inventory/groupa ; setenv INVSTYLE A' alias invb 'cd ~tal/projects/inventory/groupb ; setenv INVSTYLE B'

Instead of using a semicolon, use && to indicate 'Do this next command only if the first one succeeded.' This can be useful to protect against running a command while in the wrong directory. For example, you want to go to a particular directory and write a timestamp to a logfile. However, if the cd fails (the server is unavailable), you don't want to accidentally create a logfile in your current directory.

Bash: alias rank='cd /home/rank/data && date >>.log'

csh: alias rank 'cd /home/rank/data && date >>.log'

Warning

Don't try to turn one OS into another. Aliases are great, but don't overdo it. I've often seen people developing dozens of aliases so that they can type DOS commands in Unix. I think this is a bad idea. You're never going to learn Unix that way, and the next time you are on someone else's machine and don't have access to those aliases, you'll be stuck.

Hostname Shortcuts

If there are particular hostnames you type over and over, you can save some time by creating aliases. For example, if you are often dealing with a machine called ramanujan.company.com, you can create an alias (a DNS CNAME record) called ram.company.com. That's a little less typing.

The problem with this technique is that it can become a maintenance nightmare. If people start to depend on both names, you're stuck maintaining both names. So how can you create an alias that only you know about that won't bother other people?

Typically, if there is a machine I access a lot, I'm accessing it almost exclusively via Secure SHell (SSH). SSH is a secure (encrypted) replacement for telnet and rsh. You can also use it to copy files (scp, a replacement for rcp), and many programs, such as rsync, use SSH. Unix SSH (OpenSSH and its brothers) lets you set up host aliases for all users on a Unix machine or aliases that are private for you.

To affect only your SSH sessions, add aliases to the ~/.ssh/config file. To affect all users of the system, add aliases to either /etc/ssh_config or /etc/ssh/ssh_config, depending on how your system was configured. In this example, I create an alias, es, so that I don't have to type www.everythingsysadmin.com all the time: Host es HostName www.everythingsysadmin.com

Not only can I use ssh es where I used to type ssh www.everythingsysadmin.com , but the alias works for all SSH-related commands: scp, sftp, rsync, and so on. In fact, scripts and programs that I can't change will automatically pick up these settings. Some examples: $ ssh es $ scp file.txt es:/tmp/ $ rsync ex:/home/project/alpha ~/project/alpha

I need to use ssh es so often that I actually created a shell alias to reduce my typing further:

Bash: alias es='ssh es'

csh: alias es 'ssh es'

The result is that I can now type es on the command line to log into the machine, or I can use es to refer to the machine when using scp or rsync. Same two letters either way. Cool, huh?

It is tempting to create two-letter aliases for every server in the world. However, you will soon find yourself spending more time remembering your coding system than using it. Personally, I limit myself to a few common machines that I access via SSH.

The ssh_config(5) manpage lists many other configuration options. For example, there is one machine that I occasionally access that requires a very specific combination of options on the command line. (It's a home-grown version of the SSH server that not only doesn't implement all the features but gets confused if you try to negotiate anything it doesn't understand.) The command I have to type to get it just right is: $ ssh -x -o RSAAuthentication=yes -o PasswordAuthentication=yes -o ChallengeResponseAuthentication=no -1 peter.example.net

I could have set up a shell alias, but instead I can modify the SSH configuration, and all systems that use SSH will do the right thing. If a script that I can't modify uses SSH to reach that machine, these settings will still be used.

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату